diff --git a/app/src/main/java/net/pokeranalytics/android/ui/activity/components/PokerAnalyticsActivity.kt b/app/src/main/java/net/pokeranalytics/android/ui/activity/components/PokerAnalyticsActivity.kt index e02ebd15..d3977e9f 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/activity/components/PokerAnalyticsActivity.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/activity/components/PokerAnalyticsActivity.kt @@ -8,16 +8,20 @@ import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability import com.google.android.libraries.places.api.model.PlaceLikelihood import io.realm.Realm import net.pokeranalytics.android.model.realm.Location import net.pokeranalytics.android.util.LocationManager +import timber.log.Timber open class PokerAnalyticsActivity : AppCompatActivity() { companion object { const val PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 1000 + const val PLAY_SERVICES_RESOLUTION_REQUEST = 2000 } private val realm = Realm.getDefaultInstance() @@ -71,7 +75,7 @@ open class PokerAnalyticsActivity : AppCompatActivity() { /** * Return if the location permission has been granted by the user */ - fun hasLocationPermissionGranted() : Boolean { + fun hasLocationPermissionGranted(): Boolean { return ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED } @@ -127,22 +131,36 @@ open class PokerAnalyticsActivity : AppCompatActivity() { /** * Find the current location */ - fun findCurrentLocation(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) - } + fun findCurrentLocation(callback: ((location: android.location.Location?) -> Unit)?) { + if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + LocationManager(this).findCurrentLocation(callback) + } else { + askForLocationPermission { granted -> + if (granted) { + LocationManager(this).findCurrentLocation(callback) + } else { + callback?.invoke(null) } } - } else { - callback?.invoke(null) } } + /** + * Check the Google Play Services + */ + fun checkPlayServices(): Boolean { + val googleAPI = GoogleApiAvailability.getInstance() + val result = googleAPI.isGooglePlayServicesAvailable(this) + Timber.d("checkPlayServices: $result") + if (result != ConnectionResult.SUCCESS) { + if (googleAPI.isUserResolvableError(result)) { + googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST).show() + } + + return false + } + + return true + } + } \ No newline at end of file