• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.server.wifi;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.os.Handler;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * Note: This is a hack to provide backward compatibility with the
30  * {@link Settings.Global#WIFI_SCAN_ALWAYS_AVAILABLE} @hide settings usage. We migrated storage
31  * of the scan always available state from this setting to our internal storage, but need to support
32  * the existing @hide users.
33  * TODO(b/149954910): We should find a path to stop supporting this!
34  */
35 public class WifiScanAlwaysAvailableSettingsCompatibility {
36     private static final String TAG = "WifiScanAlwaysAvailableSettingsCompatibility";
37 
38     /**
39      * Copy of the settings string. Can't directly use the constant because it is @hide.
40      */
41     @VisibleForTesting
42     public static final String SETTINGS_GLOBAL_WIFI_SCAN_ALWAYS_AVAILABLE =
43             "wifi_scan_always_enabled";
44 
45     private final Context mContext;
46     private final ContentResolver mContentResolver;
47     private final Handler mHandler;
48     private final WifiSettingsStore mWifiSettingsStore;
49     private final ActiveModeWarden mActiveModeWarden;
50     private final FrameworkFacade mFrameworkFacade;
51 
WifiScanAlwaysAvailableSettingsCompatibility(Context context, Handler handler, WifiSettingsStore wifiSettingsStore, ActiveModeWarden activeModeWarden, FrameworkFacade frameworkFacade)52     public WifiScanAlwaysAvailableSettingsCompatibility(Context context,
53             Handler handler, WifiSettingsStore wifiSettingsStore,
54             ActiveModeWarden activeModeWarden, FrameworkFacade frameworkFacade) {
55         mContext = context;
56         mHandler = handler;
57         mWifiSettingsStore = wifiSettingsStore;
58         mActiveModeWarden = activeModeWarden;
59         mFrameworkFacade = frameworkFacade;
60 
61         // Cache the content resolver to ensure that we can detect self changes.
62         mContentResolver = context.getContentResolver();
63     }
64 
65     /**
66      * Register settings change observer.
67      */
initialize()68     public void initialize() {
69         ContentObserver contentObserver = new ContentObserver(mHandler) {
70             @Override
71             public void onChange(boolean selfChange) {
72                 // Ignore any changes we triggered to avoid causing a loop.
73                 if (selfChange) return;
74 
75                 boolean settingsIsAvailable =
76                         mFrameworkFacade.getIntegerSetting(
77                                 mContentResolver, SETTINGS_GLOBAL_WIFI_SCAN_ALWAYS_AVAILABLE, 0)
78                                 == 1;
79                 // Check if the new state is different from our current state.
80                 if (mWifiSettingsStore.isScanAlwaysAvailableToggleEnabled()
81                         != settingsIsAvailable) {
82                     Log.i(TAG, "settings changed, new value: " + settingsIsAvailable
83                             + ", triggering update");
84                     mWifiSettingsStore.handleWifiScanAlwaysAvailableToggled(settingsIsAvailable);
85                     mActiveModeWarden.scanAlwaysModeChanged();
86                 }
87             }
88         };
89         mContentResolver.registerContentObserver(
90                 Settings.Global.getUriFor(SETTINGS_GLOBAL_WIFI_SCAN_ALWAYS_AVAILABLE),
91                 false, contentObserver);
92     }
93 
94     /**
95      * Handle scan always available toggle from {@link android.net.wifi.WifiManager#
96      * setScanAlwaysAvailable(boolean)}
97      */
handleWifiScanAlwaysAvailableToggled(boolean isAvailable)98     public void handleWifiScanAlwaysAvailableToggled(boolean isAvailable) {
99         mFrameworkFacade.setIntegerSetting(
100                 mContentResolver, SETTINGS_GLOBAL_WIFI_SCAN_ALWAYS_AVAILABLE, isAvailable ? 1 : 0);
101     }
102 }
103