1 /* 2 * Copyright (C) 2021 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.car.settings.wifi; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.car.drivingstate.CarUxRestrictionsManager; 21 import android.content.Context; 22 import android.net.wifi.WifiConfiguration; 23 import android.net.wifi.WifiManager; 24 import android.widget.Toast; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.PreferenceGroup; 29 30 import com.android.car.settings.R; 31 import com.android.car.settings.common.CarUxRestrictionsHelper; 32 import com.android.car.settings.common.FragmentController; 33 import com.android.car.settings.common.Logger; 34 import com.android.car.settings.wifi.details.WifiDetailsFragment; 35 import com.android.wifitrackerlib.WifiEntry; 36 37 import java.util.ArrayList; 38 import java.util.List; 39 40 /** 41 * Renders a list of {@link WifiEntry} as a list of preferences. 42 */ 43 public class WifiEntryListPreferenceController extends 44 WifiBasePreferenceController<PreferenceGroup> implements 45 CarUxRestrictionsManager.OnUxRestrictionsChangedListener { 46 private static final Logger LOG = new Logger(WifiEntryListPreferenceController.class); 47 private final WifiManager.ActionListener mConnectionListener = 48 new WifiManager.ActionListener() { 49 @Override 50 public void onSuccess() { 51 LOG.d("connected to network"); 52 } 53 54 @Override 55 public void onFailure(int reason) { 56 LOG.d("Failed to connect to network. Failure code: " + reason); 57 Toast.makeText(getContext(), R.string.wifi_failed_connect_message, 58 Toast.LENGTH_SHORT).show(); 59 } 60 }; 61 62 @VisibleForTesting 63 final WifiPasswordDialog.WifiDialogListener mDialogListener = 64 new WifiPasswordDialog.WifiDialogListener() { 65 @Override 66 public void onSubmit(WifiPasswordDialog dialog) { 67 WifiConfiguration config = dialog.getConfig(); 68 WifiEntry wifiEntry = dialog.getWifiEntry(); 69 if (config == null) { 70 wifiEntry.connect( 71 new WifiEntryConnectCallback(wifiEntry, 72 /* editIfNoConfig= */ false)); 73 } else { 74 getWifiManager().connect(config, mConnectionListener); 75 } 76 } 77 }; 78 79 private List<WifiEntry> mWifiEntries = new ArrayList<>(); 80 WifiEntryListPreferenceController(@onNull Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)81 public WifiEntryListPreferenceController(@NonNull Context context, String preferenceKey, 82 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 83 super(context, preferenceKey, fragmentController, uxRestrictions); 84 } 85 86 @Override getPreferenceType()87 protected Class<PreferenceGroup> getPreferenceType() { 88 return PreferenceGroup.class; 89 } 90 91 @Override updateState(PreferenceGroup preferenceGroup)92 protected void updateState(PreferenceGroup preferenceGroup) { 93 if (getCarWifiManager() == null) { 94 return; 95 } 96 mWifiEntries = fetchWifiEntries(); 97 98 LOG.d("showing wifiEntries: " + mWifiEntries.size()); 99 100 preferenceGroup.setVisible(!mWifiEntries.isEmpty()); 101 preferenceGroup.removeAll(); 102 103 WifiEntry connectedWifiEntry = getCarWifiManager().getConnectedWifiEntry(); 104 for (WifiEntry wifiEntry : mWifiEntries) { 105 if (wifiEntry.equals(connectedWifiEntry)) { 106 preferenceGroup.addPreference( 107 createWifiEntryPreference(wifiEntry, /* connected= */ true)); 108 } else { 109 preferenceGroup.addPreference( 110 createWifiEntryPreference(wifiEntry, /* connected= */ false)); 111 } 112 } 113 } 114 115 @Override onApplyUxRestrictions(CarUxRestrictions uxRestrictions)116 protected void onApplyUxRestrictions(CarUxRestrictions uxRestrictions) { 117 // Since the list dynamically changes based on the UX restrictions, we enable this fragment 118 // regardless of the restriction. Intentional no-op. 119 } 120 121 @Override onWifiEntriesChanged()122 public void onWifiEntriesChanged() { 123 refreshUi(); 124 } 125 126 @Override onWifiStateChanged(int state)127 public void onWifiStateChanged(int state) { 128 if (state == WifiManager.WIFI_STATE_ENABLED) { 129 refreshUi(); 130 } 131 } 132 133 /** 134 * Get all {@link WifiEntry} that should be displayed as a list. 135 * @return List of wifi entries that should be displayed 136 */ fetchWifiEntries()137 protected List<WifiEntry> fetchWifiEntries() { 138 List<WifiEntry> wifiEntries = CarUxRestrictionsHelper.isNoSetup(getUxRestrictions()) 139 ? getCarWifiManager().getSavedWifiEntries() 140 : getCarWifiManager().getAllWifiEntries(); 141 142 WifiEntry connectedWifiEntry = getCarWifiManager().getConnectedWifiEntry(); 143 // Insert current connected network as first item, if available 144 if (connectedWifiEntry != null) { 145 wifiEntries.add(0, connectedWifiEntry); 146 } 147 return wifiEntries; 148 } 149 150 @VisibleForTesting getWifiManager()151 WifiManager getWifiManager() { 152 return getContext().getSystemService(WifiManager.class); 153 } 154 createWifiEntryPreference(WifiEntry wifiEntry, boolean connected)155 private WifiEntryPreference createWifiEntryPreference(WifiEntry wifiEntry, boolean connected) { 156 LOG.d("Adding preference for " + WifiUtil.getKey(wifiEntry)); 157 WifiEntryPreference wifiEntryPreference = new WifiEntryPreference(getContext(), wifiEntry); 158 wifiEntryPreference.setOnPreferenceClickListener(pref -> { 159 if (connected) { 160 if (wifiEntry.canSignIn()) { 161 wifiEntry.signIn(/* callback= */ null); 162 } else { 163 getFragmentController().launchFragment( 164 WifiDetailsFragment.getInstance(wifiEntry)); 165 } 166 } else if (wifiEntry.shouldEditBeforeConnect()) { 167 getFragmentController().showDialog( 168 new WifiPasswordDialog(wifiEntry, mDialogListener), WifiPasswordDialog.TAG); 169 } else { 170 wifiEntry.connect( 171 new WifiEntryConnectCallback(wifiEntry, /* editIfNoConfig= */ true)); 172 } 173 return true; 174 }); 175 176 if (wifiEntry.isSaved()) { 177 wifiEntryPreference.setSecondaryActionIcon(R.drawable.ic_delete); 178 wifiEntryPreference.setOnSecondaryActionClickListener( 179 () -> wifiEntry.forget(/* callback= */ null)); 180 wifiEntryPreference.setSecondaryActionVisible(true); 181 } 182 183 return wifiEntryPreference; 184 } 185 186 private class WifiEntryConnectCallback implements WifiEntry.ConnectCallback { 187 final WifiEntry mConnectWifiEntry; 188 final boolean mEditIfNoConfig; 189 WifiEntryConnectCallback(WifiEntry connectWifiEntry, boolean editIfNoConfig)190 WifiEntryConnectCallback(WifiEntry connectWifiEntry, boolean editIfNoConfig) { 191 mConnectWifiEntry = connectWifiEntry; 192 mEditIfNoConfig = editIfNoConfig; 193 } 194 195 @Override onConnectResult(@onnectStatus int status)196 public void onConnectResult(@ConnectStatus int status) { 197 if (!isStarted()) { 198 return; 199 } 200 201 if (status == WifiEntry.ConnectCallback.CONNECT_STATUS_FAILURE_NO_CONFIG) { 202 if (mEditIfNoConfig) { 203 getFragmentController().showDialog( 204 new WifiPasswordDialog(mConnectWifiEntry, mDialogListener), 205 WifiPasswordDialog.TAG); 206 } 207 } else if (status == CONNECT_STATUS_FAILURE_UNKNOWN) { 208 Toast.makeText(getContext(), R.string.wifi_failed_connect_message, 209 Toast.LENGTH_SHORT).show(); 210 } 211 } 212 } 213 } 214