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