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.ActivityManager;
20import android.app.AppGlobals;
21import android.app.Notification;
22import android.app.PendingIntent;
23import android.content.Context;
24import android.content.Intent;
25import android.database.ContentObserver;
26import android.net.TrafficStats;
27import android.net.Uri;
28import android.net.ip.IpManager;
29import android.os.BatteryStats;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.os.storage.StorageManager;
35import android.provider.Settings;
36import android.telephony.CarrierConfigManager;
37
38import com.android.internal.app.IBatteryStats;
39import com.android.server.wifi.util.WifiAsyncChannel;
40
41/**
42 * This class allows overriding objects with mocks to write unit tests
43 */
44public class FrameworkFacade {
45    public static final String TAG = "FrameworkFacade";
46
47    public boolean setIntegerSetting(Context context, String name, int def) {
48        return Settings.Global.putInt(context.getContentResolver(), name, def);
49    }
50
51    public int getIntegerSetting(Context context, String name, int def) {
52        return Settings.Global.getInt(context.getContentResolver(), name, def);
53    }
54
55    public long getLongSetting(Context context, String name, long def) {
56        return Settings.Global.getLong(context.getContentResolver(), name, def);
57    }
58
59    public boolean setStringSetting(Context context, String name, String def) {
60        return Settings.Global.putString(context.getContentResolver(), name, def);
61    }
62
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     */
76    public void registerContentObserver(Context context, Uri uri,
77            boolean notifyForDescendants, ContentObserver contentObserver) {
78        context.getContentResolver().registerContentObserver(uri, notifyForDescendants,
79                contentObserver);
80    }
81
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     */
90    public IBatteryStats getBatteryService() {
91        return IBatteryStats.Stub.asInterface(getService(BatteryStats.SERVICE_NAME));
92    }
93
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     */
101    public PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) {
102        return PendingIntent.getActivity(context, requestCode, intent, flags);
103    }
104
105    public SupplicantStateTracker makeSupplicantStateTracker(Context context,
106            WifiConfigManager configManager, Handler handler) {
107        return new SupplicantStateTracker(context, configManager, this, handler);
108    }
109
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     */
127    public WifiApConfigStore makeApConfigStore(Context context,
128                                               BackupManagerProxy backupManagerProxy) {
129        return new WifiApConfigStore(context, backupManagerProxy);
130    }
131
132    public long getTxPackets(String iface) {
133        return TrafficStats.getTxPackets(iface);
134    }
135
136    public long getRxPackets(String iface) {
137        return TrafficStats.getRxPackets(iface);
138    }
139
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     */
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     */
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     */
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     */
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     */
190    public Notification.Builder makeNotificationBuilder(Context context, String channelId) {
191        return new Notification.Builder(context, channelId);
192    }
193}
194