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 android.app.ActivityManagerInternal; 20 import android.app.AppGlobals; 21 import android.app.Notification; 22 import android.app.PendingIntent; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.database.ContentObserver; 26 import android.net.TrafficStats; 27 import android.net.Uri; 28 import android.net.ip.IpClientCallbacks; 29 import android.net.ip.IpClientUtil; 30 import android.os.BatteryStats; 31 import android.os.Handler; 32 import android.os.IBinder; 33 import android.os.RemoteException; 34 import android.os.ServiceManager; 35 import android.os.storage.StorageManager; 36 import android.provider.Settings; 37 import android.telephony.CarrierConfigManager; 38 39 import com.android.internal.app.IBatteryStats; 40 import com.android.server.LocalServices; 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 ActivityManagerInternal mActivityManagerInternal; 50 setIntegerSetting(Context context, String name, int def)51 public boolean setIntegerSetting(Context context, String name, int def) { 52 return Settings.Global.putInt(context.getContentResolver(), name, def); 53 } 54 getIntegerSetting(Context context, String name, int def)55 public int getIntegerSetting(Context context, String name, int def) { 56 return Settings.Global.getInt(context.getContentResolver(), name, def); 57 } 58 getLongSetting(Context context, String name, long def)59 public long getLongSetting(Context context, String name, long def) { 60 return Settings.Global.getLong(context.getContentResolver(), name, def); 61 } 62 setStringSetting(Context context, String name, String def)63 public boolean setStringSetting(Context context, String name, String def) { 64 return Settings.Global.putString(context.getContentResolver(), name, def); 65 } 66 getStringSetting(Context context, String name)67 public String getStringSetting(Context context, String name) { 68 return Settings.Global.getString(context.getContentResolver(), name); 69 } 70 71 /** 72 * Mockable facade to Settings.Secure.getInt(.). 73 */ getSecureIntegerSetting(Context context, String name, int def)74 public int getSecureIntegerSetting(Context context, String name, int def) { 75 return Settings.Secure.getInt(context.getContentResolver(), name, def); 76 } 77 78 /** 79 * Mockable facade to Settings.Secure.getString(.). 80 */ getSecureStringSetting(Context context, String name)81 public String getSecureStringSetting(Context context, String name) { 82 return Settings.Secure.getString(context.getContentResolver(), name); 83 } 84 85 /** 86 * Helper method for classes to register a ContentObserver 87 * {@see ContentResolver#registerContentObserver(Uri,boolean,ContentObserver)}. 88 * 89 * @param context 90 * @param uri 91 * @param notifyForDescendants 92 * @param contentObserver 93 */ registerContentObserver(Context context, Uri uri, boolean notifyForDescendants, ContentObserver contentObserver)94 public void registerContentObserver(Context context, Uri uri, 95 boolean notifyForDescendants, ContentObserver contentObserver) { 96 context.getContentResolver().registerContentObserver(uri, notifyForDescendants, 97 contentObserver); 98 } 99 100 /** 101 * Helper method for classes to unregister a ContentObserver 102 * {@see ContentResolver#unregisterContentObserver(ContentObserver)}. 103 * 104 * @param context 105 * @param contentObserver 106 */ unregisterContentObserver(Context context, ContentObserver contentObserver)107 public void unregisterContentObserver(Context context, ContentObserver contentObserver) { 108 context.getContentResolver().unregisterContentObserver(contentObserver); 109 } 110 getService(String serviceName)111 public IBinder getService(String serviceName) { 112 return ServiceManager.getService(serviceName); 113 } 114 115 /** 116 * Returns the battery stats interface 117 * @return IBatteryStats BatteryStats service interface 118 */ getBatteryService()119 public IBatteryStats getBatteryService() { 120 return IBatteryStats.Stub.asInterface(getService(BatteryStats.SERVICE_NAME)); 121 } 122 getBroadcast(Context context, int requestCode, Intent intent, int flags)123 public PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) { 124 return PendingIntent.getBroadcast(context, requestCode, intent, flags); 125 } 126 127 /** 128 * Wrapper for {@link PendingIntent#getActivity}. 129 */ getActivity(Context context, int requestCode, Intent intent, int flags)130 public PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { 131 return PendingIntent.getActivity(context, requestCode, intent, flags); 132 } 133 makeSupplicantStateTracker(Context context, WifiConfigManager configManager, Handler handler)134 public SupplicantStateTracker makeSupplicantStateTracker(Context context, 135 WifiConfigManager configManager, Handler handler) { 136 return new SupplicantStateTracker(context, configManager, this, handler); 137 } 138 getConfigWiFiDisableInECBM(Context context)139 public boolean getConfigWiFiDisableInECBM(Context context) { 140 CarrierConfigManager configManager = (CarrierConfigManager) context 141 .getSystemService(Context.CARRIER_CONFIG_SERVICE); 142 if (configManager != null) { 143 return configManager.getConfig().getBoolean( 144 CarrierConfigManager.KEY_CONFIG_WIFI_DISABLE_IN_ECBM); 145 } 146 /* Default to TRUE */ 147 return true; 148 } 149 getTxPackets(String iface)150 public long getTxPackets(String iface) { 151 return TrafficStats.getTxPackets(iface); 152 } 153 getRxPackets(String iface)154 public long getRxPackets(String iface) { 155 return TrafficStats.getRxPackets(iface); 156 } 157 158 /** 159 * Request a new IpClient to be created asynchronously. 160 * @param context Context to use for creation. 161 * @param iface Interface the client should act on. 162 * @param callback IpClient event callbacks. 163 */ makeIpClient(Context context, String iface, IpClientCallbacks callback)164 public void makeIpClient(Context context, String iface, IpClientCallbacks callback) { 165 IpClientUtil.makeIpClient(context, iface, callback); 166 } 167 168 /** 169 * Checks whether the given uid has been granted the given permission. 170 * @param permName the permission to check 171 * @param uid The uid to check 172 * @return {@link PackageManager.PERMISSION_GRANTED} if the permission has been granted and 173 * {@link PackageManager.PERMISSION_DENIED} otherwise 174 */ checkUidPermission(String permName, int uid)175 public int checkUidPermission(String permName, int uid) throws RemoteException { 176 return AppGlobals.getPackageManager().checkUidPermission(permName, uid); 177 } 178 179 /** 180 * Create a new instance of WifiAsyncChannel 181 * @param tag String corresponding to the service creating the channel 182 * @return WifiAsyncChannel object created 183 */ makeWifiAsyncChannel(String tag)184 public WifiAsyncChannel makeWifiAsyncChannel(String tag) { 185 return new WifiAsyncChannel(tag); 186 } 187 188 /** 189 * Check if the device will be restarting after decrypting during boot by calling {@link 190 * StorageManager.inCryptKeeperBounce}. 191 * @return true if the device will restart, false otherwise 192 */ inStorageManagerCryptKeeperBounce()193 public boolean inStorageManagerCryptKeeperBounce() { 194 return StorageManager.inCryptKeeperBounce(); 195 } 196 197 /** 198 * Check if the provided uid is the app in the foreground. 199 * @param uid the uid to check 200 * @return true if the app is in the foreground, false otherwise 201 */ isAppForeground(int uid)202 public boolean isAppForeground(int uid) { 203 if (mActivityManagerInternal == null) { 204 mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); 205 } 206 return mActivityManagerInternal.isAppForeground(uid); 207 } 208 209 /** 210 * Create a new instance of {@link Notification.Builder}. 211 * @param context reference to a Context 212 * @param channelId ID of the notification channel 213 * @return an instance of Notification.Builder 214 */ makeNotificationBuilder(Context context, String channelId)215 public Notification.Builder makeNotificationBuilder(Context context, String channelId) { 216 return new Notification.Builder(context, channelId); 217 } 218 } 219