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; 18 19 import static android.car.hardware.power.PowerComponent.WIFI; 20 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.Context; 23 import android.net.wifi.WifiManager; 24 import android.widget.Toast; 25 26 import androidx.annotation.VisibleForTesting; 27 28 import com.android.car.settings.R; 29 import com.android.car.settings.common.ClickableWhileDisabledSwitchPreference; 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.settings.common.PowerPolicyListener; 32 import com.android.car.settings.common.PreferenceController; 33 34 /** 35 * Enables/disables Wifi state via SwitchPreference. 36 */ 37 public class WifiStateSwitchPreferenceController extends 38 PreferenceController<ClickableWhileDisabledSwitchPreference> 39 implements CarWifiManager.Listener { 40 41 private final CarWifiManager mCarWifiManager; 42 43 @VisibleForTesting 44 final PowerPolicyListener mPowerPolicyListener; 45 WifiStateSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public WifiStateSwitchPreferenceController(Context context, String preferenceKey, 47 FragmentController fragmentController, 48 CarUxRestrictions uxRestrictions) { 49 super(context, preferenceKey, fragmentController, uxRestrictions); 50 mPowerPolicyListener = new PowerPolicyListener(context, WIFI, 51 isOn -> { 52 enableSwitchPreference(getPreference(), isOn); 53 }); 54 mCarWifiManager = new CarWifiManager(context, 55 getFragmentController().getSettingsLifecycle()); 56 } 57 58 @Override getPreferenceType()59 protected Class<ClickableWhileDisabledSwitchPreference> getPreferenceType() { 60 return ClickableWhileDisabledSwitchPreference.class; 61 } 62 63 @Override updateState(ClickableWhileDisabledSwitchPreference preference)64 protected void updateState(ClickableWhileDisabledSwitchPreference preference) { 65 updateSwitchPreference(preference, mCarWifiManager.isWifiEnabled()); 66 } 67 68 @Override handlePreferenceChanged(ClickableWhileDisabledSwitchPreference preference, Object newValue)69 protected boolean handlePreferenceChanged(ClickableWhileDisabledSwitchPreference preference, 70 Object newValue) { 71 boolean wifiEnabled = (Boolean) newValue; 72 mCarWifiManager.setWifiEnabled(wifiEnabled); 73 return true; 74 } 75 76 @Override onCreateInternal()77 protected void onCreateInternal() { 78 getPreference().setContentDescription( 79 getContext().getString(R.string.wifi_state_switch_content_description)); 80 getPreference().setDisabledClickListener(p -> 81 Toast.makeText(getContext(), 82 getContext().getString(R.string.power_component_disabled), 83 Toast.LENGTH_LONG).show()); 84 } 85 86 @Override onStartInternal()87 protected void onStartInternal() { 88 mCarWifiManager.addListener(this); 89 onWifiStateChanged(mCarWifiManager.getWifiState()); 90 } 91 92 @Override onResumeInternal()93 protected void onResumeInternal() { 94 mPowerPolicyListener.handleCurrentPolicy(); 95 } 96 97 @Override onStopInternal()98 protected void onStopInternal() { 99 mCarWifiManager.removeListener(this); 100 } 101 102 @Override onDestroyInternal()103 protected void onDestroyInternal() { 104 mPowerPolicyListener.release(); 105 } 106 107 @Override onWifiEntriesChanged()108 public void onWifiEntriesChanged() { 109 // intentional no-op 110 } 111 112 @Override onWifiStateChanged(int state)113 public void onWifiStateChanged(int state) { 114 updateSwitchPreference(getPreference(), state == WifiManager.WIFI_STATE_ENABLED 115 || state == WifiManager.WIFI_STATE_ENABLING); 116 } 117 updateSwitchPreference(ClickableWhileDisabledSwitchPreference preference, boolean enabled)118 private void updateSwitchPreference(ClickableWhileDisabledSwitchPreference preference, 119 boolean enabled) { 120 preference.setChecked(enabled); 121 } 122 enableSwitchPreference(ClickableWhileDisabledSwitchPreference preference, boolean enabled)123 private void enableSwitchPreference(ClickableWhileDisabledSwitchPreference preference, 124 boolean enabled) { 125 preference.setEnabled(enabled); 126 } 127 } 128