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
17package com.android.settings;
18
19
20import android.content.ContentQueryMap;
21import android.content.ContentResolver;
22import android.content.Intent;
23import android.database.Cursor;
24import android.location.LocationManager;
25import android.preference.CheckBoxPreference;
26import android.preference.Preference;
27import android.preference.Preference.OnPreferenceChangeListener;
28import android.preference.PreferenceScreen;
29import android.provider.Settings;
30
31import java.util.Observable;
32import java.util.Observer;
33
34/**
35 * Gesture lock pattern settings.
36 */
37public class LocationSettings extends SettingsPreferenceFragment
38        implements OnPreferenceChangeListener {
39
40    // Location Settings
41    private static final String KEY_LOCATION_NETWORK = "location_network";
42    private static final String KEY_LOCATION_GPS = "location_gps";
43    private static final String KEY_ASSISTED_GPS = "assisted_gps";
44    private static final String KEY_USE_LOCATION = "location_use_for_services";
45
46    private CheckBoxPreference mNetwork;
47    private CheckBoxPreference mGps;
48    private CheckBoxPreference mAssistedGps;
49    private CheckBoxPreference mUseLocation;
50
51    // These provide support for receiving notification when Location Manager settings change.
52    // This is necessary because the Network Location Provider can change settings
53    // if the user does not confirm enabling the provider.
54    private ContentQueryMap mContentQueryMap;
55
56    private Observer mSettingsObserver;
57
58    @Override
59    public void onStart() {
60        super.onStart();
61        // listen for Location Manager settings changes
62        Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null,
63                "(" + Settings.System.NAME + "=?)",
64                new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
65                null);
66        mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
67    }
68
69    @Override
70    public void onStop() {
71        super.onStop();
72        if (mSettingsObserver != null) {
73            mContentQueryMap.deleteObserver(mSettingsObserver);
74        }
75    }
76
77    private PreferenceScreen createPreferenceHierarchy() {
78        PreferenceScreen root = getPreferenceScreen();
79        if (root != null) {
80            root.removeAll();
81        }
82        addPreferencesFromResource(R.xml.location_settings);
83        root = getPreferenceScreen();
84
85        mNetwork = (CheckBoxPreference) root.findPreference(KEY_LOCATION_NETWORK);
86        mGps = (CheckBoxPreference) root.findPreference(KEY_LOCATION_GPS);
87        mAssistedGps = (CheckBoxPreference) root.findPreference(KEY_ASSISTED_GPS);
88        if (GoogleLocationSettingHelper.isAvailable(getActivity())) {
89            // GSF present, Add setting for 'Use My Location'
90            CheckBoxPreference useLocation = new CheckBoxPreference(getActivity());
91            useLocation.setKey(KEY_USE_LOCATION);
92            useLocation.setTitle(R.string.use_location_title);
93            useLocation.setSummary(R.string.use_location_summary);
94            useLocation.setChecked(
95                    GoogleLocationSettingHelper.getUseLocationForServices(getActivity())
96                    == GoogleLocationSettingHelper.USE_LOCATION_FOR_SERVICES_ON);
97            useLocation.setPersistent(false);
98            useLocation.setOnPreferenceChangeListener(this);
99            getPreferenceScreen().addPreference(useLocation);
100            mUseLocation = useLocation;
101        }
102
103        // Change the summary for wifi-only devices
104        if (Utils.isWifiOnly(getActivity())) {
105            mNetwork.setSummaryOn(R.string.location_neighborhood_level_wifi);
106        }
107
108        return root;
109    }
110
111    @Override
112    public void onResume() {
113        super.onResume();
114
115        // Make sure we reload the preference hierarchy since some of these settings
116        // depend on others...
117        createPreferenceHierarchy();
118        updateLocationToggles();
119
120        if (mSettingsObserver == null) {
121            mSettingsObserver = new Observer() {
122                public void update(Observable o, Object arg) {
123                    updateLocationToggles();
124                }
125            };
126            mContentQueryMap.addObserver(mSettingsObserver);
127        }
128    }
129
130    @Override
131    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
132
133        if (preference == mNetwork) {
134            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
135                    LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
136        } else if (preference == mGps) {
137            boolean enabled = mGps.isChecked();
138            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
139                    LocationManager.GPS_PROVIDER, enabled);
140            if (mAssistedGps != null) {
141                mAssistedGps.setEnabled(enabled);
142            }
143        } else if (preference == mAssistedGps) {
144            Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSISTED_GPS_ENABLED,
145                    mAssistedGps.isChecked() ? 1 : 0);
146        } else {
147            // If we didn't handle it, let preferences handle it.
148            return super.onPreferenceTreeClick(preferenceScreen, preference);
149        }
150
151        return true;
152    }
153
154    /*
155     * Creates toggles for each available location provider
156     */
157    private void updateLocationToggles() {
158        ContentResolver res = getContentResolver();
159        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
160                res, LocationManager.GPS_PROVIDER);
161        mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
162                res, LocationManager.NETWORK_PROVIDER));
163        mGps.setChecked(gpsEnabled);
164        if (mAssistedGps != null) {
165            mAssistedGps.setChecked(Settings.Secure.getInt(res,
166                    Settings.Secure.ASSISTED_GPS_ENABLED, 2) == 1);
167            mAssistedGps.setEnabled(gpsEnabled);
168        }
169    }
170
171    /**
172     * see confirmPatternThenDisableAndClear
173     */
174    @Override
175    public void onActivityResult(int requestCode, int resultCode, Intent data) {
176        super.onActivityResult(requestCode, resultCode, data);
177        createPreferenceHierarchy();
178    }
179
180    public boolean onPreferenceChange(Preference preference, Object value) {
181        if (preference == mUseLocation) {
182            boolean newValue = (value == null ? false : (Boolean) value);
183            GoogleLocationSettingHelper.setUseLocationForServices(getActivity(), newValue);
184            // We don't want to change the value immediately here, since the user may click
185            // disagree in the dialog that pops up. When the activity we just launched exits, this
186            // activity will be restated and the new value re-read, so the checkbox will get its
187            // new value then.
188            return false;
189        }
190        return true;
191    }
192}
193