WifiServiceImpl.java revision 3c6e92f51984ba8999d5efe622b4a52dd18bdaff
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
19eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED;
20eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_BATTERY_CHANGED;
21090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_EMERGENCY_CALL_STATE_CHANGED;
22eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_EMERGENCY_MODE_CHANGED;
23eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_LOCKS_CHANGED;
24eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCAN_ALWAYS_MODE_CHANGED;
25eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_OFF;
26eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SCREEN_ON;
27eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_SET_AP;
28eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_USER_PRESENT;
29eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpandeimport static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED;
30eee1d479d8d402a2e78e2f143e957030cfc77749Vinit Deshpande
319086afccf6938a49eb9a2cd248917c1cb0943942vandwalleimport android.Manifest;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.app.AppOpsManager;
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.bluetooth.BluetoothAdapter;
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.BroadcastReceiver;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Context;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Intent;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.IntentFilter;
380469548a809654861f74dc42a3409f29ae750550Svetoslav Ganovimport android.content.pm.ApplicationInfo;
39155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.pm.PackageManager;
40155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.database.ContentObserver;
4134f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwaltimport android.net.ConnectivityManager;
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpInfo;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.DhcpResults;
449846078598c3468f8813dbfa58238a1846bd81efSanket Padaweimport android.net.Network;
45155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.NetworkUtils;
46e487a4648dd41881e754f1224aaedead78a0777dSky Faberimport android.net.Uri;
4784d962ec8f487f824214744498bba505a6db0c59Randy Panimport android.net.ip.IpManager;
483ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.wifi.IWifiManager;
498be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.ScanResult;
508be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.ScanSettings;
518be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiActivityEnergyInfo;
528be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiConfiguration;
538be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiConnectionStatistics;
548be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiEnterpriseConfig;
558be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiInfo;
568be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiLinkLayerStats;
578be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport android.net.wifi.WifiManager;
586c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silbersteinimport android.net.wifi.WifiScanner;
593ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiuimport android.net.wifi.hotspot2.PasspointConfiguration;
603ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.AsyncTask;
61c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinskiimport android.os.BatteryStats;
62155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Binder;
63e660aa2b9c9ca3a5c7903c37571ca7b91feb4ccfFyodor Kupolovimport android.os.Build;
64c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinskiimport android.os.Bundle;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.HandlerThread;
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.IBinder;
6740abf54c81c5624641543d86e1d7ab21ebe30175Paul Stewartimport android.os.Looper;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Message;
693ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.os.Messenger;
7003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackbornimport android.os.PowerManager;
710469548a809654861f74dc42a3409f29ae750550Svetoslav Ganovimport android.os.Process;
72155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.RemoteException;
73c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinskiimport android.os.ResultReceiver;
74186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinskiimport android.os.SystemClock;
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.UserHandle;
76fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghinaimport android.os.UserManager;
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.WorkSource;
78155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.provider.Settings;
794d381bc39f5263effdae73ec99065eb299b806caVinit Deshpandeimport android.text.TextUtils;
80992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalleimport android.util.Log;
81155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Slog;
82155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
83f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Raoimport com.android.internal.annotations.VisibleForTesting;
8498e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpandeimport com.android.internal.telephony.IccCardConstants;
85090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpandeimport com.android.internal.telephony.PhoneConstants;
868be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport com.android.internal.telephony.TelephonyIntents;
878be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport com.android.internal.util.AsyncChannel;
88c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiuimport com.android.server.wifi.hotspot2.PasspointManager;
89f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Raoimport com.android.server.wifi.util.WifiHandler;
9092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport com.android.server.wifi.util.WifiPermissionsUtil;
918be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kalele
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.BufferedReader;
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileDescriptor;
948be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport java.io.FileNotFoundException;
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileReader;
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.IOException;
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.PrintWriter;
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.Inet4Address;
998be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kaleleimport java.net.InetAddress;
100a1edc185d46d85e04930a5e12b465de9fea64afeJan Nordqvistimport java.security.GeneralSecurityException;
10131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.KeyStore;
10231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPath;
10331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertPathValidator;
1049a069cba2e5ae31ee00d9ec9a3c25bdb7052aa78Jan Nordqvistimport java.security.cert.CertPathValidatorException;
10531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.CertificateFactory;
10631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.PKIXParameters;
10731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.security.cert.X509Certificate;
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.ArrayList;
10931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvistimport java.util.Arrays;
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.util.List;
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WifiService handles remote WiFi operation requests by implementing
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * the IWifiManager interface.
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * @hide
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
11879a4204d12f32d2f6a4dfc8500f5e74718cabb8dVinit Deshpandepublic class WifiServiceImpl extends IWifiManager.Stub {
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String TAG = "WifiService";
120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean DBG = true;
12170b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle    private static final boolean VDBG = false;
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12384ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart    // Dumpsys argument to enable/disable disconnect on IP reachability failures.
12484ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart    private static final String DUMP_ARG_SET_IPREACH_DISCONNECT = "set-ipreach-disconnect";
12584ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart    private static final String DUMP_ARG_SET_IPREACH_DISCONNECT_ENABLED = "enabled";
12684ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart    private static final String DUMP_ARG_SET_IPREACH_DISCONNECT_DISABLED = "disabled";
12784ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiStateMachine mWifiStateMachine;
129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final Context mContext;
1310091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang    private final FrameworkFacade mFacade;
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    private final PowerManager mPowerManager;
134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final AppOpsManager mAppOps;
135f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott    private final UserManager mUserManager;
136d02611ce4158fda6c2d14ee13ad7f9553f416d21Ningyuan Wang    private final WifiCountryCode mCountryCode;
137ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    // Debug counter tracking scan requests sent by WifiManager
138ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle    private int scanRequestCounter = 0;
139ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Polls traffic stats and notifies clients */
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiTrafficPoller mTrafficPoller;
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the persisted states for wi-fi & airplane mode */
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    final WifiSettingsStore mSettingsStore;
1441b067831bbff14f8e7a99b927b69f714d1b03448Glen Kuhne    /* Logs connection events and some general router and scan stats */
1451b067831bbff14f8e7a99b927b69f714d1b03448Glen Kuhne    private final WifiMetrics mWifiMetrics;
1462bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    /* Manages affiliated certificates for current user */
1472bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    private final WifiCertManager mCertManager;
1482bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu
149637a86ffb3a036a4f26a471378b57d8817f35c25Glen Kuhne    private final WifiInjector mWifiInjector;
150ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    /* Backup/Restore Module */
151ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    private final WifiBackupRestore mWifiBackupRestore;
152ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius
1536c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein    private WifiScanner mWifiScanner;
1546c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein
155f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    private WifiLog mLog;
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Asynchronous channel to WifiStateMachine
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private AsyncChannel mWifiStateMachineChannel;
160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private WifiPermissionsUtil mWifiPermissionsUtil;
16292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
1630469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov    private final boolean mPermissionReviewRequired;
164c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu    private final PasspointManager mPasspointManager;
165f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    private final FrameworkFacade mFrameworkFacade;
1660469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov
167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles client connections
169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
170f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    private class ClientHandler extends WifiHandler {
171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
172f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        ClientHandler(String tag, Looper looper) {
173f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            super(tag, looper);
174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
176155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
177155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
178f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            super.handleMessage(msg);
179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
181155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // We track the clients by the Messenger
184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        // since it is expected to be always available
185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mTrafficPoller.addClient(msg.replyTo);
186155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Send failed, client connection lost");
194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mTrafficPoller.removeClient(msg.replyTo);
198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
201f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                    AsyncChannel ac = mFrameworkFacade.makeWifiAsyncChannel(TAG);
202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ac.connect(mContext, this, msg.replyTo);
203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Client commands are forwarded to state machine */
206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CONNECT_NETWORK:
207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.SAVE_NETWORK: {
208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration config = (WifiConfiguration) msg.obj;
209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    int networkId = msg.arg1;
210992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.SAVE_NETWORK) {
211f9946f5663f2d7a9a69cb8b7e82a4ef8b1825c48Mitchell Wills                        Slog.d("WiFiServiceImpl ", "SAVE"
212992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
213e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
214e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
215e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
216992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
217992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (msg.what == WifiManager.CONNECT_NETWORK) {
218f9946f5663f2d7a9a69cb8b7e82a4ef8b1825c48Mitchell Wills                        Slog.d("WiFiServiceImpl ", "CONNECT "
219992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + " nid=" + Integer.toString(networkId)
220e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " uid=" + msg.sendingUid
221e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + " name="
222e487a4648dd41881e754f1224aaedead78a0777dSky Faber                                + mContext.getPackageManager().getNameForUid(msg.sendingUid));
223992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
224e487a4648dd41881e754f1224aaedead78a0777dSky Faber
225d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande                    if (config != null && isValid(config)) {
22664c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        if (DBG) Slog.d(TAG, "Connect with config" + config);
22764c98f4afda6c7970c5d563580165a308d5e7c15Jason Monk                        mWifiStateMachine.sendMessage(Message.obtain(msg));
228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else if (config == null
229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            && networkId != WifiConfiguration.INVALID_NETWORK_ID) {
230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (DBG) Slog.d(TAG, "Connect with networkId" + networkId);
231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachine.sendMessage(Message.obtain(msg));
232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "ClientHandler.handleMessage ignoring invalid msg=" + msg);
234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (msg.what == WifiManager.CONNECT_NETWORK) {
235c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.CONNECT_NETWORK_FAILED,
236c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        } else {
238c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                            replyFailed(msg, WifiManager.SAVE_NETWORK_FAILED,
239c35d728a15e9270f5642ef79f5245c89d749285fSky Faber                                    WifiManager.INVALID_ARGS);
240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.FORGET_NETWORK:
245bace539f9cdc4ffb521c8251dd1c56073e805cd4Bartosz Fabianowski                    mWifiStateMachine.sendMessage(Message.obtain(msg));
246002ea67f42b0f69a8c9fa4719391f9476567b762Sky Faber                    break;
247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.START_WPS:
248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.CANCEL_WPS:
249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.DISABLE_NETWORK:
250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case WifiManager.RSSI_PKTCNT_FETCH: {
251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachine.sendMessage(Message.obtain(msg));
252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "ClientHandler.handleMessage ignoring msg=" + msg);
256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
261c35d728a15e9270f5642ef79f5245c89d749285fSky Faber        private void replyFailed(Message msg, int what, int why) {
2628fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley            Message reply = Message.obtain();
263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            reply.what = what;
264c35d728a15e9270f5642ef79f5245c89d749285fSky Faber            reply.arg1 = why;
265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                msg.replyTo.send(reply);
267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (RemoteException e) {
268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // There's not much we can do if reply can't be sent!
269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private ClientHandler mClientHandler;
273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Handles interaction with WifiStateMachine
276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
277f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    private class WifiStateMachineHandler extends WifiHandler {
278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        private AsyncChannel mWsmChannel;
279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
280f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        WifiStateMachineHandler(String tag, Looper looper, AsyncChannel asyncChannel) {
281f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            super(tag, looper);
282da918df16e03ee19be62343313d954027d3eb3abRebecca Silberstein            mWsmChannel = asyncChannel;
283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void handleMessage(Message msg) {
288f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            super.handleMessage(msg);
289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (msg.what) {
290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {
291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = mWsmChannel;
293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } else {
294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1);
295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        mWifiStateMachineChannel = null;
296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1);
301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWifiStateMachineChannel = null;
302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //Re-establish connection to state machine
303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler());
304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default: {
307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg);
308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WifiStateMachineHandler mWifiStateMachineHandler;
315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiController mWifiController;
3162a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein    private final WifiLockManager mWifiLockManager;
31761312e14a088a9487d4db64f08285162476e870fPaul Stewart    private final WifiMulticastLockManager mWifiMulticastLockManager;
318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
319da918df16e03ee19be62343313d954027d3eb3abRebecca Silberstein    public WifiServiceImpl(Context context, WifiInjector wifiInjector, AsyncChannel asyncChannel) {
320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext = context;
321da918df16e03ee19be62343313d954027d3eb3abRebecca Silberstein        mWifiInjector = wifiInjector;
322fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein
323fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mFacade = mWifiInjector.getFrameworkFacade();
324637a86ffb3a036a4f26a471378b57d8817f35c25Glen Kuhne        mWifiMetrics = mWifiInjector.getWifiMetrics();
325fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mTrafficPoller = mWifiInjector.getWifiTrafficPoller();
3260fb9cf43830d67894b519c9a6271320c4842a5c0Bartosz Fabianowski        mUserManager = UserManager.get(mContext);
327fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mCountryCode = mWifiInjector.getWifiCountryCode();
328fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mWifiStateMachine = mWifiInjector.getWifiStateMachine();
329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableRssiPolling(true);
330fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mSettingsStore = mWifiInjector.getWifiSettingsStore();
331fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mPowerManager = mContext.getSystemService(PowerManager.class);
332fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
333fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mCertManager = mWifiInjector.getWifiCertManager();
334fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mWifiLockManager = mWifiInjector.getWifiLockManager();
33561312e14a088a9487d4db64f08285162476e870fPaul Stewart        mWifiMulticastLockManager = mWifiInjector.getWifiMulticastLockManager();
336fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        HandlerThread wifiServiceHandlerThread = mWifiInjector.getWifiServiceHandlerThread();
337f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mClientHandler = new ClientHandler(TAG, wifiServiceHandlerThread.getLooper());
338f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mWifiStateMachineHandler = new WifiStateMachineHandler(TAG,
339f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                wifiServiceHandlerThread.getLooper(), asyncChannel);
340fca64fc16ec43befde2e7ac7c3bfd84ced1f7778Rebecca Silberstein        mWifiController = mWifiInjector.getWifiController();
3413204fb9682242a7b5a749489076c66d448c42577Roshan Pius        mWifiBackupRestore = mWifiInjector.getWifiBackupRestore();
3420469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        mPermissionReviewRequired = Build.PERMISSIONS_REVIEW_REQUIRED
3430469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                || context.getResources().getBoolean(
3440469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                com.android.internal.R.bool.config_permissionReviewRequired);
345d098b17dcef5b03a0626f34cc72d5fbce1bacb8fSohani Rao        mWifiPermissionsUtil = mWifiInjector.getWifiPermissionsUtil();
346c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        mPasspointManager = mWifiInjector.getPasspointManager();
347f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog = mWifiInjector.makeLog(TAG);
348f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mFrameworkFacade = wifiInjector.getFrameworkFacade();
34900ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein        enableVerboseLoggingInternal(getVerboseLoggingLevel());
3501c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    }
3511c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde
3521c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    /**
353f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao     * Provide a way for unit tests to set valid log object in the WifiHandler
354f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao     * @param log WifiLog object to assign to the clientHandler
355f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao     */
356f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    @VisibleForTesting
357f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    public void setWifiHandlerLogForTest(WifiLog log) {
358f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mClientHandler.setWifiLog(log);
359f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    }
360f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao    /**
3611c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * Check if Wi-Fi needs to be enabled and start
3621c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * if needed
3631c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     *
3641c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     * This function is used only at boot time
3651c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde     */
3661c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde    public void checkAndStartWifi() {
3671c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        /* Check if wi-fi needs to be enabled */
3681c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
3691c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
3701c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde                (wifiEnabled ? "enabled" : "disabled"));
371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForScanModeChange();
373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new BroadcastReceiver() {
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    @Override
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    public void onReceive(Context context, Intent intent) {
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (mSettingsStore.handleAirplaneModeToggled()) {
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
38098e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                        if (mSettingsStore.isAirplaneModeOn()) {
38198e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                            Log.d(TAG, "resetting country code because Airplane mode is ON");
382d02611ce4158fda6c2d14ee13ad7f9553f416d21Ningyuan Wang                            mCountryCode.airplaneModeEnabled();
38398e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                        }
384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                },
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
38898e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande        mContext.registerReceiver(
38998e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                new BroadcastReceiver() {
39098e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                    @Override
39198e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                    public void onReceive(Context context, Intent intent) {
39298e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                        String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
393a7bf9d90d68a4833cc3c64da98024167931c4b1epkanwar                        if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(state)) {
394fdce524fd05844c996cf1c5c0c102a87fde8e32cVinit Deshpande                            Log.d(TAG, "resetting networks because SIM was removed");
3953c8094ab45f3320dbe45e6460c5d62dcc24ce7aeMitchell Wills                            mWifiStateMachine.resetSimAuthNetworks(false);
39698e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                            Log.d(TAG, "resetting country code because SIM is removed");
397d02611ce4158fda6c2d14ee13ad7f9553f416d21Ningyuan Wang                            mCountryCode.simCardRemoved();
3983c8094ab45f3320dbe45e6460c5d62dcc24ce7aeMitchell Wills                        } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(state)) {
3993c8094ab45f3320dbe45e6460c5d62dcc24ce7aeMitchell Wills                            Log.d(TAG, "resetting networks because SIM was loaded");
4003c8094ab45f3320dbe45e6460c5d62dcc24ce7aeMitchell Wills                            mWifiStateMachine.resetSimAuthNetworks(true);
40198e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                        }
40298e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                    }
40398e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                },
40498e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande                new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED));
40598e43e5f48a4c87343fc311feda4fa5489948822Vinit Deshpande
406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Adding optimizations of only receiving broadcasts when wifi is enabled
407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // can result in race conditions when apps toggle wifi in the background
408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // without active user involvement. Always receive broadcasts.
409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        registerForBroadcasts();
410faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        registerForPackageOrUserRemoval();
41103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        mInIdleMode = mPowerManager.isDeviceIdleMode();
412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
413d84fd37259c6e956d0f00c261f573dfa319acb91Roshan Pius        if (!mWifiStateMachine.syncInitialize(mWifiStateMachineChannel)) {
414d84fd37259c6e956d0f00c261f573dfa319acb91Roshan Pius            Log.wtf(TAG, "Failed to initialize WifiStateMachine");
415d84fd37259c6e956d0f00c261f573dfa319acb91Roshan Pius        }
4161c28bb29cb19367d5e3bdbe6d8558d0fd4b8f33eVinit Deshapnde        mWifiController.start();
417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // If we are already disabled (could be due to airplane mode), avoid changing persist
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // state here
4200469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        if (wifiEnabled) {
4210469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            try {
4220469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                setWifiEnabled(mContext.getPackageName(), wifiEnabled);
4230469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            } catch (RemoteException e) {
4240469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                /* ignore - local call */
4250469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            }
4260469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        }
427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
429bcdabb1fa1894fcca610692ec94459fe623afa74Bartosz Fabianowski    public void handleUserSwitch(int userId) {
430bcdabb1fa1894fcca610692ec94459fe623afa74Bartosz Fabianowski        mWifiStateMachine.handleUserSwitch(userId);
431bcdabb1fa1894fcca610692ec94459fe623afa74Bartosz Fabianowski    }
4323bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius
4333bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius    public void handleUserUnlock(int userId) {
4343bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius        mWifiStateMachine.handleUserUnlock(userId);
4353bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius    }
4363bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius
4373bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius    public void handleUserStop(int userId) {
4383bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius        mWifiStateMachine.handleUserStop(userId);
4393bc487aa49deecbc358ee819e0dd4b2534412281Roshan Pius    }
440bcdabb1fa1894fcca610692ec94459fe623afa74Bartosz Fabianowski
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#pingSupplicant()}
443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeds, {@code false} otherwise
444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
4458fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean pingSupplicant() {
447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
448f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("pingSupplicant uid=%").c(Binder.getCallingUid()).flush();
449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel);
451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
458a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * see {@link android.net.wifi.WifiManager#startScan}
459a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * and {@link android.net.wifi.WifiManager#startCustomizedScan}
460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
461a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param settings If null, use default parameter, i.e. full scan.
462a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng     * @param workSource If null, all blame is given to the calling uid.
463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
4648fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
465a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng    public void startScan(ScanSettings settings, WorkSource workSource) {
466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
467f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("startScan uid=%").c(Binder.getCallingUid()).flush();
4681227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
4696c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein            if (mWifiScanner == null) {
4706c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                mWifiScanner = mWifiInjector.getWifiScanner();
4716c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein            }
47203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode) {
47349dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // Need to send an immediate scan result broadcast in case the
47449dfc6768889a33eb67102b16faf21667af2a60dVinit Deshpande                // caller is waiting for a result ..
475691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande
476691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                // clear calling identity to send broadcast
477691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                long callingIdentity = Binder.clearCallingIdentity();
478691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                try {
4796c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    // TODO: investigate if the logic to cancel scans when idle can move to
4806c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    // WifiScanningServiceImpl.  This will 1 - clean up WifiServiceImpl and 2 -
4816c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    // avoid plumbing an awkward path to report a cancelled/failed scan.  This will
4826c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    // be sent directly until b/31398592 is fixed.
4836c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    Intent intent = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
4846c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
4856c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    intent.putExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
4866c11cd5d6ff04afdf1bbbc99a850025d46c7bdc9Rebecca Silberstein                    mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
487691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                } finally {
488691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                    // restore calling identity
489691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                    Binder.restoreCallingIdentity(callingIdentity);
490691fb51e7ac5e4d0b91a4719cd0ed10a8c212e50Vinit Deshpande                }
49103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mScanPending = true;
49203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                return;
49303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
49403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
495a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        if (settings != null) {
496a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            settings = new ScanSettings(settings);
497a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            if (!settings.isValid()) {
498a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                Slog.e(TAG, "invalid scan setting");
499a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng                return;
500a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng            }
501a0009d14075b5345b8f916c3fb3f2260c938cb9dYuhao Zheng        }
502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (workSource != null) {
503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            enforceWorkSourcePermission();
504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // WifiManager currently doesn't use names, so need to clear names out of the
505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // supplied WorkSource to allow future WorkSource combining.
506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            workSource.clearNames();
507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
50848444cb4214a48a3a0bf4bbb93945c2aa68c9980Mitchell Wills        if (workSource == null && Binder.getCallingUid() >= 0) {
50948444cb4214a48a3a0bf4bbb93945c2aa68c9980Mitchell Wills            workSource = new WorkSource(Binder.getCallingUid());
51048444cb4214a48a3a0bf4bbb93945c2aa68c9980Mitchell Wills        }
511ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle        mWifiStateMachine.startScan(Binder.getCallingUid(), scanRequestCounter++,
512ebb7e0cb4572f73325a749539d589ebdefb8635cvandwalle                settings, workSource);
513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5158fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
5163f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    public String getWpsNfcConfigurationToken(int netId) {
517f3f4029b3ac41da4cc6a1bc22bae47c750a47048Andres Morales        enforceConnectivityInternalPermission();
518f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getWpsNfcConfigurationToken uid=%").c(Binder.getCallingUid()).flush();
519f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        // TODO Add private logging for netId b/33807876
5203f7ef65ab71619040032aee96b5599849881d6fdAndres Morales        return mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId);
5213f7ef65ab71619040032aee96b5599849881d6fdAndres Morales    }
5223f7ef65ab71619040032aee96b5599849881d6fdAndres Morales
5231227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mInIdleMode;
5241227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande    boolean mScanPending;
5251227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande
52603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    void handleIdleModeChanged() {
52703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        boolean doScan = false;
5281227b49a94f33844ad0606b48b591bea4d27b08eVinit Deshpande        synchronized (this) {
52903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            boolean idle = mPowerManager.isDeviceIdleMode();
53003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            if (mInIdleMode != idle) {
53103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                mInIdleMode = idle;
53203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                if (!idle) {
53303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    if (mScanPending) {
53403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        mScanPending = false;
53503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                        doScan = true;
53603b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                    }
53703b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                }
53803b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            }
53903b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
54003b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        if (doScan) {
54103b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            // Someone requested a scan while we were idle; do a full scan now.
54203b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            startScan(null, null);
54303b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        }
54403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn    }
54503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceAccessPermission() {
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE,
548a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceChangePermission() {
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE,
55331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                "WifiService");
5549086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    }
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5569086afccf6938a49eb9a2cd248917c1cb0943942vandwalle    private void enforceLocationHardwarePermission() {
5579086afccf6938a49eb9a2cd248917c1cb0943942vandwalle        mContext.enforceCallingOrSelfPermission(Manifest.permission.LOCATION_HARDWARE,
5589086afccf6938a49eb9a2cd248917c1cb0943942vandwalle                "LocationHardware");
559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
5619878c61bbd81176561991be025af44efc67332feWenchao Tong    private void enforceReadCredentialPermission() {
5629878c61bbd81176561991be025af44efc67332feWenchao Tong        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.READ_WIFI_CREDENTIAL,
5639878c61bbd81176561991be025af44efc67332feWenchao Tong                                                "WifiService");
5649878c61bbd81176561991be025af44efc67332feWenchao Tong    }
5659878c61bbd81176561991be025af44efc67332feWenchao Tong
566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceWorkSourcePermission() {
567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingPermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
568a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande                "WifiService");
569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceMulticastChangePermission() {
573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE,
575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "WifiService");
576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void enforceConnectivityInternalPermission() {
579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.enforceCallingOrSelfPermission(
580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                android.Manifest.permission.CONNECTIVITY_INTERNAL,
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                "ConnectivityService");
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enable {@code true} to enable, {@code false} to disable.
587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the enable/disable operation was
588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         started or is already in the queue.
589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
5908fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
5910469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov    public synchronized boolean setWifiEnabled(String packageName, boolean enable)
5920469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            throws RemoteException {
593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()
595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
596f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setWifiEnabled uid=% enable=%").c(Binder.getCallingUid()).c(enable).flush();
597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * Caller might not have WRITE_SECURE_SETTINGS,
599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * only CHANGE_WIFI_STATE is enforced
600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (! mSettingsStore.handleWifiToggled(enable)) {
604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Nothing to do if wifi cannot be toggled
605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return true;
606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
6110469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov
6120469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        if (mPermissionReviewRequired) {
6130469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            final int wiFiEnabledState = getWifiEnabledState();
6140469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            if (enable) {
6150469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                if (wiFiEnabledState == WifiManager.WIFI_STATE_DISABLING
6160469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                        || wiFiEnabledState == WifiManager.WIFI_STATE_DISABLED) {
61702938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov                    if (startConsentUi(packageName, Binder.getCallingUid(),
6180469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                            WifiManager.ACTION_REQUEST_ENABLE)) {
6190469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                        return true;
6200469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                    }
6210469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                }
6220469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            } else if (wiFiEnabledState == WifiManager.WIFI_STATE_ENABLING
6230469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                    || wiFiEnabledState == WifiManager.WIFI_STATE_ENABLED) {
62402938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov                if (startConsentUi(packageName, Binder.getCallingUid(),
6250469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                        WifiManager.ACTION_REQUEST_DISABLE)) {
6260469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                    return true;
6270469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                }
6280469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            }
6290469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        }
6300469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov
631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiController.sendMessage(CMD_WIFI_TOGGLED);
632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiState()}
637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_STATE_DISABLED},
638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_DISABLING},
639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLED},
640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_ENABLING},
641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_STATE_UNKNOWN}
642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6438fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiEnabledState() {
645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
646f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getWifiEnabledState uid=%").c(Binder.getCallingUid()).flush();
647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiState();
648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)}
652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig SSID, security and channel details as
653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *        part of WifiConfiguration
654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param enabled true to enable and false to disable
655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6568fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
658328fc5407927799843c11f2b767a8cf47b89f366Robert Greenwalt        enforceChangePermission();
65934f173a54307c39ffe5dfab52e7ef8166a00539eRobert Greenwalt        ConnectivityManager.enforceTetherChangePermission(mContext);
660f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao
661f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setWifiApEnabled uid=% enable=%").c(Binder.getCallingUid()).c(enabled).flush();
662f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao
663f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
66413cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds            throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
66513cddb5337418359eb5f9bebd0504fbc5c47fc41Julia Reynolds        }
666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // null wifiConfig is a meaningful input for CMD_SET_AP
667d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (wifiConfig == null || isValid(wifiConfig)) {
668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApState()}
676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED},
677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_DISABLING},
678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLED},
679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_ENABLING},
680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *         {@link WifiManager#WIFI_AP_STATE_FAILED}
681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6828fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int getWifiApEnabledState() {
684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
685f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getWifiApEnabledState uid=%").c(Binder.getCallingUid()).flush();
686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApState();
687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#getWifiApConfiguration()}
691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return soft access point configuration
692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
6938fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiConfiguration getWifiApConfiguration() {
695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
696f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getWifiApConfiguration uid=%").c(Binder.getCallingUid()).flush();
697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncGetWifiApConfiguration();
698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)}
702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param wifiConfig WifiConfiguration details for soft access point
703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
7048fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setWifiApConfiguration(WifiConfiguration wifiConfig) {
706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
707f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setWifiApConfiguration uid=%").c(Binder.getCallingUid()).flush();
708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (wifiConfig == null)
709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
710d03a9283285bb7adef6c687eb3a91fa4a8c4b502Vinit Deshpande        if (isValid(wifiConfig)) {
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiStateMachine.setWifiApConfiguration(wifiConfig);
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Invalid WifiConfiguration");
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
7188fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley     * see {@link android.net.wifi.WifiManager#isScanAlwaysAvailable()}
719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
7208fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isScanAlwaysAvailable() {
722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
723f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("isScanAlwaysAvailable uid=%").c(Binder.getCallingUid()).flush();
724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mSettingsStore.isScanAlwaysAvailable();
725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#disconnect()}
729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
7308fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void disconnect() {
732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
733f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("disconnect uid=%").c(Binder.getCallingUid()).flush();
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.disconnectCommand();
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reconnect()}
739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
7408fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reconnect() {
742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
743f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("reconnect uid=%").c(Binder.getCallingUid()).flush();
744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reconnectCommand();
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#reassociate()}
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
7508fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void reassociate() {
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
753f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("reassociate uid=%").c(Binder.getCallingUid()).flush();
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.reassociateCommand();
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
758048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande     * see {@link android.net.wifi.WifiManager#getSupportedFeatures}
759a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande     */
7608fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
761048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public int getSupportedFeatures() {
762a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        enforceAccessPermission();
763f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getSupportedFeatures uid=%").c(Binder.getCallingUid()).flush();
764a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        if (mWifiStateMachineChannel != null) {
765048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return mWifiStateMachine.syncGetSupportedFeatures(mWifiStateMachineChannel);
766a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        } else {
767a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
768048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            return 0;
769a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande        }
770a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    }
771a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande
772c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski    @Override
773c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski    public void requestActivityInfo(ResultReceiver result) {
774c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski        Bundle bundle = new Bundle();
775f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("requestActivityInfo uid=%").c(Binder.getCallingUid()).flush();
776c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski        bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, reportActivityInfo());
777c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski        result.send(0, bundle);
778c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski    }
779c48fd721a9a3273f0faa927d93caad4c9832af7dAdam Lesinski
780a632d8a6edd350c7644b593b18eceaa5b368505bVinit Deshpande    /**
78194a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski     * see {@link android.net.wifi.WifiManager#getControllerActivityEnergyInfo(int)}
782200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle     */
7838fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
784048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande    public WifiActivityEnergyInfo reportActivityInfo() {
785200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        enforceAccessPermission();
786f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("reportActivityInfo uid=%").c(Binder.getCallingUid()).flush();
7871f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills        if ((getSupportedFeatures() & WifiManager.WIFI_FEATURE_LINK_LAYER_STATS) == 0) {
7881f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills            return null;
7891f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills        }
790200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiLinkLayerStats stats;
791200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        WifiActivityEnergyInfo energyInfo = null;
792200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        if (mWifiStateMachineChannel != null) {
793048dc8df2bd9394979dbb090b0cfe8d6627d23e3Vinit Deshpande            stats = mWifiStateMachine.syncGetLinkLayerStats(mWifiStateMachineChannel);
794200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            if (stats != null) {
79594a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxIdleCurrent = mContext.getResources().getInteger(
79694a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_idle_receive_cur_ma);
79794a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxCurrent = mContext.getResources().getInteger(
79894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_active_rx_cur_ma);
79994a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long txCurrent = mContext.getResources().getInteger(
80094a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_tx_cur_ma);
80194a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final double voltage = mContext.getResources().getInteger(
80294a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        com.android.internal.R.integer.config_wifi_operating_voltage_mv)
80394a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        / 1000.0;
80494a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski
80594a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long rxIdleTime = stats.on_time - stats.tx_time - stats.rx_time;
8069c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                final long[] txTimePerLevel;
8079c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                if (stats.tx_time_per_level != null) {
8089c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    txTimePerLevel = new long[stats.tx_time_per_level.length];
8099c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    for (int i = 0; i < txTimePerLevel.length; i++) {
8109c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                        txTimePerLevel[i] = stats.tx_time_per_level[i];
8119c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                        // TODO(b/27227497): Need to read the power consumed per level from config
8129c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    }
8139c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                } else {
8149c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    // This will happen if the HAL get link layer API returned null.
8159c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    txTimePerLevel = new long[0];
816a1514cb347fccf11566e5172ed71030c695d2abdRoshan Pius                }
81794a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                final long energyUsed = (long)((stats.tx_time * txCurrent +
81894a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        stats.rx_time * rxCurrent +
81994a2dd090b7d3823058b7a69cf4653d5663708cdAdam Lesinski                        rxIdleTime * rxIdleCurrent) * voltage);
820d76cdd8e08476ad5025f07a5d77a8d8f920a0721Adam Lesinski                if (VDBG || rxIdleTime < 0 || stats.on_time < 0 || stats.tx_time < 0 ||
821d76cdd8e08476ad5025f07a5d77a8d8f920a0721Adam Lesinski                        stats.rx_time < 0 || energyUsed < 0) {
82270b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    StringBuilder sb = new StringBuilder();
82370b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxIdleCur=" + rxIdleCurrent);
82470b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxCur=" + rxCurrent);
82570b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" txCur=" + txCurrent);
82670b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" voltage=" + voltage);
82770b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" on_time=" + stats.on_time);
82870b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" tx_time=" + stats.tx_time);
8299c8892ebd2ec5340785f6b787805fdc6088069d7Roshan Pius                    sb.append(" tx_time_per_level=" + Arrays.toString(txTimePerLevel));
83070b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rx_time=" + stats.rx_time);
83170b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" rxIdleTime=" + rxIdleTime);
83270b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                    sb.append(" energy=" + energyUsed);
833f9946f5663f2d7a9a69cb8b7e82a4ef8b1825c48Mitchell Wills                    Log.d(TAG, " reportActivityInfo: " + sb.toString());
83470b75cda33a05e8b6e6724ed08e4e72f6fee0471Pierre Vandwalle                }
83584a11a3d54b7c6f043dcf7a874b7c0d45709677aPierre Vandwalle
836200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                // Convert the LinkLayerStats into EnergyActivity
837186f7513a080eb1a419b6d83286d62961168a5feAdam Lesinski                energyInfo = new WifiActivityEnergyInfo(SystemClock.elapsedRealtime(),
838200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle                        WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, stats.tx_time,
839a1514cb347fccf11566e5172ed71030c695d2abdRoshan Pius                        txTimePerLevel, stats.rx_time, rxIdleTime, energyUsed);
840200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            }
8411f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills            if (energyInfo != null && energyInfo.isValid()) {
8421f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills                return energyInfo;
8431f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills            } else {
8441f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills                return null;
8451f8f2971309aee4de9c35723f6fbca6fcab9a3beMitchell Wills            }
846200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        } else {
847200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
848200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle            return null;
849200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle        }
850200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    }
851200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle
852200e8ee5097134010a6edee8d031bb02ff7eeb5avandwalle    /**
853155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()}
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of configured networks
855155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
8568fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<WifiConfiguration> getConfiguredNetworks() {
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
859f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush();
860155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
86182199a285f4a45a46b44eb8253999aa918534753vandwalle            return mWifiStateMachine.syncGetConfiguredNetworks(Binder.getCallingUid(),
86282199a285f4a45a46b44eb8253999aa918534753vandwalle                    mWifiStateMachineChannel);
863155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
864155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
865155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return null;
866155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
867155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
868155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
869155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
8709878c61bbd81176561991be025af44efc67332feWenchao Tong     * see {@link android.net.wifi.WifiManager#getPrivilegedConfiguredNetworks()}
8719878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return the list of configured networks with real preSharedKey
8729878c61bbd81176561991be025af44efc67332feWenchao Tong     */
8738fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
8749878c61bbd81176561991be025af44efc67332feWenchao Tong    public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
8759878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceReadCredentialPermission();
8769878c61bbd81176561991be025af44efc67332feWenchao Tong        enforceAccessPermission();
877f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getPrivilegedConfiguredNetworks uid=%").c(Binder.getCallingUid()).flush();
8789878c61bbd81176561991be025af44efc67332feWenchao Tong        if (mWifiStateMachineChannel != null) {
8799878c61bbd81176561991be025af44efc67332feWenchao Tong            return mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
8809878c61bbd81176561991be025af44efc67332feWenchao Tong        } else {
8819878c61bbd81176561991be025af44efc67332feWenchao Tong            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
8829878c61bbd81176561991be025af44efc67332feWenchao Tong            return null;
8839878c61bbd81176561991be025af44efc67332feWenchao Tong        }
8849878c61bbd81176561991be025af44efc67332feWenchao Tong    }
8859878c61bbd81176561991be025af44efc67332feWenchao Tong
8869878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
887ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * Returns a WifiConfiguration matching this ScanResult
888ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @param scanResult scanResult that represents the BSSID
889ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     * @return {@link WifiConfiguration} that matches this BSSID or null
890ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande     */
8918fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
892ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    public WifiConfiguration getMatchingWifiConfig(ScanResult scanResult) {
893ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        enforceAccessPermission();
894f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getMatchingWifiConfig uid=%").c(Binder.getCallingUid()).flush();
895ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande        return mWifiStateMachine.syncGetMatchingWifiConfig(scanResult, mWifiStateMachineChannel);
896ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    }
897ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande
898ccbe820337a230f0827f734aca2c51a244a0837fVinit Deshpande    /**
899155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
900155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the supplicant-assigned identifier for the new or updated
901155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * network if the operation succeeds, or {@code -1} if it fails
902155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
9038fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
904155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public int addOrUpdateNetwork(WifiConfiguration config) {
905155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
906f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("addOrUpdateNetwork uid=%").c(Binder.getCallingUid()).flush();
9074aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        if (isValid(config) && isValidPasspoint(config)) {
9088be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kalele
90931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
91031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
9112e814680f4dd27a5f825afab189843582235cedcJan Nordqvist            if (config.isPasspoint() &&
9122e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                    (enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS ||
9132e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                            enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS)) {
9142e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                if (config.updateIdentifier != null) {
9152e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                    enforceAccessPermission();
9162e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                }
9172e814680f4dd27a5f825afab189843582235cedcJan Nordqvist                else {
918e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                    try {
919e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                        verifyCert(enterpriseConfig.getCaCertificate());
920e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                    } catch (CertPathValidatorException cpve) {
921e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                        Slog.e(TAG, "CA Cert " +
922e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                                enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
923e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                                " untrusted: " + cpve.getMessage());
924e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                        return -1;
925e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                    } catch (GeneralSecurityException | IOException e) {
926e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                        Slog.e(TAG, "Failed to verify certificate" +
927e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                                enterpriseConfig.getCaCertificate().getSubjectX500Principal() +
928e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                                ": " + e);
929e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                        return -1;
930e511ec7a4f53f7204a85075b23318f9f11c46d94Mitchell Wills                    }
93131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                }
93231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            }
93331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
934992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            //TODO: pass the Uid the WifiStateMachine as a message parameter
93531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            Slog.i("addOrUpdateNetwork", " uid = " + Integer.toString(Binder.getCallingUid())
936992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " SSID " + config.SSID
937992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " nid=" + Integer.toString(config.networkId));
938992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (config.networkId == WifiConfiguration.INVALID_NETWORK_ID) {
939992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.creatorUid = Binder.getCallingUid();
940992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            } else {
941992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                config.lastUpdateUid = Binder.getCallingUid();
942992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mWifiStateMachineChannel != null) {
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config);
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return -1;
948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "bad network configuration");
951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return -1;
952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
95531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static void verifyCert(X509Certificate caCert)
95631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            throws GeneralSecurityException, IOException {
95731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertificateFactory factory = CertificateFactory.getInstance("X.509");
95831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPathValidator validator =
95931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                CertPathValidator.getInstance(CertPathValidator.getDefaultType());
96031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        CertPath path = factory.generateCertPath(
96131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                Arrays.asList(caCert));
96231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
96331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        ks.load(null, null);
96431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        PKIXParameters params = new PKIXParameters(ks);
96531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        params.setRevocationEnabled(false);
96631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        validator.validate(path, params);
96731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
96831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist
96931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    /**
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#removeNetwork(int)}
971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
9758fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean removeNetwork(int netId) {
977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
978f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("removeNetwork uid=%").c(Binder.getCallingUid()).flush();
979f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        // TODO Add private logging for netId b/33807876
980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId);
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)}
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param disableOthers if true, disable all other networks.
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
9958fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean enableNetwork(int netId, boolean disableOthers) {
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
998f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        // TODO b/33807876 Log netId
999f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableNetwork uid=% disableOthers=%")
1000f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1001f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(disableOthers).flush();
1002f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId,
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    disableOthers);
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#disableNetwork(int)}
1014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId the integer that identifies the network configuration
1015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * to the supplicant
1016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
1017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
10188fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean disableNetwork(int netId) {
1020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1021f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        // TODO b/33807876 Log netId
1022f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("disableNetwork uid=%").c(Binder.getCallingUid()).flush();
1023f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
1025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId);
1026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * See {@link android.net.wifi.WifiManager#getConnectionInfo()}
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the Wi-Fi information, contained in {@link WifiInfo}.
1035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
10368fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public WifiInfo getConnectionInfo() {
1038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1039f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getConnectionInfo uid=%").c(Binder.getCallingUid()).flush();
1040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
1041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * Make sure we have the latest information, by sending
1042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * a status request to the supplicant.
1043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
1044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiStateMachine.syncRequestConnectionInfo();
1045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the results of the most recent access point scan, in the form of
1049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a list of {@link ScanResult} objects.
1050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the list of results
1051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
10528fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public List<ScanResult> getScanResults(String callingPackage) {
1054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int uid = Binder.getCallingUid();
1056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        long ident = Binder.clearCallingIdentity();
1057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
105892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            if (!mWifiPermissionsUtil.canAccessScanResults(callingPackage,
105992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                      uid, Build.VERSION_CODES.M)) {
10601ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber                return new ArrayList<ScanResult>();
10611ecb0083490436303cdb89bc4c46b6743ea0afa8Sky Faber            }
1062d94a62ffdf0e7f282948109d05d96e682eb32eefRebecca Silberstein            if (mWifiScanner == null) {
1063d94a62ffdf0e7f282948109d05d96e682eb32eefRebecca Silberstein                mWifiScanner = mWifiInjector.getWifiScanner();
1064d94a62ffdf0e7f282948109d05d96e682eb32eefRebecca Silberstein            }
1065d94a62ffdf0e7f282948109d05d96e682eb32eefRebecca Silberstein            return mWifiScanner.getSingleScanResults();
1066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(ident);
1068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10711d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist    /**
1072d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu     * Add or update a Passpoint configuration.
10733ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     *
10743ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * @param config The Passpoint configuration to be added
10753ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * @return true on success or false on failure
10763ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     */
10773ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    @Override
1078d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu    public boolean addOrUpdatePasspointConfiguration(PasspointConfiguration config) {
1079c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        enforceChangePermission();
1080f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("addorUpdatePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush();
1081d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu        return mPasspointManager.addOrUpdateProvider(config);
10823ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    }
10833ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu
10843ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    /**
10853ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * Remove the Passpoint configuration identified by its FQDN (Fully Qualified Domain Name).
10863ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     *
10873ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * @param fqdn The FQDN of the Passpoint configuration to be removed
10883ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * @return true on success or false on failure
10893ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     */
10903ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    @Override
10913ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    public boolean removePasspointConfiguration(String fqdn) {
1092c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        enforceChangePermission();
1093f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("removePasspointConfiguration uid=%").c(Binder.getCallingUid()).flush();
1094c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        return mPasspointManager.removeProvider(fqdn);
10953ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    }
10963ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu
10973ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    /**
10983ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     * Return the list of the installed Passpoint configurations.
10993ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     *
1100d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu     * An empty list will be returned when no configuration is installed.
1101d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu     *
1102d28cfdde236d3d7c72f0c57ca7f18622b16d421aPeter Qiu     * @return A list of {@link PasspointConfiguration}
11033ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu     */
11043ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    @Override
11053ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    public List<PasspointConfiguration> getPasspointConfigurations() {
1106c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        enforceAccessPermission();
1107f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getPasspointConfigurations uid=%").c(Binder.getCallingUid()).flush();
1108c04479601696f56c9cc240d4c7fc49fa99f51825Peter Qiu        return mPasspointManager.getProviderConfigs();
11093ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    }
11103ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu
11113ebf0b17fd1cdd3a0ef22d9e199c1686c01dcc9fPeter Qiu    /**
11122e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * Query for a Hotspot 2.0 release 2 OSU icon
11132e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @param bssid The BSSID of the AP
11142e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @param fileName Icon file name
11152e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     */
11168fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
11172e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    public void queryPasspointIcon(long bssid, String fileName) {
111883b8a3d25eb4991277d8ec3720bc1f3d821166b6Peter Qiu        enforceAccessPermission();
1119f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("queryPasspointIcon uid=%").c(Binder.getCallingUid()).flush();
11202e814680f4dd27a5f825afab189843582235cedcJan Nordqvist        mWifiStateMachine.syncQueryPasspointIcon(mWifiStateMachineChannel, bssid, fileName);
11212e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    }
11221d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist
11232e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    /**
11242e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * Match the currently associated network against the SP matching the given FQDN
11252e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @param fqdn FQDN of the SP
11262e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @return ordinal [HomeProvider, RoamingProvider, Incomplete, None, Declined]
11272e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     */
11288fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
11292e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    public int matchProviderWithCurrentNetwork(String fqdn) {
1130f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("matchProviderWithCurrentNetwork uid=%").c(Binder.getCallingUid()).flush();
11312e814680f4dd27a5f825afab189843582235cedcJan Nordqvist        return mWifiStateMachine.matchProviderWithCurrentNetwork(mWifiStateMachineChannel, fqdn);
11321d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist    }
11331d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist
11342e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    /**
11352e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * Deauthenticate and set the re-authentication hold off time for the current network
11362e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @param holdoff hold off time in milliseconds
11372e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     * @param ess set if the hold off pertains to an ESS rather than a BSS
11382e814680f4dd27a5f825afab189843582235cedcJan Nordqvist     */
11398fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
11402e814680f4dd27a5f825afab189843582235cedcJan Nordqvist    public void deauthenticateNetwork(long holdoff, boolean ess) {
1141f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("deauthenticateNetwork uid=%").c(Binder.getCallingUid()).flush();
11422e814680f4dd27a5f825afab189843582235cedcJan Nordqvist        mWifiStateMachine.deauthenticateNetwork(mWifiStateMachineChannel, holdoff, ess);
11431d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist    }
11441d5cd3938f9191184cd9aea3059a3b62bf3a0372Jan Nordqvist
1145fd5470391e5342daa38d00b68ccbccfeacbe6d33Alexandra Gherghina    /**
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Tell the supplicant to persist the current list of configured networks.
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if the operation succeeded
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * TODO: deprecate this
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11518fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean saveConfiguration() {
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1154f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("saveConfiguration uid=%").c(Binder.getCallingUid()).flush();
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiStateMachineChannel != null) {
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel);
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Set the country code
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param countryCode ISO 3166 country code.
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param persist {@code true} if the setting should be remembered.
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1168155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The persist behavior exists so that wifi can fall back to the last
1169155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * persisted country code on a restart, when the locale information is
1170155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * not available from telephony.
1171155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
11728fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1173155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void setCountryCode(String countryCode, boolean persist) {
1174155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Slog.i(TAG, "WifiService trying to set country code to " + countryCode +
1175155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                " with persist set to " + persist);
11769c22c8ea193360f50ad490368ca6a11af0684362Robert Greenwalt        enforceConnectivityInternalPermission();
1177f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setCountryCode uid=%").c(Binder.getCallingUid()).flush();
1178f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        // TODO b/35150708 Log list of channels when country code is updated
1179155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final long token = Binder.clearCallingIdentity();
1180155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
11810091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang            if (mCountryCode.setCountryCode(countryCode, persist) && persist) {
11820091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang                // Save this country code to persistent storage
11830091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang                mFacade.setStringSetting(mContext,
11840091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang                        Settings.Global.WIFI_COUNTRY_CODE,
11850091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang                        countryCode);
11860091305175e8c6fe7fc6d01efb9d405961db4ac7Ningyuan Wang            }
1187155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } finally {
1188155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Binder.restoreCallingIdentity(token);
1189155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1190155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1191155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
119280d14d67a956e6c69433470aaa73100898166efaxinhe     /**
119380d14d67a956e6c69433470aaa73100898166efaxinhe     * Get the country code
119437b06cd7aae7fe27cfaf1d95cc9901548765406bNingyuan Wang     * @return Get the best choice country code for wifi, regardless of if it was set or
119537b06cd7aae7fe27cfaf1d95cc9901548765406bNingyuan Wang     * not.
119637b06cd7aae7fe27cfaf1d95cc9901548765406bNingyuan Wang     * Returns null when there is no country code available.
119780d14d67a956e6c69433470aaa73100898166efaxinhe     */
11988fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
119980d14d67a956e6c69433470aaa73100898166efaxinhe    public String getCountryCode() {
120080d14d67a956e6c69433470aaa73100898166efaxinhe        enforceConnectivityInternalPermission();
1201f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getCountryCode uid=%").c(Binder.getCallingUid()).flush();
120237b06cd7aae7fe27cfaf1d95cc9901548765406bNingyuan Wang        String country = mCountryCode.getCountryCode();
120380d14d67a956e6c69433470aaa73100898166efaxinhe        return country;
120480d14d67a956e6c69433470aaa73100898166efaxinhe    }
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
12068fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isDualBandSupported() {
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        //TODO: Should move towards adding a driver API that checks at runtime
1209f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("isDualBandSupported uid=%").c(Binder.getCallingUid()).flush();
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mContext.getResources().getBoolean(
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                com.android.internal.R.bool.config_wifi_dual_band_support);
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return the DHCP-assigned addresses from the last successful DHCP request,
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * if any.
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return the DHCP information
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @deprecated
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
12208fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
12218fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Deprecated
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public DhcpInfo getDhcpInfo() {
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1224f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getDhcpInfo uid=%").c(Binder.getCallingUid()).flush();
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        DhcpInfo info = new DhcpInfo();
12283b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
12293b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.ipAddress != null &&
12303b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                dhcpResults.ipAddress.getAddress() instanceof Inet4Address) {
12313b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.ipAddress.getAddress());
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
12333b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
12343b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (dhcpResults.gateway != null) {
12353b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) dhcpResults.gateway);
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
12373b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int dnsFound = 0;
12393b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        for (InetAddress dns : dhcpResults.dnsServers) {
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (dns instanceof Inet4Address) {
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (dnsFound == 0) {
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns);
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (++dnsFound > 1) break;
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
12492af03130d7f85823223b8591dc52858d851b301dMitchell Wills        Inet4Address serverAddress = dhcpResults.serverAddress;
12502af03130d7f85823223b8591dc52858d851b301dMitchell Wills        if (serverAddress != null) {
12512af03130d7f85823223b8591dc52858d851b301dMitchell Wills            info.serverAddress = NetworkUtils.inetAddressToInt(serverAddress);
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        info.leaseDuration = dhcpResults.leaseDuration;
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return info;
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * enable TDLS for the local NIC to remote NIC
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * The APPs don't know the remote MAC address to identify NIC though,
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * so we need to do additional work to find it from remote IP address
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1264155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTaskParams {
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public String remoteIpAddress;
1266155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public boolean enable;
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1268155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class TdlsTask extends AsyncTask<TdlsTaskParams, Integer, Integer> {
1270155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        protected Integer doInBackground(TdlsTaskParams... params) {
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Retrieve parameters for the call
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            TdlsTaskParams param = params[0];
1275155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String remoteIpAddress = param.remoteIpAddress.trim();
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean enable = param.enable;
1277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Get MAC address of Remote IP
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String macAddress = null;
1280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            BufferedReader reader = null;
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                reader = new BufferedReader(new FileReader("/proc/net/arp"));
1285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Skip over the line bearing colum titles
1287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                String line = reader.readLine();
1288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                while ((line = reader.readLine()) != null) {
1290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String[] tokens = line.split("[ ]+");
1291155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (tokens.length < 6) {
1292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        continue;
1293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1294155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1295155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // ARP column format is
1296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Address HWType HWAddress Flags Mask IFace
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String ip = tokens[0];
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    String mac = tokens[3];
1299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (remoteIpAddress.equals(ip)) {
1301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        macAddress = mac;
1302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break;
1303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1304155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (macAddress == null) {
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.w(TAG, "Did not find remoteAddress {" + remoteIpAddress + "} in " +
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            "/proc/net/arp");
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    enableTdlsWithMacAddress(macAddress, enable);
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (FileNotFoundException e) {
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not open /proc/net/arp to lookup mac address");
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (IOException e) {
1316155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not read /proc/net/arp to lookup mac address");
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } finally {
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (reader != null) {
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        reader.close();
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                catch (IOException e) {
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Do nothing
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return 0;
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13328fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdls(String remoteAddress, boolean enable) {
13348e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteAddress == null) {
13358e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteAddress cannot be null");
13368e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
1337f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableTdls uid=% enable=%").c(Binder.getCallingUid()).c(enable).flush();
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        TdlsTaskParams params = new TdlsTaskParams();
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.remoteIpAddress = remoteAddress;
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        params.enable = enable;
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        new TdlsTask().execute(params);
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13458fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable) {
1347f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableTdlsWithMacAddress uid=% enable=%")
1348f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1349f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(enable)
1350f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .flush();
13518e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        if (remoteMacAddress == null) {
13528e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer          throw new IllegalArgumentException("remoteMacAddress cannot be null");
13538e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer        }
13548e6144b4059fc2117be23d7222893bc57a33f64dJon Larimer
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiStateMachine.enableTdls(remoteMacAddress, enable);
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Get a reference to handler. This is used by a client to establish
1360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * an AsyncChannel communication with WifiService
1361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
13628fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public Messenger getWifiServiceMessenger() {
1364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceChangePermission();
1366f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getWifiServiceMessenger uid=%").c(Binder.getCallingUid()).flush();
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return new Messenger(mClientHandler);
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
13702ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    /**
13712ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     * Disable an ephemeral network, i.e. network that is created thru a WiFi Scorer
13722ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle     */
13738fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
13742ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    public void disableEphemeralNetwork(String SSID) {
13752ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceAccessPermission();
13762ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        enforceChangePermission();
1377f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("disableEphemeralNetwork uid=%").c(Binder.getCallingUid()).flush();
13782ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle        mWifiStateMachine.disableEphemeralNetwork(SSID);
13792ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle    }
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void onReceive(Context context, Intent intent) {
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String action = intent.getAction();
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (action.equals(Intent.ACTION_SCREEN_ON)) {
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_ON);
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_USER_PRESENT);
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCREEN_OFF);
1391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
1392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int pluggedType = intent.getIntExtra("plugged", 0);
1393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_BATTERY_CHANGED, pluggedType, 0, null);
1394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
1395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
1396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        BluetoothAdapter.STATE_DISCONNECTED);
1397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiStateMachine.sendBluetoothAdapterStateChange(state);
1398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
1399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                boolean emergencyMode = intent.getBooleanExtra("phoneinECMState", false);
1400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_EMERGENCY_MODE_CHANGED, emergencyMode ? 1 : 0, 0);
1401090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande            } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED)) {
1402090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande                boolean inCall = intent.getBooleanExtra(PhoneConstants.PHONE_IN_EMERGENCY_CALL, false);
1403090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande                mWifiController.sendMessage(CMD_EMERGENCY_CALL_STATE_CHANGED, inCall ? 1 : 0, 0);
140403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn            } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) {
140503b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn                handleIdleModeChanged();
1406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    };
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
141002938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov    private boolean startConsentUi(String packageName,
14110469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            int callingUid, String intentAction) throws RemoteException {
14120469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        if (UserHandle.getAppId(callingUid) == Process.SYSTEM_UID) {
14130469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            return false;
14140469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        }
14150469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        try {
14160469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            // Validate the package only if we are going to use it
14170469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            ApplicationInfo applicationInfo = mContext.getPackageManager()
14180469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                    .getApplicationInfoAsUser(packageName,
14190469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                            PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
14200469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                            UserHandle.getUserId(callingUid));
14210469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            if (applicationInfo.uid != callingUid) {
14220469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                throw new SecurityException("Package " + callingUid
14230469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                        + " not in uid " + callingUid);
14240469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            }
14250469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov
142602938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            // Permission review mode, trigger a user prompt
142702938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            Intent intent = new Intent(intentAction);
142802938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
142902938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
143002938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
143102938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            mContext.startActivity(intent);
143202938a0a735da7fafaaed84e31e1aa93cdf80a56Ivan Podogov            return true;
14330469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        } catch (PackageManager.NameNotFoundException e) {
14340469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            throw new RemoteException(e.getMessage());
14350469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov        }
14360469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov    }
14370469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov
1438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Observes settings changes to scan always mode.
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForScanModeChange() {
1442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        ContentObserver contentObserver = new ContentObserver(null) {
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            @Override
1444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            public void onChange(boolean selfChange) {
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mSettingsStore.handleWifiScanAlwaysAvailableToggled();
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiController.sendMessage(CMD_SCAN_ALWAYS_MODE_CHANGED);
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        };
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.getContentResolver().registerContentObserver(
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE),
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                false, contentObserver);
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void registerForBroadcasts() {
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        IntentFilter intentFilter = new IntentFilter();
1457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
1458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
1459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
1460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
1461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
1462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
1463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
146403b70b37a8a20cb92d860b5015f6762dcb5e75ccDianne Hackborn        intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
1465090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande
1466090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande        boolean trackEmergencyCallState = mContext.getResources().getBoolean(
1467090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande                com.android.internal.R.bool.config_wifi_turn_off_during_emergency_call);
1468090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande        if (trackEmergencyCallState) {
1469090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande            intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALL_STATE_CHANGED);
1470090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande        }
1471090aa74f2d36010abba6563a39c87ab7ba8a7473Vinit Deshpande
1472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.registerReceiver(mReceiver, intentFilter);
1473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1475faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee    private void registerForPackageOrUserRemoval() {
1476e487a4648dd41881e754f1224aaedead78a0777dSky Faber        IntentFilter intentFilter = new IntentFilter();
1477e487a4648dd41881e754f1224aaedead78a0777dSky Faber        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1478faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        intentFilter.addAction(Intent.ACTION_USER_REMOVED);
1479faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        mContext.registerReceiverAsUser(new BroadcastReceiver() {
1480e487a4648dd41881e754f1224aaedead78a0777dSky Faber            @Override
1481e487a4648dd41881e754f1224aaedead78a0777dSky Faber            public void onReceive(Context context, Intent intent) {
1482faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                switch (intent.getAction()) {
1483faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_PACKAGE_REMOVED: {
1484faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
1485faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1486faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1487faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1488faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        Uri uri = intent.getData();
1489faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        if (uid == -1 || uri == null) {
1490faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                            return;
1491faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        }
1492faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        String pkgName = uri.getSchemeSpecificPart();
1493faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeAppConfigs(pkgName, uid);
1494faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1495faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    }
1496faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                    case Intent.ACTION_USER_REMOVED: {
1497faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
1498faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        mWifiStateMachine.removeUserConfigs(userHandle);
1499faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee                        break;
1500e487a4648dd41881e754f1224aaedead78a0777dSky Faber                    }
1501e487a4648dd41881e754f1224aaedead78a0777dSky Faber                }
1502e487a4648dd41881e754f1224aaedead78a0777dSky Faber            }
1503faa2ce7f93eef2c436796fb1ea3574e2d1412491Robin Lee        }, UserHandle.ALL, intentFilter, null, null);
1504e487a4648dd41881e754f1224aaedead78a0777dSky Faber    }
1505e487a4648dd41881e754f1224aaedead78a0777dSky Faber
1506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    @Override
1507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                != PackageManager.PERMISSION_GRANTED) {
1510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("Permission Denial: can't dump WifiService from from pid="
1511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + Binder.getCallingPid()
1512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    + ", uid=" + Binder.getCallingUid());
1513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
151511638f348ba45f9f417928e79b81186cef76c561Glen Kuhne        if (args.length > 0 && WifiMetrics.PROTO_DUMP_ARG.equals(args[0])) {
151611638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            // WifiMetrics proto bytes were requested. Dump only these.
151711638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mWifiStateMachine.updateWifiMetrics();
151811638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mWifiMetrics.dump(fd, pw, args);
1519675de6070a3c1980fcc99bc6863b2689bfeb0271Lorenzo Colitti        } else if (args.length > 0 && IpManager.DUMP_ARG.equals(args[0])) {
1520675de6070a3c1980fcc99bc6863b2689bfeb0271Lorenzo Colitti            // IpManager dump was requested. Pass it along and take no further action.
1521675de6070a3c1980fcc99bc6863b2689bfeb0271Lorenzo Colitti            String[] ipManagerArgs = new String[args.length - 1];
1522675de6070a3c1980fcc99bc6863b2689bfeb0271Lorenzo Colitti            System.arraycopy(args, 1, ipManagerArgs, 0, ipManagerArgs.length);
1523675de6070a3c1980fcc99bc6863b2689bfeb0271Lorenzo Colitti            mWifiStateMachine.dumpIpManager(fd, pw, ipManagerArgs);
152484ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart        } else if (args.length > 0 && DUMP_ARG_SET_IPREACH_DISCONNECT.equals(args[0])) {
152584ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart            if (args.length > 1) {
152684ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                if (DUMP_ARG_SET_IPREACH_DISCONNECT_ENABLED.equals(args[1])) {
152784ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                    mWifiStateMachine.setIpReachabilityDisconnectEnabled(true);
152884ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                } else if (DUMP_ARG_SET_IPREACH_DISCONNECT_DISABLED.equals(args[1])) {
152984ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                    mWifiStateMachine.setIpReachabilityDisconnectEnabled(false);
153084ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                }
153184ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart            }
153284ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart            pw.println("IPREACH_DISCONNECT state is "
153384ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart                    + mWifiStateMachine.getIpReachabilityDisconnectEnabled());
153484ee4726383fd968ab43caf120d15d9f47a51a54Paul Stewart            return;
153511638f348ba45f9f417928e79b81186cef76c561Glen Kuhne        } else {
153611638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName());
153711638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println("Stay-awake conditions: " +
153811638f348ba45f9f417928e79b81186cef76c561Glen Kuhne                    Settings.Global.getInt(mContext.getContentResolver(),
153911638f348ba45f9f417928e79b81186cef76c561Glen Kuhne                                           Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0));
154011638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println("mInIdleMode " + mInIdleMode);
154111638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println("mScanPending " + mScanPending);
154211638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mWifiController.dump(fd, pw, args);
154311638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mSettingsStore.dump(fd, pw, args);
154411638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mTrafficPoller.dump(fd, pw, args);
154511638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println();
154611638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println("Locks held:");
15472a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein            mWifiLockManager.dump(pw);
15482a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein            pw.println();
154961312e14a088a9487d4db64f08285162476e870fPaul Stewart            mWifiMulticastLockManager.dump(pw);
155011638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println();
155111638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            mWifiStateMachine.dump(fd, pw, args);
15523c6e92f51984ba8999d5efe622b4a52dd18bdaffGlen Kuhne            pw.println();
15533c6e92f51984ba8999d5efe622b4a52dd18bdaffGlen Kuhne            mWifiStateMachine.updateWifiMetrics();
15543c6e92f51984ba8999d5efe622b4a52dd18bdaffGlen Kuhne            mWifiMetrics.dump(fd, pw, args);
155511638f348ba45f9f417928e79b81186cef76c561Glen Kuhne            pw.println();
15563204fb9682242a7b5a749489076c66d448c42577Roshan Pius            mWifiBackupRestore.dump(fd, pw, args);
15573204fb9682242a7b5a749489076c66d448c42577Roshan Pius            pw.println();
155811638f348ba45f9f417928e79b81186cef76c561Glen Kuhne        }
1559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15618fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) {
1563f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("acquireWifiLock uid=% lockMode=%")
1564f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1565f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(lockMode).flush();
15662a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        if (mWifiLockManager.acquireWifiLock(lockMode, tag, binder, ws)) {
1567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
1568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
1569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
15702a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        return false;
1571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15738fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
15742a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein    public void updateWifiLockWorkSource(IBinder binder, WorkSource ws) {
1575f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("updateWifiLockWorkSource uid=%").c(Binder.getCallingUid()).flush();
15762a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        mWifiLockManager.updateWifiLockWorkSource(binder, ws);
1577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15798fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
15802a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein    public boolean releaseWifiLock(IBinder binder) {
1581f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("releaseWifiLock uid=%").c(Binder.getCallingUid()).flush();
15822a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        if (mWifiLockManager.releaseWifiLock(binder)) {
15832a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein            mWifiController.sendMessage(CMD_LOCKS_CHANGED);
15842a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein            return true;
1585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
15862a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        return false;
1587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15898fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void initializeMulticastFiltering() {
1591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1592f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("initializeMulticastFiltering uid=%").c(Binder.getCallingUid()).flush();
159361312e14a088a9487d4db64f08285162476e870fPaul Stewart        mWifiMulticastLockManager.initializeFiltering();
1594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
15968fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void acquireMulticastLock(IBinder binder, String tag) {
1598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1599f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("acquireMulticastLock uid=%").c(Binder.getCallingUid()).flush();
160061312e14a088a9487d4db64f08285162476e870fPaul Stewart        mWifiMulticastLockManager.acquireLock(binder, tag);
1601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16038fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public void releaseMulticastLock() {
1605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceMulticastChangePermission();
1606f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("releaseMulticastLock uid=%").c(Binder.getCallingUid()).flush();
160761312e14a088a9487d4db64f08285162476e870fPaul Stewart        mWifiMulticastLockManager.releaseLock();
1608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
16108fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public boolean isMulticastEnabled() {
1612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enforceAccessPermission();
1613f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("isMulticastEnabled uid=%").c(Binder.getCallingUid()).flush();
161461312e14a088a9487d4db64f08285162476e870fPaul Stewart        return mWifiMulticastLockManager.isMulticastEnabled();
1615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
16169ae6b45c038fa74a9e7285ef4834551dd93da332Yuhao Zheng
16178fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1618ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public void enableVerboseLogging(int verbose) {
1619ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1620f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableVerboseLogging uid=% verbose=%")
1621f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1622f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(verbose).flush();
162300ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein        mFacade.setIntegerSetting(
162400ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein                mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, verbose);
162500ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein        enableVerboseLoggingInternal(verbose);
162600ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein    }
162700ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein
1628da918df16e03ee19be62343313d954027d3eb3abRebecca Silberstein    void enableVerboseLoggingInternal(int verbose) {
1629ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        mWifiStateMachine.enableVerboseLogging(verbose);
16302a7c2bec5d307a9e52de429c6303b7df9de900dcRebecca Silberstein        mWifiLockManager.enableVerboseLogging(verbose);
163161312e14a088a9487d4db64f08285162476e870fPaul Stewart        mWifiMulticastLockManager.enableVerboseLogging(verbose);
163275727af748e2b53baf365139ecfa7bf87a449d04Rebecca Silberstein        mWifiInjector.getWifiLastResortWatchdog().enableVerboseLogging(verbose);
163300ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein        mWifiInjector.getWifiBackupRestore().enableVerboseLogging(verbose);
1634e33b3346b262507ef2361d50a89d16bef69d9a57Sohani Rao        LogcatLog.enableVerboseLogging(verbose);
1635ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1636ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
16378fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1638ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    public int getVerboseLoggingLevel() {
1639ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        enforceAccessPermission();
1640f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getVerboseLoggingLevel uid=%").c(Binder.getCallingUid()).flush();
164100ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein        return mFacade.getIntegerSetting(
164200ec8bf178ba4abee4ee8bbc6eb09c9fcd986a89Rebecca Silberstein                mContext, Settings.Global.WIFI_VERBOSE_LOGGING_ENABLED, 0);
1643ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
1644c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
16458fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1646c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void enableAggressiveHandover(int enabled) {
1647c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1648f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableAggressiveHandover uid=% enabled=%")
1649f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            .c(Binder.getCallingUid())
1650f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            .c(enabled)
1651f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            .flush();
1652c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.enableAggressiveHandover(enabled);
1653c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1654c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
16558fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1656c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAggressiveHandover() {
1657c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1658f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getAggressiveHandover uid=%").c(Binder.getCallingUid()).flush();
1659c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAggressiveHandover();
1660c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1661c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
16628fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1663c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public void setAllowScansWithTraffic(int enabled) {
1664c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1665f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setAllowScansWithTraffic uid=% enabled=%")
1666f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1667f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(enabled).flush();
1668c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        mWifiStateMachine.setAllowScansWithTraffic(enabled);
1669c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1670c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
16718fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1672c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public int getAllowScansWithTraffic() {
1673c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        enforceAccessPermission();
1674f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getAllowScansWithTraffic uid=%").c(Binder.getCallingUid()).flush();
1675c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        return mWifiStateMachine.getAllowScansWithTraffic();
1676c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    }
1677c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
16788fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
167943eba5ba17a9af5d9a050a4ba5e12e93c92f722dSamuel Tan    public boolean setEnableAutoJoinWhenAssociated(boolean enabled) {
1680a8647b8cb29de22765062714cb265247234c3d32xinhe        enforceChangePermission();
1681f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("setEnableAutoJoinWhenAssociated uid=% enabled=%")
1682f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(Binder.getCallingUid())
1683f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao                .c(enabled).flush();
168443eba5ba17a9af5d9a050a4ba5e12e93c92f722dSamuel Tan        return mWifiStateMachine.setEnableAutoJoinWhenAssociated(enabled);
1685e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1686e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle
16878fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1688a8647b8cb29de22765062714cb265247234c3d32xinhe    public boolean getEnableAutoJoinWhenAssociated() {
1689e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle        enforceAccessPermission();
1690f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getEnableAutoJoinWhenAssociated uid=%").c(Binder.getCallingUid()).flush();
1691a8647b8cb29de22765062714cb265247234c3d32xinhe        return mWifiStateMachine.getEnableAutoJoinWhenAssociated();
1692e2614ba1b774529ee68cc4ac189f7492cff89db3Pierre Vandwalle    }
1693b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle
1694c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* Return the Wifi Connection statistics object */
16958fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1696c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public WifiConnectionStatistics getConnectionStatistics() {
1697c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceAccessPermission();
1698c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        enforceReadCredentialPermission();
1699f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getConnectionStatistics uid=%").c(Binder.getCallingUid()).flush();
1700c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        if (mWifiStateMachineChannel != null) {
1701c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return mWifiStateMachine.syncGetConnectionStatistics(mWifiStateMachineChannel);
1702c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        } else {
1703c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1704c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            return null;
1705c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1706c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
1707b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
17088fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1709b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    public void factoryReset() {
1710b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        enforceConnectivityInternalPermission();
1711f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("factoryReset uid=%").c(Binder.getCallingUid()).flush();
1712f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
1713f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            return;
1714f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1715b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
1716f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
1717f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Turn mobile hotspot off
1718f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            setWifiApEnabled(null, false);
1719f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        }
1720f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott
1721f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott        if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) {
1722f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Enable wifi
17230469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            try {
17240469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                setWifiEnabled(mContext.getOpPackageName(), true);
17250469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            } catch (RemoteException e) {
17260469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov                /* ignore - local call */
17270469548a809654861f74dc42a3409f29ae750550Svetoslav Ganov            }
1728f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            // Delete all Wifi SSIDs
1729f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            List<WifiConfiguration> networks = getConfiguredNetworks();
1730f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott            if (networks != null) {
1731f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                for (WifiConfiguration config : networks) {
1732f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                    removeNetwork(config.networkId);
1733f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                }
1734f9d866f1b4e93593b3411886fd71e729f4a5a205Stuart Scott                saveConfiguration();
1735b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott            }
1736b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott        }
1737b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott    }
17388be2ac8cec40a2000715849b627642bdc87c10b3Shirish Kalele
17394d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    /* private methods */
174031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    static boolean logAndReturnFalse(String s) {
17414d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        Log.d(TAG, s);
17424d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        return false;
17434d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
17444d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
174531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static boolean isValid(WifiConfiguration config) {
174631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        String validity = checkValidity(config);
174731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return validity == null || logAndReturnFalse(validity);
174831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    }
17494d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17504aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    public static boolean isValidPasspoint(WifiConfiguration config) {
17514aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        String validity = checkPasspointValidity(config);
17524aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        return validity == null || logAndReturnFalse(validity);
17534aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    }
17544aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist
175531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String checkValidity(WifiConfiguration config) {
175631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (config.allowedKeyManagement == null)
175731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "allowed kmgmt";
17584d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17594d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (config.allowedKeyManagement.cardinality() > 1) {
17604d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (config.allowedKeyManagement.cardinality() != 2) {
176131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "cardinality != 2";
17624d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17634d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
176431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not WPA_EAP";
17654d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17664d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if ((!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X))
17674d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    && (!config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK))) {
176831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "not PSK or 8021X";
17694d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17704d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
17714aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist        return null;
17724aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    }
17734d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
17744aef3283bf78e233a11fa34af2c7c38362879625Jan Nordqvist    public static String checkPasspointValidity(WifiConfiguration config) {
17754d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        if (!TextUtils.isEmpty(config.FQDN)) {
17764d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must not have an SSID */
17774d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (!TextUtils.isEmpty(config.SSID)) {
177831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "SSID not expected for Passpoint: '" + config.SSID +
177931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                        "' FQDN " + toHexString(config.FQDN);
17804d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17814d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have a providerFriendlyName */
17824d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            if (TextUtils.isEmpty(config.providerFriendlyName)) {
178331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no provider friendly name";
17844d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
178507f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
17864d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            /* this is passpoint configuration; it must have enterprise config */
178707f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            if (enterpriseConfig == null
178807f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    || enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.NONE ) {
178931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no enterprise config";
17904d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
179107f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist            if ((enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TLS ||
179207f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.TTLS ||
179307f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getEapMethod() == WifiEnterpriseConfig.Eap.PEAP) &&
179407f11f6f2ee7ec17cb08180035dfb5002aaaf5dfJan Nordqvist                    enterpriseConfig.getCaCertificate() == null) {
179531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                return "no CA certificate";
17964d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande            }
17974d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        }
179831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return null;
1799cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande    }
1800b8671cfafc2830ebddeafcfb2d91f2b39b19019bStuart Scott
18018fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
18029846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    public Network getCurrentNetwork() {
18039846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        enforceAccessPermission();
1804f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("getCurrentNetwork uid=%").c(Binder.getCallingUid()).flush();
18059846078598c3468f8813dbfa58238a1846bd81efSanket Padawe        return mWifiStateMachine.getCurrentNetwork();
18069846078598c3468f8813dbfa58238a1846bd81efSanket Padawe    }
18079846078598c3468f8813dbfa58238a1846bd81efSanket Padawe
180831891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    public static String toHexString(String s) {
180931891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        if (s == null) {
181031891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            return "null";
181131891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
181231891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        StringBuilder sb = new StringBuilder();
181331891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        sb.append('\'').append(s).append('\'');
181431891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        for (int n = 0; n < s.length(); n++) {
181531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist            sb.append(String.format(" %02x", s.charAt(n) & 0xffff));
181631891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        }
181731891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist        return sb.toString();
18184d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande    }
1819cd0af1c9ddcac881f49c815fb9da45433eb50736Vinit Deshpande
18202bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    public void hideCertFromUnaffiliatedUsers(String alias) {
18212bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu        mCertManager.hideCertFromUnaffiliatedUsers(alias);
18222bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    }
18232bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu
18242bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    public String[] listClientCertsForCurrentUser() {
18252bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu        return mCertManager.listClientCertsForCurrentUser();
18262bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu    }
18272bfc67c9893c0a525b224d68dd73a74212b0c29fArthur Hsu
1828c065315c23f78ec462a56aec1aaa4e2a34549b8dFyodor Kupolov    /**
1829466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan     * Enable/disable WifiConnectivityManager at runtime
1830466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan     *
1831466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan     * @param enabled true-enable; false-disable
1832466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan     */
18338fcd1fe86ddc3056e1a26e69bc68f148ae0e9ddcChristopher Wiley    @Override
1834466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan    public void enableWifiConnectivityManager(boolean enabled) {
1835466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan        enforceConnectivityInternalPermission();
1836f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("enableWifiConnectivityManager uid=% enabled=%")
1837f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            .c(Binder.getCallingUid())
1838f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao            .c(enabled).flush();
1839466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan        mWifiStateMachine.enableWifiConnectivityManager(enabled);
1840466158a6669d51541ce6c5c4e04a71dad36cdb4eRandy Pan    }
1841ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius
1842ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    /**
1843ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     * Retrieve the data to be backed to save the current state.
18443031903d4a68659350994571525fc86a47c02dd0Roshan Pius     *
1845ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     * @return  Raw byte stream of the data to be backed up.
1846ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     */
1847ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    @Override
1848ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    public byte[] retrieveBackupData() {
1849ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        enforceReadCredentialPermission();
1850ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        enforceAccessPermission();
1851f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("retrieveBackupData uid=%").c(Binder.getCallingUid()).flush();
1852ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        if (mWifiStateMachineChannel == null) {
1853ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1854ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius            return null;
1855ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        }
1856ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius
18573204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Retrieving backup data");
1858ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        List<WifiConfiguration> wifiConfigurations =
1859ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius                mWifiStateMachine.syncGetPrivilegedConfiguredNetwork(mWifiStateMachineChannel);
18603204fb9682242a7b5a749489076c66d448c42577Roshan Pius        byte[] backupData =
18613204fb9682242a7b5a749489076c66d448c42577Roshan Pius                mWifiBackupRestore.retrieveBackupDataFromConfigurations(wifiConfigurations);
18623204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Retrieved backup data");
18633204fb9682242a7b5a749489076c66d448c42577Roshan Pius        return backupData;
1864ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    }
1865ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius
1866ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    /**
18673031903d4a68659350994571525fc86a47c02dd0Roshan Pius     * Helper method to restore networks retrieved from backup data.
18683031903d4a68659350994571525fc86a47c02dd0Roshan Pius     *
18693031903d4a68659350994571525fc86a47c02dd0Roshan Pius     * @param configurations list of WifiConfiguration objects parsed from the backup data.
18703031903d4a68659350994571525fc86a47c02dd0Roshan Pius     */
18713031903d4a68659350994571525fc86a47c02dd0Roshan Pius    private void restoreNetworks(List<WifiConfiguration> configurations) {
18723031903d4a68659350994571525fc86a47c02dd0Roshan Pius        if (configurations == null) {
18733031903d4a68659350994571525fc86a47c02dd0Roshan Pius            Slog.e(TAG, "Backup data parse failed");
18743031903d4a68659350994571525fc86a47c02dd0Roshan Pius            return;
18753031903d4a68659350994571525fc86a47c02dd0Roshan Pius        }
18763031903d4a68659350994571525fc86a47c02dd0Roshan Pius        for (WifiConfiguration configuration : configurations) {
18773031903d4a68659350994571525fc86a47c02dd0Roshan Pius            int networkId = mWifiStateMachine.syncAddOrUpdateNetwork(
18783031903d4a68659350994571525fc86a47c02dd0Roshan Pius                    mWifiStateMachineChannel, configuration);
18793031903d4a68659350994571525fc86a47c02dd0Roshan Pius            if (networkId == WifiConfiguration.INVALID_NETWORK_ID) {
18803031903d4a68659350994571525fc86a47c02dd0Roshan Pius                Slog.e(TAG, "Restore network failed: " + configuration.configKey());
18813031903d4a68659350994571525fc86a47c02dd0Roshan Pius                continue;
18823031903d4a68659350994571525fc86a47c02dd0Roshan Pius            }
18833031903d4a68659350994571525fc86a47c02dd0Roshan Pius            // Enable all networks restored.
18843031903d4a68659350994571525fc86a47c02dd0Roshan Pius            mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, networkId, false);
18853031903d4a68659350994571525fc86a47c02dd0Roshan Pius        }
18863031903d4a68659350994571525fc86a47c02dd0Roshan Pius    }
18873031903d4a68659350994571525fc86a47c02dd0Roshan Pius
18883031903d4a68659350994571525fc86a47c02dd0Roshan Pius    /**
1889ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     * Restore state from the backed up data.
1890e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     *
1891ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     * @param data Raw byte stream of the backed up data.
1892ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius     */
1893ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    @Override
1894ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    public void restoreBackupData(byte[] data) {
1895ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        enforceChangePermission();
1896f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("restoreBackupData uid=%").c(Binder.getCallingUid()).flush();
1897ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        if (mWifiStateMachineChannel == null) {
1898ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1899ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius            return;
1900ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        }
1901ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius
19023204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Restoring backup data");
1903ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius        List<WifiConfiguration> wifiConfigurations =
1904ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius                mWifiBackupRestore.retrieveConfigurationsFromBackupData(data);
19053031903d4a68659350994571525fc86a47c02dd0Roshan Pius        restoreNetworks(wifiConfigurations);
19063204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Restored backup data");
1907ff9686aec5b6ee21e01556899fb13ab55915f3c0Roshan Pius    }
1908e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius
1909e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    /**
1910e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     * Restore state from the older supplicant back up data.
1911e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     * The old backup data was essentially a backup of wpa_supplicant.conf & ipconfig.txt file.
1912e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     *
1913e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     * @param supplicantData Raw byte stream of wpa_supplicant.conf
1914e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     * @param ipConfigData Raw byte stream of ipconfig.txt
1915e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius     */
1916e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    public void restoreSupplicantBackupData(byte[] supplicantData, byte[] ipConfigData) {
1917e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        enforceChangePermission();
1918f2fdf411925ad172b5e0b25b0c6df880256691d4Sohani Rao        mLog.trace("restoreSupplicantBackupData uid=%").c(Binder.getCallingUid()).flush();
1919e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        if (mWifiStateMachineChannel == null) {
1920e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            Slog.e(TAG, "mWifiStateMachineChannel is not initialized");
1921e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius            return;
1922e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        }
1923e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius
19243204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Restoring supplicant backup data");
1925e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius        List<WifiConfiguration> wifiConfigurations =
1926e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius                mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData(
1927e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius                        supplicantData, ipConfigData);
19283031903d4a68659350994571525fc86a47c02dd0Roshan Pius        restoreNetworks(wifiConfigurations);
19293204fb9682242a7b5a749489076c66d448c42577Roshan Pius        Slog.d(TAG, "Restored supplicant backup data");
1930e3831b70d4a8a967fe8df5496d542a432692c434Roshan Pius    }
1931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
1932