|
|
|
@ -1,17 +1,51 @@ |
|
|
|
package net.pokeranalytics.android.ui.activity.components |
|
|
|
package net.pokeranalytics.android.ui.activity.components |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest |
|
|
|
|
|
|
|
import android.Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
|
|
|
|
import android.content.pm.PackageManager |
|
|
|
import android.os.Bundle |
|
|
|
import android.os.Bundle |
|
|
|
import android.os.PersistableBundle |
|
|
|
|
|
|
|
import android.view.MenuItem |
|
|
|
import android.view.MenuItem |
|
|
|
import androidx.appcompat.app.AppCompatActivity |
|
|
|
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 io.realm.Realm |
|
|
|
import io.realm.Realm |
|
|
|
|
|
|
|
import timber.log.Timber |
|
|
|
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
const val PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 1000 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val realm = Realm.getDefaultInstance() |
|
|
|
private val realm = Realm.getDefaultInstance() |
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { |
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
super.onCreate(savedInstanceState, persistentState) |
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { |
|
|
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when (requestCode) { |
|
|
|
|
|
|
|
PERMISSION_REQUEST_ACCESS_FINE_LOCATION -> { |
|
|
|
|
|
|
|
if (permissions.isNotEmpty() && permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION |
|
|
|
|
|
|
|
&& grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
locationPermissionGranted() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// permission denied, boo! Disable the |
|
|
|
|
|
|
|
// functionality that depends on this permission. |
|
|
|
|
|
|
|
// showMessage(getString(R.string.error)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem?): Boolean { |
|
|
|
override fun onOptionsItemSelected(item: MenuItem?): Boolean { |
|
|
|
@ -35,4 +69,69 @@ open class PokerAnalyticsActivity : AppCompatActivity() { |
|
|
|
return realm |
|
|
|
return realm |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Ask for location permission |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
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() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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}") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
askForLocationPermission() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |