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