|
|
|
|
@ -8,12 +8,9 @@ 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.api.ApiException |
|
|
|
|
import com.google.android.libraries.places.api.Places |
|
|
|
|
import com.google.android.libraries.places.api.model.Place |
|
|
|
|
import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest |
|
|
|
|
import com.google.android.libraries.places.api.model.PlaceLikelihood |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import net.pokeranalytics.android.util.LocationManager |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -24,6 +21,8 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private val realm = Realm.getDefaultInstance() |
|
|
|
|
private var askFromPlaces = false |
|
|
|
|
private var currentCallback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)? = null |
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
|
@ -72,64 +71,34 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
|
/** |
|
|
|
|
* Ask for location permission |
|
|
|
|
*/ |
|
|
|
|
fun askForLocationPermission() { |
|
|
|
|
private fun askForLocationPermission() { |
|
|
|
|
ActivityCompat.requestPermissions( |
|
|
|
|
this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), |
|
|
|
|
PERMISSION_REQUEST_ACCESS_FINE_LOCATION |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return if the user has given the permission location |
|
|
|
|
*/ |
|
|
|
|
fun hasLocationPermission(): Boolean { |
|
|
|
|
return ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Called when the permission location has been granted |
|
|
|
|
*/ |
|
|
|
|
open fun locationPermissionGranted() {} |
|
|
|
|
open fun locationPermissionGranted() { |
|
|
|
|
if (askFromPlaces) { |
|
|
|
|
askFromPlaces = false |
|
|
|
|
askForPlacesRequest(currentCallback) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Ask for places request |
|
|
|
|
*/ |
|
|
|
|
fun askForPlacesRequest() { |
|
|
|
|
|
|
|
|
|
// Initialize Places. |
|
|
|
|
Places.initialize(applicationContext, getString(net.pokeranalytics.android.R.string.google_places_api)) |
|
|
|
|
|
|
|
|
|
// Create a new Places client instance. |
|
|
|
|
val placesClient = Places.createClient(this) |
|
|
|
|
|
|
|
|
|
// Use fields to define the data types to return. |
|
|
|
|
val placeFields = Arrays.asList(Place.Field.NAME) |
|
|
|
|
|
|
|
|
|
// Use the builder to create a FindCurrentPlaceRequest. |
|
|
|
|
val request = FindCurrentPlaceRequest.builder(placeFields).build() |
|
|
|
|
fun askForPlacesRequest(callback: ((success: Boolean, places: ArrayList<PlaceLikelihood>) -> Unit)?) { |
|
|
|
|
|
|
|
|
|
// Call findCurrentPlace and handle the response (first check that the user has granted permission). |
|
|
|
|
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
val placeResponse = placesClient.findCurrentPlace(request) |
|
|
|
|
placeResponse.addOnCompleteListener { task -> |
|
|
|
|
if (task.isSuccessful) { |
|
|
|
|
val response = task.result |
|
|
|
|
for (placeLikelihood in response!!.placeLikelihoods) { |
|
|
|
|
Timber.d( |
|
|
|
|
String.format( |
|
|
|
|
"Place '%s' has likelihood: %f", placeLikelihood.place.name, placeLikelihood.likelihood |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
val exception = task.exception |
|
|
|
|
if (exception is ApiException) { |
|
|
|
|
Timber.d("Error: ${"Place not found: " + exception.statusCode}") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LocationManager(this).askForPlacesRequest(callback) |
|
|
|
|
} else { |
|
|
|
|
askFromPlaces = true |
|
|
|
|
currentCallback = callback |
|
|
|
|
askForLocationPermission() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|