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     }
60 
61     @Override
62     @CallSuper
onStartInternal()63     protected void onStartInternal() {
64         mCarWifiManager.start();
65         getPreference().setPersistent(true);
66     }
67 
68     @Override
69     @CallSuper
onStopInternal()70     protected void onStopInternal() {
71         mCarWifiManager.stop();
72     }
73 
74     @Override
75     @CallSuper
onDestroyInternal()76     protected void onDestroyInternal() {
77         mCarWifiManager.destroy();
78     }
79 
80     @Override
81     @CallSuper
updateState(V preference)82     protected void updateState(V preference) {
83         String summary = getSummary();
84         String defaultSummary = getDefaultSummary();
85 
86         if (TextUtils.isEmpty(summary)) {
87             preference.setSummary(defaultSummary);
88         } else {
89             preference.setSummary(summary);
90         }
91     }
92 
getCarSoftApConfig()93     protected SoftApConfiguration getCarSoftApConfig() {
94         return mCarWifiManager.getSoftApConfig();
95     }
96 
setCarSoftApConfig(SoftApConfiguration configuration)97     protected void setCarSoftApConfig(SoftApConfiguration configuration) {
98         mCarWifiManager.setSoftApConfig(configuration);
99         requestWifiTetherRestart();
100     }
101 
getCarWifiManager()102     protected CarWifiManager getCarWifiManager() {
103         return mCarWifiManager;
104     }
105 
getSummary()106     protected abstract String getSummary();
107 
getDefaultSummary()108     protected abstract String getDefaultSummary();
109 
requestWifiTetherRestart()110     protected void requestWifiTetherRestart() {
111         Intent intent = new Intent(ACTION_RESTART_WIFI_TETHERING);
112         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
113     }
114 }
115