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