SetupWizard: Update Location services page

* Remove usage of deprecated setLocationProviderEnabled and
  isLocationProviderEnabled
* Use BroadcastReceiver to listen for changes to mode
* Replace GPS option with Battery Saving option (there is no longer a
  mode available in Android which disables GPS)

TODO: Create CMStats entry for ENABLE_BATTERY_SAVING_LOCATION

Change-Id: I2efe69125518637ae9b7d0dce285c39dae654d4c
This commit is contained in:
Matt Mower 2015-11-10 18:37:09 -06:00
parent b64d5e7676
commit ea2d42d654
3 changed files with 132 additions and 88 deletions

View File

@ -118,7 +118,7 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/gps" android:id="@+id/battery_saving"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -128,7 +128,7 @@
android:clickable="true"> android:clickable="true">
<CheckBox <CheckBox
android:id="@+id/gps_checkbox" android:id="@+id/battery_saving_checkbox"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="top" android:layout_gravity="top"
@ -138,7 +138,7 @@
<TextView <TextView
android:id="@+id/gps_summary" android:id="@+id/battery_saving_summary"
android:layout_width="0px" android:layout_width="0px"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -148,7 +148,7 @@
android:layout_marginLeft="@dimen/location_text_margin_left" android:layout_marginLeft="@dimen/location_text_margin_left"
android:layout_marginRight="@dimen/location_text_margin_right" android:layout_marginRight="@dimen/location_text_margin_right"
android:paddingBottom="@dimen/content_margin_bottom" android:paddingBottom="@dimen/content_margin_bottom"
android:text="@string/location_gps" android:text="@string/location_battery_saving"
android:maxLines="5" /> android:maxLines="5" />
</LinearLayout> </LinearLayout>

View File

