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.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.location.LocationManager;
26import android.os.UserManager;
27import android.preference.CheckBoxPreference;
28import android.preference.Preference;
29import android.preference.PreferenceScreen;
30import android.preference.SwitchPreference;
31import android.provider.Settings;
32import android.util.AttributeSet;
33import android.view.View;
34import android.widget.TextView;
35
36import java.util.Observable;
37import java.util.Observer;
38
39/**
40 * Gesture lock pattern settings.
41 */
42public class LocationSettings extends SettingsPreferenceFragment
43        implements Preference.OnPreferenceChangeListener {
44
45    // Location Settings
46    private static final String KEY_LOCATION_TOGGLE = "location_toggle";
47    private static final String KEY_LOCATION_NETWORK = "location_network";
48    private static final String KEY_LOCATION_GPS = "location_gps";
49    private static final String KEY_ASSISTED_GPS = "assisted_gps";
50
51    private CheckBoxPreference mNetwork;
52    private CheckBoxPreference mGps;
53    private CheckBoxPreference mAssistedGps;
54    private SwitchPreference mLocationAccess;
55
56    // These provide support for receiving notification when Location Manager settings change.
57    // This is necessary because the Network Location Provider can change settings
58    // if the user does not confirm enabling the provider.
59    private ContentQueryMap mContentQueryMap;
60
61    private Observer mSettingsObserver;
62
63    @Override
64    public void onStart() {
65        super.onStart();
66        // listen for Location Manager settings changes
67        Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null,
68                "(" + Settings.System.NAME + "=?)",
69                new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
70                null);
71        mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
72    }
73
74    @Override
75    public void onStop() {
76        super.onStop();
77        if (mSettingsObserver != null) {
78            mContentQueryMap.deleteObserver(mSettingsObserver);
79        }
80        mContentQueryMap.close();
81    }
82
83    private PreferenceScreen createPreferenceHierarchy() {
84        PreferenceScreen root = getPreferenceScreen();
85        if (root != null) {
86            root.removeAll();
87        }
88        addPreferencesFromResource(R.xml.location_settings);
89        root = getPreferenceScreen();
90
91        mLocationAccess = (SwitchPreference) root.findPreference(KEY_LOCATION_TOGGLE);
92        mNetwork = (CheckBoxPreference) root.findPreference(KEY_LOCATION_NETWORK);
93        mGps = (CheckBoxPreference) root.findPreference(KEY_LOCATION_GPS);
94        mAssistedGps = (CheckBoxPreference) root.findPreference(KEY_ASSISTED_GPS);
95
96        // Only enable these controls if this user is allowed to change location
97        // sharing settings.
98        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
99        boolean isToggleAllowed = !um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION);
100        if (mLocationAccess != null) mLocationAccess.setEnabled(isToggleAllowed);
101        if (mNetwork != null) mNetwork.setEnabled(isToggleAllowed);
102        if (mGps != null) mGps.setEnabled(isToggleAllowed);
103        if (mAssistedGps != null) mAssistedGps.setEnabled(isToggleAllowed);
104
105        mLocationAccess.setOnPreferenceChangeListener(this);
106        return root;
107    }
108
109    @Override
110    public void onResume() {
111        super.onResume();
112
113        // Make sure we reload the preference hierarchy since some of these settings
114        // depend on others...
115        createPreferenceHierarchy();
116        updateLocationToggles();
117
118        if (mSettingsObserver == null) {
119            mSettingsObserver = new Observer() {
120                @Override
121                public void update(Observable o, Object arg) {
122                    updateLocationToggles();
123                }
124            };
125        }
126
127        mContentQueryMap.addObserver(mSettingsObserver);
128    }
129
130    @Override
131    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
132        final ContentResolver cr = getContentResolver();
133        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
134        if (preference == mNetwork) {
135            if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
136                Settings.Secure.setLocationProviderEnabled(cr,
137                        LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
138            }
139        } else if (preference == mGps) {
140            boolean enabled = mGps.isChecked();
141            if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
142                Settings.Secure.setLocationProviderEnabled(cr,
143                        LocationManager.GPS_PROVIDER, enabled);
144                if (mAssistedGps != null) {
145                    mAssistedGps.setEnabled(enabled);
146                }
147            }
148        } else if (preference == mAssistedGps) {
149            Settings.Global.putInt(cr, Settings.Global.ASSISTED_GPS_ENABLED,
150                    mAssistedGps.isChecked() ? 1 : 0);
151        } else {
152            // If we didn't handle it, let preferences handle it.
153            return super.onPreferenceTreeClick(preferenceScreen, preference);
154        }
155
156        return true;
157    }
158
159    /*
160     * Creates toggles for each available location provider
161     */
162    private void updateLocationToggles() {
163        ContentResolver res = getContentResolver();
164        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
165                res, LocationManager.GPS_PROVIDER);
166        boolean networkEnabled = Settings.Secure.isLocationProviderEnabled(
167                res, LocationManager.NETWORK_PROVIDER);
168        mGps.setChecked(gpsEnabled);
169        mNetwork.setChecked(networkEnabled);
170        mLocationAccess.setChecked(gpsEnabled || networkEnabled);
171        if (mAssistedGps != null) {
172            mAssistedGps.setChecked(Settings.Global.getInt(res,
173                    Settings.Global.ASSISTED_GPS_ENABLED, 2) == 1);
174            mAssistedGps.setEnabled(gpsEnabled);
175        }
176    }
177
178    /**
179     * see confirmPatternThenDisableAndClear
180     */
181    @Override
182    public void onActivityResult(int requestCode, int resultCode, Intent data) {
183        super.onActivityResult(requestCode, resultCode, data);
184        createPreferenceHierarchy();
185    }
186
187    /** Enable or disable all providers when the master toggle is changed. */
188    private void onToggleLocationAccess(boolean checked) {
189        final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
190        if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
191            return;
192        }
193        final ContentResolver cr = getContentResolver();
194        Settings.Secure.setLocationProviderEnabled(cr,
195                LocationManager.GPS_PROVIDER, checked);
196        Settings.Secure.setLocationProviderEnabled(cr,
197                LocationManager.NETWORK_PROVIDER, checked);
198        updateLocationToggles();
199    }
200
201    @Override
202    public boolean onPreferenceChange(Preference pref, Object newValue) {
203        if (pref.getKey().equals(KEY_LOCATION_TOGGLE)) {
204            onToggleLocationAccess((Boolean) newValue);
205        }
206        return true;
207    }
208
209    @Override
210    public int getHelpResource() {
211        return R.string.help_url_location_access;
212    }
213}
214
215class WrappingSwitchPreference extends SwitchPreference {
216
217    public WrappingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
218        super(context, attrs, defStyle);
219    }
220
221    public WrappingSwitchPreference(Context context, AttributeSet attrs) {
222        super(context, attrs);
223    }
224
225    @Override
226    protected void onBindView(View view) {
227        super.onBindView(view);
228
229        TextView title = (TextView) view.findViewById(android.R.id.title);
230        if (title != null) {
231            title.setSingleLine(false);
232            title.setMaxLines(3);
233        }
234    }
235}
236
237class WrappingCheckBoxPreference extends CheckBoxPreference {
238
239    public WrappingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
240        super(context, attrs, defStyle);
241    }
242
243    public WrappingCheckBoxPreference(Context context, AttributeSet attrs) {
244        super(context, attrs);
245    }
246
247    @Override
248    protected void onBindView(View view) {
249        super.onBindView(view);
250
251        TextView title = (TextView) view.findViewById(android.R.id.title);
252        if (title != null) {
253            title.setSingleLine(false);
254            title.setMaxLines(3);
255        }
256    }
257}
258