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