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