summaryrefslogtreecommitdiff
path: root/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
blob: d8bdf32dbf4eeae9860353cfb08a1773eee99df6 (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
/*
 * Copyright (C) 2018 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.wifi.dpp;

import static android.net.wifi.WifiInfo.sanitizeSsid;

import android.app.Activity;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.net.wifi.EasyConnectStatusCallback;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.SimpleClock;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.util.Size;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModelProviders;

import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wifi.qrcode.QrCamera;
import com.android.settings.wifi.qrcode.QrDecorateView;
import com.android.wifitrackerlib.WifiEntry;
import com.android.wifitrackerlib.WifiPickerTracker;

import java.time.Clock;
import java.time.ZoneOffset;
import java.util.List;

public class WifiDppQrCodeScannerFragment extends WifiDppQrCodeBaseFragment implements
        SurfaceTextureListener,
        QrCamera.ScannerCallback,
        WifiManager.ActionListener {
    private static final String TAG = "WifiDppQrCodeScanner";

    /** Message sent to hide error message */
    private static final int MESSAGE_HIDE_ERROR_MESSAGE = 1;

    /** Message sent to show error message */
    private static final int MESSAGE_SHOW_ERROR_MESSAGE = 2;

    /** Message sent to manipulate Wi-Fi DPP QR code */
    private static final int MESSAGE_SCAN_WIFI_DPP_SUCCESS = 3;

    /** Message sent to manipulate ZXing Wi-Fi QR code */
    private static final int MESSAGE_SCAN_ZXING_WIFI_FORMAT_SUCCESS = 4;

    private static final long SHOW_ERROR_MESSAGE_INTERVAL = 10000;
    private static final long SHOW_SUCCESS_SQUARE_INTERVAL = 1000;

    // Key for Bundle usage
    private static final String KEY_IS_CONFIGURATOR_MODE = "key_is_configurator_mode";
    private static final String KEY_LATEST_ERROR_CODE = "key_latest_error_code";
    public static final String KEY_WIFI_CONFIGURATION = "key_wifi_configuration";

    private static final int ARG_RESTART_CAMERA = 1;

    // Max age of tracked WifiEntries.
    private static final long MAX_SCAN_AGE_MILLIS = 15_000;
    // Interval between initiating WifiPickerTracker scans.
    private static final long SCAN_INTERVAL_MILLIS = 10_000;

    private QrCamera mCamera;
    private TextureView mTextureView;
    private QrDecorateView mDecorateView;
    private TextView mErrorMessage;

    /** true if the fragment working for configurator, false enrollee*/
    private boolean mIsConfiguratorMode;

    /** The SSID of the Wi-Fi network which the user specify to enroll */
    private String mSsid;

    /** QR code data scanned by camera */
    private WifiQrCode mWifiQrCode;

    /** The WifiConfiguration connecting for enrollee usage */
    private WifiConfiguration mEnrolleeWifiConfiguration;

    private int mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_FAILURE_NONE;

    private WifiPickerTracker mWifiPickerTracker;
    private HandlerThread mWorkerThread;

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_HIDE_ERROR_MESSAGE:
                    mErrorMessage.setVisibility(View.INVISIBLE);
                    break;

                case MESSAGE_SHOW_ERROR_MESSAGE:
                    final String errorMessage = (String) msg.obj;

                    mErrorMessage.setVisibility(View.VISIBLE);
                    mErrorMessage.setText(errorMessage);
                    mErrorMessage.sendAccessibilityEvent(
                            AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);

                    // Cancel any pending messages to hide error view and requeue the message so
                    // user has time to see error
                    removeMessages(MESSAGE_HIDE_ERROR_MESSAGE);
                    sendEmptyMessageDelayed(MESSAGE_HIDE_ERROR_MESSAGE,
                            SHOW_ERROR_MESSAGE_INTERVAL);

                    if (msg.arg1 == ARG_RESTART_CAMERA) {
                        setProgressBarShown(false);
                        mDecorateView.setFocused(false);
                        restartCamera();
                    }
                    break;

                case MESSAGE_SCAN_WIFI_DPP_SUCCESS:
                    if (mScanWifiDppSuccessListener == null) {
                        // mScanWifiDppSuccessListener may be null after onDetach(), do nothing here
                        return;
                    }
                    mScanWifiDppSuccessListener.onScanWifiDppSuccess((WifiQrCode)msg.obj);

                    if (!mIsConfiguratorMode) {
                        setProgressBarShown(true);
                        startWifiDppEnrolleeInitiator((WifiQrCode)msg.obj);
                        updateEnrolleeSummary();
                        mSummary.sendAccessibilityEvent(
                                AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
                    }

                    notifyUserForQrCodeRecognition();
                    break;

                case MESSAGE_SCAN_ZXING_WIFI_FORMAT_SUCCESS:
                    // We may get 2 WifiConfiguration if the QR code has no password in it,
                    // one for open network and one for enhanced open network.
                    final WifiManager wifiManager =
                            getContext().getSystemService(WifiManager.class);
                    final WifiNetworkConfig qrCodeWifiNetworkConfig =
                            (WifiNetworkConfig)msg.obj;
                    final List<WifiConfiguration> qrCodeWifiConfigurations =
                            qrCodeWifiNetworkConfig.getWifiConfigurations();

                    // Adds all Wi-Fi networks in QR code to the set of configured networks and
                    // connects to it if it's reachable.
                    boolean hasHiddenOrReachableWifiNetwork = false;
                    for (WifiConfiguration qrCodeWifiConfiguration : qrCodeWifiConfigurations) {
                        final int id = wifiManager.addNetwork(qrCodeWifiConfiguration);
                        if (id == -1) {
                            continue;
                        }
                        wifiManager.enableNetwork(id, /* attemptConnect */ false);
                        // WifiTracker only contains a hidden SSID Wi-Fi network if it's saved.
                        // We can't check if a hidden SSID Wi-Fi network is reachable in advance.
                        if (qrCodeWifiConfiguration.hiddenSSID ||
                                isReachableWifiNetwork(qrCodeWifiConfiguration)) {
                            hasHiddenOrReachableWifiNetwork = true;
                            mEnrolleeWifiConfiguration = qrCodeWifiConfiguration;
                            wifiManager.connect(id,
                                    /* listener */ WifiDppQrCodeScannerFragment.this);
                        }
                    }

                    if (!hasHiddenOrReachableWifiNetwork) {
                        showErrorMessageAndRestartCamera(
                                R.string.wifi_dpp_check_connection_try_again);
                        return;
                    }

                    mMetricsFeatureProvider.action(
                            mMetricsFeatureProvider.getAttribution(getActivity()),
                            SettingsEnums.ACTION_SETTINGS_ENROLL_WIFI_QR_CODE,
                            SettingsEnums.SETTINGS_WIFI_DPP_ENROLLEE,
                            /* key */ null,
                            /* value */ Integer.MIN_VALUE);

                    notifyUserForQrCodeRecognition();
                    break;

                default:
            }
        }
    };

    @UiThread
    private void notifyUserForQrCodeRecognition() {
        if (mCamera != null) {
            mCamera.stop();
        }

        mDecorateView.setFocused(true);
        mErrorMessage.setVisibility(View.INVISIBLE);

        WifiDppUtils.triggerVibrationForQrCodeRecognition(getContext());
    }

    private boolean isReachableWifiNetwork(WifiConfiguration wifiConfiguration) {
        final List<WifiEntry> wifiEntries = mWifiPickerTracker.getWifiEntries();
        final WifiEntry connectedWifiEntry = mWifiPickerTracker.getConnectedWifiEntry();
        if (connectedWifiEntry != null) {
            // Add connected WifiEntry to prevent fail toast to users when it's connected.
            wifiEntries.add(connectedWifiEntry);
        }

        for (WifiEntry wifiEntry : wifiEntries) {
            if (!TextUtils.equals(wifiEntry.getSsid(), sanitizeSsid(wifiConfiguration.SSID))) {
                continue;
            }
            final int security =
                    WifiDppUtils.getSecurityTypeFromWifiConfiguration(wifiConfiguration);
            if (security == wifiEntry.getSecurity()) {
                return true;
            }

            // Default security type of PSK/SAE transition mode WifiEntry is SECURITY_PSK and
            // there is no way to know if a WifiEntry is of transition mode. Give it a chance.
            if (security == WifiEntry.SECURITY_SAE
                    && wifiEntry.getSecurity() == WifiEntry.SECURITY_PSK) {
                return true;
            }
        }
        return false;
    }

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

        if (savedInstanceState != null) {
            mIsConfiguratorMode = savedInstanceState.getBoolean(KEY_IS_CONFIGURATOR_MODE);
            mLatestStatusCode = savedInstanceState.getInt(KEY_LATEST_ERROR_CODE);
            mEnrolleeWifiConfiguration = savedInstanceState.getParcelable(KEY_WIFI_CONFIGURATION);
        }

        final WifiDppInitiatorViewModel model =
                ViewModelProviders.of(this).get(WifiDppInitiatorViewModel.class);

        model.getEnrolleeSuccessNetworkId().observe(this, networkId -> {
            // After configuration change, observe callback will be triggered,
            // do nothing for this case if a handshake does not end
            if (model.isWifiDppHandshaking()) {
                return;
            }

            new EasyConnectEnrolleeStatusCallback().onEnrolleeSuccess(networkId.intValue());
        });

        model.getStatusCode().observe(this, statusCode -> {
            // After configuration change, observe callback will be triggered,
            // do nothing for this case if a handshake does not end
            if (model.isWifiDppHandshaking()) {
                return;
            }

            int code = statusCode.intValue();
            Log.d(TAG, "Easy connect enrollee callback onFailure " + code);
            new EasyConnectEnrolleeStatusCallback().onFailure(code);
        });
    }

    @Override
    public void onPause() {
        if (mCamera != null) {
            mCamera.stop();
        }

        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!isWifiDppHandshaking()) {
            restartCamera();
        }
    }

    @Override
    public int getMetricsCategory() {
        if (mIsConfiguratorMode) {
            return SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR;
        } else {
            return SettingsEnums.SETTINGS_WIFI_DPP_ENROLLEE;
        }
    }

    // Container Activity must implement this interface
    public interface OnScanWifiDppSuccessListener {
        void onScanWifiDppSuccess(WifiQrCode wifiQrCode);
    }
    private OnScanWifiDppSuccessListener mScanWifiDppSuccessListener;

    /**
     * Configurator container activity of the fragment should create instance with this constructor.
     */
    public WifiDppQrCodeScannerFragment() {
        super();

        mIsConfiguratorMode = true;
    }

    /**
     * Enrollee container activity of the fragment should create instance with this constructor and
     * specify the SSID string of the WI-Fi network to be provisioned.
     */
    WifiDppQrCodeScannerFragment(String ssid) {
        super();

        mIsConfiguratorMode = false;
        mSsid = ssid;
    }

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

        mWorkerThread = new HandlerThread(
                TAG + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
                Process.THREAD_PRIORITY_BACKGROUND);
        mWorkerThread.start();
        final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
            @Override
            public long millis() {
                return SystemClock.elapsedRealtime();
            }
        };
        final Context context = getContext();
        mWifiPickerTracker = FeatureFactory.getFactory(context)
                .getWifiTrackerLibProvider()
                .createWifiPickerTracker(getSettingsLifecycle(), context,
                        new Handler(Looper.getMainLooper()),
                        mWorkerThread.getThreadHandler(),
                        elapsedRealtimeClock,
                        MAX_SCAN_AGE_MILLIS,
                        SCAN_INTERVAL_MILLIS,
                        null /* listener */);

        // setTitle for TalkBack
        if (mIsConfiguratorMode) {
            getActivity().setTitle(R.string.wifi_dpp_add_device_to_network);
        } else {
            getActivity().setTitle(R.string.wifi_dpp_scan_qr_code);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mScanWifiDppSuccessListener = (OnScanWifiDppSuccessListener) context;
    }

    @Override
    public void onDetach() {
        mScanWifiDppSuccessListener = null;

        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        mWorkerThread.quit();

        super.onDestroyView();
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.wifi_dpp_qrcode_scanner_fragment, container,
                /* attachToRoot */ false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSummary = view.findViewById(R.id.sud_layout_subtitle);

        mTextureView = view.findViewById(R.id.preview_view);
        mTextureView.setSurfaceTextureListener(this);

        mDecorateView = view.findViewById(R.id.decorate_view);

        setProgressBarShown(isWifiDppHandshaking());

        if (mIsConfiguratorMode) {
            setHeaderTitle(R.string.wifi_dpp_add_device_to_network);

            WifiNetworkConfig wifiNetworkConfig = ((WifiNetworkConfig.Retriever) getActivity())
                .getWifiNetworkConfig();
            if (!WifiNetworkConfig.isValidConfig(wifiNetworkConfig)) {
                throw new IllegalStateException("Invalid Wi-Fi network for configuring");
            }
            mSummary.setText(getString(R.string.wifi_dpp_center_qr_code,
                    wifiNetworkConfig.getSsid()));
        } else {
            setHeaderTitle(R.string.wifi_dpp_scan_qr_code);

            updateEnrolleeSummary();
        }

        mErrorMessage = view.findViewById(R.id.error_message);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.removeItem(Menu.FIRST);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        initCamera(surface);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Do nothing
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        destroyCamera();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Do nothing
    }

    @Override
    public Size getViewSize() {
        return new Size(mTextureView.getWidth(), mTextureView.getHeight());
    }

    @Override
    public Rect getFramePosition(Size previewSize, int cameraOrientation) {
        return new Rect(0, 0, previewSize.getHeight(), previewSize.getHeight());
    }

    @Override
    public void setTransform(Matrix transform) {
        mTextureView.setTransform(transform);
    }

    @Override
    public boolean isValid(String qrCode) {
        try {
            mWifiQrCode = new WifiQrCode(qrCode);
        } catch (IllegalArgumentException e) {
            showErrorMessage(R.string.wifi_dpp_qr_code_is_not_valid_format);
            return false;
        }

        // It's impossible to provision other device with ZXing Wi-Fi Network config format
        final String scheme = mWifiQrCode.getScheme();
        if (mIsConfiguratorMode && WifiQrCode.SCHEME_ZXING_WIFI_NETWORK_CONFIG.equals(scheme)) {
            showErrorMessage(R.string.wifi_dpp_qr_code_is_not_valid_format);
            return false;
        }

        return true;
    }

    /**
     * This method is only called when QrCamera.ScannerCallback.isValid returns true;
     */
    @Override
    public void handleSuccessfulResult(String qrCode) {
        switch (mWifiQrCode.getScheme()) {
            case WifiQrCode.SCHEME_DPP:
                handleWifiDpp();
                break;

            case WifiQrCode.SCHEME_ZXING_WIFI_NETWORK_CONFIG:
                handleZxingWifiFormat();
                break;

            default:
                // continue below
        }
    }

    private void handleWifiDpp() {
        Message message = mHandler.obtainMessage(MESSAGE_SCAN_WIFI_DPP_SUCCESS);
        message.obj = new WifiQrCode(mWifiQrCode.getQrCode());

        mHandler.sendMessageDelayed(message, SHOW_SUCCESS_SQUARE_INTERVAL);
    }

    private void handleZxingWifiFormat() {
        Message message = mHandler.obtainMessage(MESSAGE_SCAN_ZXING_WIFI_FORMAT_SUCCESS);
        message.obj = new WifiQrCode(mWifiQrCode.getQrCode()).getWifiNetworkConfig();

        mHandler.sendMessageDelayed(message, SHOW_SUCCESS_SQUARE_INTERVAL);
    }

    @Override
    public void handleCameraFailure() {
        destroyCamera();
    }

    private void initCamera(SurfaceTexture surface) {
        // Check if the camera has already created.
        if (mCamera == null) {
            mCamera = new QrCamera(getContext(), this);

            if (isWifiDppHandshaking()) {
                if (mDecorateView != null) {
                    mDecorateView.setFocused(true);
                }
            } else {
                mCamera.start(surface);
            }
        }
    }

    private void destroyCamera() {
        if (mCamera != null) {
            mCamera.stop();
            mCamera = null;
        }
    }

    private void showErrorMessage(@StringRes int messageResId) {
        final Message message = mHandler.obtainMessage(MESSAGE_SHOW_ERROR_MESSAGE,
                getString(messageResId));
        message.sendToTarget();
    }

    private void showErrorMessageAndRestartCamera(@StringRes int messageResId) {
        final Message message = mHandler.obtainMessage(MESSAGE_SHOW_ERROR_MESSAGE,
                getString(messageResId));
        message.arg1 = ARG_RESTART_CAMERA;
        message.sendToTarget();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(KEY_IS_CONFIGURATOR_MODE, mIsConfiguratorMode);
        outState.putInt(KEY_LATEST_ERROR_CODE, mLatestStatusCode);
        outState.putParcelable(KEY_WIFI_CONFIGURATION, mEnrolleeWifiConfiguration);

        super.onSaveInstanceState(outState);
    }

    private class EasyConnectEnrolleeStatusCallback extends EasyConnectStatusCallback {
        @Override
        public void onEnrolleeSuccess(int newNetworkId) {

            // Connect to the new network.
            final WifiManager wifiManager = getContext().getSystemService(WifiManager.class);
            final List<WifiConfiguration> wifiConfigs =
                    wifiManager.getPrivilegedConfiguredNetworks();
            for (WifiConfiguration wifiConfig : wifiConfigs) {
                if (wifiConfig.networkId == newNetworkId) {
                    mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS;
                    mEnrolleeWifiConfiguration = wifiConfig;
                    wifiManager.connect(wifiConfig, WifiDppQrCodeScannerFragment.this);
                    return;
                }
            }

            Log.e(TAG, "Invalid networkId " + newNetworkId);
            mLatestStatusCode = EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_GENERIC;
            updateEnrolleeSummary();
            showErrorMessageAndRestartCamera(R.string.wifi_dpp_check_connection_try_again);
        }

        @Override
        public void onConfiguratorSuccess(int code) {
            // Do nothing
        }

        @Override
        public void onFailure(int code) {
            Log.d(TAG, "EasyConnectEnrolleeStatusCallback.onFailure " + code);

            int errorMessageResId;
            switch (code) {
                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_INVALID_URI:
                    errorMessageResId = R.string.wifi_dpp_qr_code_is_not_valid_format;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_AUTHENTICATION:
                    errorMessageResId = R.string.wifi_dpp_failure_authentication_or_configuration;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_NOT_COMPATIBLE:
                    errorMessageResId = R.string.wifi_dpp_failure_not_compatible;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_CONFIGURATION:
                    errorMessageResId = R.string.wifi_dpp_failure_authentication_or_configuration;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_BUSY:
                    if (code == mLatestStatusCode) {
                        throw(new IllegalStateException("stopEasyConnectSession and try again for"
                                + "EASY_CONNECT_EVENT_FAILURE_BUSY but still failed"));
                    }

                    mLatestStatusCode = code;
                    final WifiManager wifiManager =
                        getContext().getSystemService(WifiManager.class);
                    wifiManager.stopEasyConnectSession();
                    startWifiDppEnrolleeInitiator(mWifiQrCode);
                    return;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_TIMEOUT:
                    errorMessageResId = R.string.wifi_dpp_failure_timeout;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_GENERIC:
                    errorMessageResId = R.string.wifi_dpp_failure_generic;
                    break;

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED:
                    throw(new IllegalStateException("EASY_CONNECT_EVENT_FAILURE_NOT_SUPPORTED" +
                            " should be a configurator only error"));

                case EasyConnectStatusCallback.EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK:
                    throw(new IllegalStateException("EASY_CONNECT_EVENT_FAILURE_INVALID_NETWORK" +
                            " should be a configurator only error"));

                default:
                    throw(new IllegalStateException("Unexpected Wi-Fi DPP error"));
            }

            mLatestStatusCode = code;
            updateEnrolleeSummary();
            showErrorMessageAndRestartCamera(errorMessageResId);
        }

        @Override
        public void onProgress(int code) {
            // Do nothing
        }
    }

    private void startWifiDppEnrolleeInitiator(WifiQrCode wifiQrCode) {
        final WifiDppInitiatorViewModel model =
                ViewModelProviders.of(this).get(WifiDppInitiatorViewModel.class);

        model.startEasyConnectAsEnrolleeInitiator(wifiQrCode.getQrCode());
    }

    @Override
    public void onSuccess() {
        final Intent resultIntent = new Intent();
        resultIntent.putExtra(KEY_WIFI_CONFIGURATION, mEnrolleeWifiConfiguration);

        final Activity hostActivity = getActivity();
        hostActivity.setResult(Activity.RESULT_OK, resultIntent);
        hostActivity.finish();
    }

    @Override
    public void onFailure(int reason) {
        Log.d(TAG, "Wi-Fi connect onFailure reason - " + reason);
        showErrorMessageAndRestartCamera(R.string.wifi_dpp_check_connection_try_again);
    }

    // Check is Easy Connect handshaking or not
    private boolean isWifiDppHandshaking() {
        final WifiDppInitiatorViewModel model =
                ViewModelProviders.of(this).get(WifiDppInitiatorViewModel.class);

        return model.isWifiDppHandshaking();
    }

    /**
     * To resume camera decoding task after handshake fail or Wi-Fi connection fail.
     */
    private void restartCamera() {
        if (mCamera == null) {
            Log.d(TAG, "mCamera is not available for restarting camera");
            return;
        }

        if (mCamera.isDecodeTaskAlive()) {
            mCamera.stop();
        }

        final SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        if (surfaceTexture == null) {
            throw new IllegalStateException("SurfaceTexture is not ready for restarting camera");
        }

        mCamera.start(surfaceTexture);
    }

    private void updateEnrolleeSummary() {
        if (isWifiDppHandshaking()) {
            mSummary.setText(R.string.wifi_dpp_connecting);
        } else {
            String description;
            if (TextUtils.isEmpty(mSsid)) {
                description = getString(R.string.wifi_dpp_scan_qr_code_join_unknown_network, mSsid);
            } else {
                description = getString(R.string.wifi_dpp_scan_qr_code_join_network, mSsid);
            }
            mSummary.setText(description);
        }
    }

    @VisibleForTesting
    protected boolean isDecodeTaskAlive() {
        return mCamera != null && mCamera.isDecodeTaskAlive();
    }

    @Override
    protected boolean isFooterAvailable() {
        return false;
    }
}