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