1 /* 2 * Copyright (C) 2019 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 import android.content.Intent; 22 import android.net.wifi.SoftApConfiguration; 23 import android.text.TextUtils; 24 25 import androidx.annotation.CallSuper; 26 import androidx.localbroadcastmanager.content.LocalBroadcastManager; 27 import androidx.preference.Preference; 28 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.PreferenceController; 31 import com.android.internal.annotations.VisibleForTesting; 32 33 /** 34 * Shared business logic for preference controllers related to Wifi Tethering 35 * 36 * @param <V> the upper bound on the type of {@link Preference} on which the controller 37 * expects to operate. 38 */ 39 public abstract class WifiTetherBasePreferenceController<V extends Preference> extends 40 PreferenceController<V> { 41 42 /** 43 * Action used in the {@link Intent} sent by the {@link LocalBroadcastManager} to request wifi 44 * restart. 45 */ 46 public static final String ACTION_RESTART_WIFI_TETHERING = 47 "com.android.car.settings.wifi.ACTION_RESTART_WIFI_TETHERING"; 48 49 private final CarWifiManager mCarWifiManager; 50 WifiTetherBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51 public WifiTetherBasePreferenceController(Context context, String preferenceKey, 52 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 53 this(context, preferenceKey, fragmentController, uxRestrictions, new CarWifiManager(context, 54 fragmentController.getSettingsLifecycle())); 55 } 56 57 @VisibleForTesting WifiTetherBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, CarWifiManager carWifiManager)58 WifiTetherBasePreferenceController(Context context, String preferenceKey, 59 FragmentController fragmentController, CarUxRestrictions uxRestrictions, 60 CarWifiManager carWifiManager) { 61 super(context, preferenceKey, fragmentController, uxRestrictions); 62 63 mCarWifiManager = carWifiManager; 64 } 65 66 @Override 67 @CallSuper onCreateInternal()68 protected void onCreateInternal() { 69 // ActionDisabledByAdminDialog will be shown if DISALLOW_CONFIG_WIFI 70 // is set by a device admin; otherwise, a default Toast will be shown 71 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> 72 WifiUtil.runClickableWhileDisabled(getContext(), getFragmentController())); 73 } 74 75 @Override 76 @CallSuper onStartInternal()77 protected void onStartInternal() { 78 getPreference().setPersistent(true); 79 } 80 81 @Override 82 @CallSuper updateState(V preference)83 protected void updateState(V preference) { 84 String summary = getSummary(); 85 String defaultSummary = getDefaultSummary(); 86 87 if (TextUtils.isEmpty(summary)) { 88 preference.setSummary(defaultSummary); 89 } else { 90 preference.setSummary(summary); 91 } 92 } 93 94 @Override getDefaultAvailabilityStatus()95 protected int getDefaultAvailabilityStatus() { 96 if (WifiUtil.isConfigWifiRestrictedByUm(getContext()) 97 || WifiUtil.isConfigWifiRestrictedByDpm(getContext())) { 98 return AVAILABLE_FOR_VIEWING; 99 } 100 return AVAILABLE; 101 } 102 getCarSoftApConfig()103 protected SoftApConfiguration getCarSoftApConfig() { 104 return mCarWifiManager.getSoftApConfig(); 105 } 106 setCarSoftApConfig(SoftApConfiguration configuration)107 protected void setCarSoftApConfig(SoftApConfiguration configuration) { 108 mCarWifiManager.setSoftApConfig(configuration); 109 requestWifiTetherRestart(); 110 } 111 getCarWifiManager()112 protected CarWifiManager getCarWifiManager() { 113 return mCarWifiManager; 114 } 115 getSummary()116 protected abstract String getSummary(); 117 getDefaultSummary()118 protected abstract String getDefaultSummary(); 119 requestWifiTetherRestart()120 protected void requestWifiTetherRestart() { 121 Intent intent = new Intent(ACTION_RESTART_WIFI_TETHERING); 122 LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); 123 } 124 } 125