1 /*
2  * Copyright (C) 2024 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 static android.car.settings.CarSettings.Global.ENABLE_PERSISTENT_TETHERING;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.car.feature.Flags;
23 import android.car.wifi.CarWifiManager;
24 import android.content.Context;
25 import android.provider.Settings;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.TwoStatePreference;
30 
31 import com.android.car.settings.CarSettingsApplication;
32 import com.android.car.settings.common.FragmentController;
33 import com.android.car.settings.common.PreferenceController;
34 
35 /**
36  * Controls wifi tethering persistent on configuration
37  */
38 public class WifiTetherPersistentOnPreferenceController extends
39         PreferenceController<TwoStatePreference> {
40 
41     @VisibleForTesting
42     static final String ENABLED = "true";
43     @VisibleForTesting
44     static final String DISABLED = "false";
45 
WifiTetherPersistentOnPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46     public WifiTetherPersistentOnPreferenceController(Context context, String preferenceKey,
47             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
48         super(context, preferenceKey, fragmentController, uxRestrictions);
49     }
50 
51     @Override
getPreferenceType()52     protected Class<TwoStatePreference> getPreferenceType() {
53         return TwoStatePreference.class;
54     }
55 
56     @Override
getDefaultAvailabilityStatus()57     protected int getDefaultAvailabilityStatus() {
58         if (!Flags.persistApSettings()) {
59             return UNSUPPORTED_ON_DEVICE;
60         }
61         CarWifiManager carWifiManager = getCarWifiManager();
62         if (carWifiManager != null && carWifiManager.canControlPersistTetheringSettings()) {
63             return AVAILABLE;
64         }
65         return DISABLED_FOR_PROFILE;
66     }
67 
68     @Override
updateState(TwoStatePreference preference)69     protected void updateState(TwoStatePreference preference) {
70         boolean isChecked = TextUtils.equals(ENABLED,
71                 Settings.Global.getString(getContext().getContentResolver(),
72                         ENABLE_PERSISTENT_TETHERING));
73         preference.setChecked(isChecked);
74     }
75 
76     @Override
handlePreferenceChanged(TwoStatePreference preference, Object newValue)77     protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) {
78         boolean settingsOn = (Boolean) newValue;
79 
80         Settings.Global.putString(getContext().getContentResolver(),
81                 ENABLE_PERSISTENT_TETHERING, settingsOn ? ENABLED : DISABLED);
82         return true;
83     }
84 
getCarWifiManager()85     private CarWifiManager getCarWifiManager() {
86         return ((CarSettingsApplication) getContext().getApplicationContext())
87                 .getCarWifiManager();
88     }
89 }
90