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