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.datausage;
18 
19 import static android.net.NetworkPolicy.WARNING_DISABLED;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceGroup;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.FragmentController;
30 import com.android.settingslib.net.DataUsageController;
31 
32 /** Controls setting the data warning threshold. */
33 public class DataWarningPreferenceController extends
34         DataWarningAndLimitBasePreferenceController<PreferenceGroup> implements
35         Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
36 
37     private final DataUsageController mDataUsageController;
38 
39     private TwoStatePreference mEnableDataWarningPreference;
40     private Preference mSetDataWarningPreference;
41 
DataWarningPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)42     public DataWarningPreferenceController(Context context, String preferenceKey,
43             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
44         super(context, preferenceKey, fragmentController, uxRestrictions);
45         mDataUsageController = new DataUsageController(getContext());
46     }
47 
48     @Override
getPreferenceType()49     protected Class<PreferenceGroup> getPreferenceType() {
50         return PreferenceGroup.class;
51     }
52 
53     @Override
onCreateInternal()54     protected void onCreateInternal() {
55         mEnableDataWarningPreference = (TwoStatePreference) getPreference().findPreference(
56                 getContext().getString(R.string.pk_data_set_warning));
57         mEnableDataWarningPreference.setOnPreferenceChangeListener(this);
58         mSetDataWarningPreference = getPreference().findPreference(
59                 getContext().getString(R.string.pk_data_warning));
60         mSetDataWarningPreference.setOnPreferenceClickListener(this);
61     }
62 
63     @Override
onStartInternal()64     public void onStartInternal() {
65         super.onStartInternal();
66         getNetworkPolicyEditor().read();
67     }
68 
69     @Override
updateState(PreferenceGroup preference)70     protected void updateState(PreferenceGroup preference) {
71         long warningBytes = getNetworkPolicyEditor().getPolicyWarningBytes(getNetworkTemplate());
72         if (warningBytes == WARNING_DISABLED) {
73             mSetDataWarningPreference.setSummary(null);
74             mEnableDataWarningPreference.setChecked(false);
75         } else {
76             mSetDataWarningPreference.setSummary(
77                     DataUsageUtils.bytesToIecUnits(getContext(), warningBytes));
78             mEnableDataWarningPreference.setChecked(true);
79         }
80 
81         mSetDataWarningPreference.setEnabled(mEnableDataWarningPreference.isChecked());
82     }
83 
84     @Override
onPreferenceChange(Preference preference, Object newValue)85     public boolean onPreferenceChange(Preference preference, Object newValue) {
86         boolean enabled = (Boolean) newValue;
87         getNetworkPolicyEditor().setPolicyWarningBytes(getNetworkTemplate(),
88                 enabled ? mDataUsageController.getDefaultWarningLevel() : WARNING_DISABLED);
89         refreshUi();
90         return true;
91     }
92 
93     @Override
onPreferenceClick(Preference preference)94     public boolean onPreferenceClick(Preference preference) {
95         getFragmentController().launchFragment(DataWarningSetThresholdFragment
96                 .newInstance(getNetworkTemplate()));
97         return true;
98     }
99 }
100