summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/bluetooth/DockService.java
blob: 245b10d0b8d49ab02802d82303a3656c101107e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.bluetooth;

import com.android.settings.R;
import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener,
        DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
        CompoundButton.OnCheckedChangeListener {

    // TODO check for waitlock leak
    // TODO check for service shutting down properly
    // TODO sticky vs non-sticky
    // TODO clean up static functions
    // TODO test after wiping data

    private static final String TAG = "DockService";

    // TODO clean up logs. Disable DEBUG flag for this file and receiver's too
    private static final boolean DEBUG = true;

    private static final String SHARED_PREFERENCE_KEY_AUTO_CONNECT_TO_DOCK = "auto_connect_to_dock";

    // Time allowed for the device to be undocked and redocked without severing
    // the bluetooth connection
    private static final long UNDOCKED_GRACE_PERIOD = 1000;

    // Msg for user wanting the UI to setup the dock
    private static final int MSG_TYPE_SHOW_UI = 111;
    // Msg for device docked event
    private static final int MSG_TYPE_DOCKED = 222;
    // Msg for device undocked event
    private static final int MSG_TYPE_UNDOCKED_TEMPORARY = 333;
    // Msg for undocked command to be process after UNDOCKED_GRACE_PERIOD millis
    // since MSG_TYPE_UNDOCKED_TEMPORARY
    private static final int MSG_TYPE_UNDOCKED_PERMANENT = 444;

    // Created in OnCreate()
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;
    private DockService mContext;
    private LocalBluetoothManager mBtManager;

    // Normally set after getting a docked event and unset when the connection
    // is severed. One exception is that mDevice could be null if the service
    // was started after the docked event.
    private BluetoothDevice mDevice;

    // Created and used for the duration of the dialog
    private AlertDialog mDialog;
    private Profile[] mProfiles;
    private boolean[] mCheckedItems;
    private int mStartIdAssociatedWithDialog;

    @Override
    public void onCreate() {
        if (DEBUG) Log.d(TAG, "onCreate");

        mBtManager = LocalBluetoothManager.getInstance(this);
        mContext = this;

        HandlerThread thread = new HandlerThread("DockService");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public void onDestroy() {
        if (DEBUG) Log.d(TAG, "onDestroy");
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        mServiceLooper.quit();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // not supported
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (DEBUG) Log.d(TAG, "onStartCommand startId:" + startId + " flags: " + flags);

        if (intent == null) {
            // Nothing to process, stop.
            if (DEBUG) Log.d(TAG, "START_NOT_STICKY - intent is null.");

            // NOTE: We MUST not call stopSelf() directly, since we need to
            // make sure the wake lock acquired by the Receiver is released.
            DockEventReceiver.finishStartingService(this, startId);
            return START_NOT_STICKY;
        }

        Message msg = parseIntent(intent);
        if (msg == null) {
            // Bad intent
            if (DEBUG) Log.d(TAG, "START_NOT_STICKY - Bad intent.");
            DockEventReceiver.finishStartingService(this, startId);
            return START_NOT_STICKY;
        }

        msg.arg2 = startId;
        processMessage(msg);

        return START_STICKY;
    }

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            processMessage(msg);
        }
    }

    // This method gets messages from both onStartCommand and mServiceHandler/mServiceLooper
    void processMessage(Message msg) {
        int msgType = msg.what;
        int state = msg.arg1;
        int startId = msg.arg2;
        BluetoothDevice device = (BluetoothDevice) msg.obj;

        if(DEBUG) Log.d(TAG, "processMessage: " + msgType + " state: " + state + " device = "
                + (msg.obj == null ? "null" : device.toString()));

        switch (msgType) {
            case MSG_TYPE_SHOW_UI:
                //TODO dismiss mDialog if exist? Shouldn't normally happen
                mDevice = device;
                createDialog(mContext, mDevice, state, startId);
                break;

            case MSG_TYPE_DOCKED:
                if (DEBUG) {
                    // TODO figure out why hasMsg always returns false if device
                    // is supplied
                    Log.d(TAG, "1 Has undock perm msg = "
                            + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT, mDevice));
                    Log.d(TAG, "2 Has undock perm msg = "
                            + mServiceHandler.hasMessages(MSG_TYPE_UNDOCKED_PERMANENT, device));
                }

                mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT);

                if (!device.equals(mDevice)) {
                    if (mDevice != null) {
                        // Not expected. Cleanup/undock existing
                        handleUndocked(mContext, mBtManager, mDevice);
                    }

                    mDevice = device;
                    if (getAutoConnectSetting(mBtManager, device.getAddress())) {
                        // Setting == auto connect
                        initBtSettings(mContext, device, state, false);
                        applyBtSettings();
                    } else {
                        createDialog(mContext, mDevice, state, startId);
                    }
                }
                break;

            case MSG_TYPE_UNDOCKED_PERMANENT:
                // Grace period passed. Disconnect.
                handleUndocked(mContext, mBtManager, device);
                break;

            case MSG_TYPE_UNDOCKED_TEMPORARY:
                // Undocked event received. Queue a delayed msg to sever connection
                Message newMsg = mServiceHandler.obtainMessage(MSG_TYPE_UNDOCKED_PERMANENT, state,
                        0, device);
                mServiceHandler.sendMessageDelayed(newMsg, UNDOCKED_GRACE_PERIOD);
                break;
        }

        if (mDialog == null) {
            // NOTE: We MUST not call stopSelf() directly, since we need to
            // make sure the wake lock acquired by the Receiver is released.
            DockEventReceiver.finishStartingService(DockService.this, msg.arg1);
        }
    }

    private Message parseIntent(Intent intent) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1234);

        if (DEBUG) {
            Log.d(TAG, "Action: " + intent.getAction() + " State:" + state
                    + " Device: " + (device == null ? "null" : device.getName()));
        }

        if (device == null) {
            Log.e(TAG, "device is null");
            return null;
        }

        int msgType;
        switch (state) {
            case Intent.EXTRA_DOCK_STATE_UNDOCKED:
                msgType = MSG_TYPE_UNDOCKED_TEMPORARY;
                break;
            case Intent.EXTRA_DOCK_STATE_DESK:
            case Intent.EXTRA_DOCK_STATE_CAR:
                if (DockEventReceiver.ACTION_DOCK_SHOW_UI.equals(intent.getAction())) {
                    msgType = MSG_TYPE_SHOW_UI;
                } else {
                    msgType = MSG_TYPE_DOCKED;
                }
                break;
            default:
                return null;
        }

        return mServiceHandler.obtainMessage(msgType, state, 0, device);
    }

    private boolean createDialog(DockService service, BluetoothDevice device, int state,
            int startId) {
        switch (state) {
            case Intent.EXTRA_DOCK_STATE_CAR:
            case Intent.EXTRA_DOCK_STATE_DESK:
                break;
            default:
                return false;
        }

        startForeground(0, new Notification());

        // Device in a new dock.
        boolean firstTime = !hasAutoConnectSetting(mBtManager, device.getAddress());

        CharSequence[] items = initBtSettings(service, device, state, firstTime);

        final AlertDialog.Builder ab = new AlertDialog.Builder(service);
        ab.setTitle(service.getString(R.string.bluetooth_dock_settings_title));

        // Profiles
        ab.setMultiChoiceItems(items, mCheckedItems, service);

        // Remember this settings
        LayoutInflater inflater = (LayoutInflater) service
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        float pixelScaleFactor = service.getResources().getDisplayMetrics().density;
        View view = inflater.inflate(R.layout.remember_dock_setting, null);
        CheckBox rememberCheckbox = (CheckBox) view.findViewById(R.id.remember);

        // check "Remember setting" by default if no value was saved
        boolean checked = firstTime || getAutoConnectSetting(mBtManager, device.getAddress());
        rememberCheckbox.setChecked(checked);
        rememberCheckbox.setOnCheckedChangeListener(this);
        int viewSpacingLeft = (int) (14 * pixelScaleFactor);
        int viewSpacingRight = (int) (14 * pixelScaleFactor);
        ab.setView(view, viewSpacingLeft, 0 /* top */, viewSpacingRight, 0 /* bottom */);
        if (DEBUG) {
            Log.d(TAG, "Auto connect = " + getAutoConnectSetting(mBtManager, device.getAddress()));
        }

        // Ok Button
        ab.setPositiveButton(service.getString(android.R.string.ok), service);

        mStartIdAssociatedWithDialog = startId;
        mDialog = ab.create();
        mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        mDialog.setOnDismissListener(service);
        mDialog.show();
        return true;
    }

    // Called when the individual bt profiles are clicked.
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (DEBUG) Log.d(TAG, "Item " + which + " changed to " + isChecked);
        mCheckedItems[which] = isChecked;
    }

    // Called when the "Remember" Checkbox is clicked
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (DEBUG) Log.d(TAG, "onCheckedChanged: Remember Settings = " + isChecked);
        saveAutoConnectSetting(mBtManager, mDevice.getAddress(), isChecked);
    }

    // Called when the dialog is dismissed
    public void onDismiss(DialogInterface dialog) {
        // NOTE: We MUST not call stopSelf() directly, since we need to
        // make sure the wake lock acquired by the Receiver is released.
        DockEventReceiver.finishStartingService(mContext, mStartIdAssociatedWithDialog);
        mContext.stopForeground(true);
    }

    // Called when clicked on the OK button
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            if (!hasAutoConnectSetting(mBtManager, mDevice.getAddress())) {
                saveAutoConnectSetting(mBtManager, mDevice.getAddress(), true);
            }

            // TODO move this to a background thread
            switch (mBtManager.getBluetoothState()) {
                case BluetoothAdapter.STATE_OFF:
                case BluetoothAdapter.STATE_TURNING_OFF:
                    mBtManager.getBluetoothAdapter().enable();
                    // TODO can I call connect right away? probably not.
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    // TODO wait? probably
                    break;
                case BluetoothAdapter.STATE_ON:
                    break;
            }

            applyBtSettings();
        }
    }

    private CharSequence[] initBtSettings(DockService service, BluetoothDevice device, int state,
            boolean firstTime) {
        // TODO Avoid hardcoding dock and profiles. Read from system properties
        int numOfProfiles = 0;
        switch (state) {
            case Intent.EXTRA_DOCK_STATE_DESK:
                numOfProfiles = 1;
                break;
            case Intent.EXTRA_DOCK_STATE_CAR:
                numOfProfiles = 2;
                break;
            default:
                return null;
        }

        mProfiles = new Profile[numOfProfiles];
        mCheckedItems = new boolean[numOfProfiles];
        CharSequence[] items = new CharSequence[numOfProfiles];

        int i = 0;
        switch (state) {
            case Intent.EXTRA_DOCK_STATE_CAR:
                items[i] = service.getString(R.string.bluetooth_dock_settings_headset);
                mProfiles[i] = Profile.HEADSET;
                if (firstTime) {
                    mCheckedItems[i] = true;
                } else {
                    mCheckedItems[i] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
                            Profile.HEADSET).isPreferred(device);
                }
                ++i;
                // fall through
            case Intent.EXTRA_DOCK_STATE_DESK:
                items[i] = service.getString(R.string.bluetooth_dock_settings_a2dp);
                mProfiles[i] = Profile.A2DP;
                if (firstTime) {
                    mCheckedItems[i] = true;
                } else {
                    mCheckedItems[i] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
                            Profile.A2DP).isPreferred(device);
                }
                break;
        }
        return items;
    }

    private void applyBtSettings() {
        if (mProfiles == null) return;
        for (int i = 0; i < mProfiles.length; i++) {
            LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                    .getProfileManager(mBtManager, mProfiles[i]);
            boolean isConnected = profileManager.isConnected(mDevice);
            CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(mContext, mBtManager,
                    mDevice);

            if (DEBUG) Log.d(TAG, mProfiles[i].toString() + " = " + mCheckedItems[i]);

            if (mCheckedItems[i] && !isConnected) {
                // Checked but not connected
                if (DEBUG) Log.d(TAG, "Connecting ");
                cachedDevice.connect(mProfiles[i]);
            } else if (!mCheckedItems[i] && isConnected) {
                // Unchecked but connected
                if (DEBUG) Log.d(TAG, "Disconnecting");
                cachedDevice.disconnect(mProfiles[i]);
            }
            profileManager.setPreferred(mDevice, mCheckedItems[i]);
            if (DEBUG) {
                if (mCheckedItems[i] != LocalBluetoothProfileManager.getProfileManager(
                        mBtManager, Profile.HEADSET).isPreferred(mDevice)) {
                    Log.e(TAG, "Can't save prefered value");
                }
            }
        }
    }

    void handleUndocked(Context context, LocalBluetoothManager localManager,
            BluetoothDevice device) {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        mDevice = null;
        CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
                localManager, device);
        cachedBluetoothDevice.disconnect();
    }

    void handleDocked(Context context, LocalBluetoothManager localManager,
            BluetoothDevice device, int state) {
        CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
                localManager, device);
        cachedBluetoothDevice.connect();
    }

    private static CachedBluetoothDevice getCachedBluetoothDevice(Context context,
            LocalBluetoothManager localManager, BluetoothDevice device) {
        CachedBluetoothDeviceManager cachedDeviceManager = localManager.getCachedDeviceManager();
        CachedBluetoothDevice cachedBluetoothDevice = cachedDeviceManager.findDevice(device);
        if (cachedBluetoothDevice == null) {
            cachedBluetoothDevice = new CachedBluetoothDevice(context, device);
        }
        return cachedBluetoothDevice;
    }

    public static boolean hasAutoConnectSetting(LocalBluetoothManager localManager, String addr) {
        return localManager.getSharedPreferences().contains(
                SHARED_PREFERENCE_KEY_AUTO_CONNECT_TO_DOCK + addr);
    }

    public static boolean getAutoConnectSetting(LocalBluetoothManager localManager, String addr) {
        return localManager.getSharedPreferences().getBoolean(
                SHARED_PREFERENCE_KEY_AUTO_CONNECT_TO_DOCK + addr, false);
    }

    public static void saveAutoConnectSetting(LocalBluetoothManager localManager, String addr,
            boolean autoConnect) {
        SharedPreferences.Editor editor = localManager.getSharedPreferences().edit();
        editor.putBoolean(SHARED_PREFERENCE_KEY_AUTO_CONNECT_TO_DOCK + addr, autoConnect);
        editor.commit();
    }

    // TODO Delete this method if not needed.
    private Notification getNotification(Service service) {
        CharSequence title = service.getString(R.string.dock_settings_title);

        Notification n = new Notification(R.drawable.ic_bt_headphones_a2dp, title, System
                .currentTimeMillis());

        CharSequence contentText = service.getString(R.string.dock_settings_summary);
        Intent notificationIntent = new Intent(service, DockEventReceiver.class);
        notificationIntent.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
        PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0);

        n.setLatestEventInfo(service, title, contentText, pendingIntent);
        return n;
    }
}