The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Irfan Sheriff | d017f35 | 2013-02-20 13:30:44 -0800 | [diff] [blame] | 17 | package com.android.server.wifi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Irfan Sheriff | 330b187 | 2012-09-16 12:27:57 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.app.AlarmManager; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Jaikumar Ganesh | 7440fc2 | 2010-09-27 17:04:14 -0700 | [diff] [blame] | 23 | import android.bluetooth.BluetoothAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.net.wifi.IWifiManager; |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 30 | import android.net.wifi.ScanResult; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.net.wifi.WifiInfo; |
| 32 | import android.net.wifi.WifiManager; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 33 | import android.net.wifi.WifiStateMachine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.net.wifi.WifiConfiguration; |
Isaac Levy | 654f509 | 2011-07-13 17:41:45 -0700 | [diff] [blame] | 35 | import android.net.wifi.WifiWatchdogStateMachine; |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 36 | import android.net.ConnectivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.net.DhcpInfo; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 38 | import android.net.DhcpResults; |
| 39 | import android.net.LinkAddress; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 40 | import android.net.NetworkInfo; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 41 | import android.net.NetworkInfo.DetailedState; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 42 | import android.net.NetworkUtils; |
| 43 | import android.net.RouteInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.Binder; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 45 | import android.os.Handler; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 46 | import android.os.Messenger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.HandlerThread; |
| 48 | import android.os.IBinder; |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 49 | import android.os.INetworkManagementService; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 50 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.os.RemoteException; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 52 | import android.os.SystemProperties; |
Irfan Sheriff | 330b187 | 2012-09-16 12:27:57 -0700 | [diff] [blame] | 53 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 54 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.provider.Settings; |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 56 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 57 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 59 | import java.io.FileDescriptor; |
| 60 | import java.io.PrintWriter; |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 61 | import java.net.InetAddress; |
| 62 | import java.net.Inet4Address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 66 | import com.android.internal.app.IBatteryStats; |
Irfan Sheriff | 616f317 | 2011-09-11 19:59:01 -0700 | [diff] [blame] | 67 | import com.android.internal.telephony.TelephonyIntents; |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 68 | import com.android.internal.util.AsyncChannel; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 69 | import com.android.server.am.BatteryStatsService; |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 70 | import com.android.internal.R; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | /** |
| 73 | * WifiService handles remote WiFi operation requests by implementing |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 74 | * the IWifiManager interface. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * |
| 76 | * @hide |
| 77 | */ |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 78 | //TODO: Clean up multiple locks and implement WifiService |
| 79 | // as a SM to track soft AP/client/adhoc bring up based |
| 80 | // on device idle state, airplane mode and boot. |
| 81 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 82 | public final class WifiService extends IWifiManager.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | private static final String TAG = "WifiService"; |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 84 | private static final boolean DBG = false; |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 85 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 86 | private final WifiStateMachine mWifiStateMachine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 88 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | private AlarmManager mAlarmManager; |
| 91 | private PendingIntent mIdleIntent; |
| 92 | private static final int IDLE_REQUEST = 0; |
| 93 | private boolean mScreenOff; |
| 94 | private boolean mDeviceIdle; |
Irfan Sheriff | 616f317 | 2011-09-11 19:59:01 -0700 | [diff] [blame] | 95 | private boolean mEmergencyCallbackMode = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private int mPluggedType; |
| 97 | |
| 98 | private final LockList mLocks = new LockList(); |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 99 | // some wifi lock statistics |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 100 | private int mFullHighPerfLocksAcquired; |
| 101 | private int mFullHighPerfLocksReleased; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 102 | private int mFullLocksAcquired; |
| 103 | private int mFullLocksReleased; |
| 104 | private int mScanLocksAcquired; |
| 105 | private int mScanLocksReleased; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 106 | |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 107 | private final List<Multicaster> mMulticasters = |
| 108 | new ArrayList<Multicaster>(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 109 | private int mMulticastEnabled; |
| 110 | private int mMulticastDisabled; |
| 111 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 112 | private final IBatteryStats mBatteryStats; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 113 | private final AppOpsManager mAppOps; |
Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 114 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 115 | private String mInterfaceName; |
| 116 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 117 | /* Tracks the open wi-fi network notification */ |
| 118 | private WifiNotificationController mNotificationController; |
| 119 | /* Polls traffic stats and notifies clients */ |
| 120 | private WifiTrafficPoller mTrafficPoller; |
| 121 | /* Tracks the persisted states for wi-fi & airplane mode */ |
| 122 | private WifiSettingsStore mSettingsStore; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | /** |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 125 | * See {@link Settings.Global#WIFI_IDLE_MS}. This is the default value if a |
| 126 | * Settings.Global value is not present. This timeout value is chosen as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | * the approximate point at which the battery drain caused by Wi-Fi |
| 128 | * being enabled but not active exceeds the battery drain caused by |
| 129 | * re-establishing a connection to the mobile data network. |
| 130 | */ |
Irfan Sheriff | 4f5f7c9 | 2010-10-14 17:01:27 -0700 | [diff] [blame] | 131 | private static final long DEFAULT_IDLE_MS = 15 * 60 * 1000; /* 15 minutes */ |
| 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | private static final String ACTION_DEVICE_IDLE = |
| 134 | "com.android.server.WifiManager.action.DEVICE_IDLE"; |
| 135 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 136 | /* The work source (UID) that triggered the current WIFI scan, synchronized |
| 137 | * on this */ |
| 138 | private WorkSource mScanWorkSource; |
| 139 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 140 | private boolean mIsReceiverRegistered = false; |
| 141 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 142 | NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", ""); |
| 143 | |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 144 | /** |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 145 | * Asynchronous channel to WifiStateMachine |
| 146 | */ |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 147 | private AsyncChannel mWifiStateMachineChannel; |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 150 | * Clients receiving asynchronous messages |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 151 | */ |
Irfan Sheriff | 302b06d | 2013-02-22 12:35:31 -0800 | [diff] [blame^] | 152 | private List<Messenger> mClients = new ArrayList<Messenger>(); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 153 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Handles client connections |
| 156 | */ |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 157 | private class ClientHandler extends Handler { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 158 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 159 | ClientHandler(android.os.Looper looper) { |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 160 | super(looper); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void handleMessage(Message msg) { |
| 165 | switch (msg.what) { |
| 166 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
| 167 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
Irfan Sheriff | 6bfc888 | 2012-08-29 15:35:57 -0700 | [diff] [blame] | 168 | if (DBG) Slog.d(TAG, "New client listening to asynchronous messages"); |
Irfan Sheriff | 302b06d | 2013-02-22 12:35:31 -0800 | [diff] [blame^] | 169 | // We track the clients by the Messenger |
| 170 | // since it is expected to be always available |
| 171 | mClients.add(msg.replyTo); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 172 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 173 | Slog.e(TAG, "Client connection failure, error=" + msg.arg1); |
| 174 | } |
| 175 | break; |
| 176 | } |
Irfan Sheriff | c23971b | 2011-03-04 17:06:31 -0800 | [diff] [blame] | 177 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 178 | if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) { |
Irfan Sheriff | 6bfc888 | 2012-08-29 15:35:57 -0700 | [diff] [blame] | 179 | if (DBG) Slog.d(TAG, "Send failed, client connection lost"); |
Irfan Sheriff | c23971b | 2011-03-04 17:06:31 -0800 | [diff] [blame] | 180 | } else { |
Irfan Sheriff | 6bfc888 | 2012-08-29 15:35:57 -0700 | [diff] [blame] | 181 | if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1); |
Irfan Sheriff | c23971b | 2011-03-04 17:06:31 -0800 | [diff] [blame] | 182 | } |
Irfan Sheriff | 302b06d | 2013-02-22 12:35:31 -0800 | [diff] [blame^] | 183 | mClients.remove(msg.replyTo); |
Irfan Sheriff | c23971b | 2011-03-04 17:06:31 -0800 | [diff] [blame] | 184 | break; |
| 185 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 186 | case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { |
| 187 | AsyncChannel ac = new AsyncChannel(); |
| 188 | ac.connect(mContext, this, msg.replyTo); |
| 189 | break; |
| 190 | } |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 191 | /* Client commands are forwarded to state machine */ |
| 192 | case WifiManager.CONNECT_NETWORK: |
| 193 | case WifiManager.SAVE_NETWORK: |
| 194 | case WifiManager.FORGET_NETWORK: |
| 195 | case WifiManager.START_WPS: |
| 196 | case WifiManager.CANCEL_WPS: |
| 197 | case WifiManager.DISABLE_NETWORK: |
Yuhao Zheng | f630782 | 2012-08-14 14:21:25 -0700 | [diff] [blame] | 198 | case WifiManager.RSSI_PKTCNT_FETCH: { |
| 199 | mWifiStateMachine.sendMessage(Message.obtain(msg)); |
| 200 | break; |
| 201 | } |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 202 | default: { |
| 203 | Slog.d(TAG, "WifiServicehandler.handleMessage ignoring msg=" + msg); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 209 | private ClientHandler mClientHandler; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * Handles interaction with WifiStateMachine |
| 213 | */ |
| 214 | private class WifiStateMachineHandler extends Handler { |
| 215 | private AsyncChannel mWsmChannel; |
| 216 | |
| 217 | WifiStateMachineHandler(android.os.Looper looper) { |
| 218 | super(looper); |
| 219 | mWsmChannel = new AsyncChannel(); |
| 220 | mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void handleMessage(Message msg) { |
| 225 | switch (msg.what) { |
| 226 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
| 227 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 228 | mWifiStateMachineChannel = mWsmChannel; |
| 229 | } else { |
| 230 | Slog.e(TAG, "WifiStateMachine connection failure, error=" + msg.arg1); |
| 231 | mWifiStateMachineChannel = null; |
| 232 | } |
| 233 | break; |
| 234 | } |
Irfan Sheriff | 6da83d5 | 2011-06-06 12:54:06 -0700 | [diff] [blame] | 235 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 236 | Slog.e(TAG, "WifiStateMachine channel lost, msg.arg1 =" + msg.arg1); |
| 237 | mWifiStateMachineChannel = null; |
| 238 | //Re-establish connection to state machine |
| 239 | mWsmChannel.connect(mContext, this, mWifiStateMachine.getHandler()); |
| 240 | break; |
| 241 | } |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 242 | default: { |
| 243 | Slog.d(TAG, "WifiStateMachineHandler.handleMessage ignoring msg=" + msg); |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | WifiStateMachineHandler mWifiStateMachineHandler; |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 250 | |
| 251 | /** |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 252 | * Temporary for computing UIDS that are responsible for starting WIFI. |
| 253 | * Protected by mWifiStateTracker lock. |
| 254 | */ |
| 255 | private final WorkSource mTmpWorkSource = new WorkSource(); |
Isaac Levy | 654f509 | 2011-07-13 17:41:45 -0700 | [diff] [blame] | 256 | private WifiWatchdogStateMachine mWifiWatchdogStateMachine; |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 257 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 258 | public WifiService(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | mContext = context; |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 260 | |
| 261 | mInterfaceName = SystemProperties.get("wifi.interface", "wlan0"); |
| 262 | |
| 263 | mWifiStateMachine = new WifiStateMachine(mContext, mInterfaceName); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 264 | mWifiStateMachine.enableRssiPolling(true); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 265 | mBatteryStats = BatteryStatsService.getService(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 266 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 269 | Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null); |
| 270 | mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0); |
| 271 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 272 | mNotificationController = new WifiNotificationController(mContext, mWifiStateMachine); |
| 273 | mTrafficPoller = new WifiTrafficPoller(mContext, mClients, mInterfaceName); |
| 274 | mSettingsStore = new WifiSettingsStore(mContext); |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | mContext.registerReceiver( |
| 277 | new BroadcastReceiver() { |
| 278 | @Override |
| 279 | public void onReceive(Context context, Intent intent) { |
Irfan Sheriff | 8a64b1a | 2013-02-20 15:12:41 -0800 | [diff] [blame] | 280 | if (mSettingsStore.handleAirplaneModeToggled()) { |
| 281 | updateWifiState(); |
| 282 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
| 284 | }, |
| 285 | new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)); |
| 286 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 287 | mContext.registerReceiver( |
| 288 | new BroadcastReceiver() { |
| 289 | @Override |
| 290 | public void onReceive(Context context, Intent intent) { |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 291 | if (intent.getAction().equals( |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 292 | WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 293 | noteScanEnd(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 296 | }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 297 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 298 | HandlerThread wifiThread = new HandlerThread("WifiService"); |
| 299 | wifiThread.start(); |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 300 | mClientHandler = new ClientHandler(wifiThread.getLooper()); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 301 | mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper()); |
Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 302 | } |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 303 | |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 304 | /** Tell battery stats about a new WIFI scan */ |
| 305 | private void noteScanStart() { |
| 306 | WorkSource scanWorkSource = null; |
| 307 | synchronized (WifiService.this) { |
| 308 | if (mScanWorkSource != null) { |
| 309 | // Scan already in progress, don't add this one to battery stats |
| 310 | return; |
| 311 | } |
| 312 | scanWorkSource = new WorkSource(Binder.getCallingUid()); |
| 313 | mScanWorkSource = scanWorkSource; |
| 314 | } |
| 315 | |
| 316 | long id = Binder.clearCallingIdentity(); |
| 317 | try { |
| 318 | mBatteryStats.noteWifiScanStartedFromSource(scanWorkSource); |
| 319 | } catch (RemoteException e) { |
| 320 | Log.w(TAG, e); |
| 321 | } finally { |
| 322 | Binder.restoreCallingIdentity(id); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** Tell battery stats that the current WIFI scan has completed */ |
| 327 | private void noteScanEnd() { |
| 328 | WorkSource scanWorkSource = null; |
| 329 | synchronized (WifiService.this) { |
| 330 | scanWorkSource = mScanWorkSource; |
| 331 | mScanWorkSource = null; |
| 332 | } |
| 333 | if (scanWorkSource != null) { |
| 334 | try { |
| 335 | mBatteryStats.noteWifiScanStoppedFromSource(scanWorkSource); |
| 336 | } catch (RemoteException e) { |
| 337 | Log.w(TAG, e); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 342 | /** |
| 343 | * Check if Wi-Fi needs to be enabled and start |
| 344 | * if needed |
Irfan Sheriff | 60e3ba0 | 2010-04-02 12:18:45 -0700 | [diff] [blame] | 345 | * |
| 346 | * This function is used only at boot time |
Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 347 | */ |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 348 | public void checkAndStartWifi() { |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 349 | /* Check if wi-fi needs to be enabled */ |
| 350 | boolean wifiEnabled = mSettingsStore.shouldWifiBeEnabled(); |
Irfan Sheriff | 7b00978 | 2010-03-11 16:37:45 -0800 | [diff] [blame] | 351 | Slog.i(TAG, "WifiService starting up with Wi-Fi " + |
| 352 | (wifiEnabled ? "enabled" : "disabled")); |
Irfan Sheriff | f03d620 | 2012-05-17 12:33:46 -0700 | [diff] [blame] | 353 | |
| 354 | // If we are already disabled (could be due to airplane mode), avoid changing persist |
| 355 | // state here |
| 356 | if (wifiEnabled) setWifiEnabled(wifiEnabled); |
Isaac Levy | bc7dfb5 | 2011-06-06 15:34:01 -0700 | [diff] [blame] | 357 | |
Isaac Levy | 654f509 | 2011-07-13 17:41:45 -0700 | [diff] [blame] | 358 | mWifiWatchdogStateMachine = WifiWatchdogStateMachine. |
| 359 | makeWifiWatchdogStateMachine(mContext); |
| 360 | |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 361 | } |
| 362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | /** |
| 364 | * see {@link android.net.wifi.WifiManager#pingSupplicant()} |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 365 | * @return {@code true} if the operation succeeds, {@code false} otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | */ |
| 367 | public boolean pingSupplicant() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 368 | enforceAccessPermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 369 | if (mWifiStateMachineChannel != null) { |
| 370 | return mWifiStateMachine.syncPingSupplicant(mWifiStateMachineChannel); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 371 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 372 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 373 | return false; |
| 374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * see {@link android.net.wifi.WifiManager#startScan()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | */ |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 380 | public void startScan() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | enforceChangePermission(); |
Irfan Sheriff | bcc97ca | 2013-02-21 14:15:18 -0800 | [diff] [blame] | 382 | mWifiStateMachine.startScan(); |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 383 | noteScanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | private void enforceAccessPermission() { |
| 387 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_WIFI_STATE, |
| 388 | "WifiService"); |
| 389 | } |
| 390 | |
| 391 | private void enforceChangePermission() { |
| 392 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CHANGE_WIFI_STATE, |
| 393 | "WifiService"); |
| 394 | |
| 395 | } |
| 396 | |
Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 397 | private void enforceMulticastChangePermission() { |
| 398 | mContext.enforceCallingOrSelfPermission( |
| 399 | android.Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, |
| 400 | "WifiService"); |
| 401 | } |
| 402 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 403 | private void enforceConnectivityInternalPermission() { |
| 404 | mContext.enforceCallingOrSelfPermission( |
| 405 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 406 | "ConnectivityService"); |
| 407 | } |
| 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | /** |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 410 | * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)} |
| 411 | * @param enable {@code true} to enable, {@code false} to disable. |
| 412 | * @return {@code true} if the enable/disable operation was |
| 413 | * started or is already in the queue. |
| 414 | */ |
| 415 | public synchronized boolean setWifiEnabled(boolean enable) { |
| 416 | enforceChangePermission(); |
Irfan Sheriff | bd21b78 | 2012-05-16 13:13:54 -0700 | [diff] [blame] | 417 | Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid() |
| 418 | + ", uid=" + Binder.getCallingUid()); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 419 | if (DBG) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 420 | Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n"); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Irfan Sheriff | 6118069 | 2010-08-18 16:07:39 -0700 | [diff] [blame] | 423 | /* |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 424 | * Caller might not have WRITE_SECURE_SETTINGS, |
| 425 | * only CHANGE_WIFI_STATE is enforced |
| 426 | */ |
Irfan Sheriff | 5401f0b | 2011-12-07 16:27:49 -0800 | [diff] [blame] | 427 | |
Irfan Sheriff | f03d620 | 2012-05-17 12:33:46 -0700 | [diff] [blame] | 428 | long ident = Binder.clearCallingIdentity(); |
Irfan Sheriff | 3d33a63 | 2012-09-16 17:59:13 -0700 | [diff] [blame] | 429 | try { |
Irfan Sheriff | 8a64b1a | 2013-02-20 15:12:41 -0800 | [diff] [blame] | 430 | if (! mSettingsStore.handleWifiToggled(enable)) { |
| 431 | // Nothing to do if wifi cannot be toggled |
| 432 | return true; |
| 433 | } |
Irfan Sheriff | 3d33a63 | 2012-09-16 17:59:13 -0700 | [diff] [blame] | 434 | } finally { |
| 435 | Binder.restoreCallingIdentity(ident); |
| 436 | } |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 437 | |
| 438 | if (enable) { |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 439 | reportStartWorkSource(); |
| 440 | } |
| 441 | |
| 442 | mWifiStateMachine.setWifiEnabled(enable); |
| 443 | |
| 444 | if (enable) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 445 | if (!mIsReceiverRegistered) { |
| 446 | registerForBroadcasts(); |
| 447 | mIsReceiverRegistered = true; |
| 448 | } |
Irfan Sheriff | 5401f0b | 2011-12-07 16:27:49 -0800 | [diff] [blame] | 449 | } else if (mIsReceiverRegistered) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 450 | mContext.unregisterReceiver(mReceiver); |
| 451 | mIsReceiverRegistered = false; |
| 452 | } |
| 453 | |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | * see {@link WifiManager#getWifiState()} |
| 459 | * @return One of {@link WifiManager#WIFI_STATE_DISABLED}, |
| 460 | * {@link WifiManager#WIFI_STATE_DISABLING}, |
| 461 | * {@link WifiManager#WIFI_STATE_ENABLED}, |
| 462 | * {@link WifiManager#WIFI_STATE_ENABLING}, |
| 463 | * {@link WifiManager#WIFI_STATE_UNKNOWN} |
| 464 | */ |
| 465 | public int getWifiEnabledState() { |
| 466 | enforceAccessPermission(); |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 467 | return mWifiStateMachine.syncGetWifiState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
Irfan Sheriff | c2f54c2 | 2010-03-18 14:02:22 -0700 | [diff] [blame] | 471 | * see {@link android.net.wifi.WifiManager#setWifiApEnabled(WifiConfiguration, boolean)} |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 472 | * @param wifiConfig SSID, security and channel details as |
| 473 | * part of WifiConfiguration |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 474 | * @param enabled true to enable and false to disable |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 475 | */ |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 476 | public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 477 | enforceChangePermission(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 478 | mWifiStateMachine.setWifiApEnabled(wifiConfig, enabled); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 481 | /** |
| 482 | * see {@link WifiManager#getWifiApState()} |
| 483 | * @return One of {@link WifiManager#WIFI_AP_STATE_DISABLED}, |
| 484 | * {@link WifiManager#WIFI_AP_STATE_DISABLING}, |
| 485 | * {@link WifiManager#WIFI_AP_STATE_ENABLED}, |
| 486 | * {@link WifiManager#WIFI_AP_STATE_ENABLING}, |
| 487 | * {@link WifiManager#WIFI_AP_STATE_FAILED} |
| 488 | */ |
| 489 | public int getWifiApEnabledState() { |
Irfan Sheriff | 17b232b | 2010-06-24 11:32:26 -0700 | [diff] [blame] | 490 | enforceAccessPermission(); |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 491 | return mWifiStateMachine.syncGetWifiApState(); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
| 495 | * see {@link WifiManager#getWifiApConfiguration()} |
| 496 | * @return soft access point configuration |
| 497 | */ |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 498 | public WifiConfiguration getWifiApConfiguration() { |
| 499 | enforceAccessPermission(); |
Irfan Sheriff | 9575a1b | 2011-11-07 10:34:54 -0800 | [diff] [blame] | 500 | return mWifiStateMachine.syncGetWifiApConfiguration(); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 503 | /** |
| 504 | * see {@link WifiManager#setWifiApConfiguration(WifiConfiguration)} |
| 505 | * @param wifiConfig WifiConfiguration details for soft access point |
| 506 | */ |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 507 | public void setWifiApConfiguration(WifiConfiguration wifiConfig) { |
Irfan Sheriff | 17b232b | 2010-06-24 11:32:26 -0700 | [diff] [blame] | 508 | enforceChangePermission(); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 509 | if (wifiConfig == null) |
| 510 | return; |
Irfan Sheriff | ffcea7a | 2011-05-10 16:26:06 -0700 | [diff] [blame] | 511 | mWifiStateMachine.setWifiApConfiguration(wifiConfig); |
Irfan Sheriff | 9ab518ad | 2010-03-12 15:48:17 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 514 | /** |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 515 | * see {@link android.net.wifi.WifiManager#disconnect()} |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 516 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 517 | public void disconnect() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 518 | enforceChangePermission(); |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 519 | mWifiStateMachine.disconnectCommand(); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /** |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 523 | * see {@link android.net.wifi.WifiManager#reconnect()} |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 524 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 525 | public void reconnect() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 526 | enforceChangePermission(); |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 527 | mWifiStateMachine.reconnectCommand(); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 530 | /** |
| 531 | * see {@link android.net.wifi.WifiManager#reassociate()} |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 532 | */ |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 533 | public void reassociate() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 534 | enforceChangePermission(); |
Irfan Sheriff | e498475 | 2010-08-19 11:29:22 -0700 | [diff] [blame] | 535 | mWifiStateMachine.reassociateCommand(); |
Irfan Sheriff | 5321aef | 2010-02-12 12:35:59 -0800 | [diff] [blame] | 536 | } |
| 537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | /** |
| 539 | * see {@link android.net.wifi.WifiManager#getConfiguredNetworks()} |
| 540 | * @return the list of configured networks |
| 541 | */ |
| 542 | public List<WifiConfiguration> getConfiguredNetworks() { |
| 543 | enforceAccessPermission(); |
Irfan Sheriff | e744cff | 2011-12-11 09:17:50 -0800 | [diff] [blame] | 544 | if (mWifiStateMachineChannel != null) { |
| 545 | return mWifiStateMachine.syncGetConfiguredNetworks(mWifiStateMachineChannel); |
| 546 | } else { |
| 547 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
| 548 | return null; |
| 549 | } |
Chung-yih Wang | a8d1594 | 2009-10-09 11:01:49 +0800 | [diff] [blame] | 550 | } |
| 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | /** |
| 553 | * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} |
| 554 | * @return the supplicant-assigned identifier for the new or updated |
| 555 | * network if the operation succeeds, or {@code -1} if it fails |
| 556 | */ |
Irfan Sheriff | 7aac554 | 2009-12-22 21:42:17 -0800 | [diff] [blame] | 557 | public int addOrUpdateNetwork(WifiConfiguration config) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | enforceChangePermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 559 | if (mWifiStateMachineChannel != null) { |
| 560 | return mWifiStateMachine.syncAddOrUpdateNetwork(mWifiStateMachineChannel, config); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 561 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 562 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 563 | return -1; |
| 564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 567 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | * See {@link android.net.wifi.WifiManager#removeNetwork(int)} |
| 569 | * @param netId the integer that identifies the network configuration |
| 570 | * to the supplicant |
| 571 | * @return {@code true} if the operation succeeded |
| 572 | */ |
| 573 | public boolean removeNetwork(int netId) { |
| 574 | enforceChangePermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 575 | if (mWifiStateMachineChannel != null) { |
| 576 | return mWifiStateMachine.syncRemoveNetwork(mWifiStateMachineChannel, netId); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 577 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 578 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Wink Saville | 4b7ba09 | 2010-10-20 15:37:41 -0700 | [diff] [blame] | 579 | return false; |
| 580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
| 584 | * See {@link android.net.wifi.WifiManager#enableNetwork(int, boolean)} |
| 585 | * @param netId the integer that identifies the network configuration |
| 586 | * to the supplicant |
| 587 | * @param disableOthers if true, disable all other networks. |
| 588 | * @return {@code true} if the operation succeeded |
| 589 | */ |
| 590 | public boolean enableNetwork(int netId, boolean disableOthers) { |
| 591 | enforceChangePermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 592 | if (mWifiStateMachineChannel != null) { |
| 593 | return mWifiStateMachine.syncEnableNetwork(mWifiStateMachineChannel, netId, |
| 594 | disableOthers); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 595 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 596 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 597 | return false; |
| 598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /** |
| 602 | * See {@link android.net.wifi.WifiManager#disableNetwork(int)} |
| 603 | * @param netId the integer that identifies the network configuration |
| 604 | * to the supplicant |
| 605 | * @return {@code true} if the operation succeeded |
| 606 | */ |
| 607 | public boolean disableNetwork(int netId) { |
| 608 | enforceChangePermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 609 | if (mWifiStateMachineChannel != null) { |
| 610 | return mWifiStateMachine.syncDisableNetwork(mWifiStateMachineChannel, netId); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 611 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 612 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 613 | return false; |
| 614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
| 618 | * See {@link android.net.wifi.WifiManager#getConnectionInfo()} |
| 619 | * @return the Wi-Fi information, contained in {@link WifiInfo}. |
| 620 | */ |
| 621 | public WifiInfo getConnectionInfo() { |
| 622 | enforceAccessPermission(); |
| 623 | /* |
| 624 | * Make sure we have the latest information, by sending |
| 625 | * a status request to the supplicant. |
| 626 | */ |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 627 | return mWifiStateMachine.syncRequestConnectionInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Return the results of the most recent access point scan, in the form of |
| 632 | * a list of {@link ScanResult} objects. |
| 633 | * @return the list of results |
| 634 | */ |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 635 | public List<ScanResult> getScanResults(String callingPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | enforceAccessPermission(); |
Irfan Sheriff | db831da | 2012-09-16 17:39:26 -0700 | [diff] [blame] | 637 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 638 | int uid = Binder.getCallingUid(); |
Irfan Sheriff | db831da | 2012-09-16 17:39:26 -0700 | [diff] [blame] | 639 | long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 640 | if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage) |
| 641 | != AppOpsManager.MODE_ALLOWED) { |
| 642 | return new ArrayList<ScanResult>(); |
| 643 | } |
Irfan Sheriff | 3d33a63 | 2012-09-16 17:59:13 -0700 | [diff] [blame] | 644 | try { |
| 645 | int currentUser = ActivityManager.getCurrentUser(); |
| 646 | if (userId != currentUser) { |
| 647 | return new ArrayList<ScanResult>(); |
| 648 | } else { |
| 649 | return mWifiStateMachine.syncGetScanResultsList(); |
| 650 | } |
| 651 | } finally { |
| 652 | Binder.restoreCallingIdentity(ident); |
Irfan Sheriff | 330b187 | 2012-09-16 12:27:57 -0700 | [diff] [blame] | 653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Tell the supplicant to persist the current list of configured networks. |
| 658 | * @return {@code true} if the operation succeeded |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 659 | * |
| 660 | * TODO: deprecate this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | */ |
| 662 | public boolean saveConfiguration() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 663 | boolean result = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | enforceChangePermission(); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 665 | if (mWifiStateMachineChannel != null) { |
| 666 | return mWifiStateMachine.syncSaveConfig(mWifiStateMachineChannel); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 667 | } else { |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 668 | Slog.e(TAG, "mWifiStateMachineChannel is not initialized"); |
Irfan Sheriff | 1406bcb | 2010-10-28 14:41:39 -0700 | [diff] [blame] | 669 | return false; |
| 670 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /** |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 674 | * Set the country code |
| 675 | * @param countryCode ISO 3166 country code. |
Robert Greenwalt | b5010cc | 2009-05-21 15:11:40 -0700 | [diff] [blame] | 676 | * @param persist {@code true} if the setting should be remembered. |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 677 | * |
| 678 | * The persist behavior exists so that wifi can fall back to the last |
| 679 | * persisted country code on a restart, when the locale information is |
| 680 | * not available from telephony. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | */ |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 682 | public void setCountryCode(String countryCode, boolean persist) { |
| 683 | Slog.i(TAG, "WifiService trying to set country code to " + countryCode + |
| 684 | " with persist set to " + persist); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | enforceChangePermission(); |
Irfan Sheriff | ed4f28b | 2010-10-29 15:32:10 -0700 | [diff] [blame] | 686 | mWifiStateMachine.setCountryCode(countryCode, persist); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
Irfan Sheriff | 36f7413 | 2010-11-04 16:57:37 -0700 | [diff] [blame] | 690 | * Set the operational frequency band |
| 691 | * @param band One of |
| 692 | * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, |
| 693 | * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, |
| 694 | * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, |
| 695 | * @param persist {@code true} if the setting should be remembered. |
| 696 | * |
| 697 | */ |
| 698 | public void setFrequencyBand(int band, boolean persist) { |
| 699 | enforceChangePermission(); |
| 700 | if (!isDualBandSupported()) return; |
| 701 | Slog.i(TAG, "WifiService trying to set frequency band to " + band + |
| 702 | " with persist set to " + persist); |
| 703 | mWifiStateMachine.setFrequencyBand(band, persist); |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /** |
| 708 | * Get the operational frequency band |
| 709 | */ |
| 710 | public int getFrequencyBand() { |
| 711 | enforceAccessPermission(); |
| 712 | return mWifiStateMachine.getFrequencyBand(); |
| 713 | } |
| 714 | |
| 715 | public boolean isDualBandSupported() { |
| 716 | //TODO: Should move towards adding a driver API that checks at runtime |
| 717 | return mContext.getResources().getBoolean( |
| 718 | com.android.internal.R.bool.config_wifi_dual_band_support); |
| 719 | } |
| 720 | |
| 721 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | * Return the DHCP-assigned addresses from the last successful DHCP request, |
| 723 | * if any. |
| 724 | * @return the DHCP information |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 725 | * @deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | */ |
| 727 | public DhcpInfo getDhcpInfo() { |
| 728 | enforceAccessPermission(); |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 729 | DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults(); |
| 730 | if (dhcpResults.linkProperties == null) return null; |
| 731 | |
| 732 | DhcpInfo info = new DhcpInfo(); |
| 733 | for (LinkAddress la : dhcpResults.linkProperties.getLinkAddresses()) { |
| 734 | InetAddress addr = la.getAddress(); |
| 735 | if (addr instanceof Inet4Address) { |
| 736 | info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address)addr); |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | for (RouteInfo r : dhcpResults.linkProperties.getRoutes()) { |
| 741 | if (r.isDefaultRoute()) { |
| 742 | InetAddress gateway = r.getGateway(); |
| 743 | if (gateway instanceof Inet4Address) { |
| 744 | info.gateway = NetworkUtils.inetAddressToInt((Inet4Address)gateway); |
| 745 | } |
| 746 | } else if (r.isHostRoute()) { |
| 747 | LinkAddress dest = r.getDestination(); |
| 748 | if (dest.getAddress() instanceof Inet4Address) { |
| 749 | info.netmask = NetworkUtils.prefixLengthToNetmaskInt( |
| 750 | dest.getNetworkPrefixLength()); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | int dnsFound = 0; |
| 755 | for (InetAddress dns : dhcpResults.linkProperties.getDnses()) { |
| 756 | if (dns instanceof Inet4Address) { |
| 757 | if (dnsFound == 0) { |
| 758 | info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address)dns); |
| 759 | } else { |
| 760 | info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address)dns); |
| 761 | } |
| 762 | if (++dnsFound > 1) break; |
| 763 | } |
| 764 | } |
| 765 | InetAddress serverAddress = dhcpResults.serverAddress; |
| 766 | if (serverAddress instanceof Inet4Address) { |
| 767 | info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address)serverAddress); |
| 768 | } |
| 769 | info.leaseDuration = dhcpResults.leaseDuration; |
| 770 | |
| 771 | return info; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 774 | /** |
| 775 | * see {@link android.net.wifi.WifiManager#startWifi} |
| 776 | * |
| 777 | */ |
| 778 | public void startWifi() { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 779 | enforceConnectivityInternalPermission(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 780 | /* TODO: may be add permissions for access only to connectivity service |
| 781 | * TODO: if a start issued, keep wifi alive until a stop issued irrespective |
| 782 | * of WifiLock & device idle status unless wifi enabled status is toggled |
| 783 | */ |
| 784 | |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 785 | mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 786 | mWifiStateMachine.reconnectCommand(); |
| 787 | } |
| 788 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 789 | public void captivePortalCheckComplete() { |
| 790 | enforceConnectivityInternalPermission(); |
| 791 | mWifiStateMachine.captivePortalCheckComplete(); |
| 792 | } |
| 793 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 794 | /** |
| 795 | * see {@link android.net.wifi.WifiManager#stopWifi} |
| 796 | * |
| 797 | */ |
| 798 | public void stopWifi() { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 799 | enforceConnectivityInternalPermission(); |
| 800 | /* |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 801 | * TODO: if a stop is issued, wifi is brought up only by startWifi |
| 802 | * unless wifi enabled status is toggled |
| 803 | */ |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 804 | mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 807 | /** |
| 808 | * see {@link android.net.wifi.WifiManager#addToBlacklist} |
| 809 | * |
| 810 | */ |
| 811 | public void addToBlacklist(String bssid) { |
| 812 | enforceChangePermission(); |
| 813 | |
| 814 | mWifiStateMachine.addToBlacklist(bssid); |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * see {@link android.net.wifi.WifiManager#clearBlacklist} |
| 819 | * |
| 820 | */ |
| 821 | public void clearBlacklist() { |
| 822 | enforceChangePermission(); |
| 823 | |
| 824 | mWifiStateMachine.clearBlacklist(); |
| 825 | } |
| 826 | |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 827 | /** |
| 828 | * Get a reference to handler. This is used by a client to establish |
| 829 | * an AsyncChannel communication with WifiService |
| 830 | */ |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 831 | public Messenger getWifiServiceMessenger() { |
Irfan Sheriff | 35bbe27 | 2012-08-23 16:57:43 -0700 | [diff] [blame] | 832 | enforceAccessPermission(); |
| 833 | enforceChangePermission(); |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 834 | return new Messenger(mClientHandler); |
Irfan Sheriff | 227bec4 | 2011-02-15 19:30:27 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 837 | /** Get a reference to WifiStateMachine handler for AsyncChannel communication */ |
| 838 | public Messenger getWifiStateMachineMessenger() { |
| 839 | enforceAccessPermission(); |
| 840 | enforceChangePermission(); |
| 841 | return mWifiStateMachine.getMessenger(); |
| 842 | } |
| 843 | |
Irfan Sheriff | 4aeca7c5 | 2011-03-10 16:53:33 -0800 | [diff] [blame] | 844 | /** |
| 845 | * Get the IP and proxy configuration file |
| 846 | */ |
| 847 | public String getConfigFile() { |
| 848 | enforceAccessPermission(); |
| 849 | return mWifiStateMachine.getConfigFile(); |
| 850 | } |
| 851 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 853 | @Override |
| 854 | public void onReceive(Context context, Intent intent) { |
| 855 | String action = intent.getAction(); |
| 856 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 857 | long idleMillis = |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 858 | Settings.Global.getLong(mContext.getContentResolver(), |
| 859 | Settings.Global.WIFI_IDLE_MS, DEFAULT_IDLE_MS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | int stayAwakeConditions = |
Christopher Tate | 6f5a9a9 | 2012-09-14 17:24:28 -0700 | [diff] [blame] | 861 | Settings.Global.getInt(mContext.getContentResolver(), |
| 862 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 864 | if (DBG) { |
| 865 | Slog.d(TAG, "ACTION_SCREEN_ON"); |
| 866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | mAlarmManager.cancel(mIdleIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | mScreenOff = false; |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 869 | setDeviceIdleAndUpdateWifi(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 871 | if (DBG) { |
| 872 | Slog.d(TAG, "ACTION_SCREEN_OFF"); |
| 873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | mScreenOff = true; |
| 875 | /* |
| 876 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 877 | * AND the "stay on while plugged in" setting doesn't match the |
| 878 | * current power conditions (i.e, not plugged in, plugged in to USB, |
| 879 | * or plugged in to AC). |
| 880 | */ |
| 881 | if (!shouldWifiStayAwake(stayAwakeConditions, mPluggedType)) { |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 882 | //Delayed shutdown if wifi is connected |
| 883 | if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED) { |
| 884 | if (DBG) Slog.d(TAG, "setting ACTION_DEVICE_IDLE: " + idleMillis + " ms"); |
| 885 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() |
| 886 | + idleMillis, mIdleIntent); |
Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 887 | } else { |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 888 | setDeviceIdleAndUpdateWifi(true); |
Mike Lockwood | d9c32bc | 2009-05-18 14:14:15 -0400 | [diff] [blame] | 889 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } else if (action.equals(ACTION_DEVICE_IDLE)) { |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 892 | setDeviceIdleAndUpdateWifi(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 894 | /* |
| 895 | * Set a timer to put Wi-Fi to sleep, but only if the screen is off |
| 896 | * AND we are transitioning from a state in which the device was supposed |
| 897 | * to stay awake to a state in which it is not supposed to stay awake. |
| 898 | * If "stay awake" state is not changing, we do nothing, to avoid resetting |
| 899 | * the already-set timer. |
| 900 | */ |
| 901 | int pluggedType = intent.getIntExtra("plugged", 0); |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 902 | if (DBG) { |
| 903 | Slog.d(TAG, "ACTION_BATTERY_CHANGED pluggedType: " + pluggedType); |
| 904 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | if (mScreenOff && shouldWifiStayAwake(stayAwakeConditions, mPluggedType) && |
| 906 | !shouldWifiStayAwake(stayAwakeConditions, pluggedType)) { |
| 907 | long triggerTime = System.currentTimeMillis() + idleMillis; |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 908 | if (DBG) { |
| 909 | Slog.d(TAG, "setting ACTION_DEVICE_IDLE timer for " + idleMillis + "ms"); |
| 910 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mIdleIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | } |
Irfan Sheriff | 8cef067 | 2011-12-13 17:03:59 -0800 | [diff] [blame] | 913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | mPluggedType = pluggedType; |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 915 | } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { |
| 916 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, |
| 917 | BluetoothAdapter.STATE_DISCONNECTED); |
| 918 | mWifiStateMachine.sendBluetoothAdapterStateChange(state); |
Irfan Sheriff | 616f317 | 2011-09-11 19:59:01 -0700 | [diff] [blame] | 919 | } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { |
| 920 | mEmergencyCallbackMode = intent.getBooleanExtra("phoneinECMState", false); |
| 921 | updateWifiState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /** |
| 926 | * Determines whether the Wi-Fi chipset should stay awake or be put to |
| 927 | * sleep. Looks at the setting for the sleep policy and the current |
| 928 | * conditions. |
Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 929 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | * @see #shouldDeviceStayAwake(int, int) |
| 931 | */ |
| 932 | private boolean shouldWifiStayAwake(int stayAwakeConditions, int pluggedType) { |
Irfan Sheriff | 739f6bc | 2011-01-28 16:43:12 -0800 | [diff] [blame] | 933 | //Never sleep as long as the user has not changed the settings |
Brian Muramatsu | 35d323a | 2012-09-20 19:52:34 -0700 | [diff] [blame] | 934 | int wifiSleepPolicy = Settings.Global.getInt(mContext.getContentResolver(), |
| 935 | Settings.Global.WIFI_SLEEP_POLICY, |
| 936 | Settings.Global.WIFI_SLEEP_POLICY_NEVER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | |
Brian Muramatsu | 35d323a | 2012-09-20 19:52:34 -0700 | [diff] [blame] | 938 | if (wifiSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | // Never sleep |
| 940 | return true; |
Brian Muramatsu | 35d323a | 2012-09-20 19:52:34 -0700 | [diff] [blame] | 941 | } else if ((wifiSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | (pluggedType != 0)) { |
| 943 | // Never sleep while plugged, and we're plugged |
| 944 | return true; |
| 945 | } else { |
| 946 | // Default |
| 947 | return shouldDeviceStayAwake(stayAwakeConditions, pluggedType); |
| 948 | } |
| 949 | } |
Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 950 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | /** |
| 952 | * Determine whether the bit value corresponding to {@code pluggedType} is set in |
| 953 | * the bit string {@code stayAwakeConditions}. Because a {@code pluggedType} value |
| 954 | * of {@code 0} isn't really a plugged type, but rather an indication that the |
| 955 | * device isn't plugged in at all, there is no bit value corresponding to a |
| 956 | * {@code pluggedType} value of {@code 0}. That is why we shift by |
Ben Dodson | 4e8620f | 2010-08-25 10:55:47 -0700 | [diff] [blame] | 957 | * {@code pluggedType - 1} instead of by {@code pluggedType}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | * @param stayAwakeConditions a bit string specifying which "plugged types" should |
| 959 | * keep the device (and hence Wi-Fi) awake. |
| 960 | * @param pluggedType the type of plug (USB, AC, or none) for which the check is |
| 961 | * being made |
| 962 | * @return {@code true} if {@code pluggedType} indicates that the device is |
| 963 | * supposed to stay awake, {@code false} otherwise. |
| 964 | */ |
| 965 | private boolean shouldDeviceStayAwake(int stayAwakeConditions, int pluggedType) { |
| 966 | return (stayAwakeConditions & pluggedType) != 0; |
| 967 | } |
| 968 | }; |
| 969 | |
Irfan Sheriff | e6daca5 | 2011-11-03 15:46:50 -0700 | [diff] [blame] | 970 | private void setDeviceIdleAndUpdateWifi(boolean deviceIdle) { |
| 971 | mDeviceIdle = deviceIdle; |
| 972 | reportStartWorkSource(); |
| 973 | updateWifiState(); |
| 974 | } |
| 975 | |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 976 | private synchronized void reportStartWorkSource() { |
| 977 | mTmpWorkSource.clear(); |
| 978 | if (mDeviceIdle) { |
| 979 | for (int i=0; i<mLocks.mList.size(); i++) { |
| 980 | mTmpWorkSource.add(mLocks.mList.get(i).mWorkSource); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 981 | } |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 982 | } |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 983 | mWifiStateMachine.updateBatteryWorkSource(mTmpWorkSource); |
Dianne Hackborn | 58e0eef | 2010-09-16 01:22:10 -0700 | [diff] [blame] | 984 | } |
Jaikumar Ganesh | 7440fc2 | 2010-09-27 17:04:14 -0700 | [diff] [blame] | 985 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | private void updateWifiState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | boolean lockHeld = mLocks.hasLocks(); |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 988 | int strongestLockMode = WifiManager.WIFI_MODE_FULL; |
Irfan Sheriff | 616f317 | 2011-09-11 19:59:01 -0700 | [diff] [blame] | 989 | boolean wifiShouldBeStarted; |
| 990 | |
| 991 | if (mEmergencyCallbackMode) { |
| 992 | wifiShouldBeStarted = false; |
| 993 | } else { |
| 994 | wifiShouldBeStarted = !mDeviceIdle || lockHeld; |
| 995 | } |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 996 | |
| 997 | if (lockHeld) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | strongestLockMode = mLocks.getStrongestLockMode(); |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 999 | } |
| 1000 | /* If device is not idle, lockmode cannot be scan only */ |
| 1001 | if (!mDeviceIdle && strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | strongestLockMode = WifiManager.WIFI_MODE_FULL; |
| 1003 | } |
| 1004 | |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1005 | /* Disable tethering when airplane mode is enabled */ |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 1006 | if (mSettingsStore.isAirplaneModeOn()) { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1007 | mWifiStateMachine.setWifiApEnabled(null, false); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1008 | } |
Irfan Sheriff | b2e6c01 | 2010-04-05 11:57:56 -0700 | [diff] [blame] | 1009 | |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 1010 | if (mSettingsStore.shouldWifiBeEnabled()) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1011 | if (wifiShouldBeStarted) { |
Dianne Hackborn | 03f3cb0 | 2010-09-17 23:12:26 -0700 | [diff] [blame] | 1012 | reportStartWorkSource(); |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1013 | mWifiStateMachine.setWifiEnabled(true); |
| 1014 | mWifiStateMachine.setScanOnlyMode( |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1015 | strongestLockMode == WifiManager.WIFI_MODE_SCAN_ONLY); |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 1016 | mWifiStateMachine.setDriverStart(true, mEmergencyCallbackMode); |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1017 | mWifiStateMachine.setHighPerfModeEnabled(strongestLockMode |
| 1018 | == WifiManager.WIFI_MODE_FULL_HIGH_PERF); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | } else { |
Irfan Sheriff | 4494c90 | 2011-12-08 10:47:54 -0800 | [diff] [blame] | 1020 | mWifiStateMachine.setDriverStart(false, mEmergencyCallbackMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | } |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 1022 | } else { |
Irfan Sheriff | 0d25534 | 2010-07-28 09:35:20 -0700 | [diff] [blame] | 1023 | mWifiStateMachine.setWifiEnabled(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | private void registerForBroadcasts() { |
| 1028 | IntentFilter intentFilter = new IntentFilter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | intentFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 1030 | intentFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1031 | intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 1032 | intentFilter.addAction(ACTION_DEVICE_IDLE); |
Irfan Sheriff | 65eaec8 | 2011-01-05 22:00:16 -0800 | [diff] [blame] | 1033 | intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); |
Irfan Sheriff | 616f317 | 2011-09-11 19:59:01 -0700 | [diff] [blame] | 1034 | intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | mContext.registerReceiver(mReceiver, intentFilter); |
| 1036 | } |
Jaikumar Ganesh | 084c665 | 2009-12-07 10:58:18 -0800 | [diff] [blame] | 1037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | @Override |
| 1039 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1040 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1041 | != PackageManager.PERMISSION_GRANTED) { |
| 1042 | pw.println("Permission Denial: can't dump WifiService from from pid=" |
| 1043 | + Binder.getCallingPid() |
| 1044 | + ", uid=" + Binder.getCallingUid()); |
| 1045 | return; |
| 1046 | } |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1047 | pw.println("Wi-Fi is " + mWifiStateMachine.syncGetWifiStateByName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | pw.println("Stay-awake conditions: " + |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 1049 | Settings.Global.getInt(mContext.getContentResolver(), |
| 1050 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0)); |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1051 | pw.println("mScreenOff " + mScreenOff); |
| 1052 | pw.println("mDeviceIdle " + mDeviceIdle); |
| 1053 | pw.println("mPluggedType " + mPluggedType); |
| 1054 | pw.println("mEmergencyCallbackMode " + mEmergencyCallbackMode); |
| 1055 | pw.println("mMulticastEnabled " + mMulticastEnabled); |
| 1056 | pw.println("mMulticastDisabled " + mMulticastDisabled); |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 1057 | mSettingsStore.dump(fd, pw, args); |
| 1058 | mNotificationController.dump(fd, pw, args); |
| 1059 | mTrafficPoller.dump(fd, pw, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | pw.println("Latest scan results:"); |
Irfan Sheriff | d8134ff | 2010-08-22 17:06:34 -0700 | [diff] [blame] | 1062 | List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | if (scanResults != null && scanResults.size() != 0) { |
| 1064 | pw.println(" BSSID Frequency RSSI Flags SSID"); |
| 1065 | for (ScanResult r : scanResults) { |
| 1066 | pw.printf(" %17s %9d %5d %-16s %s%n", |
| 1067 | r.BSSID, |
| 1068 | r.frequency, |
| 1069 | r.level, |
| 1070 | r.capabilities, |
| 1071 | r.SSID == null ? "" : r.SSID); |
| 1072 | } |
| 1073 | } |
| 1074 | pw.println(); |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1075 | pw.println("Locks acquired: " + mFullLocksAcquired + " full, " + |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1076 | mFullHighPerfLocksAcquired + " full high perf, " + |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1077 | mScanLocksAcquired + " scan"); |
| 1078 | pw.println("Locks released: " + mFullLocksReleased + " full, " + |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1079 | mFullHighPerfLocksReleased + " full high perf, " + |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1080 | mScanLocksReleased + " scan"); |
| 1081 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | pw.println("Locks held:"); |
| 1083 | mLocks.dump(pw); |
Isaac Levy | bc7dfb5 | 2011-06-06 15:34:01 -0700 | [diff] [blame] | 1084 | |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1085 | mWifiWatchdogStateMachine.dump(fd, pw, args); |
Isaac Levy | bc7dfb5 | 2011-06-06 15:34:01 -0700 | [diff] [blame] | 1086 | pw.println(); |
Irfan Sheriff | 6079237 | 2012-04-16 16:47:10 -0700 | [diff] [blame] | 1087 | mWifiStateMachine.dump(fd, pw, args); |
Irfan Sheriff | f0afe41 | 2012-11-30 14:07:44 -0800 | [diff] [blame] | 1088 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1091 | private class WifiLock extends DeathRecipient { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1092 | WifiLock(int lockMode, String tag, IBinder binder, WorkSource ws) { |
| 1093 | super(lockMode, tag, binder, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | public void binderDied() { |
| 1097 | synchronized (mLocks) { |
| 1098 | releaseWifiLockLocked(mBinder); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | public String toString() { |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1103 | return "WifiLock{" + mTag + " type=" + mMode + " binder=" + mBinder + "}"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | private class LockList { |
| 1108 | private List<WifiLock> mList; |
| 1109 | |
| 1110 | private LockList() { |
| 1111 | mList = new ArrayList<WifiLock>(); |
| 1112 | } |
| 1113 | |
| 1114 | private synchronized boolean hasLocks() { |
| 1115 | return !mList.isEmpty(); |
| 1116 | } |
| 1117 | |
| 1118 | private synchronized int getStrongestLockMode() { |
| 1119 | if (mList.isEmpty()) { |
| 1120 | return WifiManager.WIFI_MODE_FULL; |
| 1121 | } |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1122 | |
| 1123 | if (mFullHighPerfLocksAcquired > mFullHighPerfLocksReleased) { |
| 1124 | return WifiManager.WIFI_MODE_FULL_HIGH_PERF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | } |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (mFullLocksAcquired > mFullLocksReleased) { |
| 1128 | return WifiManager.WIFI_MODE_FULL; |
| 1129 | } |
| 1130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | return WifiManager.WIFI_MODE_SCAN_ONLY; |
| 1132 | } |
| 1133 | |
| 1134 | private void addLock(WifiLock lock) { |
| 1135 | if (findLockByBinder(lock.mBinder) < 0) { |
| 1136 | mList.add(lock); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | private WifiLock removeLock(IBinder binder) { |
| 1141 | int index = findLockByBinder(binder); |
| 1142 | if (index >= 0) { |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1143 | WifiLock ret = mList.remove(index); |
| 1144 | ret.unlinkDeathRecipient(); |
| 1145 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | } else { |
| 1147 | return null; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | private int findLockByBinder(IBinder binder) { |
| 1152 | int size = mList.size(); |
| 1153 | for (int i = size - 1; i >= 0; i--) |
| 1154 | if (mList.get(i).mBinder == binder) |
| 1155 | return i; |
| 1156 | return -1; |
| 1157 | } |
| 1158 | |
| 1159 | private void dump(PrintWriter pw) { |
| 1160 | for (WifiLock l : mList) { |
| 1161 | pw.print(" "); |
| 1162 | pw.println(l); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1167 | void enforceWakeSourcePermission(int uid, int pid) { |
Dianne Hackborn | e746f03 | 2010-09-13 16:02:57 -0700 | [diff] [blame] | 1168 | if (uid == android.os.Process.myUid()) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1169 | return; |
| 1170 | } |
| 1171 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 1172 | pid, uid, null); |
| 1173 | } |
| 1174 | |
| 1175 | public boolean acquireWifiLock(IBinder binder, int lockMode, String tag, WorkSource ws) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1177 | if (lockMode != WifiManager.WIFI_MODE_FULL && |
| 1178 | lockMode != WifiManager.WIFI_MODE_SCAN_ONLY && |
| 1179 | lockMode != WifiManager.WIFI_MODE_FULL_HIGH_PERF) { |
| 1180 | Slog.e(TAG, "Illegal argument, lockMode= " + lockMode); |
| 1181 | if (DBG) throw new IllegalArgumentException("lockMode=" + lockMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1184 | if (ws != null && ws.size() == 0) { |
| 1185 | ws = null; |
| 1186 | } |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1187 | if (ws != null) { |
| 1188 | enforceWakeSourcePermission(Binder.getCallingUid(), Binder.getCallingPid()); |
| 1189 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1190 | if (ws == null) { |
| 1191 | ws = new WorkSource(Binder.getCallingUid()); |
| 1192 | } |
| 1193 | WifiLock wifiLock = new WifiLock(lockMode, tag, binder, ws); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | synchronized (mLocks) { |
| 1195 | return acquireWifiLockLocked(wifiLock); |
| 1196 | } |
| 1197 | } |
| 1198 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1199 | private void noteAcquireWifiLock(WifiLock wifiLock) throws RemoteException { |
| 1200 | switch(wifiLock.mMode) { |
| 1201 | case WifiManager.WIFI_MODE_FULL: |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1202 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1203 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 1204 | mBatteryStats.noteFullWifiLockAcquiredFromSource(wifiLock.mWorkSource); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | private void noteReleaseWifiLock(WifiLock wifiLock) throws RemoteException { |
| 1210 | switch(wifiLock.mMode) { |
| 1211 | case WifiManager.WIFI_MODE_FULL: |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1212 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1213 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
Nick Pelly | 6ccaa54 | 2012-06-15 15:22:47 -0700 | [diff] [blame] | 1214 | mBatteryStats.noteFullWifiLockReleasedFromSource(wifiLock.mWorkSource); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1215 | break; |
| 1216 | } |
| 1217 | } |
| 1218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | private boolean acquireWifiLockLocked(WifiLock wifiLock) { |
Irfan Sheriff | c89dd54 | 2010-09-28 08:40:54 -0700 | [diff] [blame] | 1220 | if (DBG) Slog.d(TAG, "acquireWifiLockLocked: " + wifiLock); |
Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | mLocks.addLock(wifiLock); |
Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1223 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1224 | long ident = Binder.clearCallingIdentity(); |
| 1225 | try { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1226 | noteAcquireWifiLock(wifiLock); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1227 | switch(wifiLock.mMode) { |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1228 | case WifiManager.WIFI_MODE_FULL: |
| 1229 | ++mFullLocksAcquired; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1230 | break; |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1231 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| 1232 | ++mFullHighPerfLocksAcquired; |
| 1233 | break; |
| 1234 | |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1235 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1236 | ++mScanLocksAcquired; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1237 | break; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1238 | } |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1239 | |
| 1240 | // Be aggressive about adding new locks into the accounted state... |
| 1241 | // we want to over-report rather than under-report. |
| 1242 | reportStartWorkSource(); |
| 1243 | |
| 1244 | updateWifiState(); |
| 1245 | return true; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1246 | } catch (RemoteException e) { |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1247 | return false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1248 | } finally { |
| 1249 | Binder.restoreCallingIdentity(ident); |
| 1250 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1253 | public void updateWifiLockWorkSource(IBinder lock, WorkSource ws) { |
| 1254 | int uid = Binder.getCallingUid(); |
| 1255 | int pid = Binder.getCallingPid(); |
| 1256 | if (ws != null && ws.size() == 0) { |
| 1257 | ws = null; |
| 1258 | } |
| 1259 | if (ws != null) { |
| 1260 | enforceWakeSourcePermission(uid, pid); |
| 1261 | } |
| 1262 | long ident = Binder.clearCallingIdentity(); |
| 1263 | try { |
| 1264 | synchronized (mLocks) { |
| 1265 | int index = mLocks.findLockByBinder(lock); |
| 1266 | if (index < 0) { |
| 1267 | throw new IllegalArgumentException("Wifi lock not active"); |
| 1268 | } |
| 1269 | WifiLock wl = mLocks.mList.get(index); |
| 1270 | noteReleaseWifiLock(wl); |
| 1271 | wl.mWorkSource = ws != null ? new WorkSource(ws) : new WorkSource(uid); |
| 1272 | noteAcquireWifiLock(wl); |
| 1273 | } |
| 1274 | } catch (RemoteException e) { |
| 1275 | } finally { |
| 1276 | Binder.restoreCallingIdentity(ident); |
| 1277 | } |
| 1278 | } |
| 1279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | public boolean releaseWifiLock(IBinder lock) { |
| 1281 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 1282 | synchronized (mLocks) { |
| 1283 | return releaseWifiLockLocked(lock); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | private boolean releaseWifiLockLocked(IBinder lock) { |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1288 | boolean hadLock; |
Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1289 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1290 | WifiLock wifiLock = mLocks.removeLock(lock); |
Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1291 | |
Irfan Sheriff | c89dd54 | 2010-09-28 08:40:54 -0700 | [diff] [blame] | 1292 | if (DBG) Slog.d(TAG, "releaseWifiLockLocked: " + wifiLock); |
Robert Greenwalt | f1acb2d | 2009-10-13 08:20:55 -0700 | [diff] [blame] | 1293 | |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1294 | hadLock = (wifiLock != null); |
| 1295 | |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1296 | long ident = Binder.clearCallingIdentity(); |
| 1297 | try { |
| 1298 | if (hadLock) { |
Wink Saville | ce0ea1f2b | 2011-10-13 16:55:20 -0700 | [diff] [blame] | 1299 | noteReleaseWifiLock(wifiLock); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1300 | switch(wifiLock.mMode) { |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1301 | case WifiManager.WIFI_MODE_FULL: |
| 1302 | ++mFullLocksReleased; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1303 | break; |
Irfan Sheriff | 5876a42 | 2010-08-12 20:26:23 -0700 | [diff] [blame] | 1304 | case WifiManager.WIFI_MODE_FULL_HIGH_PERF: |
| 1305 | ++mFullHighPerfLocksReleased; |
| 1306 | break; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1307 | case WifiManager.WIFI_MODE_SCAN_ONLY: |
| 1308 | ++mScanLocksReleased; |
Eric Shienbrood | 5711fad | 2009-03-27 20:25:31 -0700 | [diff] [blame] | 1309 | break; |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1310 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1311 | } |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1312 | |
| 1313 | // TODO - should this only happen if you hadLock? |
| 1314 | updateWifiState(); |
| 1315 | |
| 1316 | } catch (RemoteException e) { |
| 1317 | } finally { |
| 1318 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1319 | } |
Dianne Hackborn | ecfd7f7 | 2010-10-08 14:23:40 -0700 | [diff] [blame] | 1320 | |
Eric Shienbrood | d4c5f89 | 2009-03-24 18:13:20 -0700 | [diff] [blame] | 1321 | return hadLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1323 | |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1324 | private abstract class DeathRecipient |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1325 | implements IBinder.DeathRecipient { |
| 1326 | String mTag; |
| 1327 | int mMode; |
| 1328 | IBinder mBinder; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1329 | WorkSource mWorkSource; |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1330 | |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1331 | DeathRecipient(int mode, String tag, IBinder binder, WorkSource ws) { |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1332 | super(); |
| 1333 | mTag = tag; |
| 1334 | mMode = mode; |
| 1335 | mBinder = binder; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1336 | mWorkSource = ws; |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1337 | try { |
| 1338 | mBinder.linkToDeath(this, 0); |
| 1339 | } catch (RemoteException e) { |
| 1340 | binderDied(); |
| 1341 | } |
| 1342 | } |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1343 | |
| 1344 | void unlinkDeathRecipient() { |
| 1345 | mBinder.unlinkToDeath(this, 0); |
| 1346 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1349 | private class Multicaster extends DeathRecipient { |
| 1350 | Multicaster(String tag, IBinder binder) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1351 | super(Binder.getCallingUid(), tag, binder, null); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1355 | Slog.e(TAG, "Multicaster binderDied"); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1356 | synchronized (mMulticasters) { |
| 1357 | int i = mMulticasters.indexOf(this); |
| 1358 | if (i != -1) { |
| 1359 | removeMulticasterLocked(i, mMode); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | public String toString() { |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1365 | return "Multicaster{" + mTag + " binder=" + mBinder + "}"; |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | public int getUid() { |
| 1369 | return mMode; |
| 1370 | } |
| 1371 | } |
| 1372 | |
Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1373 | public void initializeMulticastFiltering() { |
| 1374 | enforceMulticastChangePermission(); |
Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1375 | |
Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1376 | synchronized (mMulticasters) { |
| 1377 | // if anybody had requested filters be off, leave off |
| 1378 | if (mMulticasters.size() != 0) { |
| 1379 | return; |
| 1380 | } else { |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1381 | mWifiStateMachine.startFilteringMulticastV4Packets(); |
Robert Greenwalt | e2d155a | 2009-10-21 14:58:34 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 1386 | public void acquireMulticastLock(IBinder binder, String tag) { |
| 1387 | enforceMulticastChangePermission(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1388 | |
| 1389 | synchronized (mMulticasters) { |
| 1390 | mMulticastEnabled++; |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1391 | mMulticasters.add(new Multicaster(tag, binder)); |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1392 | // Note that we could call stopFilteringMulticastV4Packets only when |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1393 | // our new size == 1 (first call), but this function won't |
| 1394 | // be called often and by making the stopPacket call each |
| 1395 | // time we're less fragile and self-healing. |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1396 | mWifiStateMachine.stopFilteringMulticastV4Packets(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | int uid = Binder.getCallingUid(); |
You Kim | 2bea585 | 2012-10-23 22:56:51 +0900 | [diff] [blame] | 1400 | final long ident = Binder.clearCallingIdentity(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1401 | try { |
| 1402 | mBatteryStats.noteWifiMulticastEnabled(uid); |
| 1403 | } catch (RemoteException e) { |
| 1404 | } finally { |
| 1405 | Binder.restoreCallingIdentity(ident); |
| 1406 | } |
| 1407 | } |
| 1408 | |
Robert Greenwalt | fc1b15c | 2009-05-22 15:09:51 -0700 | [diff] [blame] | 1409 | public void releaseMulticastLock() { |
| 1410 | enforceMulticastChangePermission(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1411 | |
| 1412 | int uid = Binder.getCallingUid(); |
| 1413 | synchronized (mMulticasters) { |
| 1414 | mMulticastDisabled++; |
| 1415 | int size = mMulticasters.size(); |
| 1416 | for (int i = size - 1; i >= 0; i--) { |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1417 | Multicaster m = mMulticasters.get(i); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1418 | if ((m != null) && (m.getUid() == uid)) { |
| 1419 | removeMulticasterLocked(i, uid); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | private void removeMulticasterLocked(int i, int uid) |
| 1426 | { |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1427 | Multicaster removed = mMulticasters.remove(i); |
Irfan Sheriff | a8fbe1f | 2010-03-09 09:13:58 -0800 | [diff] [blame] | 1428 | |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1429 | if (removed != null) { |
| 1430 | removed.unlinkDeathRecipient(); |
| 1431 | } |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1432 | if (mMulticasters.size() == 0) { |
Irfan Sheriff | b0c1b80f | 2011-07-19 15:44:25 -0700 | [diff] [blame] | 1433 | mWifiStateMachine.startFilteringMulticastV4Packets(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
You Kim | 2bea585 | 2012-10-23 22:56:51 +0900 | [diff] [blame] | 1436 | final long ident = Binder.clearCallingIdentity(); |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1437 | try { |
| 1438 | mBatteryStats.noteWifiMulticastDisabled(uid); |
| 1439 | } catch (RemoteException e) { |
| 1440 | } finally { |
| 1441 | Binder.restoreCallingIdentity(ident); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Robert Greenwalt | 58ff021 | 2009-05-19 15:53:54 -0700 | [diff] [blame] | 1445 | public boolean isMulticastEnabled() { |
Robert Greenwalt | 5347bd4 | 2009-05-13 15:10:16 -0700 | [diff] [blame] | 1446 | enforceAccessPermission(); |
| 1447 | |
| 1448 | synchronized (mMulticasters) { |
| 1449 | return (mMulticasters.size() > 0); |
| 1450 | } |
| 1451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | } |