1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.wifi.details2;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.wifi.WifiManager;
22 import android.util.Log;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.wifi.dpp.WifiDppUtils;
28 import com.android.wifitrackerlib.WifiEntry;
29 
30 /**
31  * {@link BasePreferenceController} that launches Wi-Fi Easy Connect configurator flow
32  */
33 public class AddDevicePreferenceController2 extends BasePreferenceController {
34 
35     private static final String TAG = "AddDevicePreferenceController2";
36 
37     private static final String KEY_ADD_DEVICE = "add_device_to_network";
38 
39     private WifiEntry mWifiEntry;
40     private WifiManager mWifiManager;
41 
AddDevicePreferenceController2(Context context)42     public AddDevicePreferenceController2(Context context) {
43         super(context, KEY_ADD_DEVICE);
44 
45         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
46     }
47 
setWifiEntry(WifiEntry wifiEntry)48     public void setWifiEntry(WifiEntry wifiEntry) {
49         mWifiEntry = wifiEntry;
50     }
51 
52     @Override
getAvailabilityStatus()53     public int getAvailabilityStatus() {
54         return mWifiEntry.canEasyConnect() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
55     }
56 
57     @Override
handlePreferenceTreeClick(Preference preference)58     public boolean handlePreferenceTreeClick(Preference preference) {
59         if (KEY_ADD_DEVICE.equals(preference.getKey())) {
60             WifiDppUtils.showLockScreen(mContext, () -> launchWifiDppConfiguratorQrCodeScanner());
61             return true; /* click is handled */
62         }
63 
64         return false; /* click is not handled */
65     }
66 
launchWifiDppConfiguratorQrCodeScanner()67     private void launchWifiDppConfiguratorQrCodeScanner() {
68         final Intent intent = WifiDppUtils.getConfiguratorQrCodeScannerIntentOrNull(mContext,
69                 mWifiManager, mWifiEntry);
70 
71         if (intent == null) {
72             Log.e(TAG, "Launch Wi-Fi QR code scanner with a wrong Wi-Fi network!");
73         } else {
74             mContext.startActivity(intent);
75         }
76     }
77 }
78