OSDN Git Service

merge in klp-release history after reset to klp-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / location / LocationSettings.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings.location;
18
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;
30
31 import com.android.settings.R;
32 import com.android.settings.fuelgauge.BatteryStatsHelper;
33
34 /**
35  * Location access settings.
36  */
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";
45
46     private Switch mSwitch;
47     private boolean mValidListener;
48     private PreferenceScreen mLocationMode;
49     private PreferenceCategory mRecentLocationRequests;
50     private PreferenceCategory mLocationServices;
51
52     private BatteryStatsHelper mStatsHelper;
53
54     public LocationSettings() {
55         mValidListener = false;
56     }
57
58     @Override
59     public void onActivityResult(int requestCode, int resultCode, Intent data) {
60         super.onActivityResult(requestCode, resultCode, data);
61         createPreferenceHierarchy();
62     }
63
64     @Override
65     public void onAttach(Activity activity) {
66         super.onAttach(activity);
67         mStatsHelper = new BatteryStatsHelper(activity, null);
68     }
69
70     @Override
71     public void onCreate(Bundle icicle) {
72         super.onCreate(icicle);
73         mStatsHelper.create(icicle);
74     }
75
76     @Override
77     public void onResume() {
78         super.onResume();
79         mSwitch = new Switch(getActivity());
80         mSwitch.setOnCheckedChangeListener(this);
81         mValidListener = true;
82         createPreferenceHierarchy();
83     }
84
85     @Override
86     public void onPause() {
87         super.onPause();
88         mValidListener = false;
89         mSwitch.setOnCheckedChangeListener(null);
90         mStatsHelper.pause();
91     }
92
93     @Override
94     public void onDestroy() {
95         super.onDestroy();
96         mStatsHelper.destroy();
97     }
98
99     private PreferenceScreen createPreferenceHierarchy() {
100         PreferenceScreen root = getPreferenceScreen();
101         if (root != null) {
102             root.removeAll();
103         }
104         addPreferencesFromResource(R.xml.location_settings);
105         root = getPreferenceScreen();
106
107         mLocationMode = (PreferenceScreen) root.findPreference(KEY_LOCATION_MODE);
108         mLocationMode.setOnPreferenceClickListener(
109                 new Preference.OnPreferenceClickListener() {
110                     @Override
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,
116                                 0);
117                         return true;
118                     }
119                 });
120         mRecentLocationRequests =
121                 (PreferenceCategory) root.findPreference(KEY_RECENT_LOCATION_REQUESTS);
122         mLocationServices = (PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES);
123
124         PreferenceActivity activity = (PreferenceActivity) getActivity();
125         RecentLocationApps recentApps = new RecentLocationApps(activity, mStatsHelper);
126         recentApps.fillAppList(mRecentLocationRequests);
127
128         SettingsInjector.addInjectedSettings(mLocationServices, activity, getPreferenceManager());
129
130         // Only show the master switch when we're not in multi-pane mode, and not being used as
131         // Setup Wizard.
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));
142         }
143
144         setHasOptionsMenu(true);
145
146         refreshLocationMode();
147         return root;
148     }
149
150     @Override
151     public int getHelpResource() {
152         return R.string.help_url_location_access;
153     }
154
155     @Override
156     public void onModeChanged(int mode) {
157         switch (mode) {
158             case MODE_LOCATION_OFF:
159                 mLocationMode.setSummary(R.string.location_mode_location_off_title);
160                 break;
161             case MODE_SENSORS_ONLY:
162                 mLocationMode.setSummary(R.string.location_mode_sensors_only_title);
163                 break;
164             case MODE_BATTERY_SAVING:
165                 mLocationMode.setSummary(R.string.location_mode_battery_saving_title);
166                 break;
167             case MODE_HIGH_ACCURACY:
168                 mLocationMode.setSummary(R.string.location_mode_high_accuracy_title);
169                 break;
170             default:
171                 break;
172         }
173
174         boolean enabled = (mode != MODE_LOCATION_OFF);
175         mLocationMode.setEnabled(enabled);
176         mRecentLocationRequests.setEnabled(enabled);
177         mLocationServices.setEnabled(enabled);
178
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);
183             }
184             mSwitch.setChecked(enabled);
185             if (mValidListener) {
186                 mSwitch.setOnCheckedChangeListener(this);
187             }
188         }
189     }
190
191     /**
192      * Listens to the state change of the location master switch.
193      */
194     @Override
195     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
196         if (isChecked) {
197             setLocationMode(MODE_HIGH_ACCURACY);
198         } else {
199             setLocationMode(MODE_LOCATION_OFF);
200         }
201     }
202 }