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