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