• 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 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnPause;
33 import com.android.settingslib.core.lifecycle.events.OnResume;
34 
35 /**
36  * {@link TogglePreferenceController} that controls whether we should notify user when open
37  * network is available.
38  */
39 public class NotifyOpenNetworksPreferenceController extends TogglePreferenceController
40         implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
41 
42     private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
43     private SettingObserver mSettingObserver;
44 
NotifyOpenNetworksPreferenceController(Context context)45     public NotifyOpenNetworksPreferenceController(Context context) {
46         super(context, KEY_NOTIFY_OPEN_NETWORKS);
47     }
48 
49     @Override
displayPreference(PreferenceScreen screen)50     public void displayPreference(PreferenceScreen screen) {
51         super.displayPreference(screen);
52         mSettingObserver = new SettingObserver(screen.findPreference(getPreferenceKey()));
53     }
54 
55     @Override
onResume()56     public void onResume() {
57         if (mSettingObserver != null) {
58             mSettingObserver.register(mContext.getContentResolver(), true /* register */);
59         }
60     }
61 
62     @Override
onPause()63     public void onPause() {
64         if (mSettingObserver != null) {
65             mSettingObserver.register(mContext.getContentResolver(), false /* register */);
66         }
67     }
68 
69     @Override
getAvailabilityStatus()70     public int getAvailabilityStatus() {
71         return AVAILABLE;
72     }
73 
74     @Override
isChecked()75     public boolean isChecked() {
76         return Settings.Global.getInt(mContext.getContentResolver(),
77                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1;
78     }
79 
80     @Override
setChecked(boolean isChecked)81     public boolean setChecked(boolean isChecked) {
82         Settings.Global.putInt(mContext.getContentResolver(),
83                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
84                 isChecked ? 1 : 0);
85         return true;
86     }
87 
88     class SettingObserver extends ContentObserver {
89         private final Uri NETWORKS_AVAILABLE_URI = Settings.Global.getUriFor(
90                 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON);
91 
92         private final Preference mPreference;
93 
SettingObserver(Preference preference)94         public SettingObserver(Preference preference) {
95             super(new Handler());
96             mPreference = preference;
97         }
98 
register(ContentResolver cr, boolean register)99         public void register(ContentResolver cr, boolean register) {
100             if (register) {
101                 cr.registerContentObserver(NETWORKS_AVAILABLE_URI, false, this);
102             } else {
103                 cr.unregisterContentObserver(this);
104             }
105         }
106 
107         @Override
onChange(boolean selfChange, Uri uri)108         public void onChange(boolean selfChange, Uri uri) {
109             super.onChange(selfChange, uri);
110             if (NETWORKS_AVAILABLE_URI.equals(uri)) {
111                 updateState(mPreference);
112             }
113         }
114     }
115 }
116