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.details;
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.settingslib.wifi.AccessPoint;
29 
30 /**
31  * {@link BasePreferenceController} that launches Wi-Fi Easy Connect configurator flow.
32  *
33  * Migrating from Wi-Fi SettingsLib to to WifiTrackerLib, this object will be removed in the near
34  * future, please develop in
35  * {@link com.android.settings.wifi.details2.AddDevicePreferenceController2}.
36  */
37 public class AddDevicePreferenceController extends BasePreferenceController {
38 
39     private static final String TAG = "AddDevicePreferenceController";
40 
41     private static final String KEY_ADD_DEVICE = "add_device_to_network";
42 
43     private AccessPoint mAccessPoint;
44     private WifiManager mWifiManager;
45 
AddDevicePreferenceController(Context context)46     public AddDevicePreferenceController(Context context) {
47         super(context, KEY_ADD_DEVICE);
48 
49         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
50     }
51 
init(AccessPoint accessPoint)52     public AddDevicePreferenceController init(AccessPoint accessPoint) {
53         mAccessPoint = accessPoint;
54 
55         return this;
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         if (WifiDppUtils.isSupportConfiguratorQrCodeScanner(mContext, mAccessPoint)) {
61             return AVAILABLE;
62         } else {
63             return CONDITIONALLY_UNAVAILABLE;
64         }
65     }
66 
67     @Override
handlePreferenceTreeClick(Preference preference)68     public boolean handlePreferenceTreeClick(Preference preference) {
69         if (KEY_ADD_DEVICE.equals(preference.getKey())) {
70             WifiDppUtils.showLockScreen(mContext, () -> launchWifiDppConfiguratorQrCodeScanner());
71             return true; /* click is handled */
72         }
73 
74         return false; /* click is not handled */
75     }
76 
launchWifiDppConfiguratorQrCodeScanner()77     private void launchWifiDppConfiguratorQrCodeScanner() {
78         final Intent intent = WifiDppUtils.getConfiguratorQrCodeScannerIntentOrNull(mContext,
79                 mWifiManager, mAccessPoint);
80 
81         if (intent == null) {
82             Log.e(TAG, "Launch Wi-Fi QR code scanner with a wrong Wi-Fi network!");
83         } else {
84             mContext.startActivity(intent);
85         }
86     }
87 }
88