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 android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 22 import com.android.car.settings.R; 23 import com.android.car.settings.common.ColoredSwitchPreference; 24 import com.android.car.settings.common.FragmentController; 25 import com.android.car.settings.common.PreferenceController; 26 27 /** 28 * Enables/disables tether state via SwitchPreference. 29 */ 30 public class WifiTetherStateSwitchPreferenceController extends 31 PreferenceController<ColoredSwitchPreference> implements 32 WifiTetheringHandler.WifiTetheringAvailabilityListener { 33 34 private WifiTetheringHandler mWifiTetheringHandler; 35 WifiTetherStateSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)36 public WifiTetherStateSwitchPreferenceController(Context context, 37 String preferenceKey, 38 FragmentController fragmentController, 39 CarUxRestrictions uxRestrictions) { 40 super(context, preferenceKey, fragmentController, uxRestrictions); 41 mWifiTetheringHandler = new WifiTetheringHandler(context, 42 fragmentController.getSettingsLifecycle(), this); 43 } 44 45 @Override getPreferenceType()46 protected Class<ColoredSwitchPreference> getPreferenceType() { 47 return ColoredSwitchPreference.class; 48 } 49 50 @Override updateState(ColoredSwitchPreference preference)51 protected void updateState(ColoredSwitchPreference preference) { 52 updateSwitchPreference(mWifiTetheringHandler.isWifiTetheringEnabled()); 53 } 54 55 @Override handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)56 protected boolean handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue) { 57 boolean switchOn = (Boolean) newValue; 58 updateSwitchPreference(switchOn); 59 mWifiTetheringHandler.updateWifiTetheringState(switchOn); 60 return true; 61 } 62 63 @Override onCreateInternal()64 protected void onCreateInternal() { 65 getPreference().setContentDescription( 66 getContext().getString(R.string.wifi_hotspot_state_switch_content_description)); 67 } 68 69 @Override onStartInternal()70 protected void onStartInternal() { 71 mWifiTetheringHandler.onStartInternal(); 72 } 73 74 @Override onStopInternal()75 protected void onStopInternal() { 76 mWifiTetheringHandler.onStopInternal(); 77 } 78 79 @Override onWifiTetheringAvailable()80 public void onWifiTetheringAvailable() { 81 updateSwitchPreference(/* switchOn= */ true); 82 } 83 84 @Override onWifiTetheringUnavailable()85 public void onWifiTetheringUnavailable() { 86 updateSwitchPreference(/* switchOn= */ false); 87 } 88 89 @Override enablePreference()90 public void enablePreference() { 91 getPreference().setEnabled(true); 92 } 93 94 @Override disablePreference()95 public void disablePreference() { 96 getPreference().setEnabled(false); 97 } 98 updateSwitchPreference(boolean switchOn)99 private void updateSwitchPreference(boolean switchOn) { 100 getPreference().setTitle(switchOn ? R.string.car_ui_preference_switch_on 101 : R.string.car_ui_preference_switch_off); 102 getPreference().setChecked(switchOn); 103 } 104 } 105