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