• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
61 
62     @Override
63     @CallSuper
onStartInternal()64     protected void onStartInternal() {
65         getPreference().setPersistent(true);
66     }
67 
68     @Override
69     @CallSuper
updateState(V preference)70     protected void updateState(V preference) {
71         String summary = getSummary();
72         String defaultSummary = getDefaultSummary();
73 
74         if (TextUtils.isEmpty(summary)) {
75             preference.setSummary(defaultSummary);
76         } else {
77             preference.setSummary(summary);
78         }
79     }
80 
getCarSoftApConfig()81     protected SoftApConfiguration getCarSoftApConfig() {
82         return mCarWifiManager.getSoftApConfig();
83     }
84 
setCarSoftApConfig(SoftApConfiguration configuration)85     protected void setCarSoftApConfig(SoftApConfiguration configuration) {
86         mCarWifiManager.setSoftApConfig(configuration);
87         requestWifiTetherRestart();
88     }
89 
getCarWifiManager()90     protected CarWifiManager getCarWifiManager() {
91         return mCarWifiManager;
92     }
93 
getSummary()94     protected abstract String getSummary();
95 
getDefaultSummary()96     protected abstract String getDefaultSummary();
97 
requestWifiTetherRestart()98     protected void requestWifiTetherRestart() {
99         Intent intent = new Intent(ACTION_RESTART_WIFI_TETHERING);
100         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
101     }
102 }
103