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