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