mirror of
https://github.com/libretro/RetroArch
synced 2025-01-30 12:32:52 +00:00
Use new android location api
This commit is contained in:
parent
7fd49bbb2c
commit
dd1fc5f3da
@ -8,6 +8,7 @@
|
||||
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
|
||||
<uses-feature android:name="android.software.leanback" android:required="false" />
|
||||
<uses-feature android:name="android.hardware.gamepad" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.location.gps" android:required="false"/>
|
||||
<uses-sdk
|
||||
android:minSdkVersion="16"
|
||||
android:targetSdkVersion="28" />
|
||||
@ -15,6 +16,7 @@
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
@ -65,6 +65,8 @@ public final class MainMenuActivity extends PreferenceActivity
|
||||
permissionsNeeded.add("Read External Storage");
|
||||
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
|
||||
permissionsNeeded.add("Write External Storage");
|
||||
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
|
||||
permissionsNeeded.add("Access fine location");
|
||||
|
||||
if (permissionsList.size() > 0)
|
||||
{
|
||||
|
@ -1,11 +1,10 @@
|
||||
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.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.LocationRequest;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.retroarch.browser.preferences.util.UserPreferences;
|
||||
|
||||
import android.app.NativeActivity;
|
||||
@ -16,16 +15,19 @@ import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import android.content.pm.PackageManager;
|
||||
import android.Manifest;
|
||||
|
||||
/**
|
||||
* Class that implements location-based functionality for
|
||||
* the {@link RetroActivityFuture} activity.
|
||||
*/
|
||||
public class RetroActivityLocation extends NativeActivity
|
||||
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
|
||||
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
|
||||
{
|
||||
/* LOCATION VARIABLES */
|
||||
private static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 0;
|
||||
private LocationClient mLocationClient = null;
|
||||
private GoogleApiClient mGoogleApiClient = null;
|
||||
private Location mCurrentLocation;
|
||||
|
||||
// Define an object that holds accuracy and frequency parameters
|
||||
@ -42,48 +44,56 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
|
||||
@Override
|
||||
public void onConnected(Bundle dataBundle)
|
||||
{
|
||||
if (mLocationClient == null)
|
||||
if (mGoogleApiClient == null)
|
||||
return;
|
||||
|
||||
// Display the connection status
|
||||
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
|
||||
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 (mUpdatesRequested)
|
||||
{
|
||||
mLocationClient.requestLocationUpdates(mLocationRequest, this, null);
|
||||
LocationServices.FusedLocationApi.requestLocationUpdates(
|
||||
mGoogleApiClient, mLocationRequest, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get last known location
|
||||
mCurrentLocation = mLocationClient.getLastLocation();
|
||||
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
|
||||
locationChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Location Services if the connection to the
|
||||
* location client drops because of an error.
|
||||
*/
|
||||
@Override
|
||||
public void onDisconnected()
|
||||
public void onConnectionSuspended(int i)
|
||||
{
|
||||
if (mLocationClient == null)
|
||||
if (mGoogleApiClient == null)
|
||||
return;
|
||||
|
||||
// Display the connection status
|
||||
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// If the client is connected
|
||||
if (mLocationClient.isConnected())
|
||||
if (mGoogleApiClient.isConnected())
|
||||
{
|
||||
/*
|
||||
* Remove location updates for a listener.
|
||||
* The current Activity is the listener, so
|
||||
* the argument is "this".
|
||||
*/
|
||||
mLocationClient.removeLocationUpdates(this);
|
||||
LocationServices.FusedLocationApi.removeLocationUpdates(
|
||||
mGoogleApiClient, this);
|
||||
}
|
||||
|
||||
location_service_running = false;
|
||||
@ -155,8 +165,13 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
|
||||
* Create a new location client, using the enclosing class to
|
||||
* handle callbacks.
|
||||
*/
|
||||
if (mLocationClient == null)
|
||||
mLocationClient = new LocationClient(this, this, this);
|
||||
if (mGoogleApiClient == null) {
|
||||
mGoogleApiClient = new GoogleApiClient.Builder(this)
|
||||
.addApi(LocationServices.API)
|
||||
.addConnectionCallbacks(this)
|
||||
.addOnConnectionFailedListener(this)
|
||||
.build();
|
||||
}
|
||||
|
||||
// Start with updates turned off
|
||||
mUpdatesRequested = false;
|
||||
@ -170,17 +185,17 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
|
||||
|
||||
|
||||
/**
|
||||
* Executed upon starting the {@link LocationClient}.
|
||||
* Executed upon starting the {@link GoogleApiClient}.
|
||||
*/
|
||||
public void onLocationStart()
|
||||
{
|
||||
if (mLocationClient == null)
|
||||
if (mGoogleApiClient == null)
|
||||
return;
|
||||
|
||||
mUpdatesRequested = true;
|
||||
|
||||
// Connect the client.
|
||||
mLocationClient.connect();
|
||||
mGoogleApiClient.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,8 +213,8 @@ implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener
|
||||
public void onLocationStop()
|
||||
{
|
||||
// Disconnecting the client invalidates it.
|
||||
if (mLocationClient != null && mUpdatesRequested)
|
||||
mLocationClient.disconnect();
|
||||
if (mGoogleApiClient != null && mUpdatesRequested)
|
||||
mGoogleApiClient.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user