Add new location setting policy

This commit is contained in:
Jimmy Shih
2013-11-26 15:45:33 -08:00
parent 8bb8a1c266
commit e813995b34
10 changed files with 153 additions and 56 deletions
@@ -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());
}
}
@@ -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 {
@@ -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;
}
}
@@ -187,4 +187,9 @@ public class Api8Adapter implements ApiAdapter {
ViewTreeObserver observer, OnGlobalLayoutListener listener) {
observer.removeGlobalOnLayoutListener(listener);
}
@Override
public boolean hasLocationMode() {
return false;
}
}
@@ -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.
* <p>
* Due to changes in API level 19.
*/
public boolean hasLocationMode();
}
@@ -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();
@@ -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;
}
/**