@ -51,8 +51,7 @@
<string name="other_services_summary">These services put Google to work for you, and you can turn them on or off at any time. Data will be used in accordance with Google\'s <xliff:g id="name" example="Privacy Policy">%s</xliff:g>.</string> <string name="other_services_summary">These services put Google to work for you, and you can turn them on or off at any time. Data will be used in accordance with Google\'s <xliff:g id="name" example="Privacy Policy">%s</xliff:g>.</string>
<string name="location_services_summary">Location services allows system and third party apps to gather and use data such as your approximate location. For example, an app may use your approximate location to locate nearby coffee shops.</string> <string name="location_services_summary">Location services allows system and third party apps to gather and use data such as your approximate location. For example, an app may use your approximate location to locate nearby coffee shops.</string>
<string name="location_access_summary"><b>Allow apps that have asked your permission</b> to use your location information. This may include your current location and past locations.</string> <string name="location_access_summary"><b>Allow apps that have asked your permission</b> to use your location information. This may include your current location and past locations.</string>
<string name="location_gps" product="tablet"><b>Improve location accuracy</b> by allowing apps to use the GPS on your tablet.</string> <string name="location_battery_saving"><b>Reduce battery consumption</b> by restricting the number of GPS updates per hour.</string>
<string name="location_gps" product="default"><b>Improve location accuracy</b> by allowing apps to use the GPS on your phone.</string>
<string name="location_network"><b>Use Wi-Fi</b> to help apps determine your location.</string> <string name="location_network"><b>Use Wi-Fi</b> to help apps determine your location.</string>
<string name="location_network_telephony"><b>Use Wi-Fi and mobile networks</b> to help apps determine your location.</string> <string name="location_network_telephony"><b>Use Wi-Fi and mobile networks</b> to help apps determine your location.</string>
<string name="location_network_gms"><b>Use Google\'s location service</b> to help apps determine your location. This means sending anonymous location data to Google, even when no apps are running.</string> <string name="location_network_gms"><b>Use Google\'s location service</b> to help apps determine your location. This means sending anonymous location data to Google, even when no apps are running.</string>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2013 The CyanogenMod Project * Copyright (C) 2015 The CyanogenMod Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,12 +19,10 @@ package com.cyanogenmod.setupwizard.setup;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.backup.IBackupManager; import android.app.backup.IBackupManager;
import android.content.ComponentName; import android.content.BroadcastReceiver;
import android.content.ContentQueryMap;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager; import android.location.LocationManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -44,12 +42,8 @@ import com.cyanogenmod.setupwizard.R;
import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.SetupWizardApp;
import com.cyanogenmod.setupwizard.cmstats.SetupStats; import com.cyanogenmod.setupwizard.cmstats.SetupStats;
import com.cyanogenmod.setupwizard.ui.SetupPageFragment; import com.cyanogenmod.setupwizard.ui.SetupPageFragment;
import com.cyanogenmod.setupwizard.ui.WebViewDialogFragment;
import com.cyanogenmod.setupwizard.util.SetupWizardUtils; import com.cyanogenmod.setupwizard.util.SetupWizardUtils;
import java.util.Observable;
import java.util.Observer;
public class OtherSettingsPage extends SetupPage { public class OtherSettingsPage extends SetupPage {
private static final String TAG = "OtherSettingsPage"; private static final String TAG = "OtherSettingsPage";
@ -92,22 +86,25 @@ public class OtherSettingsPage extends SetupPage {
private View mBackupRow; private View mBackupRow;
private View mLocationRow; private View mLocationRow;
private View mGpsRow; private View mBatteryRow;
private View mNetworkRow; private View mNetworkRow;
private CheckBox mBackup; private CheckBox mBackup;
private CheckBox mNetwork; private CheckBox mNetwork;
private CheckBox mGps; private CheckBox mBattery;
private CheckBox mLocationAccess; private CheckBox mLocationAccess;
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private IBackupManager mBackupManager; private IBackupManager mBackupManager;
// These provide support for receiving notification when Location Manager settings change. /** Broadcast intent action when the location mode is about to change. */
// This is necessary because the Network Location Provider can change settings private static final String MODE_CHANGING_ACTION =
// if the user does not confirm enabling the provider. "com.android.settings.location.MODE_CHANGING";
private ContentQueryMap mContentQueryMap; private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
private Observer mSettingsObserver; private static final String NEW_MODE_KEY = "NEW_MODE";
private int mCurrentMode = Settings.Secure.LOCATION_MODE_OFF;
private BroadcastReceiver mReceiver;
private View.OnClickListener mBackupClickListener = new View.OnClickListener() { private View.OnClickListener mBackupClickListener = new View.OnClickListener() {
@ -124,19 +121,17 @@ public class OtherSettingsPage extends SetupPage {
} }
}; };
private View.OnClickListener mGpsClickListener = new View.OnClickListener() { private View.OnClickListener mBatteryClickListener = new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Settings.Secure.setLocationProviderEnabled(mContentResolver, onToggleBatterySaving(!mBattery.isChecked());
LocationManager.GPS_PROVIDER, !mGps.isChecked());
} }
}; };
private View.OnClickListener mNetworkClickListener = new View.OnClickListener() { private View.OnClickListener mNetworkClickListener = new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Settings.Secure.setLocationProviderEnabled(mContentResolver, onToggleNetwork(!mNetwork.isChecked());
LocationManager.NETWORK_PROVIDER, !mNetwork.isChecked());
} }
}; };
@ -181,9 +176,9 @@ public class OtherSettingsPage extends SetupPage {
mLocationRow = mRootView.findViewById(R.id.location); mLocationRow = mRootView.findViewById(R.id.location);
mLocationRow.setOnClickListener(mLocationClickListener); mLocationRow.setOnClickListener(mLocationClickListener);
mLocationAccess = (CheckBox) mRootView.findViewById(R.id.location_checkbox); mLocationAccess = (CheckBox) mRootView.findViewById(R.id.location_checkbox);
mGpsRow = mRootView.findViewById(R.id.gps); mBatteryRow = mRootView.findViewById(R.id.battery_saving);
mGpsRow.setOnClickListener(mGpsClickListener); mBatteryRow.setOnClickListener(mBatteryClickListener);
mGps = (CheckBox) mRootView.findViewById(R.id.gps_checkbox); mBattery = (CheckBox) mRootView.findViewById(R.id.battery_saving_checkbox);
mNetworkRow = mRootView.findViewById(R.id.network); mNetworkRow = mRootView.findViewById(R.id.network);
mNetworkRow.setOnClickListener(mNetworkClickListener); mNetworkRow.setOnClickListener(mNetworkClickListener);
mNetwork = (CheckBox) mRootView.findViewById(R.id.network_checkbox); mNetwork = (CheckBox) mRootView.findViewById(R.id.network_checkbox);
@ -205,40 +200,22 @@ public class OtherSettingsPage extends SetupPage {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
updateLocationToggles(); refreshLocationMode();
updateBackupToggle(); updateBackupToggle();
if (mSettingsObserver == null) { }
mSettingsObserver = new Observer() {
public void update(Observable o, Object arg) { @Override
updateLocationToggles(); public void onCreate(Bundle savedInstanceState) {
updateBackupToggle(); super.onCreate(savedInstanceState);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Received location mode change intent: " + intent);
} }
}; refreshLocationMode();
} }
};
mContentQueryMap.addObserver(mSettingsObserver);
}
@Override
public void onStart() {
super.onStart();
// listen for Location Manager settings changes
Cursor settingsCursor = getActivity().getContentResolver()
.query(Settings.Secure.CONTENT_URI, null,
"(" + Settings.System.NAME + "=?)",
new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
null);
mContentQueryMap =
new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
}
@Override
public void onStop() {
super.onStop();
if (mSettingsObserver != null) {
mContentQueryMap.deleteObserver(mSettingsObserver);
}
mContentQueryMap.close();
} }
private boolean isBackupRestoreEnabled() { private boolean isBackupRestoreEnabled() {
@ -263,40 +240,108 @@ public class OtherSettingsPage extends SetupPage {
updateBackupToggle(); updateBackupToggle();
} }
private void updateLocationToggles() { private void setLocationMode(int mode) {
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( Intent intent = new Intent(MODE_CHANGING_ACTION);
mContentResolver, LocationManager.GPS_PROVIDER); intent.putExtra(CURRENT_MODE_KEY, mCurrentMode);
boolean networkEnabled = Settings.Secure.isLocationProviderEnabled( intent.putExtra(NEW_MODE_KEY, mode);
mContentResolver, LocationManager.NETWORK_PROVIDER); getActivity().sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
mGps.setChecked(gpsEnabled); Settings.Secure.putInt(mContentResolver, Settings.Secure.LOCATION_MODE, mode);
SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED, refreshLocationMode();
SetupStats.Action.ENABLE_GPS_LOCATION, }
SetupStats.Label.CHECKED, String.valueOf(gpsEnabled));
mNetwork.setChecked(networkEnabled); private void refreshLocationMode() {
SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED, int mode = Settings.Secure.getInt(mContentResolver, Settings.Secure.LOCATION_MODE,
SetupStats.Action.ENABLE_NETWORK_LOCATION, Settings.Secure.LOCATION_MODE_OFF);
SetupStats.Label.CHECKED, String.valueOf(networkEnabled));
mLocationAccess.setChecked(gpsEnabled || networkEnabled); if (mCurrentMode != mode) {
mGps.setEnabled(gpsEnabled || networkEnabled); mCurrentMode = mode;
mGpsRow.setEnabled(gpsEnabled || networkEnabled); if (Log.isLoggable(TAG, Log.INFO)) {
mNetwork.setEnabled(gpsEnabled || networkEnabled); Log.i(TAG, "Location mode has been changed");
mNetworkRow.setEnabled(gpsEnabled || networkEnabled); }
updateLocationToggles(mode);
}
}
private void updateLocationToggles(int mode) {
switch (mode) {
case Settings.Secure.LOCATION_MODE_OFF:
mLocationAccess.setChecked(false);
mBattery.setChecked(false);
mBattery.setEnabled(false);
mBatteryRow.setEnabled(false);
mNetwork.setChecked(false);
mNetwork.setEnabled(false);
mNetworkRow.setEnabled(false);
break;
case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
mLocationAccess.setChecked(true);
mBattery.setChecked(false);
mBattery.setEnabled(true);
mBatteryRow.setEnabled(true);
mNetwork.setChecked(false);
mNetwork.setEnabled(true);
mNetworkRow.setEnabled(true);
break;
case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
mLocationAccess.setChecked(true);
mBattery.setChecked(true);
mNetwork.setChecked(false);
mNetwork.setEnabled(false);
mNetworkRow.setEnabled(false);
break;
case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
mLocationAccess.setChecked(true);
mNetwork.setChecked(true);
mBattery.setChecked(false);
mBattery.setEnabled(false);
mBatteryRow.setEnabled(false);
break;
default:
mLocationAccess.setChecked(false);
mBattery.setChecked(false);
mBattery.setEnabled(false);
mBatteryRow.setEnabled(false);
mNetwork.setChecked(false);
mNetwork.setEnabled(false);
mNetworkRow.setEnabled(false);
break;
}
} }
private void onToggleLocationAccess(boolean checked) { private void onToggleLocationAccess(boolean checked) {
SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED, SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
SetupStats.Action.ENABLE_LOCATION, SetupStats.Action.ENABLE_LOCATION,
SetupStats.Label.CHECKED, String.valueOf(checked)); SetupStats.Label.CHECKED, String.valueOf(checked));
Settings.Secure.setLocationProviderEnabled(mContentResolver,
LocationManager.GPS_PROVIDER, checked); if (checked) {
mGps.setEnabled(checked); setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
mGpsRow.setEnabled(checked); } else {
Settings.Secure.setLocationProviderEnabled(mContentResolver, setLocationMode(Settings.Secure.LOCATION_MODE_OFF);
LocationManager.NETWORK_PROVIDER, checked); }
mNetwork.setEnabled(checked);
mNetworkRow.setEnabled(checked);
updateLocationToggles();
} }
private void onToggleBatterySaving(boolean checked) {
/* SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
SetupStats.Action.ENABLE_BATTERY_SAVING_LOCATION,
SetupStats.Label.CHECKED, String.valueOf(checked)); */
if (checked) {
setLocationMode(Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
} else {
setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
}
}
private void onToggleNetwork(boolean checked) {
SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
SetupStats.Action.ENABLE_NETWORK_LOCATION,
SetupStats.Label.CHECKED, String.valueOf(checked));
if (checked) {
setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
} else {
setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
}
}
} }
} }