1 /* 2 * Copyright (C) 2016 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 static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; 20 import static android.content.pm.PackageManager.FEATURE_DEVICE_ADMIN; 21 22 import android.app.ActivityManager; 23 import android.app.AlertDialog; 24 import android.app.Notification; 25 import android.app.PendingIntent; 26 import android.app.admin.DevicePolicyManager; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.database.ContentObserver; 31 import android.net.TrafficStats; 32 import android.net.Uri; 33 import android.net.ip.IpClientCallbacks; 34 import android.net.ip.IpClientUtil; 35 import android.os.PersistableBundle; 36 import android.provider.Settings; 37 import android.telephony.CarrierConfigManager; 38 import android.util.Log; 39 import android.widget.Toast; 40 41 import com.android.server.wifi.util.WifiAsyncChannel; 42 43 /** 44 * This class allows overriding objects with mocks to write unit tests 45 */ 46 public class FrameworkFacade { 47 public static final String TAG = "FrameworkFacade"; 48 49 private ContentResolver mContentResolver = null; 50 private CarrierConfigManager mCarrierConfigManager = null; 51 private ActivityManager mActivityManager = null; 52 getContentResolver(Context context)53 private ContentResolver getContentResolver(Context context) { 54 if (mContentResolver == null) { 55 mContentResolver = context.getContentResolver(); 56 } 57 return mContentResolver; 58 } 59 getCarrierConfigManager(Context context)60 private CarrierConfigManager getCarrierConfigManager(Context context) { 61 if (mCarrierConfigManager == null) { 62 mCarrierConfigManager = 63 (CarrierConfigManager) context.getSystemService(Context.CARRIER_CONFIG_SERVICE); 64 } 65 return mCarrierConfigManager; 66 } 67 getActivityManager(Context context)68 private ActivityManager getActivityManager(Context context) { 69 if (mActivityManager == null) { 70 mActivityManager = 71 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 72 } 73 return mActivityManager; 74 } 75 76 /** 77 * Mockable setter for Settings.Global 78 */ setIntegerSetting(ContentResolver contentResolver, String name, int value)79 public boolean setIntegerSetting(ContentResolver contentResolver, String name, int value) { 80 return Settings.Global.putInt(contentResolver, name, value); 81 } 82 83 /** 84 * Mockable getter for Settings.Global 85 */ getIntegerSetting(ContentResolver contentResolver, String name, int def)86 public int getIntegerSetting(ContentResolver contentResolver, String name, int def) { 87 return Settings.Global.getInt(contentResolver, name, def); 88 } 89 setIntegerSetting(Context context, String name, int def)90 public boolean setIntegerSetting(Context context, String name, int def) { 91 return Settings.Global.putInt(getContentResolver(context), name, def); 92 } 93 getIntegerSetting(Context context, String name, int def)94 public int getIntegerSetting(Context context, String name, int def) { 95 return Settings.Global.getInt(getContentResolver(context), name, def); 96 } 97 getLongSetting(Context context, String name, long def)98 public long getLongSetting(Context context, String name, long def) { 99 return Settings.Global.getLong(getContentResolver(context), name, def); 100 } 101 setStringSetting(Context context, String name, String def)102 public boolean setStringSetting(Context context, String name, String def) { 103 return Settings.Global.putString(getContentResolver(context), name, def); 104 } 105 getStringSetting(Context context, String name)106 public String getStringSetting(Context context, String name) { 107 return Settings.Global.getString(getContentResolver(context), name); 108 } 109 110 /** 111 * Mockable facade to Settings.Secure.getInt(.). 112 */ getSecureIntegerSetting(Context context, String name, int def)113 public int getSecureIntegerSetting(Context context, String name, int def) { 114 return Settings.Secure.getInt(getContentResolver(context), name, def); 115 } 116 117 /** 118 * Mockable facade to Settings.Secure.getString(.). 119 */ getSecureStringSetting(Context context, String name)120 public String getSecureStringSetting(Context context, String name) { 121 return Settings.Secure.getString(getContentResolver(context), name); 122 } 123 124 /** 125 * Returns whether the device is in NIAP mode or not. 126 */ isNiapModeOn(Context context)127 public boolean isNiapModeOn(Context context) { 128 DevicePolicyManager devicePolicyManager = 129 context.getSystemService(DevicePolicyManager.class); 130 if (devicePolicyManager == null 131 && context.getPackageManager().hasSystemFeature(FEATURE_DEVICE_ADMIN)) { 132 Log.e(TAG, "Error retrieving DPM service"); 133 } 134 if (devicePolicyManager == null) return false; 135 return devicePolicyManager.isCommonCriteriaModeEnabled(null); 136 } 137 138 /** 139 * Helper method for classes to register a ContentObserver 140 * {@see ContentResolver#registerContentObserver(Uri,boolean,ContentObserver)}. 141 * 142 * @param context 143 * @param uri 144 * @param notifyForDescendants 145 * @param contentObserver 146 */ registerContentObserver(Context context, Uri uri, boolean notifyForDescendants, ContentObserver contentObserver)147 public void registerContentObserver(Context context, Uri uri, 148 boolean notifyForDescendants, ContentObserver contentObserver) { 149 getContentResolver(context).registerContentObserver(uri, notifyForDescendants, 150 contentObserver); 151 } 152 153 /** 154 * Helper method for classes to unregister a ContentObserver 155 * {@see ContentResolver#unregisterContentObserver(ContentObserver)}. 156 * 157 * @param context 158 * @param contentObserver 159 */ unregisterContentObserver(Context context, ContentObserver contentObserver)160 public void unregisterContentObserver(Context context, ContentObserver contentObserver) { 161 getContentResolver(context).unregisterContentObserver(contentObserver); 162 } 163 getBroadcast(Context context, int requestCode, Intent intent, int flags)164 public PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) { 165 return PendingIntent.getBroadcast(context, requestCode, intent, flags); 166 } 167 168 /** 169 * Wrapper for {@link PendingIntent#getActivity}. 170 */ getActivity(Context context, int requestCode, Intent intent, int flags)171 public PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { 172 return PendingIntent.getActivity(context, requestCode, intent, flags); 173 } 174 getConfigWiFiDisableInECBM(Context context)175 public boolean getConfigWiFiDisableInECBM(Context context) { 176 CarrierConfigManager configManager = getCarrierConfigManager(context); 177 if (configManager == null) { 178 return false; 179 } 180 PersistableBundle bundle = configManager.getConfig(); 181 if (bundle == null) { 182 return false; 183 } 184 return bundle.getBoolean(CarrierConfigManager.KEY_CONFIG_WIFI_DISABLE_IN_ECBM); 185 } 186 getTxPackets(String iface)187 public long getTxPackets(String iface) { 188 return TrafficStats.getTxPackets(iface); 189 } 190 getRxPackets(String iface)191 public long getRxPackets(String iface) { 192 return TrafficStats.getRxPackets(iface); 193 } 194 195 /** 196 * Request a new IpClient to be created asynchronously. 197 * @param context Context to use for creation. 198 * @param iface Interface the client should act on. 199 * @param callback IpClient event callbacks. 200 */ makeIpClient(Context context, String iface, IpClientCallbacks callback)201 public void makeIpClient(Context context, String iface, IpClientCallbacks callback) { 202 IpClientUtil.makeIpClient(context, iface, callback); 203 } 204 205 /** 206 * Create a new instance of WifiAsyncChannel 207 * @param tag String corresponding to the service creating the channel 208 * @return WifiAsyncChannel object created 209 */ makeWifiAsyncChannel(String tag)210 public WifiAsyncChannel makeWifiAsyncChannel(String tag) { 211 return new WifiAsyncChannel(tag); 212 } 213 214 /** 215 * Check if the provided uid is the app in the foreground. 216 * @param uid the uid to check 217 * @return true if the app is in the foreground, false otherwise 218 */ isAppForeground(Context context, int uid)219 public boolean isAppForeground(Context context, int uid) { 220 ActivityManager activityManager = getActivityManager(context); 221 if (activityManager == null) return false; 222 return activityManager.getUidImportance(uid) <= IMPORTANCE_VISIBLE; 223 } 224 225 /** 226 * Create a new instance of {@link Notification.Builder}. 227 * @param context reference to a Context 228 * @param channelId ID of the notification channel 229 * @return an instance of Notification.Builder 230 */ makeNotificationBuilder(Context context, String channelId)231 public Notification.Builder makeNotificationBuilder(Context context, String channelId) { 232 return new Notification.Builder(context, channelId); 233 } 234 235 /** 236 * Starts supplicant 237 */ startSupplicant()238 public void startSupplicant() { 239 SupplicantManager.start(); 240 } 241 242 /** 243 * Stops supplicant 244 */ stopSupplicant()245 public void stopSupplicant() { 246 SupplicantManager.stop(); 247 } 248 249 /** 250 * Create a new instance of {@link AlertDialog.Builder}. 251 * @param context reference to a Context 252 * @return an instance of AlertDialog.Builder 253 */ makeAlertDialogBuilder(Context context)254 public AlertDialog.Builder makeAlertDialogBuilder(Context context) { 255 return new AlertDialog.Builder(context); 256 } 257 258 /** 259 * Show a toast message 260 * @param context reference to a Context 261 * @param text the message to display 262 */ showToast(Context context, String text)263 public void showToast(Context context, String text) { 264 Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT); 265 toast.show(); 266 } 267 268 /** 269 * Wrapper for {@link TrafficStats#getMobileTxBytes}. 270 */ getMobileTxBytes()271 public long getMobileTxBytes() { 272 return TrafficStats.getMobileTxBytes(); 273 } 274 275 /** 276 * Wrapper for {@link TrafficStats#getMobileRxBytes}. 277 */ getMobileRxBytes()278 public long getMobileRxBytes() { 279 return TrafficStats.getMobileRxBytes(); 280 } 281 282 /** 283 * Wrapper for {@link TrafficStats#getTotalTxBytes}. 284 */ getTotalTxBytes()285 public long getTotalTxBytes() { 286 return TrafficStats.getTotalTxBytes(); 287 } 288 289 /** 290 * Wrapper for {@link TrafficStats#getTotalRxBytes}. 291 */ getTotalRxBytes()292 public long getTotalRxBytes() { 293 return TrafficStats.getTotalRxBytes(); 294 } 295 } 296