diff --git a/myTracks/build.gradle b/myTracks/build.gradle index 94617ec6e..e6091cdf4 100644 --- a/myTracks/build.gradle +++ b/myTracks/build.gradle @@ -22,7 +22,5 @@ android { dependencies { implementation project(':myTracksLib') - implementation 'com.google.android.gms:play-services-location:17.0.0' - implementation 'androidx.appcompat:appcompat:1.0.0' } diff --git a/myTracks/src/androidTest/java/com/google/android/apps/mytracks/services/TrackRecordingServiceTest.java b/myTracks/src/androidTest/java/com/google/android/apps/mytracks/services/TrackRecordingServiceTest.java index bae520e51..08811d7f5 100644 --- a/myTracks/src/androidTest/java/com/google/android/apps/mytracks/services/TrackRecordingServiceTest.java +++ b/myTracks/src/androidTest/java/com/google/android/apps/mytracks/services/TrackRecordingServiceTest.java @@ -23,7 +23,6 @@ import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint.WaypointType; import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.stats.TripStatistics; -import com.google.android.apps.mytracks.util.GoogleLocationUtils; import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.maps.mytracks.R; @@ -155,38 +154,6 @@ public class TrackRecordingServiceTest extends ServiceTestCase() { - @Override - public void onSuccess(Location location) { - // GPS location can be null if GPS is switched off - if (location != null) { - requestLastLocation.onLocationChanged(location); - } - requestLastLocation = null; - } - }); - } - if (requestLocationUpdates != null) { - LocationRequest locationRequest = new LocationRequest() - .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) - .setInterval(requestLocationUpdatesTime) - .setFastestInterval(requestLocationUpdatesTime) - .setSmallestDisplacement(requestLocationUpdatesDistance); - - locationClient.requestLocationUpdates(locationRequest, requestLocationUpdates, handler.getLooper()); - } - } - }); - } - }; - private final Context context; private final Handler handler; - private final FusedLocationProviderClient locationClient; private final LocationManager locationManager; - private final ContentResolver contentResolver; - private final GoogleSettingsObserver observer; - private boolean isAllowed; - private LocationListener requestLastLocation; - private LocationCallback requestLocationUpdates; + private LocationListener requestLocationUpdates; private float requestLocationUpdatesDistance; private long requestLocationUpdatesTime; - public MyTracksLocationManager(Context context, Looper looper, boolean enableLocationClient) { - this.context = context; + public MyTracksLocationManager(Context context, Looper looper) { this.handler = new Handler(looper); - if (enableLocationClient) { - locationClient = LocationServices.getFusedLocationProviderClient(context); - } else { - locationClient = null; - } - locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); - contentResolver = context.getContentResolver(); - observer = new GoogleSettingsObserver(handler); - - isAllowed = GoogleLocationUtils.isAllowed(context); - - contentResolver.registerContentObserver( - GoogleLocationUtils.USE_LOCATION_FOR_SERVICES_URI, false, observer); } /** - * Closes the {@link MyTracksLocationManager}. - */ - public void close() { - contentResolver.unregisterContentObserver(observer); - } - - /** - * Returns true if allowed to access the location manager. Returns true if - * there is no enforcement or the Google location settings allows access to - * location data. - */ - public boolean isAllowed() { - return isAllowed; - } - - /** - * Returns true if gps provider is enabled. - */ - public boolean isGpsProviderEnabled() { - if (!isAllowed()) { - return false; - } - return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); - } - - /** - * Request last location. - * - * @param locationListener location listener - */ - public void requestLastLocation(final LocationListener locationListener) { - handler.post(new Runnable() { - @Override - public void run() { - if (!isAllowed()) { - requestLastLocation = null; - locationListener.onLocationChanged(null); - } else { - requestLastLocation = locationListener; - connectionCallbacks.onConnected(null); - } - } - }); - } - - /** - * Requests location updates. This is an ongoing request, thus the caller - * needs to check the status of {@link #isAllowed}. + * Requests location updates. * * @param minTime the minimal time * @param minDistance the minimal distance * @param locationListener the location listener */ - public void requestLocationUpdates( - final long minTime, final float minDistance, final LocationCallback locationListener) { + public void requestLocationUpdates(final long minTime, final float minDistance, final LocationListener locationListener) { handler.post(new Runnable() { @Override + @SuppressLint("MissingPermission") public void run() { requestLocationUpdatesTime = minTime; requestLocationUpdatesDistance = minDistance; requestLocationUpdates = locationListener; - connectionCallbacks.onConnected(null); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, requestLocationUpdatesTime, requestLocationUpdatesDistance, locationListener); } }); } @@ -223,15 +74,8 @@ public class MyTracksLocationManager { * * @param locationListener the location listener */ - public void removeLocationUpdates(final LocationCallback locationListener) { - handler.post(new Runnable() { - @Override - public void run() { - requestLocationUpdates = null; - if (locationClient != null) { - locationClient.removeLocationUpdates(locationListener); - } - } - }); + public void removeLocationUpdates(final LocationListener locationListener) { + locationManager.removeUpdates(locationListener); + requestLocationUpdates = null; } } diff --git a/myTracks/src/main/java/com/google/android/apps/mytracks/services/TrackRecordingService.java b/myTracks/src/main/java/com/google/android/apps/mytracks/services/TrackRecordingService.java index b9ce2a6d9..ac1627a4d 100644 --- a/myTracks/src/main/java/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/myTracks/src/main/java/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -27,10 +27,12 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.database.sqlite.SQLiteException; import android.location.Location; +import android.location.LocationListener; import android.location.LocationManager; import android.net.Uri; import android.os.Binder; import android.os.Build; +import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.PowerManager.WakeLock; @@ -70,9 +72,6 @@ import com.google.android.apps.mytracks.util.SystemUtils; import com.google.android.apps.mytracks.util.TrackIconUtils; import com.google.android.apps.mytracks.util.TrackNameUtils; import com.google.android.apps.mytracks.util.UnitConversions; -import com.google.android.gms.location.DetectedActivity; -import com.google.android.gms.location.LocationCallback; -import com.google.android.gms.location.LocationResult; import com.google.android.maps.mytracks.R; import java.util.concurrent.ExecutorService; @@ -231,21 +230,38 @@ public class TrackRecordingService extends Service { } }; - private LocationCallback locationListener = new LocationCallback() { + private LocationListener locationListener = new LocationListener() { - public void onLocationResult(final LocationResult locationResult) { - if (myTracksLocationManager == null || executorService == null - || !myTracksLocationManager.isAllowed() || executorService.isShutdown() - || executorService.isTerminated()) { - return; - } - executorService.submit(new Runnable() { - @Override - public void run() { - onLocationChangedAsync(locationResult.getLastLocation()); - } - }); + @Override + public void onLocationChanged(final Location location) { + if (myTracksLocationManager == null + || executorService == null + || executorService.isShutdown() + || executorService.isTerminated()) { + return; } + executorService.submit(new Runnable() { + @Override + public void run() { + onLocationChangedAsync(location); + } + }); + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + Log.w(TAG, "LocationListener.onStatusChanged(): is not implemented."); + } + + @Override + public void onProviderEnabled(String provider) { + Log.w(TAG, "LocationListener.onProviderEnabled(): is not implemented."); + } + + @Override + public void onProviderDisabled(String provider) { + Log.w(TAG, "LocationListener.onProviderDisabled(): is not implemented."); + } }; private final Runnable registerLocationRunnable = new Runnable() { @@ -272,7 +288,7 @@ public class TrackRecordingService extends Service { context = this; myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this); handler = new Handler(); - myTracksLocationManager = new MyTracksLocationManager(this, handler.getLooper(), true); + myTracksLocationManager = new MyTracksLocationManager(this, handler.getLooper()); voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory()); splitExecutor = new PeriodicTaskExecutor(this, new SplitPeriodicTaskFactory()); sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE); @@ -348,7 +364,6 @@ public class TrackRecordingService extends Service { voiceExecutor = null; } - myTracksLocationManager.close(); myTracksLocationManager = null; myTracksProviderUtils = null; @@ -718,7 +733,6 @@ public class TrackRecordingService extends Service { // Update database Track track = myTracksProviderUtils.getTrack(trackId); if (track != null) { - // If not paused, add the last location if (!paused) { insertLocation(track, lastLocation, getLastValidTrackPointInCurrentSegment(trackId)); @@ -734,33 +748,6 @@ public class TrackRecordingService extends Service { track.setName(trackName); myTracksProviderUtils.updateTrack(track); } - - if (track.getCategory().equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT)) { - int activityRecognitionType = PreferencesUtils.getInt(this, - R.string.activity_recognition_type_key, - PreferencesUtils.ACTIVITY_RECOGNITION_TYPE_DEFAULT); - if (activityRecognitionType != PreferencesUtils.ACTIVITY_RECOGNITION_TYPE_DEFAULT) { - String iconValue = null; - switch (activityRecognitionType) { - case DetectedActivity.IN_VEHICLE: - iconValue = TrackIconUtils.DRIVE; - break; - case DetectedActivity.ON_BICYCLE: - iconValue = TrackIconUtils.BIKE; - break; - case DetectedActivity.ON_FOOT: - iconValue = TrackIconUtils.WALK; - break; - default: - break; - } - if (iconValue != null) { - track.setIcon(iconValue); - track.setCategory(getString(TrackIconUtils.getIconActivityType(iconValue))); - myTracksProviderUtils.updateTrack(track); - } - } - } } endRecording(true, trackId); } @@ -1041,8 +1028,7 @@ public class TrackRecordingService extends Service { } try { long interval = locationListenerPolicy.getDesiredPollingInterval(); - myTracksLocationManager.requestLocationUpdates( - interval, locationListenerPolicy.getMinDistance(), locationListener); + myTracksLocationManager.requestLocationUpdates(interval, locationListenerPolicy.getMinDistance(), locationListener); currentRecordingInterval = interval; } catch (RuntimeException e) { Log.e(TAG, "Could not register location listener.", e); diff --git a/myTracks/src/main/java/com/google/android/apps/mytracks/util/GoogleLocationUtils.java b/myTracks/src/main/java/com/google/android/apps/mytracks/util/GoogleLocationUtils.java deleted file mode 100644 index 3ad9d226f..000000000 --- a/myTracks/src/main/java/com/google/android/apps/mytracks/util/GoogleLocationUtils.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.google.android.apps.mytracks.util; - -import com.google.android.maps.mytracks.R; - -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.database.Cursor; -import android.net.Uri; -import android.provider.Settings; -import android.util.Log; - -/** - * Utilities for Google location settings - * - * @author Jimmy Shih - */ -public class GoogleLocationUtils { - - private static final String TAG = GoogleLocationUtils.class.getSimpleName(); - - // Action to launch the google location settings - private static final String - ACTION_GOOGLE_LOCATION_SETTINGS = "com.google.android.gsf.GOOGLE_LOCATION_SETTINGS"; - - // Action to check if the google apps locations settings exists - private static final String - ACTION_GOOGLE_APPS_LOCATION_SETTINGS = "com.google.android.gsf.GOOGLE_APPS_LOCATION_SETTINGS"; - - // User has disagreed to use location for Google services - private static final int USE_LOCATION_FOR_SERVICES_OFF = 0; - - // User has agreed to use location for Google services - public static final int USE_LOCATION_FOR_SERVICES_ON = 1; - - /* - * The user has neither agreed nor disagreed to use location for Google - * services yet. - */ - private static final int USE_LOCATION_FOR_SERVICES_NOT_SET = 2; - - private static final String GOOGLE_SETTINGS_AUTHORITY = "com.google.settings"; - private static final Uri GOOGLE_SETTINGS_CONTENT_URI = Uri.parse( - "content://" + GOOGLE_SETTINGS_AUTHORITY + "/partner"); - private static final String NAME = "name"; - private static final String VALUE = "value"; - private static final String USE_LOCATION_FOR_SERVICES = "use_location_for_services"; - - public static final Uri USE_LOCATION_FOR_SERVICES_URI = Uri.parse( - "content://" + GOOGLE_SETTINGS_AUTHORITY + "/partner/" + USE_LOCATION_FOR_SERVICES); - - private GoogleLocationUtils() {} - - /** - * Gets the gps disabled message. - * - * @param context the context - */ - public static String getGpsDisabledMessage(Context context) { - return context.getString(R.string.gps_disabled_location_mode, getLocationSettingsName(context)); - } - - /** - * Gets the gps disabled message when my location button is pressed. - * - * @param context the context - */ - public static String getGpsDisabledMyLocationMessage(Context context) { - return context.getString(R.string.gps_disabled_my_location_location_mode, getLocationSettingsName(context)); - } - - /** - * Creates a new location settings intent. - * - * @param context the context - */ - public static Intent newLocationSettingsIntent(Context context) { - Intent intent = new Intent(useGoogleLocationSettings(context) ? ACTION_GOOGLE_LOCATION_SETTINGS - : Settings.ACTION_LOCATION_SOURCE_SETTINGS); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } - - /** - * Returns true if there is no enforcement or google location settings allows - * access. - * - * @param context the context - */ - public static boolean isAllowed(Context context) { - if (!isEnforceable(context)) { - return true; - } - return getUseLocationForServices(context) != USE_LOCATION_FOR_SERVICES_OFF; - } - - /** - * Gets the location settings name. - * - * @param context the context - */ - private static String getLocationSettingsName(Context context) { - return context.getString( - useGoogleLocationSettings(context) ? R.string.gps_google_location_settings - : R.string.gps_location_access); - } - - /** - * Returns true to use the google location settings. - * - * @param context the context - */ - private static boolean useGoogleLocationSettings(Context context) { - if (!isEnforceable(context)) { - return false; - } - - return getUseLocationForServices(context) == USE_LOCATION_FOR_SERVICES_OFF; - } - - /** - * Returns true if the Google location settings is enforceable. - */ - private static boolean isEnforceable(Context context) { - Intent intent = new Intent(ACTION_GOOGLE_APPS_LOCATION_SETTINGS); - ResolveInfo resolveInfo = context.getPackageManager() - .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); - return resolveInfo != null; - } - - /** - * Get the current value for the 'Use value for location' setting. - * - * @return One of {@link #USE_LOCATION_FOR_SERVICES_NOT_SET}, - * {@link #USE_LOCATION_FOR_SERVICES_OFF} or - * {@link #USE_LOCATION_FOR_SERVICES_ON}. - */ - private static int getUseLocationForServices(Context context) { - ContentResolver contentResolver = context.getContentResolver(); - String stringValue = null; - try (Cursor cursor = contentResolver.query(GOOGLE_SETTINGS_CONTENT_URI, new String[]{VALUE}, - NAME + "=?", new String[]{USE_LOCATION_FOR_SERVICES}, null)) { - if (cursor != null && cursor.moveToNext()) { - stringValue = cursor.getString(0); - } - } catch (RuntimeException e) { - Log.w(TAG, "Failed to get 'Use My Location' setting", e); - } - if (stringValue == null) { - return USE_LOCATION_FOR_SERVICES_NOT_SET; - } - int value; - try { - value = Integer.parseInt(stringValue); - } catch (NumberFormatException nfe) { - value = USE_LOCATION_FOR_SERVICES_NOT_SET; - } - return value; - } -} diff --git a/myTracks/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java b/myTracks/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java index 8c384de86..350352ec4 100644 --- a/myTracks/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java +++ b/myTracks/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java @@ -16,15 +16,13 @@ package com.google.android.apps.mytracks.util; -import com.google.android.apps.mytracks.Constants; -import com.google.android.gms.location.DetectedActivity; -import com.google.android.maps.mytracks.R; - import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils; -import android.util.Log; + +import com.google.android.apps.mytracks.Constants; +import com.google.android.maps.mytracks.R; /** * Utilities to access preferences stored in {@link SharedPreferences}. @@ -32,8 +30,6 @@ import android.util.Log; * @author Jimmy Shih */ public class PreferencesUtils { - - public static final int ACTIVITY_RECOGNITION_TYPE_DEFAULT = DetectedActivity.UNKNOWN; /* * Preferences values. The defaults need to match the defaults in the xml