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.net.ConnectivityManager;
24import android.net.TrafficStats;
25import android.net.ip.IpManager;
26import android.net.wifi.IWifiScanner;
27import android.net.wifi.WifiScanner;
28import android.os.Handler;
29import android.os.IBinder;
30import android.os.INetworkManagementService;
31import android.os.Looper;
32import android.os.RemoteException;
33import android.os.ServiceManager;
34import android.os.UserManager;
35import android.provider.Settings;
36import android.security.KeyStore;
37import android.telephony.CarrierConfigManager;
38
39import java.util.ArrayList;
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 BaseWifiLogger makeBaseLogger() {
48        return new BaseWifiLogger();
49    }
50
51    public BaseWifiLogger makeRealLogger(
52            Context context, WifiStateMachine stateMachine, WifiNative wifiNative,
53            BuildProperties buildProperties) {
54        return new WifiLogger(context, stateMachine, wifiNative, buildProperties);
55    }
56
57    public boolean setIntegerSetting(Context context, String name, int def) {
58        return Settings.Global.putInt(context.getContentResolver(), name, def);
59    }
60
61    public int getIntegerSetting(Context context, String name, int def) {
62        return Settings.Global.getInt(context.getContentResolver(), name, def);
63    }
64
65    public long getLongSetting(Context context, String name, long def) {
66        return Settings.Global.getLong(context.getContentResolver(), name, def);
67    }
68
69    public boolean setStringSetting(Context context, String name, String def) {
70        return Settings.Global.putString(context.getContentResolver(), name, def);
71    }
72
73    public String getStringSetting(Context context, String name) {
74        return Settings.Global.getString(context.getContentResolver(), name);
75    }
76
77    public IBinder getService(String serviceName) {
78        return ServiceManager.getService(serviceName);
79    }
80
81    public WifiScanner makeWifiScanner(Context context, Looper looper) {
82        return new WifiScanner(context, IWifiScanner.Stub.asInterface(
83                        getService(Context.WIFI_SCANNING_SERVICE)), looper);
84    }
85
86    public PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) {
87        return PendingIntent.getBroadcast(context, requestCode, intent, flags);
88    }
89
90    public SupplicantStateTracker makeSupplicantStateTracker(Context context,
91            WifiConfigManager configManager, Handler handler) {
92        return new SupplicantStateTracker(context, configManager, handler);
93    }
94
95    public boolean getConfigWiFiDisableInECBM(Context context) {
96       CarrierConfigManager configManager = (CarrierConfigManager) context
97               .getSystemService(Context.CARRIER_CONFIG_SERVICE);
98       if (configManager != null) {
99           return configManager.getConfig().getBoolean(
100               CarrierConfigManager.KEY_CONFIG_WIFI_DISABLE_IN_ECBM);
101       }
102       /* Default to TRUE */
103       return true;
104    }
105
106    /**
107     * Create a new instance of WifiApConfigStore.
108     * @param context reference to a Context
109     * @param backupManagerProxy reference to a BackupManagerProxy
110     * @return an instance of WifiApConfigStore
111     */
112    public WifiApConfigStore makeApConfigStore(Context context,
113                                               BackupManagerProxy backupManagerProxy) {
114        return new WifiApConfigStore(context, backupManagerProxy);
115    }
116
117    public long getTxPackets(String iface) {
118        return TrafficStats.getTxPackets(iface);
119    }
120
121    public long getRxPackets(String iface) {
122        return TrafficStats.getRxPackets(iface);
123    }
124
125    public IpManager makeIpManager(
126            Context context, String iface, IpManager.Callback callback) {
127        return new IpManager(context, iface, callback);
128    }
129
130    /**
131     * Create a SoftApManager.
132     * @param context current context
133     * @param looper current thread looper
134     * @param wifiNative reference to WifiNative
135     * @param nmService reference to NetworkManagementService
136     * @param cm reference to ConnectivityManager
137     * @param countryCode Country code
138     * @param allowed2GChannels list of allowed 2G channels
139     * @param listener listener for SoftApManager
140     * @return an instance of SoftApManager
141     */
142    public SoftApManager makeSoftApManager(
143            Context context, Looper looper, WifiNative wifiNative,
144            INetworkManagementService nmService, ConnectivityManager cm,
145            String countryCode, ArrayList<Integer> allowed2GChannels,
146            SoftApManager.Listener listener) {
147        return new SoftApManager(
148                looper, wifiNative, nmService, countryCode,
149                allowed2GChannels, listener);
150    }
151
152    /**
153     * Checks whether the given uid has been granted the given permission.
154     * @param permName the permission to check
155     * @param uid The uid to check
156     * @return {@link PackageManager.PERMISSION_GRANTED} if the permission has been granted and
157     *         {@link PackageManager.PERMISSION_DENIED} otherwise
158     */
159    public int checkUidPermission(String permName, int uid) throws RemoteException {
160        return AppGlobals.getPackageManager().checkUidPermission(permName, uid);
161    }
162
163    public WifiConfigManager makeWifiConfigManager(Context context, WifiNative wifiNative,
164            FrameworkFacade frameworkFacade, Clock clock, UserManager userManager,
165            KeyStore keyStore) {
166        return new WifiConfigManager(context, wifiNative, frameworkFacade, clock, userManager,
167                keyStore);
168    }
169}
170