OSDN Git Service

merge in klp-release history after reset to klp-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / location / LocationSettingsBase.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.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;
25
26 import com.android.settings.SettingsPreferenceFragment;
27
28 import java.util.Observable;
29 import java.util.Observer;
30
31 /**
32  * A base class that listens to location settings change and modifies location
33  * settings.
34  */
35 public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
36     private ContentQueryMap mContentQueryMap;
37     private Observer mSettingsObserver;
38
39     @Override
40     public void onStart() {
41         super.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 },
46                 null);
47         mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
48         mSettingsObserver = new Observer() {
49             @Override
50             public void update(Observable o, Object arg) {
51                 refreshLocationMode();
52             }
53         };
54     }
55
56     @Override
57     public void onResume() {
58         super.onResume();
59         mContentQueryMap.addObserver(mSettingsObserver);
60     }
61
62     @Override
63     public void onPause() {
64         super.onPause();
65         mContentQueryMap.deleteObserver(mSettingsObserver);
66     }
67
68     @Override
69     public void onStop() {
70         super.onStop();
71         mContentQueryMap.close();
72     }
73
74     /** Called when location mode has changed. */
75     public abstract void onModeChanged(int mode);
76
77     public void setLocationMode(int mode) {
78         final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
79         if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
80             return;
81         }
82         Settings.Secure.setLocationMode(getContentResolver(), mode);
83         refreshLocationMode();
84     }
85
86     public void refreshLocationMode() {
87         ContentResolver res = getContentResolver();
88         int mode = Settings.Secure.getLocationMode(getContentResolver());
89         onModeChanged(mode);
90     }
91 }