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