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