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.content.ContentQueryMap;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.os.UserManager;
24 import android.provider.Settings;
26 import com.android.settings.SettingsPreferenceFragment;
28 import java.util.Observable;
29 import java.util.Observer;
32 * A base class that listens to location settings change and modifies location
35 public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
36 private ContentQueryMap mContentQueryMap;
37 private Observer mSettingsObserver;
40 public void onStart() {
42 // listen for Location Manager settings changes
43 Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null,
44 "(" + Settings.System.NAME + "=?)",
45 new String[] { Settings.Secure.LOCATION_PROVIDERS_ALLOWED },
47 mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
48 mSettingsObserver = new Observer() {
50 public void update(Observable o, Object arg) {
51 refreshLocationMode();
57 public void onResume() {
59 mContentQueryMap.addObserver(mSettingsObserver);
63 public void onPause() {
65 mContentQueryMap.deleteObserver(mSettingsObserver);
69 public void onStop() {
71 mContentQueryMap.close();
74 /** Called when location mode has changed. */
75 public abstract void onModeChanged(int mode);
77 public void setLocationMode(int mode) {
78 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
79 if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
82 Settings.Secure.setLocationMode(getContentResolver(), mode);
83 refreshLocationMode();
86 public void refreshLocationMode() {
87 ContentResolver res = getContentResolver();
88 int mode = Settings.Secure.getLocationMode(getContentResolver());