fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1 | /* |
Zhihai Xu | fa0fd39 | 2012-10-23 17:31:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 22 | import android.bluetooth.BluetoothProfile; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 23 | import android.bluetooth.IBluetooth; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 24 | import android.bluetooth.IBluetoothCallback; |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 25 | import android.bluetooth.IBluetoothGatt; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 26 | import android.bluetooth.IBluetoothHeadset; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 27 | import android.bluetooth.IBluetoothManager; |
| 28 | import android.bluetooth.IBluetoothManagerCallback; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 29 | import android.bluetooth.IBluetoothProfileServiceConnection; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 30 | import android.bluetooth.IBluetoothStateChangeCallback; |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 31 | import android.content.ActivityNotFoundException; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 32 | import android.content.BroadcastReceiver; |
| 33 | import android.content.ComponentName; |
| 34 | import android.content.ContentResolver; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| 38 | import android.content.ServiceConnection; |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 39 | import android.content.pm.ApplicationInfo; |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 41 | import android.content.pm.UserInfo; |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 42 | import android.database.ContentObserver; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 43 | import android.os.Binder; |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 44 | import android.os.Build; |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 45 | import android.os.Bundle; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 46 | import android.os.Handler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 47 | import android.os.IBinder; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 48 | import android.os.Looper; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 49 | import android.os.Message; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 50 | import android.os.Process; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 51 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 52 | import android.os.RemoteException; |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 53 | import android.os.ServiceManager; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 54 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 55 | import android.os.UserHandle; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 56 | import android.os.UserManager; |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 57 | import android.os.UserManagerInternal; |
| 58 | import android.os.UserManagerInternal.UserRestrictionsListener; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 59 | import android.provider.Settings; |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 60 | import android.provider.Settings.SettingNotFoundException; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 61 | import android.util.Slog; |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 62 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 63 | import com.android.internal.util.DumpUtils; |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 64 | import com.android.server.pm.PackageManagerService; |
| 65 | |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
| 67 | import java.io.PrintWriter; |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 68 | import java.util.concurrent.ConcurrentHashMap; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 69 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 70 | import java.util.HashMap; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 71 | import java.util.LinkedList; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 72 | import java.util.Map; |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 73 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 74 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 75 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 76 | private static final String TAG = "BluetoothManagerService"; |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 77 | private static final boolean DBG = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 78 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 79 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 80 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 81 | |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 82 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 83 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 84 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 85 | |
| 86 | private static final int ACTIVE_LOG_MAX_SIZE = 20; |
| 87 | private static final int CRASH_LOG_MAX_SIZE = 100; |
Ajay Panicker | 467bc04 | 2017-02-22 12:23:15 -0800 | [diff] [blame] | 88 | private static final String REASON_AIRPLANE_MODE = "airplane mode"; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 89 | private static final String REASON_RESTARTED = "automatic restart"; |
| 90 | private static final String REASON_START_CRASH = "turn-on crash"; |
Ajay Panicker | 467bc04 | 2017-02-22 12:23:15 -0800 | [diff] [blame] | 91 | private static final String REASON_SYSTEM_BOOT = "system boot"; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 92 | private static final String REASON_UNEXPECTED = "unexpected crash"; |
| 93 | private static final String REASON_USER_SWITCH = "user switch"; |
| 94 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 95 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 96 | //Maximum msec to wait for service restart |
| 97 | private static final int SERVICE_RESTART_TIME_MS = 200; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 98 | //Maximum msec to wait for restart due to error |
| 99 | private static final int ERROR_RESTART_TIME_MS = 3000; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 100 | //Maximum msec to delay MESSAGE_USER_SWITCHED |
| 101 | private static final int USER_SWITCHED_TIME_MS = 200; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 102 | // Delay for the addProxy function in msec |
| 103 | private static final int ADD_PROXY_DELAY_MS = 100; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 104 | |
| 105 | private static final int MESSAGE_ENABLE = 1; |
| 106 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 107 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 108 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 109 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 110 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 111 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 112 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 113 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 114 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE = 60; |
| 115 | private static final int MESSAGE_TIMEOUT_BIND = 100; |
| 116 | private static final int MESSAGE_TIMEOUT_UNBIND = 101; |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 117 | private static final int MESSAGE_GET_NAME_AND_ADDRESS = 200; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 118 | private static final int MESSAGE_USER_SWITCHED = 300; |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 119 | private static final int MESSAGE_USER_UNLOCKED = 301; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 120 | private static final int MESSAGE_ADD_PROXY_DELAYED = 400; |
| 121 | private static final int MESSAGE_BIND_PROFILE_SERVICE = 401; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 122 | |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 123 | private static final int MAX_SAVE_RETRIES = 3; |
| 124 | private static final int MAX_ERROR_RESTART_RETRIES = 6; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 125 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 126 | // Bluetooth persisted setting is off |
| 127 | private static final int BLUETOOTH_OFF=0; |
| 128 | // Bluetooth persisted setting is on |
| 129 | // and Airplane mode won't affect Bluetooth state at start up |
| 130 | private static final int BLUETOOTH_ON_BLUETOOTH=1; |
| 131 | // Bluetooth persisted setting is on |
| 132 | // but Airplane mode will affect Bluetooth state at start up |
| 133 | // and Airplane mode will have higher priority. |
| 134 | private static final int BLUETOOTH_ON_AIRPLANE=2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 135 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 136 | private static final int SERVICE_IBLUETOOTH = 1; |
| 137 | private static final int SERVICE_IBLUETOOTHGATT = 2; |
| 138 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 139 | private final Context mContext; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 140 | |
| 141 | // Locks are not provided for mName and mAddress. |
| 142 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 143 | private String mAddress; |
| 144 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 145 | private final ContentResolver mContentResolver; |
| 146 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 147 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 148 | private IBinder mBluetoothBinder; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 149 | private IBluetooth mBluetooth; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 150 | private IBluetoothGatt mBluetoothGatt; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 151 | private final ReentrantReadWriteLock mBluetoothLock = |
| 152 | new ReentrantReadWriteLock(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 153 | private boolean mBinding; |
| 154 | private boolean mUnbinding; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 155 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 156 | // used inside handler thread |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 157 | private boolean mQuietEnable = false; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 158 | private boolean mEnable; |
| 159 | |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 160 | private CharSequence timeToLog(long timestamp) { |
| 161 | return android.text.format.DateFormat.format("MM-dd HH:mm:ss", timestamp); |
| 162 | } |
| 163 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Used for tracking apps that enabled / disabled Bluetooth. |
| 166 | */ |
| 167 | private class ActiveLog { |
| 168 | private String mPackageName; |
| 169 | private boolean mEnable; |
| 170 | private long mTimestamp; |
| 171 | |
| 172 | public ActiveLog(String packageName, boolean enable, long timestamp) { |
| 173 | mPackageName = packageName; |
| 174 | mEnable = enable; |
| 175 | mTimestamp = timestamp; |
| 176 | } |
| 177 | |
| 178 | public long getTime() { |
| 179 | return mTimestamp; |
| 180 | } |
| 181 | |
| 182 | public String toString() { |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 183 | return timeToLog(mTimestamp) + (mEnable ? " Enabled " : " Disabled ") + " by " |
| 184 | + mPackageName; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | private LinkedList<ActiveLog> mActiveLogs; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 190 | private LinkedList<Long> mCrashTimestamps; |
| 191 | private int mCrashes; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 192 | |
| 193 | // configuration from external IBinder call which is used to |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 194 | // synchronize with broadcast receiver. |
| 195 | private boolean mQuietEnableExternal; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 196 | private boolean mEnableExternal; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 197 | |
| 198 | // Map of apps registered to keep BLE scanning on. |
| 199 | private Map<IBinder, ClientDeathRecipient> mBleApps = new ConcurrentHashMap<IBinder, ClientDeathRecipient>(); |
| 200 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 201 | private int mState; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 202 | private final BluetoothHandler mHandler; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 203 | private int mErrorRecoveryRetryCounter; |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 204 | private final int mSystemUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 205 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 206 | // Save a ProfileServiceConnections object for each of the bound |
| 207 | // bluetooth profile services |
| 208 | private final Map <Integer, ProfileServiceConnections> mProfileServices = |
| 209 | new HashMap <Integer, ProfileServiceConnections>(); |
| 210 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 211 | private final boolean mPermissionReviewRequired; |
| 212 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 213 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 216 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 217 | mHandler.sendMessage(msg); |
| 218 | } |
| 219 | }; |
| 220 | |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 221 | private final UserRestrictionsListener mUserRestrictionsListener = |
| 222 | new UserRestrictionsListener() { |
| 223 | @Override |
| 224 | public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, |
| 225 | Bundle prevRestrictions) { |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 226 | if (!newRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH) |
| 227 | && !prevRestrictions.containsKey(UserManager.DISALLOW_BLUETOOTH)) { |
| 228 | // The relevant restriction has not changed - do nothing. |
| 229 | return; |
| 230 | } |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 231 | final boolean bluetoothDisallowed = |
| 232 | newRestrictions.getBoolean(UserManager.DISALLOW_BLUETOOTH); |
| 233 | if ((mEnable || mEnableExternal) && bluetoothDisallowed) { |
| 234 | try { |
| 235 | disable(null, true); |
| 236 | } catch (RemoteException e) { |
| 237 | Slog.w(TAG, "Exception when disabling Bluetooth from UserRestrictionsListener", |
| 238 | e); |
| 239 | } |
| 240 | } |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 241 | updateOppLauncherComponentState(bluetoothDisallowed); |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 242 | } |
| 243 | }; |
| 244 | |
Ajay Panicker | 467bc04 | 2017-02-22 12:23:15 -0800 | [diff] [blame] | 245 | private final ContentObserver mAirplaneModeObserver = new ContentObserver(null) { |
| 246 | @Override |
| 247 | public void onChange(boolean unused) { |
| 248 | synchronized(this) { |
| 249 | if (isBluetoothPersistedStateOn()) { |
| 250 | if (isAirplaneModeOn()) { |
| 251 | persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE); |
| 252 | } else { |
| 253 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | int st = BluetoothAdapter.STATE_OFF; |
| 258 | try { |
| 259 | mBluetoothLock.readLock().lock(); |
| 260 | if (mBluetooth != null) { |
| 261 | st = mBluetooth.getState(); |
| 262 | } |
| 263 | } catch (RemoteException e) { |
| 264 | Slog.e(TAG, "Unable to call getState", e); |
| 265 | return; |
| 266 | } finally { |
| 267 | mBluetoothLock.readLock().unlock(); |
| 268 | } |
| 269 | |
| 270 | Slog.d(TAG, "Airplane Mode change - current state: " + |
| 271 | BluetoothAdapter.nameForState(st)); |
| 272 | |
| 273 | if (isAirplaneModeOn()) { |
| 274 | // Clear registered LE apps to force shut-off |
| 275 | clearBleApps(); |
| 276 | |
| 277 | // If state is BLE_ON make sure we trigger disableBLE |
| 278 | if (st == BluetoothAdapter.STATE_BLE_ON) { |
| 279 | try { |
| 280 | mBluetoothLock.readLock().lock(); |
| 281 | if (mBluetooth != null) { |
| 282 | mBluetooth.onBrEdrDown(); |
| 283 | mEnable = false; |
| 284 | mEnableExternal = false; |
| 285 | } |
| 286 | } catch (RemoteException e) { |
| 287 | Slog.e(TAG,"Unable to call onBrEdrDown", e); |
| 288 | } finally { |
| 289 | mBluetoothLock.readLock().unlock(); |
| 290 | } |
| 291 | } else if (st == BluetoothAdapter.STATE_ON){ |
| 292 | sendDisableMsg(REASON_AIRPLANE_MODE); |
| 293 | } |
| 294 | } else if (mEnableExternal) { |
| 295 | sendEnableMsg(mQuietEnableExternal, REASON_AIRPLANE_MODE); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | }; |
| 300 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 301 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 302 | @Override |
| 303 | public void onReceive(Context context, Intent intent) { |
| 304 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 305 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 306 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 307 | if (DBG) Slog.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 308 | if (newName != null) { |
| 309 | storeNameAndAddress(newName, null); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | BluetoothManagerService(Context context) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 316 | mHandler = new BluetoothHandler(IoThread.get().getLooper()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 317 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 318 | mContext = context; |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 319 | |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 320 | mPermissionReviewRequired = context.getResources().getBoolean( |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 321 | com.android.internal.R.bool.config_permissionReviewRequired); |
| 322 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 323 | mActiveLogs = new LinkedList<ActiveLog>(); |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 324 | mCrashTimestamps = new LinkedList<Long>(); |
| 325 | mCrashes = 0; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 326 | mBluetooth = null; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 327 | mBluetoothBinder = null; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 328 | mBluetoothGatt = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 329 | mBinding = false; |
| 330 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 331 | mEnable = false; |
| 332 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 333 | mQuietEnableExternal = false; |
| 334 | mEnableExternal = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 335 | mAddress = null; |
| 336 | mName = null; |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 337 | mErrorRecoveryRetryCounter = 0; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 338 | mContentResolver = context.getContentResolver(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 339 | // Observe BLE scan only mode settings change. |
| 340 | registerForBleScanModeChange(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 341 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 342 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 343 | IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 344 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 345 | mContext.registerReceiver(mReceiver, filter); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 346 | loadStoredNameAndAddress(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 347 | if (isBluetoothPersistedStateOn()) { |
Marie Janssen | 9fa2491 | 2016-10-18 10:04:24 -0700 | [diff] [blame] | 348 | if (DBG) Slog.d(TAG, "Startup: Bluetooth persisted state is ON."); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 349 | mEnableExternal = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 350 | } |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 351 | |
Ajay Panicker | 467bc04 | 2017-02-22 12:23:15 -0800 | [diff] [blame] | 352 | String airplaneModeRadios = Settings.Global.getString(mContentResolver, |
| 353 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 354 | if (airplaneModeRadios == null || |
| 355 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH)) { |
| 356 | mContentResolver.registerContentObserver( |
| 357 | Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), |
| 358 | true, mAirplaneModeObserver); |
| 359 | } |
| 360 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 361 | int systemUiUid = -1; |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 362 | try { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 363 | systemUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui", |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 364 | PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM); |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 365 | } catch (PackageManager.NameNotFoundException e) { |
Joe LaPenna | acddf2b | 2015-09-04 12:52:42 -0700 | [diff] [blame] | 366 | // Some platforms, such as wearables do not have a system ui. |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 367 | Slog.w(TAG, "Unable to resolve SystemUI's UID.", e); |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 368 | } |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 369 | mSystemUiUid = systemUiUid; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 370 | } |
| 371 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Returns true if airplane mode is currently on |
| 374 | */ |
| 375 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 376 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 377 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Returns true if the Bluetooth saved state is "on" |
| 382 | */ |
| 383 | private final boolean isBluetoothPersistedStateOn() { |
Marie Janssen | 9fa2491 | 2016-10-18 10:04:24 -0700 | [diff] [blame] | 384 | int state = Settings.Global.getInt(mContentResolver, |
| 385 | Settings.Global.BLUETOOTH_ON, -1); |
| 386 | if (DBG) Slog.d(TAG, "Bluetooth persisted state: " + state); |
| 387 | return state != BLUETOOTH_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH |
| 392 | */ |
| 393 | private final boolean isBluetoothPersistedStateOnBluetooth() { |
| 394 | return Settings.Global.getInt(mContentResolver, |
Andre Eisenbach | 8c18431 | 2016-09-06 18:03:10 -0700 | [diff] [blame] | 395 | Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) == BLUETOOTH_ON_BLUETOOTH; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Save the Bluetooth on/off state |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 400 | */ |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 401 | private void persistBluetoothSetting(int value) { |
Marie Janssen | 9fa2491 | 2016-10-18 10:04:24 -0700 | [diff] [blame] | 402 | if (DBG) Slog.d(TAG, "Persisting Bluetooth Setting: " + value); |
Marie Janssen | fa63068 | 2016-12-15 13:51:30 -0800 | [diff] [blame] | 403 | // waive WRITE_SECURE_SETTINGS permission check |
| 404 | long callingIdentity = Binder.clearCallingIdentity(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 405 | Settings.Global.putInt(mContext.getContentResolver(), |
| 406 | Settings.Global.BLUETOOTH_ON, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 407 | value); |
Marie Janssen | fa63068 | 2016-12-15 13:51:30 -0800 | [diff] [blame] | 408 | Binder.restoreCallingIdentity(callingIdentity); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Returns true if the Bluetooth Adapter's name and address is |
| 413 | * locally cached |
| 414 | * @return |
| 415 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 416 | private boolean isNameAndAddressSet() { |
| 417 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 418 | } |
| 419 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 420 | /** |
| 421 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 422 | * in the local cache |
| 423 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 424 | private void loadStoredNameAndAddress() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 425 | if (DBG) Slog.d(TAG, "Loading stored name and address"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 426 | if (mContext.getResources().getBoolean |
| 427 | (com.android.internal.R.bool.config_bluetooth_address_validation) && |
| 428 | Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) { |
| 429 | // if the valid flag is not set, don't load the address and name |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 430 | if (DBG) Slog.d(TAG, "invalid bluetooth name and address stored"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 433 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 434 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 435 | if (DBG) Slog.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 436 | } |
| 437 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 438 | /** |
| 439 | * Save the Bluetooth name and address in the persistent store. |
| 440 | * Only non-null values will be saved. |
| 441 | * @param name |
| 442 | * @param address |
| 443 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 444 | private void storeNameAndAddress(String name, String address) { |
| 445 | if (name != null) { |
| 446 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 447 | mName = name; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 448 | if (DBG) Slog.d(TAG,"Stored Bluetooth name: " + |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 449 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (address != null) { |
| 453 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 454 | mAddress=address; |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 455 | if (DBG) Slog.d(TAG,"Stored Bluetoothaddress: " + |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 456 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 457 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 458 | |
| 459 | if ((name != null) && (address != null)) { |
| 460 | Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1); |
| 461 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 465 | if (callback == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 466 | Slog.w(TAG, "Callback is null in registerAdapter"); |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 467 | return null; |
| 468 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 469 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 470 | msg.obj = callback; |
| 471 | mHandler.sendMessage(msg); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 472 | |
| 473 | return mBluetooth; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 477 | if (callback == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 478 | Slog.w(TAG, "Callback is null in unregisterAdapter"); |
Natalie Silvanovich | 55db646 | 2014-05-01 16:12:23 -0700 | [diff] [blame] | 479 | return; |
| 480 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 481 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 482 | "Need BLUETOOTH permission"); |
| 483 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 484 | msg.obj = callback; |
| 485 | mHandler.sendMessage(msg); |
| 486 | } |
| 487 | |
| 488 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 489 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 490 | "Need BLUETOOTH permission"); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 491 | if (callback == null) { |
| 492 | Slog.w(TAG, "registerStateChangeCallback: Callback is null!"); |
| 493 | return; |
| 494 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 495 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 496 | msg.obj = callback; |
| 497 | mHandler.sendMessage(msg); |
| 498 | } |
| 499 | |
| 500 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 501 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 502 | "Need BLUETOOTH permission"); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 503 | if (callback == null) { |
| 504 | Slog.w(TAG, "unregisterStateChangeCallback: Callback is null!"); |
| 505 | return; |
| 506 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 507 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 508 | msg.obj = callback; |
| 509 | mHandler.sendMessage(msg); |
| 510 | } |
| 511 | |
| 512 | public boolean isEnabled() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 513 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 514 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 515 | Slog.w(TAG,"isEnabled(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 519 | try { |
| 520 | mBluetoothLock.readLock().lock(); |
| 521 | if (mBluetooth != null) return mBluetooth.isEnabled(); |
| 522 | } catch (RemoteException e) { |
| 523 | Slog.e(TAG, "isEnabled()", e); |
| 524 | } finally { |
| 525 | mBluetoothLock.readLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 526 | } |
| 527 | return false; |
| 528 | } |
| 529 | |
Christine Hallstrom | 995c90a | 2016-05-25 15:49:08 -0700 | [diff] [blame] | 530 | public int getState() { |
| 531 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 532 | (!checkIfCallerIsForegroundUser())) { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 533 | Slog.w(TAG, "getState(): report OFF for non-active and non system user"); |
Christine Hallstrom | 995c90a | 2016-05-25 15:49:08 -0700 | [diff] [blame] | 534 | return BluetoothAdapter.STATE_OFF; |
| 535 | } |
| 536 | |
| 537 | try { |
| 538 | mBluetoothLock.readLock().lock(); |
| 539 | if (mBluetooth != null) return mBluetooth.getState(); |
| 540 | } catch (RemoteException e) { |
| 541 | Slog.e(TAG, "getState()", e); |
| 542 | } finally { |
| 543 | mBluetoothLock.readLock().unlock(); |
| 544 | } |
| 545 | return BluetoothAdapter.STATE_OFF; |
| 546 | } |
| 547 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 548 | class ClientDeathRecipient implements IBinder.DeathRecipient { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 549 | private String mPackageName; |
| 550 | |
| 551 | public ClientDeathRecipient(String packageName) { |
| 552 | mPackageName = packageName; |
| 553 | } |
| 554 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 555 | public void binderDied() { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 556 | if (DBG) Slog.d(TAG, "Binder is dead - unregister " + mPackageName); |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 557 | if (isBleAppPresent()) { |
| 558 | // Nothing to do, another app is here. |
| 559 | return; |
| 560 | } |
| 561 | if (DBG) Slog.d(TAG, "Disabling LE only mode after application crash"); |
| 562 | try { |
| 563 | mBluetoothLock.readLock().lock(); |
| 564 | if (mBluetooth != null && |
| 565 | mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) { |
| 566 | mEnable = false; |
| 567 | mBluetooth.onBrEdrDown(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 568 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 569 | } catch (RemoteException e) { |
| 570 | Slog.e(TAG,"Unable to call onBrEdrDown", e); |
| 571 | } finally { |
| 572 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 575 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 576 | public String getPackageName() { |
| 577 | return mPackageName; |
| 578 | } |
| 579 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 580 | |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 581 | @Override |
| 582 | public boolean isBleScanAlwaysAvailable() { |
Marie Janssen | a80d745 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 583 | if (isAirplaneModeOn() && !mEnable) { |
| 584 | return false; |
| 585 | } |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 586 | try { |
| 587 | return (Settings.Global.getInt(mContentResolver, |
| 588 | Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE)) != 0; |
| 589 | } catch (SettingNotFoundException e) { |
| 590 | } |
| 591 | return false; |
| 592 | } |
| 593 | |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 594 | // Monitor change of BLE scan only mode settings. |
| 595 | private void registerForBleScanModeChange() { |
| 596 | ContentObserver contentObserver = new ContentObserver(null) { |
| 597 | @Override |
| 598 | public void onChange(boolean selfChange) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 599 | if (isBleScanAlwaysAvailable()) { |
| 600 | // Nothing to do |
| 601 | return; |
| 602 | } |
| 603 | // BLE scan is not available. |
| 604 | disableBleScanMode(); |
| 605 | clearBleApps(); |
| 606 | try { |
| 607 | mBluetoothLock.readLock().lock(); |
| 608 | if (mBluetooth != null) mBluetooth.onBrEdrDown(); |
| 609 | } catch (RemoteException e) { |
| 610 | Slog.e(TAG, "error when disabling bluetooth", e); |
| 611 | } finally { |
| 612 | mBluetoothLock.readLock().unlock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | mContentResolver.registerContentObserver( |
| 618 | Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE), |
| 619 | false, contentObserver); |
| 620 | } |
| 621 | |
| 622 | // Disable ble scan only mode. |
| 623 | private void disableBleScanMode() { |
| 624 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 625 | mBluetoothLock.writeLock().lock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 626 | if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 627 | if (DBG) Slog.d(TAG, "Reseting the mEnable flag for clean disable"); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 628 | mEnable = false; |
| 629 | } |
| 630 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 631 | Slog.e(TAG, "getState()", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 632 | } finally { |
| 633 | mBluetoothLock.writeLock().unlock(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 637 | public int updateBleAppCount(IBinder token, boolean enable, String packageName) { |
| 638 | ClientDeathRecipient r = mBleApps.get(token); |
| 639 | if (r == null && enable) { |
| 640 | ClientDeathRecipient deathRec = new ClientDeathRecipient(packageName); |
| 641 | try { |
| 642 | token.linkToDeath(deathRec, 0); |
| 643 | } catch (RemoteException ex) { |
| 644 | throw new IllegalArgumentException("BLE app (" + packageName + ") already dead!"); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 645 | } |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 646 | mBleApps.put(token, deathRec); |
| 647 | if (DBG) Slog.d(TAG, "Registered for death of " + packageName); |
| 648 | } else if (!enable && r != null) { |
| 649 | // Unregister death recipient as the app goes away. |
| 650 | token.unlinkToDeath(r, 0); |
| 651 | mBleApps.remove(token); |
| 652 | if (DBG) Slog.d(TAG, "Unregistered for death of " + packageName); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 653 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 654 | int appCount = mBleApps.size(); |
| 655 | if (DBG) Slog.d(TAG, appCount + " registered Ble Apps"); |
| 656 | if (appCount == 0 && mEnable) { |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 657 | disableBleScanMode(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 658 | } |
Martin Brabham | c2b0622 | 2017-02-27 16:55:07 -0800 | [diff] [blame] | 659 | if (appCount == 0 && !mEnableExternal) { |
| 660 | sendBrEdrDownCallback(); |
| 661 | } |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 662 | return appCount; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 665 | // Clear all apps using BLE scan only mode. |
| 666 | private void clearBleApps() { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 667 | mBleApps.clear(); |
Wei Wang | e4a744b | 2015-06-11 17:50:29 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 670 | /** @hide */ |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 671 | public boolean isBleAppPresent() { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 672 | if (DBG) Slog.d(TAG, "isBleAppPresent() count: " + mBleApps.size()); |
| 673 | return mBleApps.size() > 0; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
Marie Janssen | fa63068 | 2016-12-15 13:51:30 -0800 | [diff] [blame] | 677 | * Action taken when GattService is turned on |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 678 | */ |
| 679 | private void onBluetoothGattServiceUp() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 680 | if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 681 | try { |
| 682 | mBluetoothLock.readLock().lock(); |
Marie Janssen | fa63068 | 2016-12-15 13:51:30 -0800 | [diff] [blame] | 683 | if (mBluetooth == null) { |
| 684 | if (DBG) Slog.w(TAG, "onBluetoothServiceUp: mBluetooth is null!"); |
| 685 | return; |
| 686 | } |
| 687 | int st = mBluetooth.getState(); |
| 688 | if (st != BluetoothAdapter.STATE_BLE_ON) { |
| 689 | if (DBG) Slog.v(TAG, "onBluetoothServiceUp: state isn't BLE_ON: " + |
| 690 | BluetoothAdapter.nameForState(st)); |
| 691 | return; |
| 692 | } |
| 693 | if (isBluetoothPersistedStateOnBluetooth() || !isBleAppPresent()) { |
| 694 | // This triggers transition to STATE_ON |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 695 | mBluetooth.onLeServiceUp(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 696 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 697 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 698 | } catch (RemoteException e) { |
| 699 | Slog.e(TAG,"Unable to call onServiceUp", e); |
| 700 | } finally { |
| 701 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Inform BluetoothAdapter instances that BREDR part is down |
| 707 | * and turn off all service and stack if no LE app needs it |
| 708 | */ |
| 709 | private void sendBrEdrDownCallback() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 710 | if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks"); |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 711 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 712 | if (mBluetooth == null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 713 | Slog.w(TAG, "Bluetooth handle is null"); |
Nitin Arora | bdfaa7f | 2015-04-29 12:35:03 -0700 | [diff] [blame] | 714 | return; |
| 715 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 716 | |
Martin Brabham | c2b0622 | 2017-02-27 16:55:07 -0800 | [diff] [blame] | 717 | if (isBleAppPresent()) { |
| 718 | // Need to stay at BLE ON. Disconnect all Gatt connections |
| 719 | try { |
| 720 | mBluetoothGatt.unregAll(); |
| 721 | } catch (RemoteException e) { |
| 722 | Slog.e(TAG, "Unable to disconnect all apps.", e); |
| 723 | } |
| 724 | } else { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 725 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 726 | mBluetoothLock.readLock().lock(); |
| 727 | if (mBluetooth != null) mBluetooth.onBrEdrDown(); |
| 728 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 729 | Slog.e(TAG, "Call to onBrEdrDown() failed.", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 730 | } finally { |
| 731 | mBluetoothLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 732 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 733 | } |
Martin Brabham | c2b0622 | 2017-02-27 16:55:07 -0800 | [diff] [blame] | 734 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 737 | public boolean enableNoAutoConnect(String packageName) |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 738 | { |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 739 | if (isBluetoothDisallowed()) { |
| 740 | if (DBG) { |
| 741 | Slog.d(TAG, "enableNoAutoConnect(): not enabling - bluetooth disallowed"); |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 746 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 747 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 748 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 749 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 750 | Slog.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 751 | " mBinding = " + mBinding); |
| 752 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 753 | int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); |
| 754 | |
| 755 | if (callingAppId != Process.NFC_UID) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 756 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 757 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 758 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 759 | synchronized(mReceiver) { |
| 760 | mQuietEnableExternal = true; |
| 761 | mEnableExternal = true; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 762 | sendEnableMsg(true, packageName); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 763 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 764 | return true; |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 765 | } |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 766 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 767 | public boolean enable(String packageName) throws RemoteException { |
| 768 | final int callingUid = Binder.getCallingUid(); |
| 769 | final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID; |
| 770 | |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 771 | if (isBluetoothDisallowed()) { |
| 772 | if (DBG) { |
| 773 | Slog.d(TAG,"enable(): not enabling - bluetooth disallowed"); |
| 774 | } |
| 775 | return false; |
| 776 | } |
| 777 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 778 | if (!callerSystem) { |
| 779 | if (!checkIfCallerIsForegroundUser()) { |
| 780 | Slog.w(TAG, "enable(): not allowed for non-active and non system user"); |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 785 | "Need BLUETOOTH ADMIN permission"); |
| 786 | |
| 787 | if (!isEnabled() && mPermissionReviewRequired |
| 788 | && startConsentUiIfNeeded(packageName, callingUid, |
| 789 | BluetoothAdapter.ACTION_REQUEST_ENABLE)) { |
| 790 | return false; |
| 791 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 794 | if (DBG) { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 795 | Slog.d(TAG,"enable(" + packageName + "): mBluetooth =" + mBluetooth + |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 796 | " mBinding = " + mBinding + " mState = " + |
| 797 | BluetoothAdapter.nameForState(mState)); |
Sanket Agarwal | 090bf55 | 2016-04-21 14:10:55 -0700 | [diff] [blame] | 798 | } |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 799 | |
| 800 | synchronized(mReceiver) { |
| 801 | mQuietEnableExternal = false; |
| 802 | mEnableExternal = true; |
| 803 | // waive WRITE_SECURE_SETTINGS permission check |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 804 | sendEnableMsg(false, packageName); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 805 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 806 | if (DBG) Slog.d(TAG, "enable returning"); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 807 | return true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 810 | public boolean disable(String packageName, boolean persist) throws RemoteException { |
| 811 | final int callingUid = Binder.getCallingUid(); |
| 812 | final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 813 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 814 | if (!callerSystem) { |
| 815 | if (!checkIfCallerIsForegroundUser()) { |
| 816 | Slog.w(TAG, "disable(): not allowed for non-active and non system user"); |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 821 | "Need BLUETOOTH ADMIN permission"); |
| 822 | |
| 823 | if (isEnabled() && mPermissionReviewRequired |
| 824 | && startConsentUiIfNeeded(packageName, callingUid, |
| 825 | BluetoothAdapter.ACTION_REQUEST_DISABLE)) { |
| 826 | return false; |
| 827 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 828 | } |
| 829 | |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 830 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 831 | Slog.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 832 | " mBinding = " + mBinding); |
| 833 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 834 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 835 | synchronized(mReceiver) { |
| 836 | if (persist) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 837 | persistBluetoothSetting(BLUETOOTH_OFF); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 838 | } |
| 839 | mEnableExternal = false; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 840 | sendDisableMsg(packageName); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 841 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 842 | return true; |
| 843 | } |
| 844 | |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 845 | private boolean startConsentUiIfNeeded(String packageName, |
| 846 | int callingUid, String intentAction) throws RemoteException { |
| 847 | try { |
| 848 | // Validate the package only if we are going to use it |
| 849 | ApplicationInfo applicationInfo = mContext.getPackageManager() |
| 850 | .getApplicationInfoAsUser(packageName, |
| 851 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 852 | UserHandle.getUserId(callingUid)); |
| 853 | if (applicationInfo.uid != callingUid) { |
| 854 | throw new SecurityException("Package " + callingUid |
| 855 | + " not in uid " + callingUid); |
| 856 | } |
| 857 | |
Ivan Podogov | d2d32b1 | 2016-12-05 16:46:52 +0000 | [diff] [blame] | 858 | Intent intent = new Intent(intentAction); |
Ivan Podogov | 1ab8725 | 2017-01-03 12:02:18 +0000 | [diff] [blame] | 859 | intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName); |
| 860 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 861 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 862 | try { |
| 863 | mContext.startActivity(intent); |
| 864 | } catch (ActivityNotFoundException e) { |
| 865 | // Shouldn't happen |
| 866 | Slog.e(TAG, "Intent to handle action " + intentAction + " missing"); |
| 867 | return false; |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 868 | } |
Ivan Podogov | 1ab8725 | 2017-01-03 12:02:18 +0000 | [diff] [blame] | 869 | return true; |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 870 | } catch (PackageManager.NameNotFoundException e) { |
| 871 | throw new RemoteException(e.getMessage()); |
| 872 | } |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 873 | } |
| 874 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 875 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 876 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 877 | Slog.d(TAG,"unbindAndFinish(): " + mBluetooth + |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 878 | " mBinding = " + mBinding + " mUnbinding = " + mUnbinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 881 | try { |
| 882 | mBluetoothLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 883 | if (mUnbinding) return; |
| 884 | mUnbinding = true; |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 885 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
Pavlin Radoslavov | 74f60c0 | 2016-09-21 17:28:11 -0700 | [diff] [blame] | 886 | mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 887 | if (mBluetooth != null) { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 888 | //Unregister callback object |
| 889 | try { |
| 890 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 891 | } catch (RemoteException re) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 892 | Slog.e(TAG, "Unable to unregister BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 893 | } |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 894 | mBluetoothBinder = null; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 895 | mBluetooth = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 896 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 897 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 898 | mBinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 899 | } else { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 900 | mUnbinding = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 901 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 902 | mBluetoothGatt = null; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 903 | } finally { |
| 904 | mBluetoothLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 908 | public IBluetoothGatt getBluetoothGatt() { |
| 909 | // sync protection |
| 910 | return mBluetoothGatt; |
| 911 | } |
| 912 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 913 | @Override |
| 914 | public boolean bindBluetoothProfileService(int bluetoothProfile, |
| 915 | IBluetoothProfileServiceConnection proxy) { |
| 916 | if (!mEnable) { |
| 917 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 918 | Slog.d(TAG, "Trying to bind to profile: " + bluetoothProfile + |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 919 | ", while Bluetooth was disabled"); |
| 920 | } |
| 921 | return false; |
| 922 | } |
| 923 | synchronized (mProfileServices) { |
| 924 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 925 | if (psc == null) { |
| 926 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 927 | Slog.d(TAG, "Creating new ProfileServiceConnections object for" |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 928 | + " profile: " + bluetoothProfile); |
| 929 | } |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 930 | |
| 931 | if (bluetoothProfile != BluetoothProfile.HEADSET) return false; |
| 932 | |
| 933 | Intent intent = new Intent(IBluetoothHeadset.class.getName()); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 934 | psc = new ProfileServiceConnections(intent); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 935 | if (!psc.bindService()) return false; |
| 936 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 937 | mProfileServices.put(new Integer(bluetoothProfile), psc); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
| 941 | // Introducing a delay to give the client app time to prepare |
| 942 | Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED); |
| 943 | addProxyMsg.arg1 = bluetoothProfile; |
| 944 | addProxyMsg.obj = proxy; |
| 945 | mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS); |
| 946 | return true; |
| 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public void unbindBluetoothProfileService(int bluetoothProfile, |
| 951 | IBluetoothProfileServiceConnection proxy) { |
| 952 | synchronized (mProfileServices) { |
| 953 | ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile)); |
| 954 | if (psc == null) { |
| 955 | return; |
| 956 | } |
| 957 | psc.removeProxy(proxy); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | private void unbindAllBluetoothProfileServices() { |
| 962 | synchronized (mProfileServices) { |
| 963 | for (Integer i : mProfileServices.keySet()) { |
| 964 | ProfileServiceConnections psc = mProfileServices.get(i); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 965 | try { |
| 966 | mContext.unbindService(psc); |
| 967 | } catch (IllegalArgumentException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 968 | Slog.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 969 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 970 | psc.removeAllProxies(); |
| 971 | } |
| 972 | mProfileServices.clear(); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /** |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 977 | * Send enable message and set adapter name and address. Called when the boot phase becomes |
| 978 | * PHASE_SYSTEM_SERVICES_READY. |
| 979 | */ |
| 980 | public void handleOnBootPhase() { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 981 | if (DBG) Slog.d(TAG, "Bluetooth boot completed"); |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 982 | UserManagerInternal userManagerInternal = |
| 983 | LocalServices.getService(UserManagerInternal.class); |
| 984 | userManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener); |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 985 | final boolean isBluetoothDisallowed = isBluetoothDisallowed(); |
| 986 | PackageManagerService packageManagerService = |
| 987 | (PackageManagerService) ServiceManager.getService("package"); |
| 988 | if (packageManagerService != null && !packageManagerService.isOnlyCoreApps()) { |
| 989 | updateOppLauncherComponentState(isBluetoothDisallowed); |
| 990 | } |
| 991 | if (isBluetoothDisallowed) { |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 992 | return; |
| 993 | } |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 994 | if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 995 | if (DBG) Slog.d(TAG, "Auto-enabling Bluetooth."); |
Ajay Panicker | 467bc04 | 2017-02-22 12:23:15 -0800 | [diff] [blame] | 996 | sendEnableMsg(mQuietEnableExternal, REASON_SYSTEM_BOOT); |
Ajay Panicker | bf796d8 | 2016-03-11 13:47:20 -0800 | [diff] [blame] | 997 | } else if (!isNameAndAddressSet()) { |
| 998 | if (DBG) Slog.d(TAG, "Getting adapter name and address"); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 999 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 1000 | mHandler.sendMessage(getMsg); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 1001 | } |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Called when switching to a different foreground user. |
| 1006 | */ |
| 1007 | public void handleOnSwitchUser(int userHandle) { |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1008 | if (DBG) Slog.d(TAG, "User " + userHandle + " switched"); |
| 1009 | mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0).sendToTarget(); |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Called when user is unlocked. |
| 1014 | */ |
| 1015 | public void handleOnUnlockUser(int userHandle) { |
| 1016 | if (DBG) Slog.d(TAG, "User " + userHandle + " unlocked"); |
| 1017 | mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle, 0).sendToTarget(); |
Miao Chou | 658bf2f | 2015-06-26 17:14:35 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /** |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1021 | * This class manages the clients connected to a given ProfileService |
| 1022 | * and maintains the connection with that service. |
| 1023 | */ |
| 1024 | final private class ProfileServiceConnections implements ServiceConnection, |
| 1025 | IBinder.DeathRecipient { |
| 1026 | final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies = |
| 1027 | new RemoteCallbackList <IBluetoothProfileServiceConnection>(); |
| 1028 | IBinder mService; |
| 1029 | ComponentName mClassName; |
| 1030 | Intent mIntent; |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1031 | boolean mInvokingProxyCallbacks = false; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1032 | |
| 1033 | ProfileServiceConnections(Intent intent) { |
| 1034 | mService = null; |
| 1035 | mClassName = null; |
| 1036 | mIntent = intent; |
| 1037 | } |
| 1038 | |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 1039 | private boolean bindService() { |
| 1040 | if (mIntent != null && mService == null && |
| 1041 | doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) { |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1042 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 1043 | msg.obj = this; |
| 1044 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 1045 | return true; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1046 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1047 | Slog.w(TAG, "Unable to bind with intent: " + mIntent); |
Benjamin Franz | 5b61459 | 2014-12-09 18:58:45 +0000 | [diff] [blame] | 1048 | return false; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | private void addProxy(IBluetoothProfileServiceConnection proxy) { |
| 1052 | mProxies.register(proxy); |
| 1053 | if (mService != null) { |
| 1054 | try{ |
| 1055 | proxy.onServiceConnected(mClassName, mService); |
| 1056 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1057 | Slog.e(TAG, "Unable to connect to proxy", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } else { |
| 1060 | if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) { |
| 1061 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 1062 | msg.obj = this; |
| 1063 | mHandler.sendMessage(msg); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | private void removeProxy(IBluetoothProfileServiceConnection proxy) { |
| 1069 | if (proxy != null) { |
| 1070 | if (mProxies.unregister(proxy)) { |
| 1071 | try { |
| 1072 | proxy.onServiceDisconnected(mClassName); |
| 1073 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1074 | Slog.e(TAG, "Unable to disconnect proxy", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } else { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1078 | Slog.w(TAG, "Trying to remove a null proxy"); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | private void removeAllProxies() { |
| 1083 | onServiceDisconnected(mClassName); |
| 1084 | mProxies.kill(); |
| 1085 | } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 1089 | // remove timeout message |
| 1090 | mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this); |
| 1091 | mService = service; |
| 1092 | mClassName = className; |
| 1093 | try { |
| 1094 | mService.linkToDeath(this, 0); |
| 1095 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1096 | Slog.e(TAG, "Unable to linkToDeath", e); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1097 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1098 | |
| 1099 | if (mInvokingProxyCallbacks) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1100 | Slog.e(TAG, "Proxy callbacks already in progress."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1101 | return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1102 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1103 | mInvokingProxyCallbacks = true; |
| 1104 | |
| 1105 | final int n = mProxies.beginBroadcast(); |
| 1106 | try { |
| 1107 | for (int i = 0; i < n; i++) { |
| 1108 | try { |
| 1109 | mProxies.getBroadcastItem(i).onServiceConnected(className, service); |
| 1110 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1111 | Slog.e(TAG, "Unable to connect to proxy", e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } finally { |
| 1115 | mProxies.finishBroadcast(); |
| 1116 | mInvokingProxyCallbacks = false; |
| 1117 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | @Override |
| 1121 | public void onServiceDisconnected(ComponentName className) { |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1122 | if (mService == null) return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1123 | mService.unlinkToDeath(this, 0); |
| 1124 | mService = null; |
| 1125 | mClassName = null; |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (mInvokingProxyCallbacks) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1128 | Slog.e(TAG, "Proxy callbacks already in progress."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1129 | return; |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1130 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1131 | mInvokingProxyCallbacks = true; |
| 1132 | |
| 1133 | final int n = mProxies.beginBroadcast(); |
| 1134 | try { |
| 1135 | for (int i = 0; i < n; i++) { |
| 1136 | try { |
| 1137 | mProxies.getBroadcastItem(i).onServiceDisconnected(className); |
| 1138 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1139 | Slog.e(TAG, "Unable to disconnect from proxy", e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } finally { |
| 1143 | mProxies.finishBroadcast(); |
| 1144 | mInvokingProxyCallbacks = false; |
| 1145 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | @Override |
| 1149 | public void binderDied() { |
| 1150 | if (DBG) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1151 | Slog.w(TAG, "Profile service for profile: " + mClassName |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1152 | + " died."); |
| 1153 | } |
| 1154 | onServiceDisconnected(mClassName); |
| 1155 | // Trigger rebind |
| 1156 | Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE); |
| 1157 | msg.obj = this; |
| 1158 | mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS); |
| 1159 | } |
| 1160 | } |
| 1161 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1162 | private void sendBluetoothStateCallback(boolean isUp) { |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1163 | try { |
| 1164 | int n = mStateChangeCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1165 | if (DBG) Slog.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1166 | for (int i=0; i <n;i++) { |
| 1167 | try { |
| 1168 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 1169 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1170 | Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1171 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1172 | } |
Andre Eisenbach | 3bf1ac5 | 2015-07-30 08:59:32 -0700 | [diff] [blame] | 1173 | } finally { |
| 1174 | mStateChangeCallbacks.finishBroadcast(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1175 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /** |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1179 | * Inform BluetoothAdapter instances that Adapter service is up |
| 1180 | */ |
| 1181 | private void sendBluetoothServiceUpCallback() { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1182 | try { |
| 1183 | int n = mCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1184 | Slog.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1185 | for (int i=0; i <n;i++) { |
| 1186 | try { |
| 1187 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 1188 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1189 | Slog.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1192 | } finally { |
| 1193 | mCallbacks.finishBroadcast(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | /** |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1197 | * Inform BluetoothAdapter instances that Adapter service is down |
| 1198 | */ |
| 1199 | private void sendBluetoothServiceDownCallback() { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1200 | try { |
| 1201 | int n = mCallbacks.beginBroadcast(); |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1202 | Slog.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1203 | for (int i=0; i <n;i++) { |
| 1204 | try { |
| 1205 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 1206 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1207 | Slog.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1208 | } |
| 1209 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1210 | } finally { |
| 1211 | mCallbacks.finishBroadcast(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1212 | } |
| 1213 | } |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1214 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1215 | public String getAddress() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 1216 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1217 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1218 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1219 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1220 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1221 | Slog.w(TAG,"getAddress(): not allowed for non-active and non system user"); |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1222 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 1225 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS) |
| 1226 | != PackageManager.PERMISSION_GRANTED) { |
| 1227 | return BluetoothAdapter.DEFAULT_MAC_ADDRESS; |
| 1228 | } |
| 1229 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1230 | try { |
| 1231 | mBluetoothLock.readLock().lock(); |
| 1232 | if (mBluetooth != null) return mBluetooth.getAddress(); |
| 1233 | } catch (RemoteException e) { |
| 1234 | Slog.e(TAG, "getAddress(): Unable to retrieve address remotely. Returning cached address", e); |
| 1235 | } finally { |
| 1236 | mBluetoothLock.readLock().unlock(); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 1237 | } |
Ajay Panicker | bf796d8 | 2016-03-11 13:47:20 -0800 | [diff] [blame] | 1238 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1239 | // mAddress is accessed from outside. |
| 1240 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 1241 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1242 | return mAddress; |
| 1243 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1244 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1245 | public String getName() { |
Matthew Xie | af5ddbf | 2012-12-04 10:47:43 -0800 | [diff] [blame] | 1246 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 1247 | "Need BLUETOOTH permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1248 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1249 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 1250 | (!checkIfCallerIsForegroundUser())) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1251 | Slog.w(TAG,"getName(): not allowed for non-active and non system user"); |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 1252 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1255 | try { |
| 1256 | mBluetoothLock.readLock().lock(); |
| 1257 | if (mBluetooth != null) return mBluetooth.getName(); |
| 1258 | } catch (RemoteException e) { |
| 1259 | Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e); |
| 1260 | } finally { |
| 1261 | mBluetoothLock.readLock().unlock(); |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 1262 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1263 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1264 | // mName is accessed from outside. |
| 1265 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 1266 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1267 | return mName; |
| 1268 | } |
| 1269 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1270 | private class BluetoothServiceConnection implements ServiceConnection { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1271 | public void onServiceConnected(ComponentName componentName, IBinder service) { |
| 1272 | String name = componentName.getClassName(); |
| 1273 | if (DBG) Slog.d(TAG, "BluetoothServiceConnection: " + name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1274 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1275 | if (name.equals("com.android.bluetooth.btservice.AdapterService")) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1276 | msg.arg1 = SERVICE_IBLUETOOTH; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1277 | } else if (name.equals("com.android.bluetooth.gatt.GattService")) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1278 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 1279 | } else { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1280 | Slog.e(TAG, "Unknown service connected: " + name); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1281 | return; |
| 1282 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1283 | msg.obj = service; |
| 1284 | mHandler.sendMessage(msg); |
| 1285 | } |
| 1286 | |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1287 | public void onServiceDisconnected(ComponentName componentName) { |
| 1288 | // Called if we unexpectedly disconnect. |
| 1289 | String name = componentName.getClassName(); |
| 1290 | if (DBG) Slog.d(TAG, "BluetoothServiceConnection, disconnected: " + name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1291 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1292 | if (name.equals("com.android.bluetooth.btservice.AdapterService")) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1293 | msg.arg1 = SERVICE_IBLUETOOTH; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1294 | } else if (name.equals("com.android.bluetooth.gatt.GattService")) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1295 | msg.arg1 = SERVICE_IBLUETOOTHGATT; |
| 1296 | } else { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1297 | Slog.e(TAG, "Unknown service disconnected: " + name); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1298 | return; |
| 1299 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1300 | mHandler.sendMessage(msg); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 1305 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1306 | private class BluetoothHandler extends Handler { |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1307 | boolean mGetNameAddressOnly = false; |
| 1308 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1309 | public BluetoothHandler(Looper looper) { |
| 1310 | super(looper); |
| 1311 | } |
| 1312 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1313 | @Override |
| 1314 | public void handleMessage(Message msg) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1315 | switch (msg.what) { |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1316 | case MESSAGE_GET_NAME_AND_ADDRESS: |
| 1317 | if (DBG) Slog.d(TAG, "MESSAGE_GET_NAME_AND_ADDRESS"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1318 | try { |
| 1319 | mBluetoothLock.writeLock().lock(); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1320 | if ((mBluetooth == null) && (!mBinding)) { |
| 1321 | if (DBG) Slog.d(TAG, "Binding to service to get name and address"); |
| 1322 | mGetNameAddressOnly = true; |
| 1323 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1324 | mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS); |
| 1325 | Intent i = new Intent(IBluetooth.class.getName()); |
| 1326 | if (!doBind(i, mConnection, |
| 1327 | Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1328 | UserHandle.CURRENT)) { |
| 1329 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 1330 | } else { |
| 1331 | mBinding = true; |
| 1332 | } |
| 1333 | } else if (mBluetooth != null) { |
| 1334 | try { |
| 1335 | storeNameAndAddress(mBluetooth.getName(), |
| 1336 | mBluetooth.getAddress()); |
| 1337 | } catch (RemoteException re) { |
| 1338 | Slog.e(TAG, "Unable to grab names", re); |
| 1339 | } |
| 1340 | if (mGetNameAddressOnly && !mEnable) { |
| 1341 | unbindAndFinish(); |
| 1342 | } |
| 1343 | mGetNameAddressOnly = false; |
| 1344 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1345 | } finally { |
| 1346 | mBluetoothLock.writeLock().unlock(); |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1347 | } |
| 1348 | break; |
| 1349 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1350 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 1351 | if (DBG) { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1352 | Slog.d(TAG, "MESSAGE_ENABLE(" + msg.arg1 + "): mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1353 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1354 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1355 | mEnable = true; |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1356 | |
| 1357 | // Use service interface to get the exact state |
| 1358 | try { |
| 1359 | mBluetoothLock.readLock().lock(); |
| 1360 | if (mBluetooth != null) { |
| 1361 | int state = mBluetooth.getState(); |
| 1362 | if (state == BluetoothAdapter.STATE_BLE_ON) { |
Marie Janssen | e0bfa2e | 2016-12-20 11:21:12 -0800 | [diff] [blame] | 1363 | Slog.w(TAG, "BT Enable in BLE_ON State, going to ON"); |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1364 | mBluetooth.onLeServiceUp(); |
Marie Janssen | e0bfa2e | 2016-12-20 11:21:12 -0800 | [diff] [blame] | 1365 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1366 | break; |
| 1367 | } |
| 1368 | } |
| 1369 | } catch (RemoteException e) { |
| 1370 | Slog.e(TAG, "", e); |
| 1371 | } finally { |
| 1372 | mBluetoothLock.readLock().unlock(); |
| 1373 | } |
| 1374 | |
| 1375 | mQuietEnable = (msg.arg1 == 1); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1376 | if (mBluetooth == null) { |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1377 | handleEnable(mQuietEnable); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1378 | } else { |
| 1379 | // |
| 1380 | // We need to wait until transitioned to STATE_OFF and |
| 1381 | // the previous Bluetooth process has exited. The |
| 1382 | // waiting period has three components: |
| 1383 | // (a) Wait until the local state is STATE_OFF. This |
| 1384 | // is accomplished by "waitForOnOff(false, true)". |
| 1385 | // (b) Wait until the STATE_OFF state is updated to |
| 1386 | // all components. |
| 1387 | // (c) Wait until the Bluetooth process exits, and |
| 1388 | // ActivityManager detects it. |
| 1389 | // The waiting for (b) and (c) is accomplished by |
| 1390 | // delaying the MESSAGE_RESTART_BLUETOOTH_SERVICE |
| 1391 | // message. On slower devices, that delay needs to be |
| 1392 | // on the order of (2 * SERVICE_RESTART_TIME_MS). |
| 1393 | // |
| 1394 | waitForOnOff(false, true); |
Pavlin Radoslavov | e47ec14 | 2016-06-01 22:25:18 -0700 | [diff] [blame] | 1395 | Message restartMsg = mHandler.obtainMessage( |
| 1396 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1397 | mHandler.sendMessageDelayed(restartMsg, |
| 1398 | 2 * SERVICE_RESTART_TIME_MS); |
| 1399 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1400 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1401 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1402 | case MESSAGE_DISABLE: |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1403 | if (DBG) Slog.d(TAG, "MESSAGE_DISABLE: mBluetooth = " + mBluetooth); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1404 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1405 | if (mEnable && mBluetooth != null) { |
| 1406 | waitForOnOff(true, false); |
| 1407 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1408 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1409 | waitForOnOff(false, false); |
| 1410 | } else { |
| 1411 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1412 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1413 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1414 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1415 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1416 | case MESSAGE_REGISTER_ADAPTER: |
| 1417 | { |
| 1418 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1419 | mCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1420 | break; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1421 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1422 | case MESSAGE_UNREGISTER_ADAPTER: |
| 1423 | { |
| 1424 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1425 | mCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1426 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1427 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1428 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 1429 | { |
| 1430 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1431 | mStateChangeCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1432 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1433 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1434 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 1435 | { |
| 1436 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1437 | mStateChangeCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1438 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1439 | } |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1440 | case MESSAGE_ADD_PROXY_DELAYED: |
| 1441 | { |
| 1442 | ProfileServiceConnections psc = mProfileServices.get( |
| 1443 | new Integer(msg.arg1)); |
| 1444 | if (psc == null) { |
| 1445 | break; |
| 1446 | } |
| 1447 | IBluetoothProfileServiceConnection proxy = |
| 1448 | (IBluetoothProfileServiceConnection) msg.obj; |
| 1449 | psc.addProxy(proxy); |
| 1450 | break; |
| 1451 | } |
| 1452 | case MESSAGE_BIND_PROFILE_SERVICE: |
| 1453 | { |
| 1454 | ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj; |
| 1455 | removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj); |
| 1456 | if (psc == null) { |
| 1457 | break; |
| 1458 | } |
| 1459 | psc.bindService(); |
| 1460 | break; |
| 1461 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1462 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 1463 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1464 | if (DBG) Slog.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1465 | |
| 1466 | IBinder service = (IBinder) msg.obj; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1467 | try { |
| 1468 | mBluetoothLock.writeLock().lock(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1469 | if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 1470 | mBluetoothGatt = IBluetoothGatt.Stub |
| 1471 | .asInterface(Binder.allowBlocking(service)); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1472 | onBluetoothGattServiceUp(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1473 | break; |
| 1474 | } // else must be SERVICE_IBLUETOOTH |
| 1475 | |
| 1476 | //Remove timeout |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1477 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1478 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1479 | mBinding = false; |
Marie Janssen | 9db28eb | 2016-01-12 16:05:15 -0800 | [diff] [blame] | 1480 | mBluetoothBinder = service; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 1481 | mBluetooth = IBluetooth.Stub.asInterface(Binder.allowBlocking(service)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1482 | |
Ajay Panicker | 4bb4830 | 2016-03-31 14:14:27 -0700 | [diff] [blame] | 1483 | if (!isNameAndAddressSet()) { |
| 1484 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 1485 | mHandler.sendMessage(getMsg); |
| 1486 | if (mGetNameAddressOnly) return; |
| 1487 | } |
| 1488 | |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1489 | try { |
| 1490 | boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver, |
| 1491 | Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1); |
| 1492 | if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1493 | Slog.e(TAG,"IBluetooth.configHciSnoopLog return false"); |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1494 | } |
| 1495 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1496 | Slog.e(TAG,"Unable to call configHciSnoopLog", e); |
Zhihai Xu | af5971e | 2013-06-10 20:28:31 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1499 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1500 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1501 | mBluetooth.registerCallback(mBluetoothCallback); |
| 1502 | } catch (RemoteException re) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1503 | Slog.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1504 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1505 | //Inform BluetoothAdapter instances that service is up |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1506 | sendBluetoothServiceUpCallback(); |
| 1507 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1508 | //Do enable request |
| 1509 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1510 | if (mQuietEnable == false) { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1511 | if (!mBluetooth.enable()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1512 | Slog.e(TAG,"IBluetooth.enable() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1513 | } |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1514 | } else { |
| 1515 | if (!mBluetooth.enableNoAutoConnect()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1516 | Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1517 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1520 | Slog.e(TAG,"Unable to call enable()",e); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1521 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1522 | } finally { |
| 1523 | mBluetoothLock.writeLock().unlock(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 1524 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1525 | |
| 1526 | if (!mEnable) { |
| 1527 | waitForOnOff(true, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1528 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1529 | waitForOnOff(false, false); |
| 1530 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1531 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1532 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1533 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1534 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1535 | int prevState = msg.arg1; |
| 1536 | int newState = msg.arg2; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1537 | if (DBG) { |
| 1538 | Slog.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: " + BluetoothAdapter.nameForState(prevState) + " > " + |
| 1539 | BluetoothAdapter.nameForState(newState)); |
| 1540 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1541 | mState = newState; |
| 1542 | bluetoothStateChangeHandler(prevState, newState); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1543 | // handle error state transition case from TURNING_ON to OFF |
| 1544 | // unbind and rebind bluetooth service and enable bluetooth |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1545 | if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) && |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1546 | (newState == BluetoothAdapter.STATE_OFF) && |
| 1547 | (mBluetooth != null) && mEnable) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1548 | recoverBluetoothServiceFromError(false); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1549 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1550 | if ((prevState == BluetoothAdapter.STATE_TURNING_ON) && |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1551 | (newState == BluetoothAdapter.STATE_BLE_ON) && |
| 1552 | (mBluetooth != null) && mEnable) { |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1553 | recoverBluetoothServiceFromError(true); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1554 | } |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1555 | // If we tried to enable BT while BT was in the process of shutting down, |
| 1556 | // wait for the BT process to fully tear down and then force a restart |
| 1557 | // here. This is a bit of a hack (b/29363429). |
| 1558 | if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_OFF) && |
| 1559 | (newState == BluetoothAdapter.STATE_OFF)) { |
| 1560 | if (mEnable) { |
| 1561 | Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting."); |
| 1562 | waitForOnOff(false, true); |
| 1563 | Message restartMsg = mHandler.obtainMessage( |
| 1564 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1565 | mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS); |
| 1566 | } |
| 1567 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1568 | if (newState == BluetoothAdapter.STATE_ON || |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1569 | newState == BluetoothAdapter.STATE_BLE_ON) { |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1570 | // bluetooth is working, reset the counter |
| 1571 | if (mErrorRecoveryRetryCounter != 0) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1572 | Slog.w(TAG, "bluetooth is recovered from error"); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 1573 | mErrorRecoveryRetryCounter = 0; |
| 1574 | } |
| 1575 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1576 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1577 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1578 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 1579 | { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1580 | Slog.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(" + msg.arg1 + ")"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1581 | try { |
| 1582 | mBluetoothLock.writeLock().lock(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1583 | if (msg.arg1 == SERVICE_IBLUETOOTH) { |
| 1584 | // if service is unbinded already, do nothing and return |
| 1585 | if (mBluetooth == null) break; |
| 1586 | mBluetooth = null; |
| 1587 | } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) { |
| 1588 | mBluetoothGatt = null; |
| 1589 | break; |
| 1590 | } else { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1591 | Slog.e(TAG, "Unknown argument for service disconnect!"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1592 | break; |
| 1593 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1594 | } finally { |
| 1595 | mBluetoothLock.writeLock().unlock(); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1596 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1597 | |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1598 | // log the unexpected crash |
| 1599 | addCrashLog(); |
| 1600 | addActiveLog(REASON_UNEXPECTED, false); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1601 | if (mEnable) { |
| 1602 | mEnable = false; |
| 1603 | // Send a Bluetooth Restart message |
| 1604 | Message restartMsg = mHandler.obtainMessage( |
| 1605 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 1606 | mHandler.sendMessageDelayed(restartMsg, |
| 1607 | SERVICE_RESTART_TIME_MS); |
| 1608 | } |
| 1609 | |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1610 | sendBluetoothServiceDownCallback(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1611 | |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1612 | // Send BT state broadcast to update |
| 1613 | // the BT icon correctly |
| 1614 | if ((mState == BluetoothAdapter.STATE_TURNING_ON) || |
Calvin On | a0b91d7 | 2016-06-15 17:58:23 -0700 | [diff] [blame] | 1615 | (mState == BluetoothAdapter.STATE_ON)) { |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1616 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1617 | BluetoothAdapter.STATE_TURNING_OFF); |
| 1618 | mState = BluetoothAdapter.STATE_TURNING_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1619 | } |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1620 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1621 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
| 1622 | BluetoothAdapter.STATE_OFF); |
| 1623 | } |
| 1624 | |
| 1625 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1626 | mState = BluetoothAdapter.STATE_OFF; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1627 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1628 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1629 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 1630 | { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1631 | Slog.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE"); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1632 | /* Enable without persisting the setting as |
| 1633 | it doesnt change when IBluetooth |
| 1634 | service restarts */ |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1635 | mEnable = true; |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1636 | addActiveLog(REASON_RESTARTED, true); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1637 | handleEnable(mQuietEnable); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 1638 | break; |
| 1639 | } |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1640 | case MESSAGE_TIMEOUT_BIND: { |
| 1641 | Slog.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
| 1642 | mBluetoothLock.writeLock().lock(); |
| 1643 | mBinding = false; |
| 1644 | mBluetoothLock.writeLock().unlock(); |
| 1645 | break; |
| 1646 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1647 | case MESSAGE_TIMEOUT_UNBIND: |
| 1648 | { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1649 | Slog.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1650 | mBluetoothLock.writeLock().lock(); |
| 1651 | mUnbinding = false; |
| 1652 | mBluetoothLock.writeLock().unlock(); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 1653 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1654 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1655 | |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1656 | case MESSAGE_USER_SWITCHED: { |
| 1657 | if (DBG) Slog.d(TAG, "MESSAGE_USER_SWITCHED"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1658 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1659 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1660 | /* disable and enable BT when detect a user switch */ |
| 1661 | if (mEnable && mBluetooth != null) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1662 | try { |
| 1663 | mBluetoothLock.readLock().lock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1664 | if (mBluetooth != null) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1665 | mBluetooth.unregisterCallback(mBluetoothCallback); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1666 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1667 | } catch (RemoteException re) { |
| 1668 | Slog.e(TAG, "Unable to unregister", re); |
| 1669 | } finally { |
| 1670 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1671 | } |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1672 | |
| 1673 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1674 | // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE |
| 1675 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF); |
| 1676 | mState = BluetoothAdapter.STATE_OFF; |
| 1677 | } |
| 1678 | if (mState == BluetoothAdapter.STATE_OFF) { |
| 1679 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON); |
| 1680 | mState = BluetoothAdapter.STATE_TURNING_ON; |
| 1681 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1682 | |
| 1683 | waitForOnOff(true, false); |
| 1684 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1685 | if (mState == BluetoothAdapter.STATE_TURNING_ON) { |
| 1686 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON); |
| 1687 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1688 | |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1689 | unbindAllBluetoothProfileServices(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1690 | // disable |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1691 | addActiveLog(REASON_USER_SWITCH, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1692 | handleDisable(); |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1693 | // Pbap service need receive STATE_TURNING_OFF intent to close |
| 1694 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 1695 | BluetoothAdapter.STATE_TURNING_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1696 | |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 1697 | boolean didDisableTimeout = !waitForOnOff(false, true); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1698 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1699 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1700 | BluetoothAdapter.STATE_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1701 | sendBluetoothServiceDownCallback(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1702 | |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 1703 | try { |
| 1704 | mBluetoothLock.writeLock().lock(); |
| 1705 | if (mBluetooth != null) { |
| 1706 | mBluetooth = null; |
| 1707 | // Unbind |
| 1708 | mContext.unbindService(mConnection); |
| 1709 | } |
| 1710 | mBluetoothGatt = null; |
| 1711 | } finally { |
| 1712 | mBluetoothLock.writeLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1713 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1714 | |
Pavlin Radoslavov | 4140111 | 2016-06-27 15:25:18 -0700 | [diff] [blame] | 1715 | // |
| 1716 | // If disabling Bluetooth times out, wait for an |
| 1717 | // additional amount of time to ensure the process is |
| 1718 | // shut down completely before attempting to restart. |
| 1719 | // |
| 1720 | if (didDisableTimeout) { |
| 1721 | SystemClock.sleep(3000); |
| 1722 | } else { |
| 1723 | SystemClock.sleep(100); |
| 1724 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1725 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 1726 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 1727 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1728 | // enable |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1729 | addActiveLog(REASON_USER_SWITCH, true); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1730 | handleEnable(mQuietEnable); |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1731 | } else if (mBinding || mBluetooth != null) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1732 | Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); |
| 1733 | userMsg.arg2 = 1 + msg.arg2; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1734 | // if user is switched when service is binding retry after a delay |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1735 | mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); |
| 1736 | if (DBG) { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1737 | Slog.d(TAG, "Retry MESSAGE_USER_SWITCHED " + userMsg.arg2); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1738 | } |
John Spurlock | 8a985d2 | 2014-02-25 09:40:05 -0500 | [diff] [blame] | 1739 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1740 | break; |
| 1741 | } |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1742 | case MESSAGE_USER_UNLOCKED: { |
| 1743 | if (DBG) Slog.d(TAG, "MESSAGE_USER_UNLOCKED"); |
| 1744 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
| 1745 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1746 | if (mEnable && !mBinding && (mBluetooth == null)) { |
| 1747 | // We should be connected, but we gave up for some |
| 1748 | // reason; maybe the Bluetooth service wasn't encryption |
| 1749 | // aware, so try binding again. |
| 1750 | if (DBG) Slog.d(TAG, "Enabled but not bound; retrying after unlock"); |
| 1751 | handleEnable(mQuietEnable); |
Jeff Sharkey | aacb89e | 2016-03-05 14:42:58 -0700 | [diff] [blame] | 1752 | } |
| 1753 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1756 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1757 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1758 | private void handleEnable(boolean quietMode) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1759 | mQuietEnable = quietMode; |
| 1760 | |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1761 | try { |
| 1762 | mBluetoothLock.writeLock().lock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1763 | if ((mBluetooth == null) && (!mBinding)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1764 | //Start bind timeout and bind |
| 1765 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 1766 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1767 | Intent i = new Intent(IBluetooth.class.getName()); |
Dianne Hackborn | ce09f5a | 2014-10-10 15:03:13 -0700 | [diff] [blame] | 1768 | if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 1769 | UserHandle.CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1770 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1771 | } else { |
| 1772 | mBinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1773 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1774 | } else if (mBluetooth != null) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1775 | //Enable bluetooth |
| 1776 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1777 | if (!mQuietEnable) { |
| 1778 | if(!mBluetooth.enable()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1779 | Slog.e(TAG,"IBluetooth.enable() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | else { |
| 1783 | if(!mBluetooth.enableNoAutoConnect()) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1784 | Slog.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1785 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } catch (RemoteException e) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1788 | Slog.e(TAG,"Unable to call enable()",e); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1789 | } |
| 1790 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1791 | } finally { |
| 1792 | mBluetoothLock.writeLock().unlock(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1796 | boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) { |
| 1797 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 1798 | intent.setComponent(comp); |
| 1799 | if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1800 | Slog.e(TAG, "Fail to bind to: " + intent); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
| 1803 | return true; |
| 1804 | } |
| 1805 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1806 | private void handleDisable() { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1807 | try { |
| 1808 | mBluetoothLock.readLock().lock(); |
Andre Eisenbach | 305fdab | 2015-11-11 21:43:26 -0800 | [diff] [blame] | 1809 | if (mBluetooth != null) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1810 | if (DBG) Slog.d(TAG,"Sending off request."); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1811 | if (!mBluetooth.disable()) { |
| 1812 | Slog.e(TAG,"IBluetooth.disable() returned false"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1813 | } |
| 1814 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1815 | } catch (RemoteException e) { |
| 1816 | Slog.e(TAG,"Unable to call disable()",e); |
| 1817 | } finally { |
| 1818 | mBluetoothLock.readLock().unlock(); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1819 | } |
| 1820 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1821 | |
| 1822 | private boolean checkIfCallerIsForegroundUser() { |
| 1823 | int foregroundUser; |
| 1824 | int callingUser = UserHandle.getCallingUserId(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1825 | int callingUid = Binder.getCallingUid(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1826 | long callingIdentity = Binder.clearCallingIdentity(); |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1827 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1828 | UserInfo ui = um.getProfileParent(callingUser); |
| 1829 | int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL; |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1830 | int callingAppId = UserHandle.getAppId(callingUid); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1831 | boolean valid = false; |
| 1832 | try { |
| 1833 | foregroundUser = ActivityManager.getCurrentUser(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1834 | valid = (callingUser == foregroundUser) || |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1835 | parentUser == foregroundUser || |
Adrian Roos | bd9a9a5 | 2014-08-18 15:31:57 +0200 | [diff] [blame] | 1836 | callingAppId == Process.NFC_UID || |
| 1837 | callingAppId == mSystemUiUid; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1838 | if (DBG && !valid) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1839 | Slog.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1840 | + " callingUser=" + callingUser |
Benjamin Franz | e8b9892 | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1841 | + " parentUser=" + parentUser |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1842 | + " foregroundUser=" + foregroundUser); |
| 1843 | } |
| 1844 | } finally { |
| 1845 | Binder.restoreCallingIdentity(callingIdentity); |
| 1846 | } |
| 1847 | return valid; |
| 1848 | } |
| 1849 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1850 | private void sendBleStateChanged(int prevState, int newState) { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1851 | if (DBG) Slog.d(TAG,"Sending BLE State Change: " + BluetoothAdapter.nameForState(prevState) + |
| 1852 | " > " + BluetoothAdapter.nameForState(newState)); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1853 | // Send broadcast message to everyone else |
| 1854 | Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); |
| 1855 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1856 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1857 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1858 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
| 1859 | } |
| 1860 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1861 | private void bluetoothStateChangeHandler(int prevState, int newState) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1862 | boolean isStandardBroadcast = true; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1863 | if (prevState == newState) { // No change. Nothing to do. |
| 1864 | return; |
| 1865 | } |
| 1866 | // Notify all proxy objects first of adapter state change |
| 1867 | if (newState == BluetoothAdapter.STATE_BLE_ON || |
| 1868 | newState == BluetoothAdapter.STATE_OFF) { |
| 1869 | boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF |
| 1870 | && newState == BluetoothAdapter.STATE_BLE_ON); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1871 | |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1872 | if (newState == BluetoothAdapter.STATE_OFF) { |
| 1873 | // If Bluetooth is off, send service down event to proxy objects, and unbind |
| 1874 | if (DBG) Slog.d(TAG, "Bluetooth is complete send Service Down"); |
| 1875 | sendBluetoothServiceDownCallback(); |
| 1876 | unbindAndFinish(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1877 | sendBleStateChanged(prevState, newState); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1878 | // Don't broadcast as it has already been broadcast before |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1879 | isStandardBroadcast = false; |
| 1880 | |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1881 | } else if (!intermediate_off) { |
| 1882 | // connect to GattService |
| 1883 | if (DBG) Slog.d(TAG, "Bluetooth is in LE only mode"); |
| 1884 | if (mBluetoothGatt != null) { |
| 1885 | if (DBG) Slog.d(TAG, "Calling BluetoothGattServiceUp"); |
| 1886 | onBluetoothGattServiceUp(); |
| 1887 | } else { |
| 1888 | if (DBG) Slog.d(TAG, "Binding Bluetooth GATT service"); |
| 1889 | if (mContext.getPackageManager().hasSystemFeature( |
| 1890 | PackageManager.FEATURE_BLUETOOTH_LE)) { |
| 1891 | Intent i = new Intent(IBluetoothGatt.class.getName()); |
| 1892 | doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT); |
| 1893 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1894 | } |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1895 | sendBleStateChanged(prevState, newState); |
| 1896 | //Don't broadcase this as std intent |
| 1897 | isStandardBroadcast = false; |
| 1898 | |
| 1899 | } else if (intermediate_off) { |
| 1900 | if (DBG) Slog.d(TAG, "Intermediate off, back to LE only mode"); |
| 1901 | // For LE only mode, broadcast as is |
| 1902 | sendBleStateChanged(prevState, newState); |
| 1903 | sendBluetoothStateCallback(false); // BT is OFF for general users |
| 1904 | // Broadcast as STATE_OFF |
| 1905 | newState = BluetoothAdapter.STATE_OFF; |
| 1906 | sendBrEdrDownCallback(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1907 | } |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1908 | } else if (newState == BluetoothAdapter.STATE_ON) { |
| 1909 | boolean isUp = (newState == BluetoothAdapter.STATE_ON); |
| 1910 | sendBluetoothStateCallback(isUp); |
| 1911 | sendBleStateChanged(prevState, newState); |
| 1912 | |
| 1913 | } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON || |
| 1914 | newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) { |
| 1915 | sendBleStateChanged(prevState, newState); |
| 1916 | isStandardBroadcast = false; |
| 1917 | |
| 1918 | } else if (newState == BluetoothAdapter.STATE_TURNING_ON || |
| 1919 | newState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 1920 | sendBleStateChanged(prevState, newState); |
| 1921 | } |
| 1922 | |
| 1923 | if (isStandardBroadcast) { |
| 1924 | if (prevState == BluetoothAdapter.STATE_BLE_ON) { |
| 1925 | // Show prevState of BLE_ON as OFF to standard users |
| 1926 | prevState = BluetoothAdapter.STATE_OFF; |
| 1927 | } |
| 1928 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 1929 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1930 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1931 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1932 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * if on is true, wait for state become ON |
| 1938 | * if off is true, wait for state become OFF |
| 1939 | * if both on and off are false, wait for state not ON |
| 1940 | */ |
| 1941 | private boolean waitForOnOff(boolean on, boolean off) { |
| 1942 | int i = 0; |
| 1943 | while (i < 10) { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1944 | try { |
| 1945 | mBluetoothLock.readLock().lock(); |
| 1946 | if (mBluetooth == null) break; |
| 1947 | if (on) { |
| 1948 | if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1949 | } else if (off) { |
| 1950 | if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true; |
| 1951 | } else { |
| 1952 | if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1953 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1954 | } catch (RemoteException e) { |
| 1955 | Slog.e(TAG, "getState()", e); |
| 1956 | break; |
| 1957 | } finally { |
| 1958 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1959 | } |
| 1960 | if (on || off) { |
| 1961 | SystemClock.sleep(300); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1962 | } else { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1963 | SystemClock.sleep(50); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 1964 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1965 | i++; |
| 1966 | } |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 1967 | Slog.e(TAG,"waitForOnOff time out"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1968 | return false; |
| 1969 | } |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1970 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 1971 | private void sendDisableMsg(String packageName) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1972 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE)); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 1973 | addActiveLog(packageName, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 1976 | private void sendEnableMsg(boolean quietMode, String packageName) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1977 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, |
| 1978 | quietMode ? 1 : 0, 0)); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 1979 | addActiveLog(packageName, true); |
| 1980 | } |
| 1981 | |
| 1982 | private void addActiveLog(String packageName, boolean enable) { |
| 1983 | synchronized (mActiveLogs) { |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1984 | if (mActiveLogs.size() > ACTIVE_LOG_MAX_SIZE) { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 1985 | mActiveLogs.remove(); |
| 1986 | } |
| 1987 | mActiveLogs.add(new ActiveLog(packageName, enable, System.currentTimeMillis())); |
| 1988 | } |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1989 | } |
| 1990 | |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 1991 | private void addCrashLog() { |
| 1992 | synchronized (mCrashTimestamps) { |
| 1993 | if (mCrashTimestamps.size() == CRASH_LOG_MAX_SIZE) mCrashTimestamps.removeFirst(); |
| 1994 | mCrashTimestamps.add(System.currentTimeMillis()); |
| 1995 | mCrashes++; |
| 1996 | } |
| 1997 | } |
| 1998 | |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 1999 | private void recoverBluetoothServiceFromError(boolean clearBle) { |
Jeff Sharkey | 67609c7 | 2016-03-05 14:29:13 -0700 | [diff] [blame] | 2000 | Slog.e(TAG,"recoverBluetoothServiceFromError"); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2001 | try { |
| 2002 | mBluetoothLock.readLock().lock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2003 | if (mBluetooth != null) { |
| 2004 | //Unregister callback object |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2005 | mBluetooth.unregisterCallback(mBluetoothCallback); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2006 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2007 | } catch (RemoteException re) { |
| 2008 | Slog.e(TAG, "Unable to unregister", re); |
| 2009 | } finally { |
| 2010 | mBluetoothLock.readLock().unlock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | SystemClock.sleep(500); |
| 2014 | |
| 2015 | // disable |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 2016 | addActiveLog(REASON_START_CRASH, false); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2017 | handleDisable(); |
| 2018 | |
| 2019 | waitForOnOff(false, true); |
| 2020 | |
| 2021 | sendBluetoothServiceDownCallback(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2022 | |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 2023 | try { |
| 2024 | mBluetoothLock.writeLock().lock(); |
| 2025 | if (mBluetooth != null) { |
| 2026 | mBluetooth = null; |
| 2027 | // Unbind |
| 2028 | mContext.unbindService(mConnection); |
| 2029 | } |
| 2030 | mBluetoothGatt = null; |
| 2031 | } finally { |
| 2032 | mBluetoothLock.writeLock().unlock(); |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 2036 | mState = BluetoothAdapter.STATE_OFF; |
| 2037 | |
Marie Janssen | 2977c3e | 2016-11-09 12:01:24 -0800 | [diff] [blame] | 2038 | if (clearBle) { |
| 2039 | clearBleApps(); |
| 2040 | } |
| 2041 | |
Zhihai Xu | dd9d17d | 2013-01-08 17:05:58 -0800 | [diff] [blame] | 2042 | mEnable = false; |
| 2043 | |
| 2044 | if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) { |
| 2045 | // Send a Bluetooth Restart message to reenable bluetooth |
| 2046 | Message restartMsg = mHandler.obtainMessage( |
| 2047 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 2048 | mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS); |
| 2049 | } else { |
| 2050 | // todo: notify user to power down and power up phone to make bluetooth work. |
| 2051 | } |
| 2052 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 2053 | |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 2054 | private boolean isBluetoothDisallowed() { |
| 2055 | long callingIdentity = Binder.clearCallingIdentity(); |
| 2056 | try { |
| 2057 | return mContext.getSystemService(UserManager.class) |
| 2058 | .hasUserRestriction(UserManager.DISALLOW_BLUETOOTH, UserHandle.SYSTEM); |
| 2059 | } finally { |
| 2060 | Binder.restoreCallingIdentity(callingIdentity); |
| 2061 | } |
| 2062 | } |
| 2063 | |
Lenka Trochtova | c6f0e23 | 2017-01-17 10:35:49 +0100 | [diff] [blame] | 2064 | /** |
| 2065 | * Disables BluetoothOppLauncherActivity component, so the Bluetooth sharing option is not |
| 2066 | * offered to the user if Bluetooth is disallowed. Puts the component to its default state if |
| 2067 | * Bluetooth is not disallowed. |
| 2068 | * |
| 2069 | * @param bluetoothDisallowed whether the {@link UserManager.DISALLOW_BLUETOOTH} user |
| 2070 | * restriction was set. |
| 2071 | */ |
| 2072 | private void updateOppLauncherComponentState(boolean bluetoothDisallowed) { |
| 2073 | final ComponentName oppLauncherComponent = new ComponentName("com.android.bluetooth", |
| 2074 | "com.android.bluetooth.opp.BluetoothOppLauncherActivity"); |
| 2075 | final int newState = bluetoothDisallowed |
| 2076 | ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED |
| 2077 | : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; |
| 2078 | try { |
| 2079 | mContext.getPackageManager() |
| 2080 | .setComponentEnabledSetting(oppLauncherComponent, newState, 0); |
| 2081 | } catch (Exception e) { |
| 2082 | // The component was not found, do nothing. |
| 2083 | } |
| 2084 | } |
| 2085 | |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 2086 | @Override |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2087 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2088 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2089 | String errorMsg = null; |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2090 | |
| 2091 | boolean protoOut = (args.length > 0) && args[0].startsWith("--proto"); |
| 2092 | |
| 2093 | if (!protoOut) { |
| 2094 | writer.println("Bluetooth Status"); |
| 2095 | writer.println(" enabled: " + isEnabled()); |
| 2096 | writer.println(" state: " + BluetoothAdapter.nameForState(mState)); |
| 2097 | writer.println(" address: " + mAddress); |
| 2098 | writer.println(" name: " + mName); |
| 2099 | if (mEnable) { |
| 2100 | long onDuration = System.currentTimeMillis() - mActiveLogs.getLast().getTime(); |
| 2101 | String onDurationString = String.format("%02d:%02d:%02d.%03d", |
| 2102 | (int)(onDuration / (1000 * 60 * 60)), |
| 2103 | (int)((onDuration / (1000 * 60)) % 60), |
| 2104 | (int)((onDuration / 1000) % 60), |
| 2105 | (int)(onDuration % 1000)); |
| 2106 | writer.println(" time since enabled: " + onDurationString + "\n"); |
| 2107 | } |
| 2108 | |
Marie Janssen | a95924d | 2017-01-18 09:37:52 -0800 | [diff] [blame] | 2109 | if (mActiveLogs.size() == 0) { |
| 2110 | writer.println("Bluetooth never enabled!"); |
| 2111 | } else { |
| 2112 | writer.println("Enable log:"); |
| 2113 | for (ActiveLog log : mActiveLogs) { |
| 2114 | writer.println(" " + log); |
| 2115 | } |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
Marie Janssen | e54b422 | 2017-03-16 18:10:59 -0700 | [diff] [blame] | 2118 | writer.println("Bluetooth crashed " + mCrashes + " time" + (mCrashes == 1 ? "" : "s")); |
| 2119 | if (mCrashes == CRASH_LOG_MAX_SIZE) writer.println("(last " + CRASH_LOG_MAX_SIZE + ")"); |
| 2120 | for (Long time : mCrashTimestamps) { |
| 2121 | writer.println(" " + timeToLog(time.longValue())); |
| 2122 | } |
| 2123 | |
Marie Janssen | a95924d | 2017-01-18 09:37:52 -0800 | [diff] [blame] | 2124 | String bleAppString = "No BLE Apps registered."; |
| 2125 | if (mBleApps.size() == 1) { |
| 2126 | bleAppString = "1 BLE App registered:"; |
| 2127 | } else if (mBleApps.size() > 1) { |
| 2128 | bleAppString = mBleApps.size() + " BLE Apps registered:"; |
| 2129 | } |
| 2130 | writer.println("\n" + bleAppString); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2131 | for (ClientDeathRecipient app : mBleApps.values()) { |
Marie Janssen | a95924d | 2017-01-18 09:37:52 -0800 | [diff] [blame] | 2132 | writer.println(" " + app.getPackageName()); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
Marie Janssen | a95924d | 2017-01-18 09:37:52 -0800 | [diff] [blame] | 2135 | writer.println(""); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2136 | writer.flush(); |
Marie Janssen | f5ec538 | 2017-01-03 11:37:38 -0800 | [diff] [blame] | 2137 | if (args.length == 0) { |
Marie Janssen | a95924d | 2017-01-18 09:37:52 -0800 | [diff] [blame] | 2138 | // Add arg to produce output |
| 2139 | args = new String[1]; |
| 2140 | args[0] = "--print"; |
Marie Janssen | f5ec538 | 2017-01-03 11:37:38 -0800 | [diff] [blame] | 2141 | } |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2144 | if (mBluetoothBinder == null) { |
| 2145 | errorMsg = "Bluetooth Service not connected"; |
| 2146 | } else { |
| 2147 | try { |
| 2148 | mBluetoothBinder.dump(fd, args); |
| 2149 | } catch (RemoteException re) { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2150 | errorMsg = "RemoteException while dumping Bluetooth Service"; |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2151 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 2152 | } |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2153 | if (errorMsg != null) { |
| 2154 | // Silently return if we are extracting metrics in Protobuf format |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2155 | if (protoOut) return; |
Pavlin Radoslavov | 6e8faff | 2016-02-23 11:54:37 -0800 | [diff] [blame] | 2156 | writer.println(errorMsg); |
| 2157 | } |
Mike Lockwood | 726d4de | 2014-10-28 14:06:28 -0700 | [diff] [blame] | 2158 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2159 | } |