summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/accessories/AddAccessoryActivity.java
blob: 54d2350840b243fe6b1e1ccfb4bd5da9a6f449e2 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
 * Copyright (C) 2014 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.tv.settings.accessories;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiPlaybackClient;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.tv.settings.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Activity for detecting and adding (pairing) new bluetooth devices.
 */
public class AddAccessoryActivity extends FragmentActivity
        implements BluetoothDevicePairer.EventListener {

    private static final boolean DEBUG = false;
    private static final String TAG = "AddAccessoryActivity";

    private static final String SAVED_STATE_PREFERENCE_FRAGMENT =
            "AddAccessoryActivity.PREFERENCE_FRAGMENT";
    private static final String SAVED_STATE_CONTENT_FRAGMENT =
            "AddAccessoryActivity.CONTENT_FRAGMENT";
    private static final String SAVED_STATE_BLUETOOTH_DEVICES =
            "AddAccessoryActivity.BLUETOOTH_DEVICES";

    private static final String ADDRESS_NONE = "NONE";

    public static final String ACTION_CONNECT_INPUT =
            "com.google.android.intent.action.CONNECT_INPUT";

    public static final String INTENT_EXTRA_NO_INPUT_MODE = "no_input_mode";

    private static final int AUTOPAIR_COUNT = 10;

    private static final int MSG_UPDATE_VIEW = 1;
    private static final int MSG_REMOVE_CANCELED = 2;
    private static final int MSG_PAIRING_COMPLETE = 3;
    private static final int MSG_OP_TIMEOUT = 4;
    private static final int MSG_RESTART = 5;
    private static final int MSG_TRIGGER_SELECT_DOWN = 6;
    private static final int MSG_TRIGGER_SELECT_UP = 7;
    private static final int MSG_AUTOPAIR_TICK = 8;
    private static final int MSG_START_AUTOPAIR_COUNTDOWN = 9;

    private static final int CANCEL_MESSAGE_TIMEOUT = 3000;
    private static final int DONE_MESSAGE_TIMEOUT = 3000;
    private static final int PAIR_OPERATION_TIMEOUT = 120000;
    private static final int CONNECT_OPERATION_TIMEOUT = 60000;
    private static final int RESTART_DELAY = 3000;
    private static final int LONG_PRESS_DURATION = 3000;
    private static final int KEY_DOWN_TIME = 150;
    private static final int TIME_TO_START_AUTOPAIR_COUNT = 5000;
    private static final int EXIT_TIMEOUT_MILLIS = 90 * 1000;

    private AddAccessoryPreferenceFragment mPreferenceFragment;
    private AddAccessoryContentFragment mContentFragment;

    // members related to Bluetooth pairing
    private BluetoothDevicePairer mBluetoothPairer;
    private int mPreviousStatus = BluetoothDevicePairer.STATUS_NONE;
    private boolean mPairingSuccess = false;
    private boolean mPairingBluetooth = false;
    private List<BluetoothDevice> mBluetoothDevices;
    List<BluetoothDevice> mA11yAnnouncedDevices = new ArrayList<>();
    private String mCancelledAddress = ADDRESS_NONE;
    private String mCurrentTargetAddress = ADDRESS_NONE;
    private String mCurrentTargetStatus = "";
    private boolean mPairingInBackground = false;

    private boolean mDone = false;

    private boolean mHwKeyDown;
    private boolean mHwKeyDidSelect;
    private boolean mNoInputMode;

    // Internal message handler
    private final MessageHandler mMsgHandler = new MessageHandler();

    private static class MessageHandler extends Handler {

        private WeakReference<AddAccessoryActivity> mActivityRef = new WeakReference<>(null);

        public void setActivity(AddAccessoryActivity activity) {
            mActivityRef = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            final AddAccessoryActivity activity = mActivityRef.get();
            if (activity == null) {
                return;
            }
            switch (msg.what) {
                case MSG_UPDATE_VIEW:
                    activity.updateView();
                    break;
                case MSG_REMOVE_CANCELED:
                    activity.mCancelledAddress = ADDRESS_NONE;
                    activity.updateView();
                    break;
                case MSG_PAIRING_COMPLETE:
                    activity.finish();
                    break;
                case MSG_OP_TIMEOUT:
                    activity.handlePairingTimeout();
                    break;
                case MSG_RESTART:
                    if (activity.mBluetoothPairer != null) {
                        activity.mBluetoothPairer.start();
                        activity.mBluetoothPairer.cancelPairing();
                    }
                    break;
                case MSG_TRIGGER_SELECT_DOWN:
                    activity.sendKeyEvent(KeyEvent.KEYCODE_DPAD_CENTER, true);
                    activity.mHwKeyDidSelect = true;
                    sendEmptyMessageDelayed(MSG_TRIGGER_SELECT_UP, KEY_DOWN_TIME);
                    activity.cancelPairingCountdown();
                    break;
                case MSG_TRIGGER_SELECT_UP:
                    activity.sendKeyEvent(KeyEvent.KEYCODE_DPAD_CENTER, false);
                    break;
                case MSG_START_AUTOPAIR_COUNTDOWN:
                    activity.setPairingText(
                            activity.getString(R.string.accessories_autopair_msg, AUTOPAIR_COUNT));
                    sendMessageDelayed(obtainMessage(MSG_AUTOPAIR_TICK,
                            AUTOPAIR_COUNT, 0, null), 1000);
                    break;
                case MSG_AUTOPAIR_TICK:
                    int countToAutoPair = msg.arg1 - 1;
                    if (countToAutoPair <= 0) {
                        activity.setPairingText(null);
                        // AutoPair
                        activity.startAutoPairing();
                    } else {
                        activity.setPairingText(
                                activity.getString(R.string.accessories_autopair_msg,
                                        countToAutoPair));
                        sendMessageDelayed(obtainMessage(MSG_AUTOPAIR_TICK,
                                countToAutoPair, 0, null), 1000);
                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    private final Handler mAutoExitHandler = new Handler();

    private final Runnable mAutoExitRunnable = this::finish;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RestrictedLockUtils.EnforcedAdmin admin =
                RestrictedLockUtilsInternal.checkIfRestrictionEnforced(this,
                        UserManager.DISALLOW_CONFIG_BLUETOOTH, UserHandle.myUserId());
        if (admin != null) {
            RestrictedLockUtils.sendShowAdminSupportDetailsIntent(this, admin);
            finish();
            return;
        }

        // Normally, we set contentDescription for View elements in resource files for Talkback to
        // announce when the element is being focused. However, this Activity is special as users
        // may not have a connected remote control so we need to make an accessibility announcement
        // when the Activity is launched. As the description is flexible, we construct it in runtime
        // instead of setting the label for this Activity in the AndroidManifest.xml.
        setTitle(getInitialAccessibilityAnnouncement());

        setContentView(R.layout.lb_dialog_fragment);

        mMsgHandler.setActivity(this);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        mNoInputMode = getIntent().getBooleanExtra(INTENT_EXTRA_NO_INPUT_MODE, false);
        mHwKeyDown = false;

        if (savedInstanceState == null) {
            mBluetoothDevices = new ArrayList<>();
        } else {
            mBluetoothDevices =
                    savedInstanceState.getParcelableArrayList(SAVED_STATE_BLUETOOTH_DEVICES);
        }

        final FragmentManager fm = getSupportFragmentManager();
        if (savedInstanceState == null) {
            mPreferenceFragment = AddAccessoryPreferenceFragment.newInstance();
            mContentFragment = AddAccessoryContentFragment.newInstance();
            fm.beginTransaction()
                    .add(R.id.action_fragment, mPreferenceFragment)
                    .add(R.id.content_fragment, mContentFragment)
                    .commit();
        } else {
            mPreferenceFragment = (AddAccessoryPreferenceFragment)
                    fm.getFragment(savedInstanceState,
                            SAVED_STATE_PREFERENCE_FRAGMENT);
            mContentFragment = (AddAccessoryContentFragment)
                    fm.getFragment(savedInstanceState,
                            SAVED_STATE_CONTENT_FRAGMENT);
        }
        sendCecOtpCommand((result) -> {
            if (result == HdmiControlManager.RESULT_SUCCESS) {
                Log.i(TAG, "One Touch Play successful");
            } else {
                Log.i(TAG, "One Touch Play failed");
            }
        });

        rearrangeViews();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState,
                SAVED_STATE_PREFERENCE_FRAGMENT, mPreferenceFragment);
        getSupportFragmentManager().putFragment(outState,
                SAVED_STATE_CONTENT_FRAGMENT, mContentFragment);
        outState.putParcelableList(SAVED_STATE_BLUETOOTH_DEVICES, mBluetoothDevices);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (DEBUG) {
            Log.d(TAG, "onStart() mPairingInBackground = " + mPairingInBackground);
        }

        // Only do the following if we are not coming back to this activity from
        // the Secure Pairing activity.
        if (!mPairingInBackground) {
            startBluetoothPairer();
        }

        mPairingInBackground = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mNoInputMode) {
            // Start timer count down for exiting activity.
            if (DEBUG) Log.d(TAG, "starting auto-exit timer");
            mAutoExitHandler.postDelayed(mAutoExitRunnable, EXIT_TIMEOUT_MILLIS);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (DEBUG) Log.d(TAG, "stopping auto-exit timer");
        mAutoExitHandler.removeCallbacks(mAutoExitRunnable);
    }


    @Override
    public void onStop() {
        if (DEBUG) {
            Log.d(TAG, "onStop()");
        }
        if (!mPairingBluetooth) {
            stopBluetoothPairer();
            mMsgHandler.removeCallbacksAndMessages(null);
        } else {
            // allow activity to remain in the background while we perform the
            // BT Secure pairing.
            mPairingInBackground = true;
        }

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopBluetoothPairer();
        mMsgHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            if (mPairingBluetooth && !mDone) {
                cancelBtPairing();
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @SuppressWarnings("MissingSuperCall") // TODO: Fix me
    @Override
    public void onNewIntent(Intent intent) {
        if (ACTION_CONNECT_INPUT.equals(intent.getAction()) &&
                (intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) == 0) {

            KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT, KeyEvent.class);
            if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_PAIRING) {
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    onHwKeyEvent(false);
                } else if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    onHwKeyEvent(true);
                }
            }
        } else {
            setIntent(intent);
        }
    }

    public void onActionClicked(String address) {
        cancelPairingCountdown();
        if (!mDone) {
            btDeviceClicked(address);
        }
    }

    // Events related to a device HW key
    private void onHwKeyEvent(boolean keyDown) {
        if (!mHwKeyDown) {
            // HW key was in UP state before
            if (keyDown) {
                // Back key pressed down
                mHwKeyDown = true;
                mHwKeyDidSelect = false;
                mMsgHandler.sendEmptyMessageDelayed(MSG_TRIGGER_SELECT_DOWN, LONG_PRESS_DURATION);
            }
        } else {
            // HW key was in DOWN state before
            if (!keyDown) {
                // HW key released
                mHwKeyDown = false;
                mMsgHandler.removeMessages(MSG_TRIGGER_SELECT_DOWN);
                if (!mHwKeyDidSelect) {
                    // key wasn't pressed long enough for selection, move selection
                    // to next item.
                    mPreferenceFragment.advanceSelection();
                }
                mHwKeyDidSelect = false;
            }
        }
    }

    private void sendKeyEvent(int keyCode, boolean down) {
        InputManager iMgr = (InputManager) getSystemService(INPUT_SERVICE);
        if (iMgr != null) {
            long time = SystemClock.uptimeMillis();
            KeyEvent evt = new KeyEvent(time, time,
                    down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP,
                    keyCode, 0);
            iMgr.injectInputEvent(evt, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
        }
    }

    protected void updateView() {
        if (mPreferenceFragment == null || isFinishing()) {
            // view not yet ready, update will happen on first layout event
            // or alternately we're done and don't need to do anything
            return;
        }

        int prevNumDevices = mPreferenceFragment.getPreferenceScreen().getPreferenceCount();

        mPreferenceFragment.updateList(mBluetoothDevices, mCurrentTargetAddress,
                mCurrentTargetStatus, mCancelledAddress);

        if (mNoInputMode) {
            if (DEBUG) Log.d(TAG, "stopping auto-exit timer");
            mAutoExitHandler.removeCallbacks(mAutoExitRunnable);
            if (mBluetoothDevices.size() == 1 && prevNumDevices == 0) {
                // first device added, start counter for autopair
                mMsgHandler.sendEmptyMessageDelayed(MSG_START_AUTOPAIR_COUNTDOWN,
                        TIME_TO_START_AUTOPAIR_COUNT);
            } else {

                // Start timer count down for exiting activity.
                if (DEBUG) Log.d(TAG, "starting auto-exit timer");
                mAutoExitHandler.postDelayed(mAutoExitRunnable, EXIT_TIMEOUT_MILLIS);

                if (mBluetoothDevices.size() > 1) {
                    // More than one device found, cancel auto pair
                    cancelPairingCountdown();
                }
           }
        }

        TransitionManager.beginDelayedTransition(findViewById(R.id.content_frame));

        rearrangeViews();
    }

    private void rearrangeViews() {
        final boolean empty = mBluetoothDevices.isEmpty();

        final View contentView = findViewById(R.id.content_fragment);
        final ViewGroup.LayoutParams contentLayoutParams = contentView.getLayoutParams();
        contentLayoutParams.width = empty ? ViewGroup.LayoutParams.MATCH_PARENT :
                getResources().getDimensionPixelSize(R.dimen.lb_content_section_width);
        contentView.setLayoutParams(contentLayoutParams);

        mContentFragment.setContentWidth(empty
                ? getResources().getDimensionPixelSize(R.dimen.progress_fragment_content_width)
                : getResources().getDimensionPixelSize(R.dimen.bt_progress_width_narrow));
    }

    private void setPairingText(CharSequence text) {
        if (mContentFragment != null) {
            mContentFragment.setExtraText(text);
        }
    }

    private void cancelPairingCountdown() {
        // Cancel countdown
        mMsgHandler.removeMessages(MSG_AUTOPAIR_TICK);
        mMsgHandler.removeMessages(MSG_START_AUTOPAIR_COUNTDOWN);
        setPairingText(null);
    }

    private void setTimeout(int timeout) {
        cancelTimeout();
        mMsgHandler.sendEmptyMessageDelayed(MSG_OP_TIMEOUT, timeout);
    }

    private void cancelTimeout() {
        mMsgHandler.removeMessages(MSG_OP_TIMEOUT);
    }

    protected void startAutoPairing() {
        if (mBluetoothDevices.size() > 0) {
            onActionClicked(mBluetoothDevices.get(0).getAddress());
        }
    }

    private void btDeviceClicked(String clickedAddress) {
        if (mBluetoothPairer != null && !mBluetoothPairer.isInProgress()) {
            if (mBluetoothPairer.getStatus() == BluetoothDevicePairer.STATUS_WAITING_TO_PAIR &&
                    mBluetoothPairer.getTargetDevice() != null) {
                cancelBtPairing();
            } else {
                if (DEBUG) {
                    Log.d(TAG, "Looking for " + clickedAddress +
                            " in available devices to start pairing");
                }
                for (BluetoothDevice target : mBluetoothDevices) {
                    if (target.getAddress().equalsIgnoreCase(clickedAddress)) {
                        if (DEBUG) {
                            Log.d(TAG, "Found it!");
                        }
                        mCancelledAddress = ADDRESS_NONE;
                        setPairingBluetooth(true);
                        mBluetoothPairer.startPairing(target);
                        break;
                    }
                }
            }
        }
    }

    private void cancelBtPairing() {
        // cancel current request to pair
        if (mBluetoothPairer != null) {
            if (mBluetoothPairer.getTargetDevice() != null) {
                mCancelledAddress = mBluetoothPairer.getTargetDevice().getAddress();
            } else {
                mCancelledAddress = ADDRESS_NONE;
            }
            mBluetoothPairer.cancelPairing();
        }
        mPairingSuccess = false;
        setPairingBluetooth(false);
        mMsgHandler.sendEmptyMessageDelayed(MSG_REMOVE_CANCELED,
                CANCEL_MESSAGE_TIMEOUT);
    }

    private void setPairingBluetooth(boolean pairing) {
        if (mPairingBluetooth != pairing) {
            mPairingBluetooth = pairing;
        }
    }

    private void startBluetoothPairer() {
        stopBluetoothPairer();
        mBluetoothPairer = new BluetoothDevicePairer(this, this);
        mBluetoothPairer.start();

        mBluetoothPairer.disableAutoPairing();

        mPairingSuccess = false;
        statusChanged();
    }

    private void stopBluetoothPairer() {
        if (mBluetoothPairer != null) {
            mBluetoothPairer.setListener(null);
            mBluetoothPairer.dispose();
            mBluetoothPairer = null;
        }
    }

    private String getMessageForStatus(int status) {
        final int msgId;
        String msg;

        switch (status) {
            case BluetoothDevicePairer.STATUS_WAITING_TO_PAIR:
            case BluetoothDevicePairer.STATUS_PAIRING:
                msgId = R.string.accessory_state_pairing;
                break;
            case BluetoothDevicePairer.STATUS_CONNECTING:
                msgId = R.string.accessory_state_connecting;
                break;
            case BluetoothDevicePairer.STATUS_ERROR:
                msgId = R.string.accessory_state_error;
                break;
            default:
                return "";
        }

        msg = getString(msgId);

        return msg;
    }

    @Override
    public void statusChanged() {
        if (mBluetoothPairer == null) return;

        int numDevices = mBluetoothPairer.getAvailableDevices().size();
        int status = mBluetoothPairer.getStatus();
        int oldStatus = mPreviousStatus;
        mPreviousStatus = status;

        String address = mBluetoothPairer.getTargetDevice() == null ? ADDRESS_NONE :
                mBluetoothPairer.getTargetDevice().getAddress();

        if (DEBUG) {
            String state = "?";
            switch (status) {
                case BluetoothDevicePairer.STATUS_NONE:
                    state = "BluetoothDevicePairer.STATUS_NONE";
                    break;
                case BluetoothDevicePairer.STATUS_SCANNING:
                    state = "BluetoothDevicePairer.STATUS_SCANNING";
                    break;
                case BluetoothDevicePairer.STATUS_WAITING_TO_PAIR:
                    state = "BluetoothDevicePairer.STATUS_WAITING_TO_PAIR";
                    break;
                case BluetoothDevicePairer.STATUS_PAIRING:
                    state = "BluetoothDevicePairer.STATUS_PAIRING";
                    break;
                case BluetoothDevicePairer.STATUS_CONNECTING:
                    state = "BluetoothDevicePairer.STATUS_CONNECTING";
                    break;
                case BluetoothDevicePairer.STATUS_ERROR:
                    state = "BluetoothDevicePairer.STATUS_ERROR";
                    break;
                case BluetoothDevicePairer.STATUS_SUCCEED_BREDRMOUSE:
                    state = "BluetoothDevicePairer.STATUS_SUCCEED_BREDRMOUSE";
                    break;
            }
            long time = mBluetoothPairer.getNextStageTime() - SystemClock.elapsedRealtime();
            Log.d(TAG, "Update received, number of devices:" + numDevices + " state: " +
                    state + " target device: " + address + " time to next event: " + time);
        }

        mBluetoothDevices.clear();
        mBluetoothDevices.addAll(mBluetoothPairer.getAvailableDevices());
        announceNewDevicesForA11y();

        cancelTimeout();

        switch (status) {
            case BluetoothDevicePairer.STATUS_NONE:
                // if we just connected to something or just tried to connect
                // to something, restart scanning just in case the user wants
                // to pair another device.
                if (oldStatus == BluetoothDevicePairer.STATUS_CONNECTING) {
                    if (mPairingSuccess) {
                        // Pairing complete
                        mCurrentTargetStatus = getString(R.string.accessory_state_paired);
                        mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
                        mMsgHandler.sendEmptyMessageDelayed(MSG_PAIRING_COMPLETE,
                                DONE_MESSAGE_TIMEOUT);
                        mDone = true;

                        // Done, return here and just wait for the message
                        // to close the activity
                        return;
                    }
                    if (DEBUG) {
                        Log.d(TAG, "Invalidating and restarting.");
                    }

                    mBluetoothPairer.invalidateDevice(mBluetoothPairer.getTargetDevice());
                    mBluetoothPairer.start();
                    mBluetoothPairer.cancelPairing();
                    setPairingBluetooth(false);

                    // if this looks like a successful connection run, reflect
                    // this in the UI, otherwise use the default message
                    if (!mPairingSuccess && BluetoothDevicePairer.hasValidInputDevice(this)) {
                        mPairingSuccess = true;
                    }
                }
                break;
            case BluetoothDevicePairer.STATUS_SCANNING:
                mPairingSuccess = false;
                break;
            case BluetoothDevicePairer.STATUS_WAITING_TO_PAIR:
                break;
            case BluetoothDevicePairer.STATUS_PAIRING:
                // reset the pairing success value since this is now a new
                // pairing run
                mPairingSuccess = true;
                setTimeout(PAIR_OPERATION_TIMEOUT);
                break;
            case BluetoothDevicePairer.STATUS_CONNECTING:
                setTimeout(CONNECT_OPERATION_TIMEOUT);
                break;
            case BluetoothDevicePairer.STATUS_ERROR:
                mPairingSuccess = false;
                setPairingBluetooth(false);
                if (mNoInputMode) {
                    clearDeviceList();
                }
                break;
            case BluetoothDevicePairer.STATUS_SUCCEED_BREDRMOUSE:
                // Pairing complete
                mCurrentTargetStatus = getString(R.string.accessory_state_paired);
                mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
                mMsgHandler.sendEmptyMessageDelayed(MSG_PAIRING_COMPLETE,
                        DONE_MESSAGE_TIMEOUT);
                mDone = true;
                // Done, return here and just wait for the message
                // to close the activity
                return;
        }

        mCurrentTargetAddress = address;
        mCurrentTargetStatus = getMessageForStatus(status);
        mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
    }

    /**
     * Announce device names as they become visible.
     */
    private void announceNewDevicesForA11y() {
        Log.d(TAG, "announceNewDevicesForA11y");

        // Filter out the already announced devices from the visible list
        List<BluetoothDevice> newDevicesToAnnounce =
                mBluetoothDevices
                        .stream()
                        .filter(device-> !mA11yAnnouncedDevices.contains(device))
                        .collect(Collectors.toList());

        // Create announcement string
        StringBuilder sb = new StringBuilder();
        for (BluetoothDevice device : newDevicesToAnnounce) {
            sb.append(device.getName()).append(" ");
        }
        getWindow().getDecorView().setAccessibilityPaneTitle(sb.toString());

        mA11yAnnouncedDevices = new ArrayList<>(mBluetoothDevices);
    }

    private void clearDeviceList() {
        mBluetoothDevices.clear();
        mBluetoothPairer.clearDeviceList();
    }

    private void handlePairingTimeout() {
        if (mPairingInBackground) {
            finish();
        } else {
            // Either Pairing or Connecting timeout out.
            // Display error message and post delayed message to the scanning process.
            mPairingSuccess = false;
            if (mBluetoothPairer != null) {
                mBluetoothPairer.cancelPairing();
            }
            mCurrentTargetStatus = getString(R.string.accessory_state_error);
            mMsgHandler.sendEmptyMessage(MSG_UPDATE_VIEW);
            mMsgHandler.sendEmptyMessageDelayed(MSG_RESTART, RESTART_DELAY);
        }
    }

    private void sendCecOtpCommand(HdmiPlaybackClient.OneTouchPlayCallback callback) {
        HdmiControlManager hdmiControlManager =
                (HdmiControlManager) getSystemService(HDMI_CONTROL_SERVICE);
        if (hdmiControlManager == null) {
            Log.wtf(TAG, "no HdmiControlManager");
            return;
        }
        HdmiPlaybackClient client = hdmiControlManager.getPlaybackClient();
        if (client == null) {
            if (DEBUG) Log.d(TAG, "no HdmiPlaybackClient");
            return;
        }
        client.oneTouchPlay(callback);
    }

    /**
     * @return String containing text to be announced when the Activity is visible to the users
     * when Talkback is on.
     */
    private String getInitialAccessibilityAnnouncement() {
        StringBuilder sb =
                new StringBuilder(getString(R.string.accessories_add_accessibility_title));
        sb.append(getString(R.string.accessories_add_title));
        sb.append(getString(R.string.accessories_add_bluetooth_inst));
        String extra = AddAccessoryContentFragment.getExtraInstructionContentDescription(this);
        if (extra != null) {
            sb.append(extra);
        }
        return sb.toString();
    }

    List<BluetoothDevice> getBluetoothDevices() {
        return mBluetoothDevices;
    }

    String getCurrentTargetAddress() {
        return mCurrentTargetAddress;
    }

    String getCurrentTargetStatus() {
        return mCurrentTargetStatus;
    }

    String getCancelledAddress() {
        return mCancelledAddress;
    }
}