From e813995b34b9e168a00eff158e47948a6ca9cf97 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 26 Nov 2013 15:45:33 -0800 Subject: [PATCH] Add new location setting policy --- MyTracks/res/values/strings.xml | 33 +++++-- .../fragments/MyTracksMapFragment.java | 8 +- .../services/MyTracksLocationManager.java | 12 +-- .../apps/mytracks/util/Api19Adapter.java | 33 +++++++ .../apps/mytracks/util/Api8Adapter.java | 5 + .../apps/mytracks/util/ApiAdapter.java | 9 +- .../apps/mytracks/util/ApiAdapterFactory.java | 4 +- .../mytracks/util/GoogleLocationUtils.java | 97 +++++++++++++------ MyTracksTest_E2E/AndroidManifest.xml | 2 +- .../common/GoToMyLocationTest.java | 6 +- 10 files changed, 153 insertions(+), 56 deletions(-) create mode 100644 MyTracks/src/com/google/android/apps/mytracks/util/Api19Adapter.java diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index 245458b02..575d554cf 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -865,6 +865,29 @@ limitations under the License. %1$s location settings. E.g., 'Google location settings'."> GPS not enabled. Tap here to go to %1$s. Press the back button to return. + + Select high-accuracy location mode. Tap here to go to %1$s. Press the back button to return. + + + Current location cannot be determined. Enable more location sources at %1$s & make sure the + sky is visible. Try again later. + + + Current location cannot be determined. Select the high-accuracy location mode at %1$s & make + sure the sky is visible. Try again later. + No GPS is found on the device. @@ -1272,16 +1295,6 @@ limitations under the License. Voice frequency - - - GPS is not available. Current location cannot be determined. Enable more location sources at - %1$s & make sure the sky is visible. Try again later. - - diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/MyTracksMapFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/MyTracksMapFragment.java index c4ee721aa..6011bddd3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/fragments/MyTracksMapFragment.java +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/MyTracksMapFragment.java @@ -163,9 +163,8 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData public void onClick(View v) { final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager( getActivity(), Looper.myLooper(), true); - if (!myTracksLocationManager.isAllowed()) { - String message = getString(R.string.my_location_no_gps, - GoogleLocationUtils.getLocationSettingsName(getActivity())); + if (!myTracksLocationManager.isGpsProviderEnabled()) { + String message = GoogleLocationUtils.getGpsDisabledMyLocationMessage(getActivity()); Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show(); myTracksLocationManager.close(); } else { @@ -723,8 +722,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData message = null; } } else { - message = getString( - R.string.gps_disabled, GoogleLocationUtils.getLocationSettingsName(getActivity())); + message = GoogleLocationUtils.getGpsDisabledMessage(getActivity()); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/MyTracksLocationManager.java b/MyTracks/src/com/google/android/apps/mytracks/services/MyTracksLocationManager.java index ea2c0e4fb..e3e160dea 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/MyTracksLocationManager.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/MyTracksLocationManager.java @@ -53,7 +53,7 @@ public class MyTracksLocationManager { @Override public void onChange(boolean selfChange) { - isAllowed = GoogleLocationUtils.isLocationAccessAllowed(context); + isAllowed = GoogleLocationUtils.isAllowed(context); } } @@ -117,7 +117,7 @@ public class MyTracksLocationManager { contentResolver = context.getContentResolver(); observer = new GoogleSettingsObserver(handler); - isAllowed = GoogleLocationUtils.isLocationAccessAllowed(context); + isAllowed = GoogleLocationUtils.isAllowed(context); contentResolver.registerContentObserver( GoogleLocationUtils.USE_LOCATION_FOR_SERVICES_URI, false, observer); @@ -135,8 +135,8 @@ public class MyTracksLocationManager { /** * Returns true if allowed to access the location manager. Returns true if - * there is no Google location settings or the Google location settings allows - * access to location data. + * there is no enforcement or the Google location settings allows access to + * location data. */ public boolean isAllowed() { return isAllowed; @@ -146,7 +146,7 @@ public class MyTracksLocationManager { * Returns true if gps provider is enabled. */ public boolean isGpsProviderEnabled() { - if (!isAllowed) { + if (!isAllowed()) { return false; } String provider = LocationManager.GPS_PROVIDER; @@ -165,7 +165,7 @@ public class MyTracksLocationManager { handler.post(new Runnable() { @Override public void run() { - if (!isAllowed) { + if (!isAllowed()) { requestLastLocation = null; locationListener.onLocationChanged(null); } else { diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/Api19Adapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/Api19Adapter.java new file mode 100644 index 000000000..9fc2f4eca --- /dev/null +++ b/MyTracks/src/com/google/android/apps/mytracks/util/Api19Adapter.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013 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 android.annotation.TargetApi; + +/** + * API level 19 specific implementation of the {@link ApiAdapter}. + * + * @author Jimmy Shih + */ +@TargetApi(19) +public class Api19Adapter extends Api17Adapter { + + @Override + public boolean hasLocationMode() { + return true; + } +} diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/Api8Adapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/Api8Adapter.java index d5b8cca9c..607d894f3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/Api8Adapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/Api8Adapter.java @@ -187,4 +187,9 @@ public class Api8Adapter implements ApiAdapter { ViewTreeObserver observer, OnGlobalLayoutListener listener) { observer.removeGlobalOnLayoutListener(listener); } + + @Override + public boolean hasLocationMode() { + return false; + } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java index d84465335..0e51f7fdd 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java @@ -245,5 +245,12 @@ public interface ApiAdapter { * @param listener the listener */ public void removeGlobalLayoutListener( - ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener); + ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener); + + /** + * Returns true if has location mode. + *

