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