forked from upstream-mirrors/OpenTracks
Add new location setting policy
This commit is contained in:
@@ -865,6 +865,29 @@ limitations under the License.
|
|||||||
%1$s location settings. E.g., 'Google location settings'.">
|
%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.
|
GPS not enabled. Tap here to go to %1$s. Press the back button to return.
|
||||||
</string>
|
</string>
|
||||||
|
<string name="gps_disabled_location_mode"
|
||||||
|
translation_description="In the main view, the message on the top asking the user to select the
|
||||||
|
high-accuracy location mode.
|
||||||
|
|
||||||
|
%1$s location settings. E.g., 'Google location settings'.">
|
||||||
|
Select high-accuracy location mode. Tap here to go to %1$s. Press the back button to return.
|
||||||
|
</string>
|
||||||
|
<string name="gps_disabled_my_location"
|
||||||
|
translation_description="When pressing the my location button, the message indicating that the
|
||||||
|
current location cannot be determined.
|
||||||
|
|
||||||
|
%1$s location settings. E.g., 'Google location settings'.">
|
||||||
|
Current location cannot be determined. Enable more location sources at %1$s & make sure the
|
||||||
|
sky is visible. Try again later.
|
||||||
|
</string>
|
||||||
|
<string name="gps_disabled_my_location_location_mode"
|
||||||
|
translation_description="When pressing the my location button, the message indicating that the
|
||||||
|
current location cannot be determined.
|
||||||
|
|
||||||
|
%1$s location settings. E.g., 'Google location settings'.">
|
||||||
|
Current location cannot be determined. Select the high-accuracy location mode at %1$s & make
|
||||||
|
sure the sky is visible. Try again later.
|
||||||
|
</string>
|
||||||
<string name="gps_not_found"
|
<string name="gps_not_found"
|
||||||
translation_description="On application start, message when there is no GPS on the device.">
|
translation_description="On application start, message when there is no GPS on the device.">
|
||||||
No GPS is found on the device.
|
No GPS is found on the device.
|
||||||
@@ -1272,16 +1295,6 @@ limitations under the License.
|
|||||||
Voice frequency
|
Voice frequency
|
||||||
</string>
|
</string>
|
||||||
|
|
||||||
<!-- My Location -->
|
|
||||||
<string name="my_location_no_gps"
|
|
||||||
translation_description="When displaying the user's current location, a status message
|
|
||||||
indicating that the current location cannot be determined.
|
|
||||||
|
|
||||||
%1$s location settings. E.g., 'Google location settings'.">
|
|
||||||
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.
|
|
||||||
</string>
|
|
||||||
|
|
||||||
<!-- My Tracks -->
|
<!-- My Tracks -->
|
||||||
<string name="my_tracks_app_name"
|
<string name="my_tracks_app_name"
|
||||||
translation_description="The My Tracks application name.">
|
translation_description="The My Tracks application name.">
|
||||||
|
|||||||
@@ -163,9 +163,8 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
getActivity(), Looper.myLooper(), true);
|
getActivity(), Looper.myLooper(), true);
|
||||||
if (!myTracksLocationManager.isAllowed()) {
|
if (!myTracksLocationManager.isGpsProviderEnabled()) {
|
||||||
String message = getString(R.string.my_location_no_gps,
|
String message = GoogleLocationUtils.getGpsDisabledMyLocationMessage(getActivity());
|
||||||
GoogleLocationUtils.getLocationSettingsName(getActivity()));
|
|
||||||
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
|
||||||
myTracksLocationManager.close();
|
myTracksLocationManager.close();
|
||||||
} else {
|
} else {
|
||||||
@@ -723,8 +722,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
message = null;
|
message = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message = getString(
|
message = GoogleLocationUtils.getGpsDisabledMessage(getActivity());
|
||||||
R.string.gps_disabled, GoogleLocationUtils.getLocationSettingsName(getActivity()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class MyTracksLocationManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange) {
|
public void onChange(boolean selfChange) {
|
||||||
isAllowed = GoogleLocationUtils.isLocationAccessAllowed(context);
|
isAllowed = GoogleLocationUtils.isAllowed(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ public class MyTracksLocationManager {
|
|||||||
contentResolver = context.getContentResolver();
|
contentResolver = context.getContentResolver();
|
||||||
observer = new GoogleSettingsObserver(handler);
|
observer = new GoogleSettingsObserver(handler);
|
||||||
|
|
||||||
isAllowed = GoogleLocationUtils.isLocationAccessAllowed(context);
|
isAllowed = GoogleLocationUtils.isAllowed(context);
|
||||||
|
|
||||||
contentResolver.registerContentObserver(
|
contentResolver.registerContentObserver(
|
||||||
GoogleLocationUtils.USE_LOCATION_FOR_SERVICES_URI, false, observer);
|
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
|
* Returns true if allowed to access the location manager. Returns true if
|
||||||
* there is no Google location settings or the Google location settings allows
|
* there is no enforcement or the Google location settings allows access to
|
||||||
* access to location data.
|
* location data.
|
||||||
*/
|
*/
|
||||||
public boolean isAllowed() {
|
public boolean isAllowed() {
|
||||||
return isAllowed;
|
return isAllowed;
|
||||||
@@ -146,7 +146,7 @@ public class MyTracksLocationManager {
|
|||||||
* Returns true if gps provider is enabled.
|
* Returns true if gps provider is enabled.
|
||||||
*/
|
*/
|
||||||
public boolean isGpsProviderEnabled() {
|
public boolean isGpsProviderEnabled() {
|
||||||
if (!isAllowed) {
|
if (!isAllowed()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String provider = LocationManager.GPS_PROVIDER;
|
String provider = LocationManager.GPS_PROVIDER;
|
||||||
@@ -165,7 +165,7 @@ public class MyTracksLocationManager {
|
|||||||
handler.post(new Runnable() {
|
handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!isAllowed) {
|
if (!isAllowed()) {
|
||||||
requestLastLocation = null;
|
requestLastLocation = null;
|
||||||
locationListener.onLocationChanged(null);
|
locationListener.onLocationChanged(null);
|
||||||
} else {
|
} 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) {
|
ViewTreeObserver observer, OnGlobalLayoutListener listener) {
|
||||||
observer.removeGlobalOnLayoutListener(listener);
|
observer.removeGlobalOnLayoutListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasLocationMode() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,5 +245,12 @@ public interface ApiAdapter {
|
|||||||
* @param listener the listener
|
* @param listener the listener
|
||||||
*/
|
*/
|
||||||
public void removeGlobalLayoutListener(
|
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() {
|
public static ApiAdapter getApiAdapter() {
|
||||||
if (apiAdapter == null) {
|
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();
|
apiAdapter = new Api17Adapter();
|
||||||
} else if (Build.VERSION.SDK_INT >= 16) {
|
} else if (Build.VERSION.SDK_INT >= 16) {
|
||||||
apiAdapter = new Api16Adapter();
|
apiAdapter = new Api16Adapter();
|
||||||
|
|||||||
@@ -45,17 +45,13 @@ public class GoogleLocationUtils {
|
|||||||
private static final String
|
private static final String
|
||||||
ACTION_GOOGLE_APPS_LOCATION_SETTINGS = "com.google.android.gsf.GOOGLE_APPS_LOCATION_SETTINGS";
|
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;
|
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;
|
public static final int USE_LOCATION_FOR_SERVICES_ON = 1;
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* The user has neither agreed nor disagreed to use location for Google
|
* The user has neither agreed nor disagreed to use location for Google
|
||||||
* services yet.
|
* services yet.
|
||||||
*/
|
*/
|
||||||
@@ -74,23 +70,25 @@ public class GoogleLocationUtils {
|
|||||||
private GoogleLocationUtils() {}
|
private GoogleLocationUtils() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the Google location settings is enforceable.
|
* Gets the gps disabled message.
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*
|
*
|
||||||
* @param context the context
|
* @param context the context
|
||||||
*/
|
*/
|
||||||
public static String getLocationSettingsName(Context context) {
|
public static String getGpsDisabledMessage(Context context) {
|
||||||
return context.getString(isEnforceable(context) ? R.string.gps_google_location_settings
|
int id = ApiAdapterFactory.getApiAdapter().hasLocationMode() ? R.string.gps_disabled_location_mode
|
||||||
: R.string.gps_location_access);
|
: 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
|
* @param context the context
|
||||||
*/
|
*/
|
||||||
public static Intent newLocationSettingsIntent(Context context) {
|
public static Intent newLocationSettingsIntent(Context context) {
|
||||||
Intent intent = isEnforceable(context) ? new Intent(
|
Intent intent = new Intent(useGoogleLocationSettings(context) ? ACTION_GOOGLE_LOCATION_SETTINGS
|
||||||
GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS)
|
: Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
||||||
: new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
return intent;
|
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
|
* @param context the context
|
||||||
*/
|
*/
|
||||||
public static boolean isLocationAccessAllowed(Context context) {
|
public static boolean isAllowed(Context context) {
|
||||||
if (!isEnforceable(context)) {
|
if (!isEnforceable(context)) {
|
||||||
return true;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ limitations under the License.
|
|||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
android:minSdkVersion="8"
|
android:minSdkVersion="8"
|
||||||
android:targetSdkVersion="17" />
|
android:targetSdkVersion="19" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:label="My Tracks Endtoend Tests" >
|
android:label="My Tracks Endtoend Tests" >
|
||||||
|
|||||||
+1
-5
@@ -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.TrackListActivity;
|
||||||
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
|
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
|
||||||
import com.google.android.apps.mytracks.util.GoogleLocationUtils;
|
import com.google.android.apps.mytracks.util.GoogleLocationUtils;
|
||||||
import com.google.android.maps.mytracks.R;
|
|
||||||
|
|
||||||
import android.app.Instrumentation;
|
import android.app.Instrumentation;
|
||||||
import android.test.ActivityInstrumentationTestCase2;
|
import android.test.ActivityInstrumentationTestCase2;
|
||||||
@@ -52,11 +51,8 @@ public class GoToMyLocationTest extends ActivityInstrumentationTestCase2<TrackLi
|
|||||||
public void testGotoMyLocation() {
|
public void testGotoMyLocation() {
|
||||||
EndToEndTestUtils.findAndClickMyLocation(activityMyTracks);
|
EndToEndTestUtils.findAndClickMyLocation(activityMyTracks);
|
||||||
if (EndToEndTestUtils.isEmulator) {
|
if (EndToEndTestUtils.isEmulator) {
|
||||||
String setting = activityMyTracks.getString(
|
|
||||||
GoogleLocationUtils.isEnforceable(activityMyTracks) ? R.string.gps_google_location_settings
|
|
||||||
: R.string.gps_location_access);
|
|
||||||
EndToEndTestUtils.SOLO.waitForText(
|
EndToEndTestUtils.SOLO.waitForText(
|
||||||
activityMyTracks.getString(R.string.my_location_no_gps, setting), 1,
|
GoogleLocationUtils.getGpsDisabledMyLocationMessage(activityMyTracks), 1,
|
||||||
EndToEndTestUtils.SHORT_WAIT_TIME);
|
EndToEndTestUtils.SHORT_WAIT_TIME);
|
||||||
} else {
|
} else {
|
||||||
// TODO How to verify the location is shown on the map.
|
// TODO How to verify the location is shown on the map.
|
||||||
|
|||||||
Reference in New Issue
Block a user