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