WifiServiceImpl.java revision e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccf
1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2010 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
199086afccf6938a49eb9a2cd248917c1cb0943942vandwalleimport android.Manifest;
20155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.app.ActivityManager;
21155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.app.AppOpsManager;
22155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.bluetooth.BluetoothAdapter;
23155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.BroadcastReceiver;
24155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Context;
25155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Intent;
26155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.IntentFilter;
27155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.pm.PackageManager;
28fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.content.pm.UserInfo;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.database.ContentObserver;
3034f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwaltimport android.net.ConnectivityManager;
31155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpInfo;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpResults;
333ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.IpConfiguration.ProxySettings;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.LinkAddress;
359846078598c3468f8813dbfa58238a1846bd81efSanket Padaweimport android.net.Network;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.NetworkUtils;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.RouteInfo;
38e487a4648dd41881e754f1224aaedead78a0777dSky Faberimport android.net.Uri;
39c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.net.wifi.*;
403ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.wifi.IWifiManager;
413ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.AsyncTask;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Binder;
43e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolovimport android.os.Build;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Handler;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.HandlerThread;
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.IBinder;
47155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Message;
483ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.Messenger;
4903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackbornimport android.os.PowerManager;
50155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.RemoteException;
51186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinskiimport android.os.SystemClock;
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.SystemProperties;
53155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.UserHandle;
54fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.os.UserManager;
55155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.WorkSource;
56155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.provider.Settings;
574d381bc39f5263effdae73ec99065eb299b806caVinit Deshpandeimport android.text.TextUtils;
58992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalleimport android.util.Log;
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Slog;
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
61155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileNotFoundException;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.BufferedReader;
63155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileDescriptor;
64155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileReader;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.IOException;
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.PrintWriter;
67155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.lang.Override;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.InetAddress;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.Inet4Address;
70a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.net.URISyntaxException;
71a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.security.GeneralSecurityException;
7231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.KeyStore;
7331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPath;
7431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPathValidator;
7531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPathValidatorException;
7631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertificateFactory;
7731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.PKIXParameters;
7831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.X509Certificate;
79155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
8031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.util.Arrays;
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.R;
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.app.IBatteryStats;
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.telephony.TelephonyIntents;
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.internal.util.AsyncChannel;
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport com.android.server.am.BatteryStatsService;
88a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport com.android.server.wifi.configparse.ConfigBuilder;
89a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
90a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport org.xml.sax.SAXException;
91a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED;
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED;
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED;
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED;
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED;
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_OFF;
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_ON;
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SET_AP;
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_USER_PRESENT;
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WifiService handles remote WiFi operation requests by implementing
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * the IWifiManager interface.
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * @hide
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepublic final class WifiServiceImpl extends IWifiManager.Stub {
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String TAG = "WifiService";
110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean DBG = true;
11170b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle    private static final boolean VDBG = false;
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiStateMachine mWifiStateMachine;
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final Context mContext;
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final LockList mLocks = new LockList();
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // some wifi lock statistics
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksAcquired;
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullHighPerfLocksReleased;
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksAcquired;
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mFullLocksReleased;
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksAcquired;
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mScanLocksReleased;
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final List<Multicaster> mMulticasters =
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            new ArrayList<Multicaster>();
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastEnabled;
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mMulticastDisabled;
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final IBatteryStats mBatteryStats;
13203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    private final PowerManager mPowerManager;
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final AppOpsManager mAppOps;
134f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott    private final UserManager mUserManager;
135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private String mInterfaceName;
137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
138ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    // Debug counter tracking scan requests sent by WifiManager
139ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    private int scanRequestCounter = 0;
140ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the open wi-fi network notification */
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiNotificationController mNotificationController;
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Polls traffic stats and notifies clients */
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiTrafficPoller mTrafficPoller;
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the persisted states for wi-fi & airplane mode */
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiSettingsStore mSettingsStore;
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Asynchronous channel to WifiStateMachine
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private AsyncChannel mWifiStateMachineChannel;
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles client connections
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class ClientHandler extends Handler {
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ClientHandler(android.os.Looper looper) {
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // We track the clients by the Messenger
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // since it is expected to be always available
170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mTrafficPoller.addClient(msg.replyTo);
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Send failed, client connection lost");
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mTrafficPoller.removeClient(msg.replyTo);
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    AsyncChannel ac = new AsyncChannel();
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ac.connect(mContext, this, msg.replyTo);
188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Client commands are forwarded to state machine */
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CONNECT_NETWORK:
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.SAVE_NETWORK: {
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration config = (WifiConfiguration) msg.obj;
194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    int networkId = msg.arg1;
195992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.SAVE_NETWORK) {
196992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "SAVE"
197992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
198e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
199e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
200e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
201992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
202992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.CONNECT_NETWORK) {
203992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        Slog.e("WiFiServiceImpl ", "CONNECT "
204992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
205e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
206e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
207e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
208992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
209e487a4648dd41881e754f1224aaedead78a0777dSky Faber
210d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande                    if (config != null && isValid(config)) {
21164c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        if (DBG) Slog.d(TAG, "Connect with config" + config);
21264c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        mWifiStateMachine.sendMessage(Message.obtain(msg));
213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else if (config == null
214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            && networkId != WifiConfiguration.INVALID_NETWORK_ID) {
215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Connect with networkId" + networkId);
216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachine.sendMessage(Message.obtain(msg));
217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg);
219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (msg.what == WifiManager.CONNECT_NETWORK) {
220c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED,
221c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        } else {
223c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED,
224c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.FORGET_NETWORK:
2302a325cd07d40941adb93c1284abb263ba650c213Sky Faber                    if (isOwner(msg.sendingUid)) {
231002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        mWifiStateMachine.sendMessage(Message.obtain(msg));
232002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    } else {
233002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        Slog.e(TAG, "Forget is not authorized for user");
234002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                        replyFailed(msg, WifiManager.FORGET_NETWORK_FAILED,
235002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                                WifiManager.NOT_AUTHORIZED);
236002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    }
237002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    break;
238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.START_WPS:
239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CANCEL_WPS:
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.DISABLE_NETWORK:
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.RSSI_PKTCNT_FETCH: {
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachine.sendMessage(Message.obtain(msg));
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg);
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
252c35d728a15e9270f5642ef79f5245c89d749285fSky Faber        private void replyFailed(Message msg, int what, int why) {
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Message reply = msg.obtain();
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            reply.what = what;
255c35d728a15e9270f5642ef79f5245c89d749285fSky Faber            reply.arg1 = why;
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                msg.replyTo.send(reply);
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // There's not much we can do if reply can't be sent!
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private ClientHandler mClientHandler;
264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles interaction with WifiStateMachine
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiStateMachineHandler extends Handler {
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private AsyncChannel mWsmChannel;
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiStateMachineHandler(android.os.Looper looper) {
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(looper);
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel = new AsyncChannel();
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = mWsmChannel;
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = null;
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachineChannel = null;
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //Re-establish connection to state machine
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WifiStateMachineHandler mWifiStateMachineHandler;
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiController mWifiController;
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiServiceImpl(Context context) {
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext = context;
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mInterfaceName =  SystemProperties.get("wifi.interface", "wlan0");
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
315f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mTrafficPoller = new WifiTrafficPoller(mContext, mInterfaceName);
316f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName, mTrafficPoller);
317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableRssiPolling(true);
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mBatteryStats = BatteryStatsService.getService();
31903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        mPowerManager = context.getSystemService(PowerManager.class);
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
321f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        mUserManager = UserManager.get(mContext);
322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController = new WifiNotificationController(mContext, mWifiStateMachine);
324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore = new WifiSettingsStore(mContext);
325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        HandlerThread wifiThread = new HandlerThread("WifiService");
327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        wifiThread.start();
328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mClientHandler = new ClientHandler(wifiThread.getLooper());
329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController = new WifiController(mContext, this, wifiThread.getLooper());
3311c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    }
3321c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3331c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3341c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    /**
3351c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * Check if Wi-Fi needs to be enabled and start
3361c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * if needed
3371c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     *
3381c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * This function is used only at boot time
3391c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     */
3401c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    public void checkAndStartWifi() {
3411c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        /* Check if wi-fi needs to be enabled */
3421c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
3431c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
3441c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde                (wifiEnabled ? "enabled" : "disabled"));
345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForScanModeChange();
347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(
348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new BroadcastReceiver() {
349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    @Override
350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    public void onReceive(Context context, Intent intent) {
351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (mSettingsStore.handleAirplaneModeToggled()) {
352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                },
356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Adding optimizations of only receiving broadcasts when wifi is enabled
359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // can result in race conditions when apps toggle wifi in the background
360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // without active user involvement. Always receive broadcasts.
361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForBroadcasts();
362faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        registerForPackageOrUserRemoval();
36303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        mInIdleMode = mPowerManager.isDeviceIdleMode();
364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3651c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        mWifiController.start();
366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // If we are already disabled (could be due to airplane mode), avoid changing persist
368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // state here
369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiEnabled) setWifiEnabled(wifiEnabled);
370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine = WifiWatchdogStateMachine.
372569b093c4afd30787308032c8259af2680a2874fNavtej Singh Mann               makeWifiWatchdogStateMachine(mContext, mWifiStateMachine.getMessenger());
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#pingSupplicant()}
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeds, {@code false} otherwise
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean pingSupplicant() {
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
390a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#getChannelList}
391a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     */
392a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public List<WifiChannel> getChannelList() {
393a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        enforceAccessPermission();
394a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (mWifiStateMachineChannel != null) {
395a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return mWifiStateMachine.syncGetChannelList(mWifiStateMachineChannel);
396a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        } else {
397a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
398a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            return null;
399a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
400a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    }
401a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng
4029086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // Start a location scan.
4039086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    // L release: A location scan is implemented as a normal scan and avoids scanning DFS channels
4041227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    // Deprecated: Will soon remove implementation
4059086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    public void startLocationRestrictedScan(WorkSource workSource) {
4069086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceChangePermission();
4079086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        enforceLocationHardwarePermission();
4089086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        List<WifiChannel> channels = getChannelList();
4099086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (channels == null) {
4109086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            Slog.e(TAG, "startLocationRestrictedScan cant get channels");
4119086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            return;
4129086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4139086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        ScanSettings settings = new ScanSettings();
4149086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        for (WifiChannel channel : channels) {
4159086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            if (!channel.isDFS) {
4169086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                settings.channelSet.add(channel);
4179086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            }
4189086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4199086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        if (workSource == null) {
4209086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // Make sure we always have a workSource indicating the origin of the scan
4219086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            // hence if there is none, pick an internal WifiStateMachine one
4229086afccf6938a49eb9a2cd248917c1cb0943942vandwalle            workSource = new WorkSource(WifiStateMachine.DFS_RESTRICTED_SCAN_REQUEST);
4239086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        }
4249086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        startScan(settings, workSource);
4259086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
4269086afccf6938a49eb9a2cd248917c1cb0943942vandwalle
427a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    /**
428a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#startScan}
429a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * and {@link android.net.wifi.WifiManager#startCustomizedScan}
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
431a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param settings If null, use default parameter, i.e. full scan.
432a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param workSource If null, all blame is given to the calling uid.
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
434a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public void startScan(ScanSettings settings, WorkSource workSource) {
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
4361227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
43703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode) {
43849dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // Need to send an immediate scan result broadcast in case the
43949dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // caller is waiting for a result ..
44049dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                mWifiStateMachine.sendScanResultsAvailableBroadcast(/* scanSucceeded = */ false);
44103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mScanPending = true;
44203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                return;
44303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
44403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
445a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (settings != null) {
446a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            settings = new ScanSettings(settings);
447a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (!settings.isValid()) {
448a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                Slog.e(TAG, "invalid scan setting");
449a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                return;
450a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            }
451a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (workSource != null) {
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWorkSourcePermission();
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // WifiManager currently doesn't use names, so need to clear names out of the
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // supplied WorkSource to allow future WorkSource combining.
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            workSource.clearNames();
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
458ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle        mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++,
459ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle                settings, workSource);
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isBatchedScanSupported() {
463ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
466ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void pollBatchedScan() { }
467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4683f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    public String getWpsNfcConfigurationToken(int netId) {
469f3f4029b3ac41da4cc6a1bc22bae47c750a47048Andres Morales        enforceConnectivityInternalPermission();
4703f7ef65ab71619040032aee96b5599849881d6fdAndres Morales        return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId);
4713f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    }
4723f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#requestBatchedScan()}
475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean requestBatchedScan(BatchedScanSettings requested, IBinder binder,
477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WorkSource workSource) {
478ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return false;
479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<BatchedScanResult> getBatchedScanResults(String callingPackage) {
482ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande        return null;
483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
485ad9984607c007b15b9eb6601facf821cb57e1555Vinit Deshpande    public void stopBatchedScan(BatchedScanSettings settings) { }
486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4871227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mInIdleMode;
4881227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mScanPending;
4891227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande
49003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    void handleIdleModeChanged() {
49103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        boolean doScan = false;
4921227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
49303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            boolean idle = mPowerManager.isDeviceIdleMode();
49403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode != idle) {
49503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mInIdleMode = idle;
49603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                if (!idle) {
49703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    if (mScanPending) {
49803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        mScanPending = false;
49903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        doScan = true;
50003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    }
50103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                }
50203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
50303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
50403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        if (doScan) {
50503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            // Someone requested a scan while we were idle; do a full scan now.
50603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            startScan(null, null);
50703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
50803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    }
50903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn
510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceAccessPermission() {
511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
512a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceChangePermission() {
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
51731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                "WifiService");
5189086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5209086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    private void enforceLocationHardwarePermission() {
5219086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE,
5229086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                "LocationHardware");
523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5259878c61bbd81176561991be025af44efc67332feWenchao Tong    private void enforceReadCredentialPermission() {
5269878c61bbd81176561991be025af44efc67332feWenchao Tong        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL,
5279878c61bbd81176561991be025af44efc67332feWenchao Tong                                                "WifiService");
5289878c61bbd81176561991be025af44efc67332feWenchao Tong    }
5299878c61bbd81176561991be025af44efc67332feWenchao Tong
530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceWorkSourcePermission() {
531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
532a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceMulticastChangePermission() {
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "WifiService");
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceConnectivityInternalPermission() {
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CONNECTIVITY_INTERNAL,
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "ConnectivityService");
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public synchronized boolean setWifiEnabled(boolean enable) {
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) {
559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * Caller might not have WRITE_SECURE_SETTINGS,
564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * only CHANGE_WIFI_STATE is enforced
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (! mSettingsStore.handleWifiToggled(enable)) {
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Nothing to do if wifi cannot be toggled
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return true;
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiState()}
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_DISABLING},
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLED},
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLING},
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_UNKNOWN}
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiEnabledState() {
590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiState();
592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig SSID, security and channel details as
597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *        part of WifiConfiguration
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enabled true to enable and false to disable
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
601328fc5407927799843c11f2b767a8cf47b89f366Robert Greenwalt        enforceChangePermission();
60234f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwalt        ConnectivityManager.enforceTetherChangePermission(mContext);
603f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
60413cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds            throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
60513cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        }
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // null wifiConfig is a meaningful input for CMD_SET_AP
607d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (wifiConfig == null || isValid(wifiConfig)) {
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApState()}
616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_DISABLING},
618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLED},
619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLING},
620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_FAILED}
621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiApEnabledState() {
623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApState();
625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApConfiguration()}
629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return soft access point configuration
630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiConfiguration getWifiApConfiguration() {
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApConfiguration();
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
637a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * see {@link WifiManager#buildWifiConfig()}
638a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     * @return a WifiConfiguration.
639a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist     */
640a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    public WifiConfiguration buildWifiConfig(String uriString, String mimeType, byte[] data) {
6414e940bb499f65b4305dbeb2e01237c43c2b0b42dJan Nordqvist        if (mimeType.equals(ConfigBuilder.WifiConfigType)) {
642a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            try {
643a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                return ConfigBuilder.buildConfig(uriString, data, mContext);
644a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
645a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            catch (IOException | GeneralSecurityException | SAXException e) {
646a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist                Log.e(TAG, "Failed to parse wi-fi configuration: " + e);
647a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            }
648a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
649a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        else {
650a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist            Log.i(TAG, "Unknown wi-fi config type: " + mimeType);
651a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        }
652a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist        return null;
653a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    }
654a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist
655a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvist    /**
656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig WifiConfiguration details for soft access point
658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiConfig == null)
662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
663d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (isValid(wifiConfig)) {
664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setWifiApConfiguration(wifiConfig);
665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isScanAlwaysAvailable() {
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mSettingsStore.isScanAlwaysAvailable();
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#disconnect()}
682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void disconnect() {
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.disconnectCommand();
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reconnect()}
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reconnect() {
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reassociate()}
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reassociate() {
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reassociateCommand();
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
705048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande     * see {@link android.net.wifi.WifiManager#getSupportedFeatures}
706a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande     */
707048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public int getSupportedFeatures() {
708a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        enforceAccessPermission();
709a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        if (mWifiStateMachineChannel != null) {
710048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel);
711a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        } else {
712a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
713048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return 0;
714a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        }
715a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
716a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande
717a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    /**
71894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski     * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)}
719200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     */
720048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public WifiActivityEnergyInfo reportActivityInfo() {
721200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        enforceAccessPermission();
722200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiLinkLayerStats stats;
723200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiActivityEnergyInfo energyInfo = null;
724200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (mWifiStateMachineChannel != null) {
725048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel);
726200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            if (stats != null) {
72794a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxIdleCurrent = mContext.getResources().getInteger(
72894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_idle_receive_cur_ma);
72994a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxCurrent = mContext.getResources().getInteger(
73094a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_active_rx_cur_ma);
73194a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long txCurrent = mContext.getResources().getInteger(
73294a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_tx_cur_ma);
73394a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final double voltage = mContext.getResources().getInteger(
73494a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_operating_voltage_mv)
73594a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        / 1000.0;
73694a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski
73794a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time;
73894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long energyUsed = (long)((stats.tx_time * txCurrent +
73994a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        stats.rx_time * rxCurrent +
74094a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        rxIdleTime * rxIdleCurrent) * voltage);
74170b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                if (VDBG) {
74270b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    StringBuilder sb = new StringBuilder();
74370b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxIdleCur=" + rxIdleCurrent);
74470b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxCur=" + rxCurrent);
74570b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" txCur=" + txCurrent);
74670b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" voltage=" + voltage);
74770b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" on_time=" + stats.on_time);
74870b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" tx_time=" + stats.tx_time);
74970b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rx_time=" + stats.rx_time);
75070b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxIdleTime=" + rxIdleTime);
75170b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" energy=" + energyUsed);
75270b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    Log.e(TAG, " reportActivityInfo: " + sb.toString());
75370b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                }
75484a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
755200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                // Convert the LinkLayerStats into EnergyActivity
756186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinski                energyInfo = new WifiActivityEnergyInfo(SystemClock.elapsedRealtime(),
757200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                        WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time,
75894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        stats.rx_time, rxIdleTime, energyUsed);
759200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            }
760200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return energyInfo;
761200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        } else {
762200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
763200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return null;
764200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        }
765200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    }
766200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle
767200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    /**
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of configured networks
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<WifiConfiguration> getConfiguredNetworks() {
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
77482199a285f4a45a46b44eb8253999aa918534753vandwalle            return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(),
77582199a285f4a45a46b44eb8253999aa918534753vandwalle                    mWifiStateMachineChannel);
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return null;
779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
7839878c61bbd81176561991be025af44efc67332feWenchao Tong     * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()}
7849878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return the list of configured networks with real preSharedKey
7859878c61bbd81176561991be025af44efc67332feWenchao Tong     */
7869878c61bbd81176561991be025af44efc67332feWenchao Tong    public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
7879878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceReadCredentialPermission();
7889878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceAccessPermission();
7899878c61bbd81176561991be025af44efc67332feWenchao Tong        if (mWifiStateMachineChannel != null) {
7909878c61bbd81176561991be025af44efc67332feWenchao Tong            return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
7919878c61bbd81176561991be025af44efc67332feWenchao Tong        } else {
7929878c61bbd81176561991be025af44efc67332feWenchao Tong            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
7939878c61bbd81176561991be025af44efc67332feWenchao Tong            return null;
7949878c61bbd81176561991be025af44efc67332feWenchao Tong        }
7959878c61bbd81176561991be025af44efc67332feWenchao Tong    }
7969878c61bbd81176561991be025af44efc67332feWenchao Tong
7979878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
798ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * Returns a WifiConfiguration matching this ScanResult
799ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @param scanResult scanResult that represents the BSSID
800ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @return {@link WifiConfiguration} that matches this BSSID or null
801ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     */
802ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) {
803ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        enforceAccessPermission();
804ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel);
805ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    }
806ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
807ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
808ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    /**
809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the supplicant-assigned identifier for the new or updated
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * network if the operation succeeds, or {@code -1} if it fails
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addOrUpdateNetwork(WifiConfiguration config) {
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
8154aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        if (isValid(config) && isValidPasspoint(config)) {
816cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
81731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
81831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
81931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            if (config.isPasspoint() &&
82031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS ||
82131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS)) {
82231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                try {
82331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    verifyCert(enterpriseConfig.getCaCertificate());
82431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                } catch (CertPathValidatorException cpve) {
82531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    Slog.e(TAG, "CA Cert " +
82631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
82731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            " untrusted: " + cpve.getMessage());
82831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    return -1;
82931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                } catch (GeneralSecurityException | IOException e) {
83031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    Slog.e(TAG, "Failed to verify certificate" +
83131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
83231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                            ": " + e);
83331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    return -1;
83431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                }
83531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            }
83631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
837992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            //TODO: pass the Uid the WifiStateMachine as a message parameter
83831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid())
839992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " SSID " + config.SSID
840992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " nid=" + Integer.toString(config.networkId));
841992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
842992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.creatorUid = Binder.getCallingUid();
843992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            } else {
844992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.lastUpdateUid = Binder.getCallingUid();
845992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mWifiStateMachineChannel != null) {
847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
848155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
849155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
850155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
851155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
852155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "bad network configuration");
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
85831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static void verifyCert(X509Certificate caCert)
85931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            throws GeneralSecurityException, IOException {
86031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertificateFactory factory = CertificateFactory.getInstance("X.509");
86131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPathValidator validator =
86231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                CertPathValidator.getInstance(CertPathValidator.getDefaultType());
86331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPath path = factory.generateCertPath(
86431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                Arrays.asList(caCert));
86531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
86631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        ks.load(null, null);
86731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        PKIXParameters params = new PKIXParameters(ks);
86831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        params.setRevocationEnabled(false);
86931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        validator.validate(path, params);
87031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
87131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
87231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    /**
873155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
874155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
875155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
876155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
877155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
878155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
879155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
880c35d728a15e9270f5642ef79f5245c89d749285fSky Faber
881002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        if (!isOwner(Binder.getCallingUid())) {
882002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Slog.e(TAG, "Remove is not authorized for user");
883002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
884002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
885002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
886155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
887155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
888155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
889155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
890155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
891155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
892155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
893155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
894155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
895155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
896155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
897155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
898155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param disableOthers if true, disable all other networks.
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
903155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    disableOthers);
906155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
907155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
908155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
909155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
910155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
911155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
912155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
913155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
914155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
915155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
923155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
924155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the Wi-Fi information, contained in {@link WifiInfo}.
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiInfo getConnectionInfo() {
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * Make sure we have the latest information, by sending
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * a status request to the supplicant.
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncRequestConnectionInfo();
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the results of the most recent access point scan, in the form of
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a list of {@link ScanResult} objects.
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of results
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<ScanResult> getScanResults(String callingPackage) {
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int userId = UserHandle.getCallingUserId();
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
9502812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov        boolean isSystemProcess = (UserHandle.getAppId(uid) == android.os.Process.SYSTEM_UID);
951f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        boolean hasInteractUsersFull = checkInteractAcrossUsersFull();
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
9532812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov        if (!isSystemProcess && !isLocationEnabled()) {
9542812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov            return new ArrayList<ScanResult>();
9552812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov        }
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
957e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            if (!isSystemProcess && !checkCallerHasLocationPermission(callingPackage, uid)) {
958e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                return new ArrayList<ScanResult>();
959e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            }
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    != AppOpsManager.MODE_ALLOWED) {
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return new ArrayList<ScanResult>();
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
964f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk            if (!isCurrentProfile(userId) && !hasInteractUsersFull) {
9651ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber                return new ArrayList<ScanResult>();
9661ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber            }
967fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return mWifiStateMachine.syncGetScanResultsList();
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
9732812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov    private boolean isLocationEnabled() {
9742812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov        return Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE,
9752812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov                Settings.Secure.LOCATION_MODE_OFF) != Settings.Secure.LOCATION_MODE_OFF;
9762812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov    }
9772812e1e19291d4f0dbabf7fe23d031d8cd641830Fyodor Kupolov
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
979f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
980f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk     */
981f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    private boolean checkInteractAcrossUsersFull() {
982f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk        return mContext.checkCallingOrSelfPermission(
983f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
984f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk                == PackageManager.PERMISSION_GRANTED;
985f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    }
986f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk
987f8704c368e0fee0c7cf7e294d109dece6b72e832Jason Monk    /**
988b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * Returns true if the calling user is the current one or a profile of the
989b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy     * current user..
990fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina     */
9911ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber    private boolean isCurrentProfile(int userId) {
992fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        int currentUser = ActivityManager.getCurrentUser();
993fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        if (userId == currentUser) {
994fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            return true;
995fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
996f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        List<UserInfo> profiles = mUserManager.getProfiles(currentUser);
997b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy        for (UserInfo user : profiles) {
998b59f69faf9e3de740bbe1e55cef70e9e22858c2aKenny Guy            if (userId == user.id) {
999fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina                return true;
1000fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina            }
1001fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        }
1002fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina        return false;
1003fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    }
1004fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina
1005fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    /**
10062a325cd07d40941adb93c1284abb263ba650c213Sky Faber     * Returns true if uid is an application running under the owner or a profile of the owner.
1007002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     *
1008002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     * Note: Should not be called if identity is cleared.
1009002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber     */
10102a325cd07d40941adb93c1284abb263ba650c213Sky Faber    private boolean isOwner(int uid) {
1011002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        long ident = Binder.clearCallingIdentity();
10122a325cd07d40941adb93c1284abb263ba650c213Sky Faber        int userId = UserHandle.getUserId(uid);
1013002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        try {
1014002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            int ownerUser = UserHandle.USER_OWNER;
1015002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            if (userId == ownerUser) {
1016002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                return true;
1017002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
1018f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            List<UserInfo> profiles = mUserManager.getProfiles(ownerUser);
1019002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            for (UserInfo profile : profiles) {
1020002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                if (userId == profile.id) {
1021002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    return true;
1022002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                }
1023002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            }
1024002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            return false;
1025002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
1026002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        finally {
1027002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber            Binder.restoreCallingIdentity(ident);
1028002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber        }
1029002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    }
1030002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
1031002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber
1032002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber    /**
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Tell the supplicant to persist the current list of configured networks.
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TODO: deprecate this
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfiguration() {
1039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean result = true;
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the country code
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param countryCode ISO 3166 country code.
1052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The persist behavior exists so that wifi can fall back to the last
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * persisted country code on a restart, when the locale information is
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * not available from telephony.
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setCountryCode(String countryCode, boolean persist) {
1059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
1060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
10619c22c8ea193360f50ad490368ca6a11af0684362Robert Greenwalt        enforceConnectivityInternalPermission();
1062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
1063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setCountryCode(countryCode, persist);
1065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
107080d14d67a956e6c69433470aaa73100898166efaxinhe     /**
107180d14d67a956e6c69433470aaa73100898166efaxinhe     * Get the country code
107280d14d67a956e6c69433470aaa73100898166efaxinhe     * @return ISO 3166 country code.
107380d14d67a956e6c69433470aaa73100898166efaxinhe     */
107480d14d67a956e6c69433470aaa73100898166efaxinhe    public String getCountryCode() {
107580d14d67a956e6c69433470aaa73100898166efaxinhe        enforceConnectivityInternalPermission();
107680d14d67a956e6c69433470aaa73100898166efaxinhe        String country = mWifiStateMachine.getCountryCode();
107780d14d67a956e6c69433470aaa73100898166efaxinhe        return country;
107880d14d67a956e6c69433470aaa73100898166efaxinhe    }
1079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the operational frequency band
1081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param band One of
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
1084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
1086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setFrequencyBand(int band, boolean persist) {
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!isDualBandSupported()) return;
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set frequency band to " + band +
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setFrequencyBand(band, persist);
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the operational frequency band
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getFrequencyBand() {
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getFrequencyBand();
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isDualBandSupported() {
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //TODO: Should move towards adding a driver API that checks at runtime
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mContext.getResources().getBoolean(
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                com.android.internal.R.bool.config_wifi_dual_band_support);
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the DHCP-assigned addresses from the last successful DHCP request,
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * if any.
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the DHCP information
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @deprecated
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public DhcpInfo getDhcpInfo() {
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpInfo info = new DhcpInfo();
11273b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
11283b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.ipAddress != null &&
11293b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                dhcpResults.ipAddress.getAddress() instanceof Inet4Address) {
11303b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress());
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
11323b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
11333b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.gateway != null) {
11343b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway);
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
11363b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int dnsFound = 0;
11383b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        for (InetAddress dns : dhcpResults.dnsServers) {
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (dns instanceof Inet4Address) {
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (dnsFound == 0) {
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (++dnsFound > 1) break;
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        InetAddress serverAddress = dhcpResults.serverAddress;
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (serverAddress instanceof Inet4Address) {
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address)serverAddress);
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        info.leaseDuration = dhcpResults.leaseDuration;
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return info;
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#startWifi}
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void startWifi() {
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* TODO: may be add permissions for access only to connectivity service
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a start issued, keep wifi alive until a stop issued irrespective
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * of WifiLock & device idle status unless wifi enabled status is toggled
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(true);
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1172155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#stopWifi}
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void stopWifi() {
1177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceConnectivityInternalPermission();
1178155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: if a stop is issued, wifi is brought up only by startWifi
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * unless wifi enabled status is toggled
1181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.setDriverStart(false);
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addToBlacklist}
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void addToBlacklist(String bssid) {
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.addToBlacklist(bssid);
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#clearBlacklist}
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void clearBlacklist() {
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.clearBlacklist();
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * enable TDLS for the local NIC to remote NIC
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The APPs don't know the remote MAC address to identify NIC though,
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * so we need to do additional work to find it from remote IP address
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTaskParams {
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String remoteIpAddress;
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public boolean enable;
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> {
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        protected Integer doInBackground(TdlsTaskParams... params) {
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Retrieve parameters for the call
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            TdlsTaskParams param = params[0];
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String remoteIpAddress = param.remoteIpAddress.trim();
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean enable = param.enable;
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Get MAC address of Remote IP
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String macAddress = null;
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            BufferedReader reader = null;
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                reader = new BufferedReader(new FileReader("/proc/net/arp"));
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Skip over the line bearing colum titles
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String line = reader.readLine();
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                while ((line = reader.readLine()) != null) {
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String[] tokens = line.split("[ ]+");
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (tokens.length < 6) {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        continue;
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // ARP column format is
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Address HWType HWAddress Flags Mask IFace
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String ip = tokens[0];
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String mac = tokens[3];
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (remoteIpAddress.equals(ip)) {
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        macAddress = mac;
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (macAddress == null) {
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " +
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            "/proc/net/arp");
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    enableTdlsWithMacAddress(macAddress, enable);
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (FileNotFoundException e) {
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address");
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (IOException e) {
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address");
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } finally {
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (reader != null) {
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        reader.close();
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                catch (IOException e) {
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Do nothing
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return 0;
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdls(String remoteAddress, boolean enable) {
12808e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteAddress == null) {
12818e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteAddress cannot be null");
12828e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
12838e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        TdlsTaskParams params = new TdlsTaskParams();
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.remoteIpAddress = remoteAddress;
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.enable = enable;
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        new TdlsTask().execute(params);
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) {
12928e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteMacAddress == null) {
12938e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteMacAddress cannot be null");
12948e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
12958e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableTdls(remoteMacAddress, enable);
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get a reference to handler. This is used by a client to establish
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * an AsyncChannel communication with WifiService
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public Messenger getWifiServiceMessenger() {
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return new Messenger(mClientHandler);
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13092ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    /**
13102ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer
13112ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     */
13122ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    public void disableEphemeralNetwork(String SSID) {
13132ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceAccessPermission();
13142ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceChangePermission();
13152ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        mWifiStateMachine.disableEphemeralNetwork(SSID);
13162ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    }
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get the IP and proxy configuration file
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getConfigFile() {
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.getConfigFile();
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void onReceive(Context context, Intent intent) {
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String action = intent.getAction();
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (action.equals(Intent.ACTION_SCREEN_ON)) {
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_ON);
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_USER_PRESENT);
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_OFF);
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int pluggedType = intent.getIntExtra("plugged", 0);
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null);
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        BluetoothAdapter.STATE_DISCONNECTED);
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.sendBluetoothAdapterStateChange(state);
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false);
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0);
134603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) {
134703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                handleIdleModeChanged();
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    };
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Observes settings changes to scan always mode.
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForScanModeChange() {
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ContentObserver contentObserver = new ContentObserver(null) {
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            @Override
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            public void onChange(boolean selfChange) {
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mSettingsStore.handleWifiScanAlwaysAvailableToggled();
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED);
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        };
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.getContentResolver().registerContentObserver(
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE),
1366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                false, contentObserver);
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForBroadcasts() {
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IntentFilter intentFilter = new IntentFilter();
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
137803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(mReceiver, intentFilter);
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1382faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee    private void registerForPackageOrUserRemoval() {
1383e487a4648dd41881e754f1224aaedead78a0777dSky Faber        IntentFilter intentFilter = new IntentFilter();
1384e487a4648dd41881e754f1224aaedead78a0777dSky Faber        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1385faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        intentFilter.addAction(Intent.ACTION_USER_REMOVED);
1386faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        mContext.registerReceiverAsUser(new BroadcastReceiver() {
1387e487a4648dd41881e754f1224aaedead78a0777dSky Faber            @Override
1388e487a4648dd41881e754f1224aaedead78a0777dSky Faber            public void onReceive(Context context, Intent intent) {
1389faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                switch (intent.getAction()) {
1390faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_PACKAGE_REMOVED: {
1391faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1392faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1393faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1394faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1395faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        Uri uri = intent.getData();
1396faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (uid == -1 || uri == null) {
1397faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1398faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1399faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        String pkgName = uri.getSchemeSpecificPart();
1400faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeAppConfigs(pkgName, uid);
1401faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1402faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    }
1403faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_USER_REMOVED: {
1404faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
1405faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeUserConfigs(userHandle);
1406faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1407e487a4648dd41881e754f1224aaedead78a0777dSky Faber                    }
1408e487a4648dd41881e754f1224aaedead78a0777dSky Faber                }
1409e487a4648dd41881e754f1224aaedead78a0777dSky Faber            }
1410faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        }, UserHandle.ALL, intentFilter, null, null);
1411e487a4648dd41881e754f1224aaedead78a0777dSky Faber    }
1412e487a4648dd41881e754f1224aaedead78a0777dSky Faber
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    @Override
1414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                != PackageManager.PERMISSION_GRANTED) {
1417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("Permission Denial: can't dump WifiService from from pid="
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + Binder.getCallingPid()
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
1423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Stay-awake conditions: " +
1424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getInt(mContext.getContentResolver(),
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                       Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0));
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastEnabled " + mMulticastEnabled);
1427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mMulticastDisabled " + mMulticastDisabled);
142803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        pw.println("mInIdleMode " + mInIdleMode);
142903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        pw.println("mScanPending " + mScanPending);
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.dump(fd, pw, args);
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mSettingsStore.dump(fd, pw, args);
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNotificationController.dump(fd, pw, args);
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mTrafficPoller.dump(fd, pw, args);
1434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Latest scan results:");
1436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
1437a0708b09ad17b086c008ab100aec7143d7613c80vandwalle        long nowMs = System.currentTimeMillis();
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (scanResults != null && scanResults.size() != 0) {
1439a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            pw.println("    BSSID              Frequency  RSSI    Age      SSID " +
1440a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    "                                Flags");
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (ScanResult r : scanResults) {
1442a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageSec = 0;
1443a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                long ageMilli = 0;
1444a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (nowMs > r.seen && r.seen > 0) {
1445a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageSec = (nowMs - r.seen) / 1000;
1446a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                    ageMilli = (nowMs - r.seen) % 1000;
1447a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                }
1448a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                String candidate = " ";
1449a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                if (r.isAutoJoinCandidate > 0) candidate = "+";
1450a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                pw.printf("  %17s  %9d  %5d  %3d.%03d%s   %-32s  %s\n",
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.BSSID,
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.frequency,
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                         r.level,
1454a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         ageSec, ageMilli,
1455a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         candidate,
1456a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.SSID == null ? "" : r.SSID,
1457a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                                         r.capabilities);
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks acquired: " + mFullLocksAcquired + " full, " +
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksAcquired + " full high perf, " +
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksAcquired + " scan");
1464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks released: " + mFullLocksReleased + " full, " +
1465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mFullHighPerfLocksReleased + " full high perf, " +
1466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mScanLocksReleased + " scan");
1467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Locks held:");
1469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.dump(pw);
1470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
14717e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande        pw.println("Multicast Locks held:");
14727e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande        for (Multicaster l : mMulticasters) {
14737e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande            pw.print("    ");
14747e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande            pw.println(l);
14757e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande        }
14767e6573a1866812a302c1bfd224a2ed2d5633259fVinit Deshpande
1477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiWatchdogStateMachine.dump(fd, pw, args);
1478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.dump(fd, pw, args);
1480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
1481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class WifiLock extends DeathRecipient {
1484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) {
1485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(lockMode, tag, binder, ws);
1486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                releaseWifiLockLocked(mBinder);
1491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}";
1496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class LockList {
1500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private List<WifiLock> mList;
1501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private LockList() {
1503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mList = new ArrayList<WifiLock>();
1504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized boolean hasLocks() {
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return !mList.isEmpty();
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized int getStrongestLockMode() {
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mList.isEmpty()) {
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) {
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mFullLocksAcquired > mFullLocksReleased) {
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return WifiManager.WIFI_MODE_FULL;
1521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return WifiManager.WIFI_MODE_SCAN_ONLY;
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized void updateWorkSource(WorkSource ws) {
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = 0; i < mLocks.mList.size(); i++) {
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ws.add(mLocks.mList.get(i).mWorkSource);
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void addLock(WifiLock lock) {
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (findLockByBinder(lock.mBinder) < 0) {
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mList.add(lock);
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private WifiLock removeLock(IBinder binder) {
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int index = findLockByBinder(binder);
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (index >= 0) {
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock ret = mList.remove(index);
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ret.unlinkDeathRecipient();
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return null;
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private int findLockByBinder(IBinder binder) {
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mList.size();
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (mList.get(i).mBinder == binder)
1553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return i;
1554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
1556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private void dump(PrintWriter pw) {
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (WifiLock l : mList) {
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.print("    ");
1561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pw.println(l);
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void enforceWakeSourcePermission(int uid, int pid) {
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (uid == android.os.Process.myUid()) {
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                pid, uid, null);
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
1575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (lockMode != WifiManager.WIFI_MODE_FULL &&
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_SCAN_ONLY &&
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) {
1579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Illegal argument, lockMode= " + lockMode);
1580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode);
1581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid());
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws == null) {
1590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = new WorkSource(Binder.getCallingUid());
1591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws);
1593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return acquireWifiLockLocked(wifiLock);
1595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException {
1599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource);
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException {
1609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        switch(wifiLock.mMode) {
1610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource);
1614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean acquireWifiLockLocked(WifiLock wifiLock) {
1619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock);
1620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLocks.addLock(wifiLock);
1622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            noteAcquireWifiLock(wifiLock);
1626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch(wifiLock.mMode) {
1627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL:
1628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullLocksAcquired;
1629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mFullHighPerfLocksAcquired;
1632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case WifiManager.WIFI_MODE_SCAN_ONLY:
1635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ++mScanLocksAcquired;
1636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
1637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
1640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) {
1648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int pid = Binder.getCallingPid();
1650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null && ws.size() == 0) {
1651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ws = null;
1652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ws != null) {
1654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWakeSourcePermission(uid, pid);
1655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mLocks) {
1659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index = mLocks.findLockByBinder(lock);
1660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (index < 0) {
1661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    throw new IllegalArgumentException("Wifi lock not active");
1662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiLock wl = mLocks.mList.get(index);
1664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wl);
1665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid);
1666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteAcquireWifiLock(wl);
1667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean releaseWifiLock(IBinder lock) {
1675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
1676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mLocks) {
1677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return releaseWifiLockLocked(lock);
1678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean releaseWifiLockLocked(IBinder lock) {
1682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean hadLock;
1683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiLock wifiLock = mLocks.removeLock(lock);
1685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock);
1687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        hadLock = (wifiLock != null);
1689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (hadLock) {
1693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                noteReleaseWifiLock(wifiLock);
1694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                switch(wifiLock.mMode) {
1695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL:
1696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullLocksReleased;
1697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_FULL_HIGH_PERF:
1699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mFullHighPerfLocksReleased;
1700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    case WifiManager.WIFI_MODE_SCAN_ONLY:
1702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        ++mScanLocksReleased;
1703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return hadLock;
1713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private abstract class DeathRecipient
1716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            implements IBinder.DeathRecipient {
1717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String mTag;
1718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int mMode;
1719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IBinder mBinder;
1720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WorkSource mWorkSource;
1721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) {
1723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super();
1724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mTag = tag;
1725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMode = mode;
1726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder = binder;
1727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWorkSource = ws;
1728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mBinder.linkToDeath(this, 0);
1730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
1731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                binderDied();
1732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        void unlinkDeathRecipient() {
1736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBinder.unlinkToDeath(this, 0);
1737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private class Multicaster extends DeathRecipient {
1741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster(String tag, IBinder binder) {
1742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(Binder.getCallingUid(), tag, binder, null);
1743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void binderDied() {
1746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Multicaster binderDied");
1747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized (mMulticasters) {
1748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int i = mMulticasters.indexOf(this);
1749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (i != -1) {
1750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, mMode);
1751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String toString() {
1756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return "Multicaster{" + mTag + " binder=" + mBinder + "}";
1757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public int getUid() {
1760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mMode;
1761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void initializeMulticastFiltering() {
1765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // if anybody had requested filters be off, leave off
1769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mMulticasters.size() != 0) {
1770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return;
1771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.startFilteringMulticastV4Packets();
1773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void acquireMulticastLock(IBinder binder, String tag) {
1778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastEnabled++;
1782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticasters.add(new Multicaster(tag, binder));
1783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Note that we could call stopFilteringMulticastV4Packets only when
1784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // our new size == 1 (first call), but this function won't
1785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // be called often and by making the stopPacket call each
1786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // time we're less fragile and self-healing.
1787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.stopFilteringMulticastV4Packets();
1788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1790155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastEnabled(uid);
1794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void releaseMulticastLock() {
1801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mMulticastDisabled++;
1806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int size = mMulticasters.size();
1807155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (int i = size - 1; i >= 0; i--) {
1808155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Multicaster m = mMulticasters.get(i);
1809155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if ((m != null) && (m.getUid() == uid)) {
1810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    removeMulticasterLocked(i, uid);
1811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1815155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1816155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void removeMulticasterLocked(int i, int uid)
1817155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    {
1818155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Multicaster removed = mMulticasters.remove(i);
1819155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1820155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (removed != null) {
1821155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            removed.unlinkDeathRecipient();
1822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1823155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mMulticasters.size() == 0) {
1824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.startFilteringMulticastV4Packets();
1825155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1826155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1827155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long ident = Binder.clearCallingIdentity();
1828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
1829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mBatteryStats.noteWifiMulticastDisabled(uid);
1830155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (RemoteException e) {
1831155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1833155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isMulticastEnabled() {
1837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized (mMulticasters) {
1840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return (mMulticasters.size() > 0);
1841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1842155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
18439ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng
1844511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng    public WifiMonitor getWifiMonitor() {
1845511a412adce5a46c41e61f90c0df6de5fb4e491cYuhao Zheng        return mWifiStateMachine.getWifiMonitor();
18469ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng    }
1847ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1848ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public void enableVerboseLogging(int verbose) {
1849ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1850ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        mWifiStateMachine.enableVerboseLogging(verbose);
1851ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1852ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
1853ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public int getVerboseLoggingLevel() {
1854ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1855ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        return mWifiStateMachine.getVerboseLoggingLevel();
1856ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1857c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1858c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void enableAggressiveHandover(int enabled) {
1859c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1860c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.enableAggressiveHandover(enabled);
1861c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1862c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1863c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAggressiveHandover() {
1864c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1865c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAggressiveHandover();
1866c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1867c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1868c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void setAllowScansWithTraffic(int enabled) {
1869c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1870c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.setAllowScansWithTraffic(enabled);
1871c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1872c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
1873c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAllowScansWithTraffic() {
1874c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1875c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAllowScansWithTraffic();
1876c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1877c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
1878a8647b8cb29de22765062714cb265247234c3d32xinhe    public boolean enableAutoJoinWhenAssociated(boolean enabled) {
1879a8647b8cb29de22765062714cb265247234c3d32xinhe        enforceChangePermission();
1880a8647b8cb29de22765062714cb265247234c3d32xinhe        return mWifiStateMachine.enableAutoJoinWhenAssociated(enabled);
1881e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1882e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
1883a8647b8cb29de22765062714cb265247234c3d32xinhe    public boolean getEnableAutoJoinWhenAssociated() {
1884e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1885a8647b8cb29de22765062714cb265247234c3d32xinhe        return mWifiStateMachine.getEnableAutoJoinWhenAssociated();
1886e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1887b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public void setHalBasedAutojoinOffload(int enabled) {
1888a8647b8cb29de22765062714cb265247234c3d32xinhe        enforceChangePermission();
1889b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        mWifiStateMachine.setHalBasedAutojoinOffload(enabled);
1890b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1891b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1892b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    public int getHalBasedAutojoinOffload() {
1893b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        enforceAccessPermission();
1894b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        return mWifiStateMachine.getHalBasedAutojoinOffload();
1895b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle    }
1896b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1897c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* Return the Wifi Connection statistics object */
1898c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public WifiConnectionStatistics getConnectionStatistics() {
1899c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceAccessPermission();
1900c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceReadCredentialPermission();
1901c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        if (mWifiStateMachineChannel != null) {
1902c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel);
1903c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        } else {
1904c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1905c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return null;
1906c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1907c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
1908b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1909b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    public void factoryReset() {
1910b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        enforceConnectivityInternalPermission();
1911b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1912f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
1913f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            return;
1914f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1915b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1916f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
1917f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Turn mobile hotspot off
1918f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            setWifiApEnabled(null, false);
1919f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1920f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott
1921f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) {
1922f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Enable wifi
1923f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            setWifiEnabled(true);
1924f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Delete all Wifi SSIDs
1925f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            List<WifiConfiguration> networks = getConfiguredNetworks();
1926f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            if (networks != null) {
1927f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                for (WifiConfiguration config : networks) {
1928f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                    removeNetwork(config.networkId);
1929f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                }
1930f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                saveConfiguration();
1931b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott            }
1932b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        }
1933b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    }
1934cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
19354d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    /* private methods */
193631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    static boolean logAndReturnFalse(String s) {
19374d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        Log.d(TAG, s);
19384d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        return false;
19394d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
19404d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
194131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static boolean isValid(WifiConfiguration config) {
194231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        String validity = checkValidity(config);
194331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return validity == null || logAndReturnFalse(validity);
194431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
19454d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
19464aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    public static boolean isValidPasspoint(WifiConfiguration config) {
19474aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        String validity = checkPasspointValidity(config);
19484aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        return validity == null || logAndReturnFalse(validity);
19494aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    }
19504aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist
195131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String checkValidity(WifiConfiguration config) {
195231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (config.allowedKeyManagement == null)
195331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "allowed kmgmt";
19544d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
19554d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config.allowedKeyManagement.cardinality() > 1) {
19564d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.allowedKeyManagement.cardinality() != 2) {
195731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "cardinality != 2";
19584d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19594d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
196031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not WPA_EAP";
19614d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19624d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X))
19634d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) {
196431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not PSK or 8021X";
19654d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19664d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
19674aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        return null;
19684aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    }
19694d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
19704aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    public static String checkPasspointValidity(WifiConfiguration config) {
19714d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (!TextUtils.isEmpty(config.FQDN)) {
19724d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must not have an SSID */
19734d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!TextUtils.isEmpty(config.SSID)) {
197431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "SSID not expected for Passpoint: '" + config.SSID +
197531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                        "' FQDN " + toHexString(config.FQDN);
19764d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19774d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have a providerFriendlyName */
19784d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (TextUtils.isEmpty(config.providerFriendlyName)) {
197931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no provider friendly name";
19804d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
198107f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
19824d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have enterprise config */
198307f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            if (enterpriseConfig == null
198407f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    || enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) {
198531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no enterprise config";
19864d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
198707f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            if ((enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS ||
198807f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS ||
198907f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.PEAP) &&
199007f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getCaCertificate() == null) {
199131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no CA certificate";
19924d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
19934d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
199431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return null;
1995cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande    }
1996b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
19979846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    public Network getCurrentNetwork() {
19989846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        enforceAccessPermission();
19999846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        return mWifiStateMachine.getCurrentNetwork();
20009846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    }
20019846078598c3468f8813dbfa58238a1846bd81efSanket Padawe
200231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String toHexString(String s) {
200331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (s == null) {
200431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "null";
200531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
200631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        StringBuilder sb = new StringBuilder();
200731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        sb.append('\'').append(s).append('\'');
200831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        for (int n = 0; n < s.length(); n++) {
200931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            sb.append(String.format(" %02x", s.charAt(n) & 0xffff));
201031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
201131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return sb.toString();
20124d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
2013cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
2014e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov    /**
2015e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov     * Checks that calling process has android.Manifest.permission.ACCESS_COARSE_LOCATION or
2016e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov     * android.Manifest.permission.ACCESS_FINE_LOCATION and a corresponding app op is allowed
2017e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov     */
2018e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov    private boolean checkCallerHasLocationPermission(String callingPackage, int uid) {
2019e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_FINE_LOCATION, uid)
2020e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                == PackageManager.PERMISSION_GRANTED
2021e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov                && isAppOppAllowed(AppOpsManager.OP_FINE_LOCATION, callingPackage, uid)) {
2022e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov            return true;
2023e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov        }
2024e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov
2025e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        if (ActivityManager.checkUidPermission(Manifest.permission.ACCESS_COARSE_LOCATION, uid)
2026e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                == PackageManager.PERMISSION_GRANTED
2027e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                && isAppOppAllowed(AppOpsManager.OP_COARSE_LOCATION, callingPackage, uid)) {
2028e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            return true;
2029e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        }
2030e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        // Enforce location permission for apps targeting MNC and later versions
2031e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        boolean enforceLocationPermission = true;
2032e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        try {
2033e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            enforceLocationPermission = mContext.getPackageManager().getApplicationInfo(
2034e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                    callingPackage, 0).targetSdkVersion >= Build.VERSION_CODES.MNC;
2035e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        } catch (PackageManager.NameNotFoundException e) {
2036e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            // In case of exception, enforce permission anyway
2037e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        }
2038e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        if (enforceLocationPermission) {
2039e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            throw new SecurityException("Need ACCESS_COARSE_LOCATION or "
2040e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                    + "ACCESS_FINE_LOCATION permission to get scan results");
2041e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        } else {
2042e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov            Log.e(TAG, "Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION "
2043e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov                    + "permission to get scan results");
2044e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        }
2045e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolov        return false;
2046e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov    }
2047e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov
2048e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov    private boolean isAppOppAllowed(int op, String callingPackage, int uid) {
2049e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov        return mAppOps.noteOp(op, uid, callingPackage) == AppOpsManager.MODE_ALLOWED;
2050e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov    }
2051e87a8da7bb513ca720b0160e3bff8c9f355faabbFyodor Kupolov
2052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
2053