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