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