1 /* 2 * Copyright (C) 2018 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 android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.net.LinkProperties; 22 import android.net.Network; 23 import android.net.NetworkCapabilities; 24 import android.net.NetworkInfo; 25 import android.net.wifi.WifiConfiguration; 26 import android.net.wifi.WifiInfo; 27 28 import androidx.preference.Preference; 29 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.settings.common.PreferenceController; 32 import com.android.car.settings.wifi.WifiUtil; 33 import com.android.settingslib.wifi.AccessPoint; 34 35 /** 36 * Controller for logic pertaining to displaying Wifi information. Only available when wifi is 37 * active. 38 * 39 * <p>Subclasses should use 40 * {@link com.android.car.settings.common.PreferenceController#updateState(Preference)} to render UI 41 * with latest info if desired. 42 * 43 * @param <V> the upper bound on the type of {@link Preference} on which the controller 44 * expects to operate. 45 */ 46 public abstract class WifiDetailsBasePreferenceController<V extends Preference> extends 47 PreferenceController<V> implements WifiInfoProvider.Listener { 48 49 private AccessPoint mAccessPoint; 50 private WifiInfoProvider mWifiInfoProvider; 51 WifiDetailsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)52 public WifiDetailsBasePreferenceController(Context context, String preferenceKey, 53 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 54 super(context, preferenceKey, fragmentController, uxRestrictions); 55 } 56 57 /** 58 * Sets all parameters for the controller to run, need to get called as early as possible. 59 */ init( AccessPoint accessPoint, WifiInfoProvider wifiInfoProvider)60 public WifiDetailsBasePreferenceController init( 61 AccessPoint accessPoint, WifiInfoProvider wifiInfoProvider) { 62 mAccessPoint = accessPoint; 63 mWifiInfoProvider = wifiInfoProvider; 64 return this; 65 } 66 67 /** Gets the access poing that this controller was initialized with. */ getAccessPoint()68 protected AccessPoint getAccessPoint() { 69 return mAccessPoint; 70 } 71 72 /** Gets the wifi info provider that this controller was initialized with. */ getWifiInfoProvider()73 protected WifiInfoProvider getWifiInfoProvider() { 74 return mWifiInfoProvider; 75 } 76 77 @Override onStartInternal()78 protected void onStartInternal() { 79 mWifiInfoProvider.addListener(this); 80 } 81 82 @Override onStopInternal()83 protected void onStopInternal() { 84 mWifiInfoProvider.removeListener(this); 85 } 86 87 @Override onWifiConfigurationChanged(WifiConfiguration wifiConfiguration, NetworkInfo networkInfo, WifiInfo wifiInfo)88 public void onWifiConfigurationChanged(WifiConfiguration wifiConfiguration, 89 NetworkInfo networkInfo, WifiInfo wifiInfo) { 90 refreshUi(); 91 } 92 93 @Override onLinkPropertiesChanged(Network network, LinkProperties lp)94 public void onLinkPropertiesChanged(Network network, LinkProperties lp) { 95 refreshUi(); 96 } 97 98 @Override onCapabilitiesChanged(Network network, NetworkCapabilities nc)99 public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { 100 } 101 102 @Override onWifiChanged(NetworkInfo networkInfo, WifiInfo wifiInfo)103 public void onWifiChanged(NetworkInfo networkInfo, WifiInfo wifiInfo) { 104 getPreference().setEnabled(true); 105 refreshUi(); 106 } 107 108 @Override onLost(Network network)109 public void onLost(Network network) { 110 getPreference().setEnabled(false); 111 refreshUi(); 112 } 113 114 @Override getAvailabilityStatus()115 protected int getAvailabilityStatus() { 116 if (!WifiUtil.isWifiAvailable(getContext())) { 117 return UNSUPPORTED_ON_DEVICE; 118 } 119 return getAccessPoint().isActive() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 120 } 121 } 122