From be5609269aa76695209f4bf5326e5be62865f85b Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 21 Dec 2013 14:58:29 +0100 Subject: [PATCH] (Android location) Gets a bit further but still not calling onLocationChanged - just renders the text for the first location retrieval and nothing else after that --- .../retroactivity/RetroActivityLocation.java | 188 +++++++++--------- 1 file changed, 90 insertions(+), 98 deletions(-) diff --git a/android/phoenix/src/com/retroarch/browser/retroactivity/RetroActivityLocation.java b/android/phoenix/src/com/retroarch/browser/retroactivity/RetroActivityLocation.java index 81a458e0d7..d9d843ef68 100644 --- a/android/phoenix/src/com/retroarch/browser/retroactivity/RetroActivityLocation.java +++ b/android/phoenix/src/com/retroarch/browser/retroactivity/RetroActivityLocation.java @@ -1,8 +1,10 @@ package com.retroarch.browser.retroactivity; import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GooglePlayServicesClient; +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.retroarch.browser.preferences.util.UserPreferences; @@ -10,7 +12,6 @@ import android.app.NativeActivity; import android.content.IntentSender; import android.content.SharedPreferences; import android.location.Location; -import android.location.LocationListener; import android.os.Bundle; import android.util.Log; import android.widget.Toast; @@ -21,9 +22,7 @@ import android.widget.Toast; * activities. */ public class RetroActivityLocation extends NativeActivity -implements GooglePlayServicesClient.ConnectionCallbacks, -GooglePlayServicesClient.OnConnectionFailedListener, -LocationListener, com.google.android.gms.location.LocationListener +implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { /* LOCATION VARIABLES */ private static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 0; @@ -47,11 +46,21 @@ LocationListener, com.google.android.gms.location.LocationListener @Override public void onConnected(Bundle dataBundle) { - // Display the connection status - Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); - location_service_running = true; - // If already requested, start periodic updates - mLocationClient.requestLocationUpdates(mLocationRequest, this); + // Display the connection status + Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); + location_service_running = true; + + // If already requested, start periodic updates + if (mUpdatesRequested) + { + mLocationClient.requestLocationUpdates(mLocationRequest, this, null); + } + else + { + // Get last known location + mCurrentLocation = mLocationClient.getLastLocation(); + locationChanged = true; + } } /* @@ -61,22 +70,21 @@ LocationListener, com.google.android.gms.location.LocationListener @Override public void onDisconnected() { - // Display the connection status - Toast.makeText(this, "Disconnected. Please re-connect.", - Toast.LENGTH_SHORT).show(); - - // If the client is connected - if (mLocationClient.isConnected()) - { - /* - * Remove location updates for a listener. - * The current Activity is the listener, so - * the argument is "this". - */ - mLocationClient.removeLocationUpdates((com.google.android.gms.location.LocationListener) this); - } - - location_service_running = false; + // Display the connection status + Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); + + // If the client is connected + if (mLocationClient.isConnected()) + { + /* + * Remove location updates for a listener. + * The current Activity is the listener, so + * the argument is "this". + */ + mLocationClient.removeLocationUpdates(this); + } + + location_service_running = false; } /* @@ -86,39 +94,33 @@ LocationListener, com.google.android.gms.location.LocationListener @Override public void onConnectionFailed(ConnectionResult connectionResult) { - /* - * Google Play services can resolve some errors it detects. - * If the error has a resolution, try sending an Intent to - * start a Google Play services activity that can resolve - * error. - */ - if (connectionResult.hasResolution()) - { - try - { - // Start an Activity that tries to resolve the error - connectionResult.startResolutionForResult( - this, - CONNECTION_FAILURE_RESOLUTION_REQUEST); - /* - * Thrown if Google Play services cancelled the original - * PendingIntent - */ - } - catch (IntentSender.SendIntentException e) - { - // Log the error - e.printStackTrace(); - } - } - else - { - /* - * If no resolution is available, display a dialog to the - * user with the error. - */ - //showErrorDialog(connectionResult.getErrorCode()); - } + /* + * Google Play services can resolve some errors it detects. + * If the error has a resolution, try sending an Intent to + * start a Google Play services activity that can resolve + * error. + */ + if (connectionResult.hasResolution()) + { + try + { + // Start an Activity that tries to resolve the error + connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); + } + catch (IntentSender.SendIntentException e) + { + // Thrown if Google Play services cancelled the original PendingIntent + e.printStackTrace(); + } + } + else + { + /* + * If no resolution is available, display a dialog to the + * user with the error. + */ + //showErrorDialog(connectionResult.getErrorCode()); + } } /** @@ -127,15 +129,16 @@ LocationListener, com.google.android.gms.location.LocationListener */ public void onLocationSetInterval(int update_interval_in_ms, int distance_interval) { - // Use high accuracy - mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); - if (update_interval_in_ms == 0) - mLocationRequest.setInterval(1000 * 5); - else - mLocationRequest.setInterval(update_interval_in_ms); + // Use high accuracy + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); - // Set the fastest update interval to 1 second - mLocationRequest.setFastestInterval(1000 * 1); + if (update_interval_in_ms == 0) + mLocationRequest.setInterval(5 * 1000); // 5 seconds + else + mLocationRequest.setInterval(update_interval_in_ms); + + // Set the fastest update interval to 1 second + mLocationRequest.setFastestInterval(1000); } /** @@ -143,20 +146,21 @@ LocationListener, com.google.android.gms.location.LocationListener */ public void onLocationInit() { - /* - * Create a new location client, using the enclosing class to - * handle callbacks. - */ - if (mLocationClient == null) - mLocationClient = new LocationClient(this, this, this); - // Start with updates turned off - mUpdatesRequested = false; + /* + * Create a new location client, using the enclosing class to + * handle callbacks. + */ + if (mLocationClient == null) + mLocationClient = new LocationClient(this, this, this); - // Create the LocationRequest object - if (mLocationRequest == null) - mLocationRequest = LocationRequest.create(); - - onLocationSetInterval(0, 0); + // Start with updates turned off + mUpdatesRequested = false; + + // Create the LocationRequest object + if (mLocationRequest == null) + mLocationRequest = LocationRequest.create(); + + onLocationSetInterval(0, 0); } @@ -232,10 +236,13 @@ LocationListener, com.google.android.gms.location.LocationListener */ public boolean onLocationHasChanged() { - boolean ret = locationChanged; - if (ret) + boolean hasChanged = locationChanged; + + // Reset flag + if (hasChanged) locationChanged = false; - return ret; + + return hasChanged; } // Define the callback method that receives location updates @@ -254,21 +261,6 @@ LocationListener, com.google.android.gms.location.LocationListener //Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } - @Override - public void onStatusChanged(String provider, int status, Bundle extras) - { - } - - @Override - public void onProviderEnabled(String provider) - { - } - - @Override - public void onProviderDisabled(String provider) - { - } - @Override public void onPause() {