+ * Due to changes in API level 19. + */ + public boolean hasLocationMode(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java index 85edb5b28..7a55afd8b 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java @@ -31,7 +31,9 @@ public class ApiAdapterFactory { */ public static ApiAdapter getApiAdapter() { if (apiAdapter == null) { - if (Build.VERSION.SDK_INT >= 17) { + if (Build.VERSION.SDK_INT >= 19) { + apiAdapter = new Api19Adapter(); + } else if (Build.VERSION.SDK_INT >= 17) { apiAdapter = new Api17Adapter(); } else if (Build.VERSION.SDK_INT >= 16) { apiAdapter = new Api16Adapter(); diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/GoogleLocationUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/GoogleLocationUtils.java index 7dc3af163..dabac6251 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/GoogleLocationUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/GoogleLocationUtils.java @@ -45,17 +45,13 @@ public class GoogleLocationUtils { 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. - */ + // 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. - */ + // 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. */ @@ -74,23 +70,25 @@ public class GoogleLocationUtils { private GoogleLocationUtils() {} /** - * Returns true if the Google location settings is enforceable. - */ - public 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; - } - - /** - * Gets the location settings name. + * Gets the gps disabled message. * * @param context the context */ - public static String getLocationSettingsName(Context context) { - return context.getString(isEnforceable(context) ? R.string.gps_google_location_settings - : R.string.gps_location_access); + public static String getGpsDisabledMessage(Context context) { + int id = ApiAdapterFactory.getApiAdapter().hasLocationMode() ? R.string.gps_disabled_location_mode + : R.string.gps_disabled; + return context.getString(id, getLocationSettingsName(context)); + } + + /** + * Gets the gps disabled message when my location button is pressed. + * + * @param context the context + */ + public static String getGpsDisabledMyLocationMessage(Context context) { + int id = ApiAdapterFactory.getApiAdapter().hasLocationMode() ? R.string.gps_disabled_my_location_location_mode + : R.string.gps_disabled_my_location; + return context.getString(id, getLocationSettingsName(context)); } /** @@ -99,23 +97,68 @@ public class GoogleLocationUtils { * @param context the context */ public static Intent newLocationSettingsIntent(Context context) { - Intent intent = isEnforceable(context) ? new Intent( - GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS) - : new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + 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 location access is allowed. + * Returns true if there is no enforcement or google location settings allows + * access. * * @param context the context */ - public static boolean isLocationAccessAllowed(Context context) { + public static boolean isAllowed(Context context) { if (!isEnforceable(context)) { return true; } - return getUseLocationForServices(context) == USE_LOCATION_FOR_SERVICES_ON; + if (!ApiAdapterFactory.getApiAdapter().hasLocationMode()) { + // Before KitKat + return getUseLocationForServices(context) == USE_LOCATION_FOR_SERVICES_ON; + } else { + // KitKat+ + 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; + } + if (!ApiAdapterFactory.getApiAdapter().hasLocationMode()) { + // Before KitKat + return true; + } else { + // KitKat+ + 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; } /** diff --git a/MyTracksTest_E2E/AndroidManifest.xml b/MyTracksTest_E2E/AndroidManifest.xml index 935f50dc6..76e0ef041 100644 --- a/MyTracksTest_E2E/AndroidManifest.xml +++ b/MyTracksTest_E2E/AndroidManifest.xml @@ -29,7 +29,7 @@ limitations under the License. + android:targetSdkVersion="19" /> diff --git a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/GoToMyLocationTest.java b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/GoToMyLocationTest.java index 9bacf9672..ac3080705 100644 --- a/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/GoToMyLocationTest.java +++ b/MyTracksTest_E2E/src/com/google/android/apps/mytracks/endtoendtest/common/GoToMyLocationTest.java @@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.endtoendtest.common; import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils; import com.google.android.apps.mytracks.util.GoogleLocationUtils; -import com.google.android.maps.mytracks.R; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; @@ -52,11 +51,8 @@ public class GoToMyLocationTest extends ActivityInstrumentationTestCase2