• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.wifi;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.provider.Settings;
25 import android.support.v14.preference.SwitchPreference;
26 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceScreen;
28 import android.text.TextUtils;
29 
30 import com.android.settings.core.PreferenceController;
31 import com.android.settings.core.lifecycle.Lifecycle;
32 import com.android.settings.core.lifecycle.LifecycleObserver;
33 import com.android.settings.core.lifecycle.events.OnPause;
34 import com.android.settings.core.lifecycle.events.OnResume;
35 
36 /**
37  * {@link PreferenceController} that controls whether we should notify user when open network is
38  * available.
39  */
40 public class NotifyOpenNetworksPreferenceController extends PreferenceController implements
41         LifecycleObserver, OnResume, OnPause {
42 
43     private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
44     private SettingObserver mSettingObserver;
45 
NotifyOpenNetworksPreferenceController(Context context, Lifecycle lifecycle)46     public NotifyOpenNetworksPreferenceController(Context context, Lifecycle lifecycle) {
47         super(context);
48         lifecycle.addObserver(this);
49     }
50 
51     @Override
displayPreference(PreferenceScreen screen)52     public void displayPreference(PreferenceScreen screen) {
53         super.displayPreference(screen);
54         mSettingObserver = new SettingObserver(screen.findPreference(KEY_NOTIFY_OPEN_NETWORKS));
55     }
56 
57     @Override
onResume()58     public void onResume() {
59         if (mSettingObserver != null) {
60             mSettingObserver.register(mContext.getContentResolver(), true /* register */);
61         }
62     }
63 
64     @Override
onPause()65     public void onPause() {
66         if (mSettingObserver != null) {
67             mSettingObserver.register(mContext.getContentResolver(), false /* register */);
68         }
69     }
70 
71     @Override
isAvailable()72     public boolean isAvailable() {
73         return true;
74     }
75 
76     @Override
handlePreferenceTreeClick(Preference preference)77     public boolean handlePreferenceTreeClick(Preference preference) {
78         if (!TextUtils.equals(preference.getKey(), KEY_NOTIFY_OPEN_NETWORKS)) {
79             return false;
80         }
81         if (!(preference instanceof SwitchPreference)) {
82             return false;
83         }
84         Settings.Global.putInt(mContext.getContentResolver(),
85                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
86                 ((SwitchPreference) preference).isChecked() ? 1 : 0);
87         return true;
88     }
89 
90     @Override
getPreferenceKey()91     public String getPreferenceKey() {
92         return KEY_NOTIFY_OPEN_NETWORKS;
93     }
94 
95     @Override
updateState(Preference preference)96     public void updateState(Preference preference) {
97         if (!(preference instanceof SwitchPreference)) {
98             return;
99         }
100         final SwitchPreference notifyOpenNetworks = (SwitchPreference) preference;
101         notifyOpenNetworks.setChecked(Settings.Global.getInt(mContext.getContentResolver(),
102                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
103     }
104 
105     class SettingObserver extends ContentObserver {
106         private final Uri NETWORKS_AVAILABLE_URI = Settings.Global.getUriFor(
107                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);
108 
109         private final Preference mPreference;
110 
SettingObserver(Preference preference)111         public SettingObserver(Preference preference) {
112             super(new Handler());
113             mPreference = preference;
114         }
115 
register(ContentResolver cr, boolean register)116         public void register(ContentResolver cr, boolean register) {
117             if (register) {
118                 cr.registerContentObserver(NETWORKS_AVAILABLE_URI, false, this);
119             } else {
120                 cr.unregisterContentObserver(this);
121             }
122         }
123 
124         @Override
onChange(boolean selfChange, Uri uri)125         public void onChange(boolean selfChange, Uri uri) {
126             super.onChange(selfChange, uri);
127             if (NETWORKS_AVAILABLE_URI.equals(uri)) {
128                 updateState(mPreference);
129             }
130         }
131     }
132 }
133