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.net.wifi.WifiConfiguration;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.CallSuper;
25 import androidx.preference.Preference;
26 
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.common.PreferenceController;
29 
30 /**
31  * Shared business logic for preference controllers related to Wifi Tethering
32  *
33  * @param <V> the upper bound on the type of {@link Preference} on which the controller
34  *            expects to operate.
35  */
36 public abstract class WifiTetherBasePreferenceController<V extends Preference> extends
37         PreferenceController<V> {
38 
39     private CarWifiManager mCarWifiManager;
40 
WifiTetherBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public WifiTetherBasePreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
43         super(context, preferenceKey, fragmentController, uxRestrictions);
44     }
45 
46     @Override
47     @CallSuper
onCreateInternal()48     protected void onCreateInternal() {
49         mCarWifiManager = new CarWifiManager(getContext());
50     }
51 
52     @Override
53     @CallSuper
onStartInternal()54     protected void onStartInternal() {
55         mCarWifiManager.start();
56         getPreference().setPersistent(true);
57     }
58 
59     @Override
60     @CallSuper
onStopInternal()61     protected void onStopInternal() {
62         mCarWifiManager.stop();
63     }
64 
65     @Override
66     @CallSuper
onDestroyInternal()67     protected void onDestroyInternal() {
68         mCarWifiManager.destroy();
69     }
70 
71     @Override
72     @CallSuper
updateState(V preference)73     protected void updateState(V preference) {
74         String summary = getSummary();
75         String defaultSummary = getDefaultSummary();
76 
77         if (TextUtils.isEmpty(summary)) {
78             preference.setSummary(defaultSummary);
79         } else {
80             preference.setSummary(summary);
81         }
82     }
83 
getCarWifiApConfig()84     protected WifiConfiguration getCarWifiApConfig() {
85         return mCarWifiManager.getWifiApConfig();
86     }
87 
setCarWifiApConfig(WifiConfiguration configuration)88     protected void setCarWifiApConfig(WifiConfiguration configuration) {
89         mCarWifiManager.setWifiApConfig(configuration);
90     }
91 
getCarWifiManager()92     protected CarWifiManager getCarWifiManager() {
93         return mCarWifiManager;
94     }
95 
getSummary()96     protected abstract String getSummary();
97 
getDefaultSummary()98     protected abstract String getDefaultSummary();
99 }
100