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