(Android) Backport

dd1fc5f3da
- not sure if we should include the location permission - some users
would question why they would need to grant this permission for just one
test core
This commit is contained in:
twinaphex 2019-06-27 11:00:06 +02:00
parent 6e4c8ec6a6
commit adf4bd753b

View File

@ -1,11 +1,10 @@
package com.retroarch.browser.retroactivity; package com.retroarch.browser.retroactivity;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.retroarch.browser.preferences.util.UserPreferences; import com.retroarch.browser.preferences.util.UserPreferences;
import android.app.NativeActivity; import android.app.NativeActivity;
@ -16,17 +15,19 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import android.content.pm.PackageManager;
import android.Manifest;
/** /**
* Class that implements location-based functionality for * Class that implements location-based functionality for
* the {@link RetroActivityFuture} and {@link RetroActivityPast} * the {@link RetroActivityFuture} activity.
* activities.
*/ */
public class RetroActivityLocation extends NativeActivity public class RetroActivityLocation extends NativeActivity
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{ {
/* LOCATION VARIABLES */ /* LOCATION VARIABLES */
private static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 0; private static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 0;
private LocationClient mLocationClient = null; private GoogleApiClient mGoogleApiClient = null;
private Location mCurrentLocation; private Location mCurrentLocation;
// Define an object that holds accuracy and frequency parameters // Define an object that holds accuracy and frequency parameters
@ -43,24 +44,31 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
@Override @Override
public void onConnected(Bundle dataBundle) public void onConnected(Bundle dataBundle)
{ {
if (mLocationClient == null) if (mGoogleApiClient == null)
return; return;
// Display the connection status // Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
location_service_running = true; location_service_running = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
// If already requested, start periodic updates // If already requested, start periodic updates
if (mUpdatesRequested) if (mUpdatesRequested)
{ {
mLocationClient.requestLocationUpdates(mLocationRequest, this, null); LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} }
else else
{ {
// Get last known location // Get last known location
mCurrentLocation = mLocationClient.getLastLocation(); mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
locationChanged = true; locationChanged = true;
} }
}
}
} }
/** /**
@ -68,23 +76,24 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
* location client drops because of an error. * location client drops because of an error.
*/ */
@Override @Override
public void onDisconnected() public void onConnectionSuspended(int i)
{ {
if (mLocationClient == null) if (mGoogleApiClient == null)
return; return;
// Display the connection status // Display the connection status
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
// If the client is connected // If the client is connected
if (mLocationClient.isConnected()) if (mGoogleApiClient.isConnected())
{ {
/* /*
* Remove location updates for a listener. * Remove location updates for a listener.
* The current Activity is the listener, so * The current Activity is the listener, so
* the argument is "this". * the argument is "this".
*/ */
mLocationClient.removeLocationUpdates(this); LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
} }
location_service_running = false; location_service_running = false;
@ -156,8 +165,13 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
* Create a new location client, using the enclosing class to * Create a new location client, using the enclosing class to
* handle callbacks. * handle callbacks.
*/ */
if (mLocationClient == null) if (mGoogleApiClient == null) {
mLocationClient = new LocationClient(this, this, this); mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Start with updates turned off // Start with updates turned off
mUpdatesRequested = false; mUpdatesRequested = false;
@ -171,17 +185,17 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
/** /**
* Executed upon starting the {@link LocationClient}. * Executed upon starting the {@link GoogleApiClient}.
*/ */
public void onLocationStart() public void onLocationStart()
{ {
if (mLocationClient == null) if (mGoogleApiClient == null)
return; return;
mUpdatesRequested = true; mUpdatesRequested = true;
// Connect the client. // Connect the client.
mLocationClient.connect(); mGoogleApiClient.connect();
} }
/** /**
@ -199,8 +213,8 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
public void onLocationStop() public void onLocationStop()
{ {
// Disconnecting the client invalidates it. // Disconnecting the client invalidates it.
if (mLocationClient != null && mUpdatesRequested) if (mGoogleApiClient != null && mUpdatesRequested)
mLocationClient.disconnect(); mGoogleApiClient.disconnect();
} }
/** /**