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 android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.text.format.Time;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.car.settings.common.FragmentController;
26 
27 /**
28  * Preference which opens a {@link UsageCycleResetDayOfMonthPickerDialog} in order to pick the date
29  * on which the data warning/limit cycle should end.
30  */
31 public class CycleResetDayOfMonthPickerPreferenceController extends
32         DataWarningAndLimitBasePreferenceController<Preference> implements
33         UsageCycleResetDayOfMonthPickerDialog.ResetDayOfMonthPickedListener {
34 
35     private static final String CYCLE_PICKER_DIALOG_TAG = "cycle_picker_dialog_tag";
36 
CycleResetDayOfMonthPickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public CycleResetDayOfMonthPickerPreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
39         super(context, preferenceKey, fragmentController, uxRestrictions);
40     }
41 
42     @Override
getPreferenceType()43     protected Class<Preference> getPreferenceType() {
44         return Preference.class;
45     }
46 
47     @Override
onCreateInternal()48     protected void onCreateInternal() {
49         UsageCycleResetDayOfMonthPickerDialog dialog =
50                 (UsageCycleResetDayOfMonthPickerDialog) getFragmentController().findDialogByTag(
51                         CYCLE_PICKER_DIALOG_TAG);
52         if (dialog != null) {
53             dialog.setResetDayOfMonthPickedListener(/* listener= */ this);
54         }
55     }
56 
57     @Override
handlePreferenceClicked(Preference preference)58     protected boolean handlePreferenceClicked(Preference preference) {
59 
60         UsageCycleResetDayOfMonthPickerDialog dialog =
61                 UsageCycleResetDayOfMonthPickerDialog.newInstance(
62                         getNetworkPolicyEditor().getPolicyCycleDay(getNetworkTemplate()));
63         dialog.setResetDayOfMonthPickedListener(/* listener= */ this);
64         getFragmentController().showDialog(dialog, CYCLE_PICKER_DIALOG_TAG);
65         return true;
66     }
67 
68     @Override
onDayOfMonthPicked(int dayOfMonth)69     public void onDayOfMonthPicked(int dayOfMonth) {
70         getNetworkPolicyEditor().setPolicyCycleDay(getNetworkTemplate(), dayOfMonth,
71                 new Time().timezone);
72     }
73 }
74