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.ActivityManager; 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.IpManager; 29 import android.os.BatteryStats; 30 import android.os.Handler; 31 import android.os.IBinder; 32 import android.os.RemoteException; 33 import android.os.ServiceManager; 34 import android.os.storage.StorageManager; 35 import android.provider.Settings; 36 import android.telephony.CarrierConfigManager; 37 38 import com.android.internal.app.IBatteryStats; 39 import com.android.server.wifi.util.WifiAsyncChannel; 40 41 /** 42 * This class allows overriding objects with mocks to write unit tests 43 */ 44 public class FrameworkFacade { 45 public static final String TAG = "FrameworkFacade"; 46 setIntegerSetting(Context context, String name, int def)47 public boolean setIntegerSetting(Context context, String name, int def) { 48 return Settings.Global.putInt(context.getContentResolver(), name, def); 49 } 50 getIntegerSetting(Context context, String name, int def)51 public int getIntegerSetting(Context context, String name, int def) { 52 return Settings.Global.getInt(context.getContentResolver(), name, def); 53 } 54 getLongSetting(Context context, String name, long def)55 public long getLongSetting(Context context, String name, long def) { 56 return Settings.Global.getLong(context.getContentResolver(), name, def); 57 } 58 setStringSetting(Context context, String name, String def)59 public boolean setStringSetting(Context context, String name, String def) { 60 return Settings.Global.putString(context.getContentResolver(), name, def); 61 } 62 getStringSetting(Context context, String name)63 public String getStringSetting(Context context, String name) { 64 return Settings.Global.getString(context.getContentResolver(), name); 65 } 66 67 /** 68 * Helper method for classes to register a ContentObserver 69 * {@see ContentResolver#registerContentObserver(Uri,boolean,ContentObserver)}. 70 * 71 * @param context 72 * @param uri 73 * @param notifyForDescendants 74 * @param contentObserver 75 */ registerContentObserver(Context context, Uri uri, boolean notifyForDescendants, ContentObserver contentObserver)76 public void registerContentObserver(Context context, Uri uri, 77 boolean notifyForDescendants, ContentObserver contentObserver) { 78 context.getContentResolver().registerContentObserver(uri, notifyForDescendants, 79 contentObserver); 80 } 81 getService(String serviceName)82 public IBinder getService(String serviceName) { 83 return ServiceManager.getService(serviceName); 84 } 85 86 /** 87 * Returns the battery stats interface 88 * @return IBatteryStats BatteryStats service interface 89 */ getBatteryService()90 public IBatteryStats getBatteryService() { 91 return IBatteryStats.Stub.asInterface(getService(BatteryStats.SERVICE_NAME)); 92 } 93 getBroadcast(Context context, int requestCode, Intent intent, int flags)94 public PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) { 95 return PendingIntent.getBroadcast(context, requestCode, intent, flags); 96 } 97 98 /** 99 * Wrapper for {@link PendingIntent#getActivity}. 100 */ getActivity(Context context, int requestCode, Intent intent, int flags)101 public PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { 102 return PendingIntent.getActivity(context, requestCode, intent, flags); 103 } 104 makeSupplicantStateTracker(Context context, WifiConfigManager configManager, Handler handler)105 public SupplicantStateTracker makeSupplicantStateTracker(Context context, 106 WifiConfigManager configManager, Handler handler) { 107 return new SupplicantStateTracker(context, configManager, this, handler); 108 } 109 getConfigWiFiDisableInECBM(Context context)110 public boolean getConfigWiFiDisableInECBM(Context context) { 111 CarrierConfigManager configManager = (CarrierConfigManager) context 112 .getSystemService(Context.CARRIER_CONFIG_SERVICE); 113 if (configManager != null) { 114 return configManager.getConfig().getBoolean( 115 CarrierConfigManager.KEY_CONFIG_WIFI_DISABLE_IN_ECBM); 116 } 117 /* Default to TRUE */ 118 return true; 119 } 120 121 /** 122 * Create a new instance of WifiApConfigStore. 123 * @param context reference to a Context 124 * @param backupManagerProxy reference to a BackupManagerProxy 125 * @return an instance of WifiApConfigStore 126 */ makeApConfigStore(Context context, BackupManagerProxy backupManagerProxy)127 public WifiApConfigStore makeApConfigStore(Context context, 128 BackupManagerProxy backupManagerProxy) { 129 return new WifiApConfigStore(context, backupManagerProxy); 130 } 131 getTxPackets(String iface)132 public long getTxPackets(String iface) { 133 return TrafficStats.getTxPackets(iface); 134 } 135 getRxPackets(String iface)136 public long getRxPackets(String iface) { 137 return TrafficStats.getRxPackets(iface); 138 } 139 makeIpManager( Context context, String iface, IpManager.Callback callback)140 public IpManager makeIpManager( 141 Context context, String iface, IpManager.Callback callback) { 142 return new IpManager(context, iface, callback); 143 } 144 145 /** 146 * Checks whether the given uid has been granted the given permission. 147 * @param permName the permission to check 148 * @param uid The uid to check 149 * @return {@link PackageManager.PERMISSION_GRANTED} if the permission has been granted and 150 * {@link PackageManager.PERMISSION_DENIED} otherwise 151 */ checkUidPermission(String permName, int uid)152 public int checkUidPermission(String permName, int uid) throws RemoteException { 153 return AppGlobals.getPackageManager().checkUidPermission(permName, uid); 154 } 155 156 /** 157 * Create a new instance of WifiAsyncChannel 158 * @param tag String corresponding to the service creating the channel 159 * @return WifiAsyncChannel object created 160 */ makeWifiAsyncChannel(String tag)161 public WifiAsyncChannel makeWifiAsyncChannel(String tag) { 162 return new WifiAsyncChannel(tag); 163 } 164 165 /** 166 * Check if the device will be restarting after decrypting during boot by calling {@link 167 * StorageManager.inCryptKeeperBounce}. 168 * @return true if the device will restart, false otherwise 169 */ inStorageManagerCryptKeeperBounce()170 public boolean inStorageManagerCryptKeeperBounce() { 171 return StorageManager.inCryptKeeperBounce(); 172 } 173 174 /** 175 * Check if the provided uid is the app in the foreground. 176 * @param uid the uid to check 177 * @return true if the app is in the foreground, false otherwise 178 * @throws RemoteException 179 */ isAppForeground(int uid)180 public boolean isAppForeground(int uid) throws RemoteException { 181 return ActivityManager.getService().isAppForeground(uid); 182 } 183 184 /** 185 * Create a new instance of {@link Notification.Builder}. 186 * @param context reference to a Context 187 * @param channelId ID of the notification channel 188 * @return an instance of Notification.Builder 189 */ makeNotificationBuilder(Context context, String channelId)190 public Notification.Builder makeNotificationBuilder(Context context, String channelId) { 191 return new Notification.Builder(context, channelId); 192 } 193 } 194