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 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 20 | import android.bluetooth.BluetoothAdapter; |
| 21 | import android.bluetooth.IBluetooth; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 22 | import android.bluetooth.IBluetoothCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 23 | import android.bluetooth.IBluetoothManager; |
| 24 | import android.bluetooth.IBluetoothManagerCallback; |
| 25 | import android.bluetooth.IBluetoothStateChangeCallback; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.ServiceConnection; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 35 | import android.os.HandlerThread; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 36 | import android.os.IBinder; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 37 | import android.os.Looper; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 38 | import android.os.Message; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 39 | import android.os.Process; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 40 | import android.os.RemoteCallbackList; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 42 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 44 | import android.provider.Settings; |
| 45 | import android.util.Log; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 46 | class BluetoothManagerService extends IBluetoothManager.Stub { |
| 47 | private static final String TAG = "BluetoothManagerService"; |
| 48 | private static final boolean DBG = true; |
| 49 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 50 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 51 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 52 | private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED"; |
| 53 | private static final String EXTRA_ACTION="action"; |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 54 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 55 | private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address"; |
| 56 | private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 57 | private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind |
| 58 | private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 59 | //Maximum msec to wait for service restart |
| 60 | private static final int SERVICE_RESTART_TIME_MS = 200; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 61 | //Maximum msec to delay MESSAGE_USER_SWITCHED |
| 62 | private static final int USER_SWITCHED_TIME_MS = 200; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 63 | |
| 64 | private static final int MESSAGE_ENABLE = 1; |
| 65 | private static final int MESSAGE_DISABLE = 2; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 66 | private static final int MESSAGE_REGISTER_ADAPTER = 20; |
| 67 | private static final int MESSAGE_UNREGISTER_ADAPTER = 21; |
| 68 | private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30; |
| 69 | private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31; |
| 70 | private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40; |
| 71 | private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 72 | private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 73 | private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 74 | private static final int MESSAGE_TIMEOUT_BIND =100; |
| 75 | private static final int MESSAGE_TIMEOUT_UNBIND =101; |
| 76 | private static final int MESSAGE_GET_NAME_AND_ADDRESS=200; |
| 77 | private static final int MESSAGE_SAVE_NAME_AND_ADDRESS=201; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 78 | private static final int MESSAGE_USER_SWITCHED = 300; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 79 | private static final int MAX_SAVE_RETRIES=3; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 80 | // Bluetooth persisted setting is off |
| 81 | private static final int BLUETOOTH_OFF=0; |
| 82 | // Bluetooth persisted setting is on |
| 83 | // and Airplane mode won't affect Bluetooth state at start up |
| 84 | private static final int BLUETOOTH_ON_BLUETOOTH=1; |
| 85 | // Bluetooth persisted setting is on |
| 86 | // but Airplane mode will affect Bluetooth state at start up |
| 87 | // and Airplane mode will have higher priority. |
| 88 | private static final int BLUETOOTH_ON_AIRPLANE=2; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 89 | |
| 90 | private final Context mContext; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 91 | |
| 92 | // Locks are not provided for mName and mAddress. |
| 93 | // They are accessed in handler or broadcast receiver, same thread context. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 94 | private String mAddress; |
| 95 | private String mName; |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 96 | private final ContentResolver mContentResolver; |
| 97 | private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks; |
| 98 | private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 99 | private IBluetooth mBluetooth; |
| 100 | private boolean mBinding; |
| 101 | private boolean mUnbinding; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 102 | // used inside handler thread |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 103 | private boolean mQuietEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 104 | // configuarion from external IBinder call which is used to |
| 105 | // synchronize with broadcast receiver. |
| 106 | private boolean mQuietEnableExternal; |
| 107 | // configuarion from external IBinder call which is used to |
| 108 | // synchronize with broadcast receiver. |
| 109 | private boolean mEnableExternal; |
| 110 | // used inside handler thread |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 111 | private boolean mEnable; |
| 112 | private int mState; |
| 113 | private HandlerThread mThread; |
| 114 | private final BluetoothHandler mHandler; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 115 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 116 | private void registerForAirplaneMode(IntentFilter filter) { |
| 117 | final ContentResolver resolver = mContext.getContentResolver(); |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 118 | final String airplaneModeRadios = Settings.Global.getString(resolver, |
| 119 | Settings.Global.AIRPLANE_MODE_RADIOS); |
| 120 | final String toggleableRadios = Settings.Global.getString(resolver, |
| 121 | Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 122 | boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true : |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 123 | airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 124 | if (mIsAirplaneSensitive) { |
| 125 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 126 | } |
| 127 | } |
| 128 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 129 | private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() { |
| 130 | @Override |
| 131 | public void onBluetoothStateChange(int prevState, int newState) throws RemoteException { |
| 132 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState); |
| 133 | mHandler.sendMessage(msg); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public void onReceive(Context context, Intent intent) { |
| 140 | String action = intent.getAction(); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 141 | if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 142 | String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 143 | if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 144 | if (newName != null) { |
| 145 | storeNameAndAddress(newName, null); |
| 146 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 147 | } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 148 | synchronized(mReceiver) { |
| 149 | if (isBluetoothPersistedStateOn()) { |
| 150 | if (isAirplaneModeOn()) { |
| 151 | persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE); |
| 152 | } else { |
| 153 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 154 | } |
| 155 | } |
| 156 | if (isAirplaneModeOn()) { |
| 157 | // disable without persisting the setting |
| 158 | sendDisableMsg(); |
| 159 | } else if (mEnableExternal) { |
| 160 | // enable without persisting the setting |
| 161 | sendEnableMsg(mQuietEnableExternal); |
| 162 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 163 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 164 | } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 165 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED, |
| 166 | intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0)); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 167 | } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { |
| 168 | synchronized(mReceiver) { |
| 169 | if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) { |
| 170 | //Enable |
| 171 | if (DBG) Log.d(TAG, "Auto-enabling Bluetooth."); |
| 172 | sendEnableMsg(mQuietEnableExternal); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (!isNameAndAddressSet()) { |
| 177 | //Sync the Bluetooth name and address from the Bluetooth Adapter |
| 178 | if (DBG) Log.d(TAG,"Retrieving Bluetooth Adapter name and address..."); |
| 179 | getNameAndAddress(); |
| 180 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | BluetoothManagerService(Context context) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 186 | mThread = new HandlerThread("BluetoothManager"); |
| 187 | mThread.start(); |
| 188 | mHandler = new BluetoothHandler(mThread.getLooper()); |
| 189 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 190 | mContext = context; |
| 191 | mBluetooth = null; |
| 192 | mBinding = false; |
| 193 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 194 | mEnable = false; |
| 195 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 196 | mQuietEnableExternal = false; |
| 197 | mEnableExternal = false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 198 | mAddress = null; |
| 199 | mName = null; |
| 200 | mContentResolver = context.getContentResolver(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 201 | mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>(); |
| 202 | mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 203 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 204 | filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 205 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Matthew Xie | 6fde309 | 2012-07-11 17:10:07 -0700 | [diff] [blame] | 206 | registerForAirplaneMode(filter); |
| 207 | mContext.registerReceiver(mReceiver, filter); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 208 | loadStoredNameAndAddress(); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 209 | if (isBluetoothPersistedStateOn()) { |
| 210 | mEnableExternal = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Returns true if airplane mode is currently on |
| 216 | */ |
| 217 | private final boolean isAirplaneModeOn() { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 218 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 219 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns true if the Bluetooth saved state is "on" |
| 224 | */ |
| 225 | private final boolean isBluetoothPersistedStateOn() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 226 | return Settings.Global.getInt(mContentResolver, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 227 | Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH |
| 232 | */ |
| 233 | private final boolean isBluetoothPersistedStateOnBluetooth() { |
| 234 | return Settings.Global.getInt(mContentResolver, |
| 235 | Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Save the Bluetooth on/off state |
| 240 | * |
| 241 | */ |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 242 | private void persistBluetoothSetting(int value) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 243 | Settings.Global.putInt(mContext.getContentResolver(), |
| 244 | Settings.Global.BLUETOOTH_ON, |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 245 | value); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns true if the Bluetooth Adapter's name and address is |
| 250 | * locally cached |
| 251 | * @return |
| 252 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 253 | private boolean isNameAndAddressSet() { |
| 254 | return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0; |
| 255 | } |
| 256 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Retrieve the Bluetooth Adapter's name and address and save it in |
| 259 | * in the local cache |
| 260 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 261 | private void loadStoredNameAndAddress() { |
| 262 | if (DBG) Log.d(TAG, "Loading stored name and address"); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 263 | if (mContext.getResources().getBoolean |
| 264 | (com.android.internal.R.bool.config_bluetooth_address_validation) && |
| 265 | Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) { |
| 266 | // if the valid flag is not set, don't load the address and name |
| 267 | if (DBG) Log.d(TAG, "invalid bluetooth name and address stored"); |
| 268 | return; |
| 269 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 270 | mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME); |
| 271 | mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS); |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 272 | if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 273 | } |
| 274 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 275 | /** |
| 276 | * Save the Bluetooth name and address in the persistent store. |
| 277 | * Only non-null values will be saved. |
| 278 | * @param name |
| 279 | * @param address |
| 280 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 281 | private void storeNameAndAddress(String name, String address) { |
| 282 | if (name != null) { |
| 283 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 284 | mName = name; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 285 | if (DBG) Log.d(TAG,"Stored Bluetooth name: " + |
| 286 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (address != null) { |
| 290 | Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 291 | mAddress=address; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 292 | if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " + |
| 293 | Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 294 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 295 | |
| 296 | if ((name != null) && (address != null)) { |
| 297 | Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1); |
| 298 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | public IBluetooth registerAdapter(IBluetoothManagerCallback callback){ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 302 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER); |
| 303 | msg.obj = callback; |
| 304 | mHandler.sendMessage(msg); |
| 305 | synchronized(mConnection) { |
| 306 | return mBluetooth; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | public void unregisterAdapter(IBluetoothManagerCallback callback) { |
| 311 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 312 | "Need BLUETOOTH permission"); |
| 313 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER); |
| 314 | msg.obj = callback; |
| 315 | mHandler.sendMessage(msg); |
| 316 | } |
| 317 | |
| 318 | public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 319 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 320 | "Need BLUETOOTH permission"); |
| 321 | Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK); |
| 322 | msg.obj = callback; |
| 323 | mHandler.sendMessage(msg); |
| 324 | } |
| 325 | |
| 326 | public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) { |
| 327 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, |
| 328 | "Need BLUETOOTH permission"); |
| 329 | Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK); |
| 330 | msg.obj = callback; |
| 331 | mHandler.sendMessage(msg); |
| 332 | } |
| 333 | |
| 334 | public boolean isEnabled() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 335 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 336 | (!checkIfCallerIsForegroundUser())) { |
| 337 | Log.w(TAG,"isEnabled(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 341 | synchronized(mConnection) { |
| 342 | try { |
| 343 | return (mBluetooth != null && mBluetooth.isEnabled()); |
| 344 | } catch (RemoteException e) { |
| 345 | Log.e(TAG, "isEnabled()", e); |
| 346 | } |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | public void getNameAndAddress() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 352 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 353 | Log.d(TAG,"getNameAndAddress(): mBluetooth = " + mBluetooth + |
| 354 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 355 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 356 | Message msg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 357 | mHandler.sendMessage(msg); |
| 358 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 359 | public boolean enableNoAutoConnect() |
| 360 | { |
| 361 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 362 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 363 | |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 364 | if (DBG) { |
| 365 | Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth + |
| 366 | " mBinding = " + mBinding); |
| 367 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 368 | int callingAppId = UserHandle.getAppId(Binder.getCallingUid()); |
| 369 | |
| 370 | if (callingAppId != Process.NFC_UID) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 371 | throw new SecurityException("no permission to enable Bluetooth quietly"); |
| 372 | } |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 373 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 374 | synchronized(mReceiver) { |
| 375 | mQuietEnableExternal = true; |
| 376 | mEnableExternal = true; |
| 377 | sendEnableMsg(true); |
| 378 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 379 | return true; |
| 380 | |
| 381 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 382 | public boolean enable() { |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 383 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 384 | (!checkIfCallerIsForegroundUser())) { |
| 385 | Log.w(TAG,"enable(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 386 | return false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 389 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 390 | "Need BLUETOOTH ADMIN permission"); |
| 391 | if (DBG) { |
| 392 | Log.d(TAG,"enable(): mBluetooth =" + mBluetooth + |
| 393 | " mBinding = " + mBinding); |
| 394 | } |
| 395 | |
| 396 | synchronized(mReceiver) { |
| 397 | mQuietEnableExternal = false; |
| 398 | mEnableExternal = true; |
| 399 | // waive WRITE_SECURE_SETTINGS permission check |
| 400 | long callingIdentity = Binder.clearCallingIdentity(); |
| 401 | persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH); |
| 402 | Binder.restoreCallingIdentity(callingIdentity); |
| 403 | sendEnableMsg(false); |
| 404 | } |
| 405 | return true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | public boolean disable(boolean persist) { |
| 409 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 410 | "Need BLUETOOTH ADMIN permissicacheNameAndAddresson"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 411 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 412 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 413 | (!checkIfCallerIsForegroundUser())) { |
| 414 | Log.w(TAG,"disable(): not allowed for non-active and non system user"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 418 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 419 | Log.d(TAG,"disable(): mBluetooth = " + mBluetooth + |
| 420 | " mBinding = " + mBinding); |
| 421 | } |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 422 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 423 | synchronized(mReceiver) { |
| 424 | if (persist) { |
| 425 | // waive WRITE_SECURE_SETTINGS permission check |
| 426 | long callingIdentity = Binder.clearCallingIdentity(); |
| 427 | persistBluetoothSetting(BLUETOOTH_OFF); |
| 428 | Binder.restoreCallingIdentity(callingIdentity); |
| 429 | } |
| 430 | mEnableExternal = false; |
| 431 | sendDisableMsg(); |
| 432 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 433 | return true; |
| 434 | } |
| 435 | |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 436 | public void unbindAndFinish() { |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 437 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 438 | Log.d(TAG,"unbindAndFinish(): " + mBluetooth + |
| 439 | " mBinding = " + mBinding); |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 440 | } |
| 441 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 442 | synchronized (mConnection) { |
| 443 | if (mUnbinding) return; |
| 444 | mUnbinding = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 445 | if (mBluetooth != null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 446 | if (!mConnection.isGetNameAddressOnly()) { |
| 447 | //Unregister callback object |
| 448 | try { |
| 449 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 450 | } catch (RemoteException re) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 451 | Log.e(TAG, "Unable to unregister BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 454 | if (DBG) Log.d(TAG, "Sending unbind request."); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 455 | mBluetooth = null; |
| 456 | //Unbind |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 457 | mContext.unbindService(mConnection); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 458 | mUnbinding = false; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 459 | mBinding = false; |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 460 | } else { |
| 461 | mUnbinding=false; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 466 | private void sendBluetoothStateCallback(boolean isUp) { |
| 467 | int n = mStateChangeCallbacks.beginBroadcast(); |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 468 | if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers."); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 469 | for (int i=0; i <n;i++) { |
| 470 | try { |
| 471 | mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp); |
| 472 | } catch (RemoteException e) { |
| 473 | Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e); |
| 474 | } |
| 475 | } |
| 476 | mStateChangeCallbacks.finishBroadcast(); |
| 477 | } |
| 478 | |
| 479 | /** |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 480 | * Inform BluetoothAdapter instances that Adapter service is up |
| 481 | */ |
| 482 | private void sendBluetoothServiceUpCallback() { |
| 483 | if (!mConnection.isGetNameAddressOnly()) { |
| 484 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks"); |
| 485 | int n = mCallbacks.beginBroadcast(); |
| 486 | Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers."); |
| 487 | for (int i=0; i <n;i++) { |
| 488 | try { |
| 489 | mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth); |
| 490 | } catch (RemoteException e) { |
| 491 | Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e); |
| 492 | } |
| 493 | } |
| 494 | mCallbacks.finishBroadcast(); |
| 495 | } |
| 496 | } |
| 497 | /** |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 498 | * Inform BluetoothAdapter instances that Adapter service is down |
| 499 | */ |
| 500 | private void sendBluetoothServiceDownCallback() { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 501 | if (!mConnection.isGetNameAddressOnly()) { |
| 502 | if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks"); |
| 503 | int n = mCallbacks.beginBroadcast(); |
| 504 | Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers."); |
| 505 | for (int i=0; i <n;i++) { |
| 506 | try { |
| 507 | mCallbacks.getBroadcastItem(i).onBluetoothServiceDown(); |
| 508 | } catch (RemoteException e) { |
| 509 | Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e); |
| 510 | } |
| 511 | } |
| 512 | mCallbacks.finishBroadcast(); |
| 513 | } |
| 514 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 515 | public String getAddress() { |
| 516 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 517 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 518 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 519 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 520 | (!checkIfCallerIsForegroundUser())) { |
| 521 | Log.w(TAG,"getAddress(): not allowed for non-active and non system user"); |
| 522 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 523 | } |
| 524 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 525 | synchronized(mConnection) { |
| 526 | if (mBluetooth != null) { |
| 527 | try { |
| 528 | return mBluetooth.getAddress(); |
| 529 | } catch (RemoteException e) { |
| 530 | Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e); |
| 531 | } |
| 532 | } |
| 533 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 534 | // mAddress is accessed from outside. |
| 535 | // It is alright without a lock. Here, bluetooth is off, no other thread is |
| 536 | // changing mAddress |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 537 | return mAddress; |
| 538 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 539 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 540 | public String getName() { |
| 541 | mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, |
| 542 | "Need BLUETOOTH ADMIN permission"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 543 | |
Zhihai Xu | 6eb7652 | 2012-11-29 15:41:04 -0800 | [diff] [blame] | 544 | if ((Binder.getCallingUid() != Process.SYSTEM_UID) && |
| 545 | (!checkIfCallerIsForegroundUser())) { |
| 546 | Log.w(TAG,"getName(): not allowed for non-active and non system user"); |
| 547 | return null; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 548 | } |
| 549 | |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 550 | synchronized(mConnection) { |
| 551 | if (mBluetooth != null) { |
| 552 | try { |
| 553 | return mBluetooth.getName(); |
| 554 | } catch (RemoteException e) { |
| 555 | Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e); |
| 556 | } |
| 557 | } |
| 558 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 559 | // mName is accessed from outside. |
| 560 | // It alright without a lock. Here, bluetooth is off, no other thread is |
| 561 | // changing mName |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 562 | return mName; |
| 563 | } |
| 564 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 565 | private class BluetoothServiceConnection implements ServiceConnection { |
| 566 | |
| 567 | private boolean mGetNameAddressOnly; |
| 568 | |
| 569 | public void setGetNameAddressOnly(boolean getOnly) { |
| 570 | mGetNameAddressOnly = getOnly; |
| 571 | } |
| 572 | |
| 573 | public boolean isGetNameAddressOnly() { |
| 574 | return mGetNameAddressOnly; |
| 575 | } |
| 576 | |
| 577 | public void onServiceConnected(ComponentName className, IBinder service) { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 578 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: connected to AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 579 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED); |
| 580 | msg.obj = service; |
| 581 | mHandler.sendMessage(msg); |
| 582 | } |
| 583 | |
| 584 | public void onServiceDisconnected(ComponentName className) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 585 | // Called if we unexpected disconnected. |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 586 | if (DBG) Log.d(TAG, "BluetoothServiceConnection: disconnected from AdapterService"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 587 | Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED); |
| 588 | mHandler.sendMessage(msg); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | private BluetoothServiceConnection mConnection = new BluetoothServiceConnection(); |
| 593 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 594 | private class BluetoothHandler extends Handler { |
| 595 | public BluetoothHandler(Looper looper) { |
| 596 | super(looper); |
| 597 | } |
| 598 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 599 | @Override |
| 600 | public void handleMessage(Message msg) { |
| 601 | if (DBG) Log.d (TAG, "Message: " + msg.what); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 602 | switch (msg.what) { |
| 603 | case MESSAGE_GET_NAME_AND_ADDRESS: { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 604 | if (DBG) Log.d(TAG,"MESSAGE_GET_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 605 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 606 | //Start bind request |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 607 | if ((mBluetooth == null) && (!mBinding)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 608 | if (DBG) Log.d(TAG, "Binding to service to get name and address"); |
| 609 | mConnection.setGetNameAddressOnly(true); |
| 610 | //Start bind timeout and bind |
| 611 | Message timeoutMsg = mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 612 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 613 | Intent i = new Intent(IBluetooth.class.getName()); |
| 614 | if (!mContext.bindService(i, mConnection, |
Matthew Xie | fca9d63 | 2012-10-04 12:25:28 -0700 | [diff] [blame] | 615 | Context.BIND_AUTO_CREATE, UserHandle.USER_CURRENT)) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 616 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 617 | Log.e(TAG, "fail to bind to: " + IBluetooth.class.getName()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 618 | } else { |
| 619 | mBinding = true; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 622 | else { |
| 623 | Message saveMsg= mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 624 | saveMsg.arg1 = 0; |
| 625 | if (mBluetooth != null) { |
| 626 | mHandler.sendMessage(saveMsg); |
| 627 | } else { |
| 628 | // if enable is also called to bind the service |
| 629 | // wait for MESSAGE_BLUETOOTH_SERVICE_CONNECTED |
| 630 | mHandler.sendMessageDelayed(saveMsg, TIMEOUT_SAVE_MS); |
| 631 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 632 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 633 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 634 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 635 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 636 | case MESSAGE_SAVE_NAME_AND_ADDRESS: { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 637 | boolean unbind = false; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 638 | if (DBG) Log.d(TAG,"MESSAGE_SAVE_NAME_AND_ADDRESS"); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 639 | synchronized(mConnection) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 640 | if (!mEnable && mBluetooth != null) { |
| 641 | try { |
| 642 | mBluetooth.enable(); |
| 643 | } catch (RemoteException e) { |
| 644 | Log.e(TAG,"Unable to call enable()",e); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | if (mBluetooth != null) waitForOnOff(true, false); |
| 649 | synchronized(mConnection) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 650 | if (mBluetooth != null) { |
| 651 | String name = null; |
| 652 | String address = null; |
| 653 | try { |
| 654 | name = mBluetooth.getName(); |
| 655 | address = mBluetooth.getAddress(); |
| 656 | } catch (RemoteException re) { |
| 657 | Log.e(TAG,"",re); |
| 658 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 659 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 660 | if (name != null && address != null) { |
| 661 | storeNameAndAddress(name,address); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 662 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 663 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 664 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 665 | } else { |
| 666 | if (msg.arg1 < MAX_SAVE_RETRIES) { |
| 667 | Message retryMsg = mHandler.obtainMessage(MESSAGE_SAVE_NAME_AND_ADDRESS); |
| 668 | retryMsg.arg1= 1+msg.arg1; |
| 669 | if (DBG) Log.d(TAG,"Retrying name/address remote retrieval and save.....Retry count =" + retryMsg.arg1); |
| 670 | mHandler.sendMessageDelayed(retryMsg, TIMEOUT_SAVE_MS); |
| 671 | } else { |
| 672 | Log.w(TAG,"Maximum name/address remote retrieval retry exceeded"); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 673 | if (mConnection.isGetNameAddressOnly()) { |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 674 | unbind = true; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 675 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 676 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 677 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 678 | if (!mEnable) { |
| 679 | try { |
| 680 | mBluetooth.disable(); |
| 681 | } catch (RemoteException e) { |
| 682 | Log.e(TAG,"Unable to call disable()",e); |
| 683 | } |
| 684 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 685 | } else { |
| 686 | // rebind service by Request GET NAME AND ADDRESS |
| 687 | // if service is unbinded by disable or |
| 688 | // MESSAGE_BLUETOOTH_SERVICE_CONNECTED is not received |
| 689 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 690 | mHandler.sendMessage(getMsg); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
Zhihai Xu | d31c322 | 2012-10-31 16:08:57 -0700 | [diff] [blame] | 693 | if (!mEnable && mBluetooth != null) waitForOnOff(false, true); |
| 694 | if (unbind) { |
| 695 | unbindAndFinish(); |
| 696 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 697 | break; |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 698 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 699 | case MESSAGE_ENABLE: |
fredc | f245886 | 2012-04-16 15:18:27 -0700 | [diff] [blame] | 700 | if (DBG) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 701 | Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 702 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 703 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 704 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 705 | handleEnable(msg.arg1 == 1); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 706 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 707 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 708 | case MESSAGE_DISABLE: |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 709 | mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 710 | if (mEnable && mBluetooth != null) { |
| 711 | waitForOnOff(true, false); |
| 712 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 713 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 714 | waitForOnOff(false, false); |
| 715 | } else { |
| 716 | mEnable = false; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 717 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 718 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 719 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 720 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 721 | case MESSAGE_REGISTER_ADAPTER: |
| 722 | { |
| 723 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 724 | boolean added = mCallbacks.register(callback); |
| 725 | Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added ); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 726 | } |
| 727 | break; |
| 728 | case MESSAGE_UNREGISTER_ADAPTER: |
| 729 | { |
| 730 | IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 731 | boolean removed = mCallbacks.unregister(callback); |
| 732 | Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 733 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 734 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 735 | case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: |
| 736 | { |
| 737 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 738 | mStateChangeCallbacks.register(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 739 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 740 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 741 | case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK: |
| 742 | { |
| 743 | IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj; |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 744 | mStateChangeCallbacks.unregister(callback); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 745 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 746 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 747 | case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: |
| 748 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 749 | if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED"); |
| 750 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 751 | //Remove timeout |
| 752 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 753 | |
| 754 | IBinder service = (IBinder) msg.obj; |
| 755 | synchronized(mConnection) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 756 | mBinding = false; |
| 757 | mBluetooth = IBluetooth.Stub.asInterface(service); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 758 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 759 | if (mConnection.isGetNameAddressOnly()) { |
| 760 | //Request GET NAME AND ADDRESS |
| 761 | Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); |
| 762 | mHandler.sendMessage(getMsg); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 763 | if (!mEnable) return; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 764 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 765 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 766 | mConnection.setGetNameAddressOnly(false); |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 767 | //Register callback object |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 768 | try { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 769 | mBluetooth.registerCallback(mBluetoothCallback); |
| 770 | } catch (RemoteException re) { |
| 771 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 772 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 773 | //Inform BluetoothAdapter instances that service is up |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 774 | sendBluetoothServiceUpCallback(); |
| 775 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 776 | //Do enable request |
| 777 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 778 | if (mQuietEnable == false) { |
| 779 | if(!mBluetooth.enable()) { |
| 780 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 781 | } |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | if(!mBluetooth.enableNoAutoConnect()) { |
| 786 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 787 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 788 | } |
| 789 | } catch (RemoteException e) { |
| 790 | Log.e(TAG,"Unable to call enable()",e); |
| 791 | } |
Fred | a8c6df0 | 2012-07-11 10:25:23 -0700 | [diff] [blame] | 792 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 793 | |
| 794 | if (!mEnable) { |
| 795 | waitForOnOff(true, false); |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 796 | handleDisable(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 797 | waitForOnOff(false, false); |
| 798 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 799 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 800 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 801 | case MESSAGE_TIMEOUT_BIND: { |
| 802 | Log.e(TAG, "MESSAGE_TIMEOUT_BIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 803 | synchronized(mConnection) { |
| 804 | mBinding = false; |
| 805 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 806 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 807 | } |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 808 | case MESSAGE_BLUETOOTH_STATE_CHANGE: |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 809 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 810 | int prevState = msg.arg1; |
| 811 | int newState = msg.arg2; |
| 812 | if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 813 | mState = newState; |
| 814 | bluetoothStateChangeHandler(prevState, newState); |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 815 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 816 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 817 | case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: |
| 818 | { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 819 | Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED"); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 820 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 821 | // if service is unbinded already, do nothing and return |
| 822 | if (mBluetooth == null) return; |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 823 | mBluetooth = null; |
| 824 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 825 | |
| 826 | if (mEnable) { |
| 827 | mEnable = false; |
| 828 | // Send a Bluetooth Restart message |
| 829 | Message restartMsg = mHandler.obtainMessage( |
| 830 | MESSAGE_RESTART_BLUETOOTH_SERVICE); |
| 831 | mHandler.sendMessageDelayed(restartMsg, |
| 832 | SERVICE_RESTART_TIME_MS); |
| 833 | } |
| 834 | |
| 835 | if (!mConnection.isGetNameAddressOnly()) { |
| 836 | sendBluetoothServiceDownCallback(); |
| 837 | |
| 838 | // Send BT state broadcast to update |
| 839 | // the BT icon correctly |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 840 | if ((mState == BluetoothAdapter.STATE_TURNING_ON) || |
| 841 | (mState == BluetoothAdapter.STATE_ON)) { |
| 842 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 843 | BluetoothAdapter.STATE_TURNING_OFF); |
| 844 | mState = BluetoothAdapter.STATE_TURNING_OFF; |
| 845 | } |
| 846 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 847 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
| 848 | BluetoothAdapter.STATE_OFF); |
| 849 | } |
| 850 | |
| 851 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 852 | mState = BluetoothAdapter.STATE_OFF; |
| 853 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 854 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 855 | } |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 856 | case MESSAGE_RESTART_BLUETOOTH_SERVICE: |
| 857 | { |
| 858 | Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:" |
| 859 | +" Restart IBluetooth service"); |
| 860 | /* Enable without persisting the setting as |
| 861 | it doesnt change when IBluetooth |
| 862 | service restarts */ |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 863 | mEnable = true; |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 864 | handleEnable(mQuietEnable); |
Syed Ibrahim M | 1223e5a | 2012-08-29 18:07:26 +0530 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 868 | case MESSAGE_TIMEOUT_UNBIND: |
| 869 | { |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 870 | Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 871 | synchronized(mConnection) { |
| 872 | mUnbinding = false; |
| 873 | } |
fredc | 649fe49 | 2012-04-19 01:07:18 -0700 | [diff] [blame] | 874 | break; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 875 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 876 | |
| 877 | case MESSAGE_USER_SWITCHED: |
| 878 | { |
| 879 | if (DBG) { |
| 880 | Log.d(TAG, "MESSAGE_USER_SWITCHED"); |
| 881 | } |
| 882 | mHandler.removeMessages(MESSAGE_USER_SWITCHED); |
| 883 | /* disable and enable BT when detect a user switch */ |
| 884 | if (mEnable && mBluetooth != null) { |
| 885 | synchronized (mConnection) { |
| 886 | if (mBluetooth != null) { |
| 887 | //Unregister callback object |
| 888 | try { |
| 889 | mBluetooth.unregisterCallback(mBluetoothCallback); |
| 890 | } catch (RemoteException re) { |
| 891 | Log.e(TAG, "Unable to unregister",re); |
| 892 | } |
| 893 | } |
| 894 | } |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 895 | |
| 896 | if (mState == BluetoothAdapter.STATE_TURNING_OFF) { |
| 897 | // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE |
| 898 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF); |
| 899 | mState = BluetoothAdapter.STATE_OFF; |
| 900 | } |
| 901 | if (mState == BluetoothAdapter.STATE_OFF) { |
| 902 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON); |
| 903 | mState = BluetoothAdapter.STATE_TURNING_ON; |
| 904 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 905 | |
| 906 | waitForOnOff(true, false); |
| 907 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 908 | if (mState == BluetoothAdapter.STATE_TURNING_ON) { |
| 909 | bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON); |
| 910 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 911 | |
| 912 | // disable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 913 | handleDisable(); |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 914 | // Pbap service need receive STATE_TURNING_OFF intent to close |
| 915 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON, |
| 916 | BluetoothAdapter.STATE_TURNING_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 917 | |
| 918 | waitForOnOff(false, true); |
| 919 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 920 | bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF, |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 921 | BluetoothAdapter.STATE_OFF); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 922 | sendBluetoothServiceDownCallback(); |
| 923 | synchronized (mConnection) { |
| 924 | if (mBluetooth != null) { |
| 925 | mBluetooth = null; |
| 926 | //Unbind |
| 927 | mContext.unbindService(mConnection); |
| 928 | } |
| 929 | } |
| 930 | SystemClock.sleep(100); |
| 931 | |
Zhihai Xu | 4e22ad3 | 2012-11-13 15:11:26 -0800 | [diff] [blame] | 932 | mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE); |
| 933 | mState = BluetoothAdapter.STATE_OFF; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 934 | // enable |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 935 | handleEnable(mQuietEnable); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 936 | } else if (mBinding || mBluetooth != null) { |
| 937 | Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED); |
| 938 | userMsg.arg2 = 1 + msg.arg2; |
| 939 | // if user is switched when service is being binding |
| 940 | // delay sending MESSAGE_USER_SWITCHED |
| 941 | mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS); |
| 942 | if (DBG) { |
| 943 | Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2); |
| 944 | } |
| 945 | } |
| 946 | break; |
| 947 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 950 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 951 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 952 | private void handleEnable(boolean quietMode) { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 953 | mQuietEnable = quietMode; |
| 954 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 955 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 956 | if ((mBluetooth == null) && (!mBinding)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 957 | //Start bind timeout and bind |
| 958 | Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND); |
| 959 | mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS); |
| 960 | mConnection.setGetNameAddressOnly(false); |
| 961 | Intent i = new Intent(IBluetooth.class.getName()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 962 | if (!mContext.bindService(i, mConnection,Context.BIND_AUTO_CREATE, |
Matthew Xie | fca9d63 | 2012-10-04 12:25:28 -0700 | [diff] [blame] | 963 | UserHandle.USER_CURRENT)) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 964 | mHandler.removeMessages(MESSAGE_TIMEOUT_BIND); |
| 965 | Log.e(TAG, "Fail to bind to: " + IBluetooth.class.getName()); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 966 | } else { |
| 967 | mBinding = true; |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 968 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 969 | } else if (mBluetooth != null) { |
| 970 | if (mConnection.isGetNameAddressOnly()) { |
| 971 | // if GetNameAddressOnly is set, we can clear this flag, |
| 972 | // so the service won't be unbind |
| 973 | // after name and address are saved |
| 974 | mConnection.setGetNameAddressOnly(false); |
| 975 | //Register callback object |
| 976 | try { |
| 977 | mBluetooth.registerCallback(mBluetoothCallback); |
| 978 | } catch (RemoteException re) { |
| 979 | Log.e(TAG, "Unable to register BluetoothCallback",re); |
| 980 | } |
| 981 | //Inform BluetoothAdapter instances that service is up |
| 982 | sendBluetoothServiceUpCallback(); |
| 983 | } |
| 984 | |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 985 | //Enable bluetooth |
| 986 | try { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 987 | if (!mQuietEnable) { |
| 988 | if(!mBluetooth.enable()) { |
| 989 | Log.e(TAG,"IBluetooth.enable() returned false"); |
| 990 | } |
| 991 | } |
| 992 | else { |
| 993 | if(!mBluetooth.enableNoAutoConnect()) { |
| 994 | Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false"); |
| 995 | } |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 996 | } |
| 997 | } catch (RemoteException e) { |
| 998 | Log.e(TAG,"Unable to call enable()",e); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1004 | private void handleDisable() { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1005 | synchronized(mConnection) { |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1006 | // don't need to disable if GetNameAddressOnly is set, |
| 1007 | // service will be unbinded after Name and Address are saved |
| 1008 | if ((mBluetooth != null) && (!mConnection.isGetNameAddressOnly())) { |
Matthew Xie | cdce0b9 | 2012-07-12 19:06:15 -0700 | [diff] [blame] | 1009 | if (DBG) Log.d(TAG,"Sending off request."); |
| 1010 | |
| 1011 | try { |
| 1012 | if(!mBluetooth.disable()) { |
| 1013 | Log.e(TAG,"IBluetooth.disable() returned false"); |
| 1014 | } |
| 1015 | } catch (RemoteException e) { |
| 1016 | Log.e(TAG,"Unable to call disable()",e); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1021 | |
| 1022 | private boolean checkIfCallerIsForegroundUser() { |
| 1023 | int foregroundUser; |
| 1024 | int callingUser = UserHandle.getCallingUserId(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1025 | int callingUid = Binder.getCallingUid(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1026 | long callingIdentity = Binder.clearCallingIdentity(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1027 | int callingAppId = UserHandle.getAppId(callingUid); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1028 | boolean valid = false; |
| 1029 | try { |
| 1030 | foregroundUser = ActivityManager.getCurrentUser(); |
Martijn Coenen | 8385c5a | 2012-11-29 10:14:16 -0800 | [diff] [blame] | 1031 | valid = (callingUser == foregroundUser) || |
| 1032 | callingAppId == Process.NFC_UID; |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1033 | if (DBG) { |
| 1034 | Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid |
| 1035 | + " callingUser=" + callingUser |
| 1036 | + " foregroundUser=" + foregroundUser); |
| 1037 | } |
| 1038 | } finally { |
| 1039 | Binder.restoreCallingIdentity(callingIdentity); |
| 1040 | } |
| 1041 | return valid; |
| 1042 | } |
| 1043 | |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1044 | private void bluetoothStateChangeHandler(int prevState, int newState) { |
| 1045 | if (prevState != newState) { |
| 1046 | //Notify all proxy objects first of adapter state change |
| 1047 | if (newState == BluetoothAdapter.STATE_ON || newState == BluetoothAdapter.STATE_OFF) { |
| 1048 | boolean isUp = (newState==BluetoothAdapter.STATE_ON); |
| 1049 | sendBluetoothStateCallback(isUp); |
| 1050 | |
| 1051 | //If Bluetooth is off, send service down event to proxy objects, and unbind |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1052 | if (!isUp && canUnbindBluetoothService()) { |
| 1053 | sendBluetoothServiceDownCallback(); |
| 1054 | unbindAndFinish(); |
Zhihai Xu | 40874a0 | 2012-10-08 17:57:03 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | //Send broadcast message to everyone else |
| 1059 | Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 1060 | intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState); |
| 1061 | intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState); |
| 1062 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 1063 | if (DBG) Log.d(TAG,"Bluetooth State Change Intent: " + prevState + " -> " + newState); |
| 1064 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL, |
| 1065 | BLUETOOTH_PERM); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * if on is true, wait for state become ON |
| 1071 | * if off is true, wait for state become OFF |
| 1072 | * if both on and off are false, wait for state not ON |
| 1073 | */ |
| 1074 | private boolean waitForOnOff(boolean on, boolean off) { |
| 1075 | int i = 0; |
| 1076 | while (i < 10) { |
| 1077 | synchronized(mConnection) { |
| 1078 | try { |
| 1079 | if (mBluetooth == null) break; |
| 1080 | if (on) { |
| 1081 | if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1082 | } else if (off) { |
| 1083 | if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true; |
| 1084 | } else { |
| 1085 | if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true; |
| 1086 | } |
| 1087 | } catch (RemoteException e) { |
| 1088 | Log.e(TAG, "getState()", e); |
| 1089 | break; |
| 1090 | } |
| 1091 | } |
| 1092 | if (on || off) { |
| 1093 | SystemClock.sleep(300); |
| 1094 | } else { |
| 1095 | SystemClock.sleep(50); |
| 1096 | } |
| 1097 | i++; |
| 1098 | } |
| 1099 | Log.e(TAG,"waitForOnOff time out"); |
| 1100 | return false; |
| 1101 | } |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1102 | |
Zhihai Xu | 401202b | 2012-12-03 11:36:21 -0800 | [diff] [blame] | 1103 | private void sendDisableMsg() { |
| 1104 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE)); |
| 1105 | } |
| 1106 | |
| 1107 | private void sendEnableMsg(boolean quietMode) { |
| 1108 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, |
| 1109 | quietMode ? 1 : 0, 0)); |
| 1110 | } |
| 1111 | |
Zhihai Xu | 681ae7f | 2012-11-12 15:14:18 -0800 | [diff] [blame] | 1112 | private boolean canUnbindBluetoothService() { |
| 1113 | synchronized(mConnection) { |
| 1114 | //Only unbind with mEnable flag not set |
| 1115 | //For race condition: disable and enable back-to-back |
| 1116 | //Avoid unbind right after enable due to callback from disable |
| 1117 | //Only unbind with Bluetooth at OFF state |
| 1118 | //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message |
| 1119 | try { |
| 1120 | if (mEnable || (mBluetooth == null)) return false; |
| 1121 | if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false; |
| 1122 | return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF); |
| 1123 | } catch (RemoteException e) { |
| 1124 | Log.e(TAG, "getState()", e); |
| 1125 | } |
| 1126 | } |
| 1127 | return false; |
| 1128 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1129 | } |