|
|
|
@ -10,8 +10,8 @@ import androidx.core.app.ActivityCompat |
|
|
|
import androidx.core.content.ContextCompat |
|
|
|
import androidx.core.content.ContextCompat |
|
|
|
import com.google.android.libraries.places.api.model.PlaceLikelihood |
|
|
|
import com.google.android.libraries.places.api.model.PlaceLikelihood |
|
|
|
import io.realm.Realm |
|
|
|
import io.realm.Realm |
|
|
|
|
|
|
|
import net.pokeranalytics.android.model.realm.Location |
|
|
|
import net.pokeranalytics.android.util.LocationManager |
|
|
|
import net.pokeranalytics.android.util.LocationManager |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
@ -21,8 +21,7 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val realm = Realm.getDefaultInstance() |
|
|
|
private val realm = Realm.getDefaultInstance() |
|
|
|
private var askFromPlaces = false |
|
|
|
private var permissionCallback: ((granted: Boolean) -> Unit)? = null |
|
|
|
private var currentCallback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)? = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
@ -36,8 +35,9 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
if (permissions.isNotEmpty() && permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
if (permissions.isNotEmpty() && permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
&& grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED |
|
|
|
&& grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED |
|
|
|
) { |
|
|
|
) { |
|
|
|
locationPermissionGranted() |
|
|
|
permissionCallback?.invoke(true) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
permissionCallback?.invoke(false) |
|
|
|
// permission denied, boo! Disable the |
|
|
|
// permission denied, boo! Disable the |
|
|
|
// functionality that depends on this permission. |
|
|
|
// functionality that depends on this permission. |
|
|
|
// showMessage(getString(R.string.error)); |
|
|
|
// showMessage(getString(R.string.error)); |
|
|
|
@ -71,35 +71,50 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Ask for location permission |
|
|
|
* Ask for location permission |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun askForLocationPermission() { |
|
|
|
private fun askForLocationPermission(permissionCallback: ((granted: Boolean) -> Unit)) { |
|
|
|
|
|
|
|
this.permissionCallback = permissionCallback |
|
|
|
ActivityCompat.requestPermissions( |
|
|
|
ActivityCompat.requestPermissions( |
|
|
|
this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), |
|
|
|
this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_ACCESS_FINE_LOCATION |
|
|
|
PERMISSION_REQUEST_ACCESS_FINE_LOCATION |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Called when the permission location has been granted |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
open fun locationPermissionGranted() { |
|
|
|
|
|
|
|
if (askFromPlaces) { |
|
|
|
|
|
|
|
askFromPlaces = false |
|
|
|
|
|
|
|
askForPlacesRequest(currentCallback) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Ask for places request |
|
|
|
* Ask for places request |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) { |
|
|
|
fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) { |
|
|
|
|
|
|
|
|
|
|
|
// Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
// Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
LocationManager(this).askForPlacesRequest(callback) |
|
|
|
LocationManager(this).askForPlacesRequest(callback) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
askFromPlaces = true |
|
|
|
askForLocationPermission { granted -> |
|
|
|
currentCallback = callback |
|
|
|
if (granted) { |
|
|
|
askForLocationPermission() |
|
|
|
LocationManager(this).askForPlacesRequest(callback) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
callback?.invoke(false, ArrayList()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Find the nearest location from the user |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun findNearestLocation(callback: ((location: Location?) -> Unit)?) { |
|
|
|
|
|
|
|
if (LocationManager(this).databaseContainsLocationsWithCoordinates()) { |
|
|
|
|
|
|
|
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
|
|
|
LocationManager(this).findNearestLocationFromUser(callback) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
askForLocationPermission { granted -> |
|
|
|
|
|
|
|
if (granted) { |
|
|
|
|
|
|
|
LocationManager(this).findNearestLocationFromUser(callback) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
callback?.invoke(null) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
callback?.invoke(null) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|