2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.location;
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 import android.preference.PreferenceActivity;
25 import android.preference.PreferenceCategory;
26 import android.preference.PreferenceScreen;
27 import android.view.Gravity;
28 import android.widget.CompoundButton;
29 import android.widget.Switch;
31 import com.android.settings.R;
32 import com.android.settings.fuelgauge.BatteryStatsHelper;
35 * Location access settings.
37 public class LocationSettings extends LocationSettingsBase
38 implements CompoundButton.OnCheckedChangeListener {
39 /** Key for preference screen "Mode" */
40 private static final String KEY_LOCATION_MODE = "location_mode";
41 /** Key for preference category "Recent location requests" */
42 private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests";
43 /** Key for preference category "Location services" */
44 private static final String KEY_LOCATION_SERVICES = "location_services";
46 private Switch mSwitch;
47 private boolean mValidListener;
48 private PreferenceScreen mLocationMode;
49 private PreferenceCategory mRecentLocationRequests;
50 private PreferenceCategory mLocationServices;
52 private BatteryStatsHelper mStatsHelper;
54 public LocationSettings() {
55 mValidListener = false;
59 public void onActivityResult(int requestCode, int resultCode, Intent data) {
60 super.onActivityResult(requestCode, resultCode, data);
61 createPreferenceHierarchy();
65 public void onAttach(Activity activity) {
66 super.onAttach(activity);
67 mStatsHelper = new BatteryStatsHelper(activity, null);
71 public void onCreate(Bundle icicle) {
72 super.onCreate(icicle);
73 mStatsHelper.create(icicle);
77 public void onResume() {
79 mSwitch = new Switch(getActivity());
80 mSwitch.setOnCheckedChangeListener(this);
81 mValidListener = true;
82 createPreferenceHierarchy();
86 public void onPause() {
88 mValidListener = false;
89 mSwitch.setOnCheckedChangeListener(null);
94 public void onDestroy() {
96 mStatsHelper.destroy();
99 private PreferenceScreen createPreferenceHierarchy() {
100 PreferenceScreen root = getPreferenceScreen();
104 addPreferencesFromResource(R.xml.location_settings);
105 root = getPreferenceScreen();
107 mLocationMode = (PreferenceScreen) root.findPreference(KEY_LOCATION_MODE);
108 mLocationMode.setOnPreferenceClickListener(
109 new Preference.OnPreferenceClickListener() {
111 public boolean onPreferenceClick(Preference preference) {
112 PreferenceActivity activity = (PreferenceActivity) getActivity();
113 activity.startPreferencePanel(
114 LocationMode.class.getName(), null,
115 R.string.location_mode_screen_title, null, LocationSettings.this,
120 mRecentLocationRequests =
121 (PreferenceCategory) root.findPreference(KEY_RECENT_LOCATION_REQUESTS);
122 mLocationServices = (PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES);
124 PreferenceActivity activity = (PreferenceActivity) getActivity();
125 RecentLocationApps recentApps = new RecentLocationApps(activity, mStatsHelper);
126 recentApps.fillAppList(mRecentLocationRequests);
128 SettingsInjector.addInjectedSettings(mLocationServices, activity, getPreferenceManager());
130 // Only show the master switch when we're not in multi-pane mode, and not being used as
132 if (activity.onIsHidingHeaders() || !activity.onIsMultiPane()) {
133 final int padding = activity.getResources().getDimensionPixelSize(
134 R.dimen.action_bar_switch_padding);
135 mSwitch.setPaddingRelative(0, 0, padding, 0);
136 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
137 ActionBar.DISPLAY_SHOW_CUSTOM);
138 activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
139 ActionBar.LayoutParams.WRAP_CONTENT,
140 ActionBar.LayoutParams.WRAP_CONTENT,
141 Gravity.CENTER_VERTICAL | Gravity.END));
144 setHasOptionsMenu(true);
146 refreshLocationMode();
151 public int getHelpResource() {
152 return R.string.help_url_location_access;
156 public void onModeChanged(int mode) {
158 case MODE_LOCATION_OFF:
159 mLocationMode.setSummary(R.string.location_mode_location_off_title);
161 case MODE_SENSORS_ONLY:
162 mLocationMode.setSummary(R.string.location_mode_sensors_only_title);
164 case MODE_BATTERY_SAVING:
165 mLocationMode.setSummary(R.string.location_mode_battery_saving_title);
167 case MODE_HIGH_ACCURACY:
168 mLocationMode.setSummary(R.string.location_mode_high_accuracy_title);
174 boolean enabled = (mode != MODE_LOCATION_OFF);
175 mLocationMode.setEnabled(enabled);
176 mRecentLocationRequests.setEnabled(enabled);
177 mLocationServices.setEnabled(enabled);
179 if (enabled != mSwitch.isChecked()) {
180 // set listener to null so that that code below doesn't trigger onCheckedChanged()
181 if (mValidListener) {
182 mSwitch.setOnCheckedChangeListener(null);
184 mSwitch.setChecked(enabled);
185 if (mValidListener) {
186 mSwitch.setOnCheckedChangeListener(this);
192 * Listens to the state change of the location master switch.
195 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
197 setLocationMode(MODE_HIGH_ACCURACY);
199 setLocationMode(MODE_LOCATION_OFF);