• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.provider.Settings;
22 
23 import java.io.FileDescriptor;
24 import java.io.PrintWriter;
25 
26 /* Tracks persisted settings for Wi-Fi and airplane mode interaction */
27 public class WifiSettingsStore {
28     /* Values tracked in Settings.Global.WIFI_ON */
29     static final int WIFI_DISABLED                      = 0;
30     static final int WIFI_ENABLED                       = 1;
31 
32     /* Wifi enabled while in airplane mode */
33     private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE     = 2;
34     /* Wifi disabled due to airplane mode on */
35     private static final int WIFI_DISABLED_AIRPLANE_ON          = 3;
36 
37     /* Persisted state that tracks the wifi & airplane interaction from settings */
38     private int mPersistWifiState = WIFI_DISABLED;
39     /* Tracks current airplane mode state */
40     private boolean mAirplaneModeOn = false;
41 
42     private final Context mContext;
43     private final WifiSettingsConfigStore mSettingsConfigStore;
44 
WifiSettingsStore(Context context, WifiSettingsConfigStore sharedPreferences)45     WifiSettingsStore(Context context, WifiSettingsConfigStore sharedPreferences) {
46         mContext = context;
47         mSettingsConfigStore = sharedPreferences;
48         mAirplaneModeOn = getPersistedAirplaneModeOn();
49         mPersistWifiState = getPersistedWifiState();
50     }
51 
isWifiToggleEnabled()52     public synchronized boolean isWifiToggleEnabled() {
53         if (mAirplaneModeOn) {
54             return mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE;
55         } else {
56             return mPersistWifiState != WIFI_DISABLED;
57         }
58     }
59 
60     /**
61      * Returns true if airplane mode is currently on.
62      * @return {@code true} if airplane mode is on.
63      */
isAirplaneModeOn()64     public synchronized boolean isAirplaneModeOn() {
65         return mAirplaneModeOn;
66     }
67 
isScanAlwaysAvailableToggleEnabled()68     public synchronized boolean isScanAlwaysAvailableToggleEnabled() {
69         return getPersistedScanAlwaysAvailable();
70     }
71 
isScanAlwaysAvailable()72     public synchronized boolean isScanAlwaysAvailable() {
73         return !mAirplaneModeOn && getPersistedScanAlwaysAvailable();
74     }
75 
isWifiScoringEnabled()76     public synchronized boolean isWifiScoringEnabled() {
77         return getPersistedWifiScoringEnabled();
78     }
79 
handleWifiToggled(boolean wifiEnabled)80     public synchronized boolean handleWifiToggled(boolean wifiEnabled) {
81         // Can Wi-Fi be toggled in airplane mode ?
82         if (mAirplaneModeOn && !isAirplaneToggleable()) {
83             return false;
84         }
85 
86         if (wifiEnabled) {
87             if (mAirplaneModeOn) {
88                 persistWifiState(WIFI_ENABLED_AIRPLANE_OVERRIDE);
89             } else {
90                 persistWifiState(WIFI_ENABLED);
91             }
92         } else {
93             // When wifi state is disabled, we do not care
94             // if airplane mode is on or not. The scenario of
95             // wifi being disabled due to airplane mode being turned on
96             // is handled handleAirplaneModeToggled()
97             persistWifiState(WIFI_DISABLED);
98         }
99         return true;
100     }
101 
handleAirplaneModeToggled()102     synchronized boolean handleAirplaneModeToggled() {
103         // Is Wi-Fi sensitive to airplane mode changes ?
104         if (!isAirplaneSensitive()) {
105             return false;
106         }
107 
108         mAirplaneModeOn = getPersistedAirplaneModeOn();
109         if (mAirplaneModeOn) {
110             // Wifi disabled due to airplane on
111             if (mPersistWifiState == WIFI_ENABLED) {
112                 persistWifiState(WIFI_DISABLED_AIRPLANE_ON);
113             }
114         } else {
115             /* On airplane mode disable, restore wifi state if necessary */
116             if (mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE
117                     || mPersistWifiState == WIFI_DISABLED_AIRPLANE_ON) {
118                 persistWifiState(WIFI_ENABLED);
119             }
120         }
121         return true;
122     }
123 
handleWifiScanAlwaysAvailableToggled(boolean isAvailable)124     synchronized void handleWifiScanAlwaysAvailableToggled(boolean isAvailable) {
125         persistScanAlwaysAvailableState(isAvailable);
126     }
127 
handleWifiScoringEnabled(boolean enabled)128     synchronized boolean handleWifiScoringEnabled(boolean enabled) {
129         persistWifiScoringEnabledState(enabled);
130         return true;
131     }
132 
dump(FileDescriptor fd, PrintWriter pw, String[] args)133     void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
134         pw.println("WifiState " + getPersistedWifiState());
135         pw.println("AirplaneModeOn " + getPersistedAirplaneModeOn());
136         pw.println("ScanAlwaysAvailable " + getPersistedScanAlwaysAvailable());
137         pw.println("WifiScoringState " + getPersistedWifiScoringEnabled());
138     }
139 
persistWifiState(int state)140     private void persistWifiState(int state) {
141         final ContentResolver cr = mContext.getContentResolver();
142         mPersistWifiState = state;
143         Settings.Global.putInt(cr, Settings.Global.WIFI_ON, state);
144     }
145 
persistScanAlwaysAvailableState(boolean isAvailable)146     private void persistScanAlwaysAvailableState(boolean isAvailable) {
147         mSettingsConfigStore.put(
148                 WifiSettingsConfigStore.WIFI_SCAN_ALWAYS_AVAILABLE, isAvailable);
149     }
150 
persistWifiScoringEnabledState(boolean enabled)151     private void persistWifiScoringEnabledState(boolean enabled) {
152         mSettingsConfigStore.put(
153                 WifiSettingsConfigStore.WIFI_SCORING_ENABLED, enabled);
154     }
155 
156     /* Does Wi-Fi need to be disabled when airplane mode is on ? */
isAirplaneSensitive()157     private boolean isAirplaneSensitive() {
158         String airplaneModeRadios = Settings.Global.getString(mContext.getContentResolver(),
159                 Settings.Global.AIRPLANE_MODE_RADIOS);
160         return airplaneModeRadios == null
161                 || airplaneModeRadios.contains(Settings.Global.RADIO_WIFI);
162     }
163 
164     /* Is Wi-Fi allowed to be re-enabled while airplane mode is on ? */
isAirplaneToggleable()165     private boolean isAirplaneToggleable() {
166         String toggleableRadios = Settings.Global.getString(mContext.getContentResolver(),
167                 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
168         return toggleableRadios != null
169                 && toggleableRadios.contains(Settings.Global.RADIO_WIFI);
170     }
171 
getPersistedWifiState()172     private int getPersistedWifiState() {
173         final ContentResolver cr = mContext.getContentResolver();
174         try {
175             return Settings.Global.getInt(cr, Settings.Global.WIFI_ON);
176         } catch (Settings.SettingNotFoundException e) {
177             Settings.Global.putInt(cr, Settings.Global.WIFI_ON, WIFI_DISABLED);
178             return WIFI_DISABLED;
179         }
180     }
181 
getPersistedAirplaneModeOn()182     private boolean getPersistedAirplaneModeOn() {
183         return Settings.Global.getInt(mContext.getContentResolver(),
184                 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
185     }
186 
getPersistedScanAlwaysAvailable()187     private boolean getPersistedScanAlwaysAvailable() {
188         return mSettingsConfigStore.get(
189                 WifiSettingsConfigStore.WIFI_SCAN_ALWAYS_AVAILABLE);
190     }
191 
getPersistedWifiScoringEnabled()192     private boolean getPersistedWifiScoringEnabled() {
193         return mSettingsConfigStore.get(
194                 WifiSettingsConfigStore.WIFI_SCORING_ENABLED);
195     }
196 }
197