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