1 /* 2 * Copyright (C) 2020 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.details; 18 19 import static com.android.car.settings.common.ActionButtonsPreference.ActionButtons; 20 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.Context; 23 import android.net.Network; 24 import android.net.NetworkCapabilities; 25 import android.widget.Toast; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.ActionButtonsPreference; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.wifi.WifiUtil; 31 import com.android.wifitrackerlib.WifiEntry; 32 33 /** 34 * Shows Wifi details action buttons (forget and connect). 35 */ 36 public class WifiDetailsActionButtonsPreferenceController 37 extends WifiDetailsBasePreferenceController<ActionButtonsPreference> 38 implements WifiEntry.ConnectCallback { 39 WifiDetailsActionButtonsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40 public WifiDetailsActionButtonsPreferenceController(Context context, 41 String preferenceKey, FragmentController fragmentController, 42 CarUxRestrictions uxRestrictions) { 43 super(context, preferenceKey, fragmentController, uxRestrictions); 44 } 45 46 @Override getPreferenceType()47 protected Class<ActionButtonsPreference> getPreferenceType() { 48 return ActionButtonsPreference.class; 49 } 50 51 @Override updateState(ActionButtonsPreference preference)52 protected void updateState(ActionButtonsPreference preference) { 53 getPreference() 54 .getButton(ActionButtons.BUTTON1) 55 .setText(R.string.forget) 56 .setIcon(R.drawable.ic_delete) 57 .setVisible(canForgetNetwork()) 58 .setOnClickListener(v -> { 59 getWifiEntry().forget(/* callback= */ null); 60 getFragmentController().goBack(); 61 }); 62 63 boolean canConnectOrDisconnect = getWifiEntry().canConnect() 64 || getWifiEntry().canDisconnect(); 65 preference 66 .getButton(ActionButtons.BUTTON2) 67 .setVisible(canConnectOrDisconnect || getWifiEntry().getConnectedState() 68 == WifiEntry.CONNECTED_STATE_CONNECTING) 69 .setEnabled(canConnectOrDisconnect) 70 .setText(getConnectDisconnectButtonTextResource()) 71 .setIcon(getConnectDisconnectButtonIconResource()) 72 .setOnClickListener(v -> connectOrDisconnect()); 73 } 74 75 @Override getAvailabilityStatus()76 protected int getAvailabilityStatus() { 77 if (!WifiUtil.isWifiAvailable(getContext())) { 78 return UNSUPPORTED_ON_DEVICE; 79 } 80 return AVAILABLE; 81 } 82 83 @Override onCapabilitiesChanged(Network network, NetworkCapabilities nc)84 public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { 85 refreshUi(); 86 } 87 88 @Override onConnectResult(@ifiEntry.ConnectCallback.ConnectStatus int status)89 public void onConnectResult(@WifiEntry.ConnectCallback.ConnectStatus int status) { 90 if (getWifiEntry().getLevel() == WifiEntry.WIFI_LEVEL_UNREACHABLE) { 91 Toast.makeText(getContext(), 92 R.string.wifi_not_in_range_message, 93 Toast.LENGTH_SHORT).show(); 94 } else if (status != WifiEntry.ConnectCallback.CONNECT_STATUS_SUCCESS) { 95 Toast.makeText(getContext(), 96 R.string.wifi_failed_connect_message, 97 Toast.LENGTH_SHORT).show(); 98 } 99 } 100 canForgetNetwork()101 private boolean canForgetNetwork() { 102 return getWifiEntry().canForget() 103 && !WifiUtil.isNetworkLockedDown(getContext(), 104 getWifiEntry().getWifiConfiguration()); 105 } 106 connectOrDisconnect()107 private void connectOrDisconnect() { 108 if (!WifiUtil.isWifiEntryConnectedOrConnecting(getWifiEntry())) { 109 getWifiEntry().connect(/* callback= */ this); 110 } else { 111 getWifiEntry().disconnect(/* callback= */ null); 112 } 113 } 114 getConnectDisconnectButtonTextResource()115 private int getConnectDisconnectButtonTextResource() { 116 switch (getWifiEntry().getConnectedState()) { 117 case WifiEntry.CONNECTED_STATE_DISCONNECTED: 118 return R.string.wifi_setup_connect; 119 case WifiEntry.CONNECTED_STATE_CONNECTED: 120 return R.string.disconnect; 121 case WifiEntry.CONNECTED_STATE_CONNECTING: 122 return R.string.wifi_connecting; 123 default: 124 throw new IllegalStateException("Invalid WifiEntry connected state"); 125 } 126 } 127 getConnectDisconnectButtonIconResource()128 private int getConnectDisconnectButtonIconResource() { 129 switch (getWifiEntry().getConnectedState()) { 130 case WifiEntry.CONNECTED_STATE_DISCONNECTED: 131 case WifiEntry.CONNECTED_STATE_CONNECTING: 132 return R.drawable.ic_settings_wifi; 133 case WifiEntry.CONNECTED_STATE_CONNECTED: 134 return R.drawable.ic_close; 135 default: 136 throw new IllegalStateException("Invalid WifiEntry connected state"); 137 } 138 } 139 } 140