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.settings.notification.modes;
18 
19 import android.app.Dialog;
20 import android.app.TimePickerDialog;
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.text.format.DateFormat;
25 import android.widget.TimePicker;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
30 
31 /**
32  * Dialog that shows when a user selects a (start or end) time to edit for a schedule-based mode.
33  */
34 public class ZenModeTimePickerFragment extends InstrumentedDialogFragment implements
35         TimePickerDialog.OnTimeSetListener {
36     private final Context mContext;
37     private final TimeSetter mTimeSetter;
38     private final int mHour;
39     private final int mMinute;
40 
ZenModeTimePickerFragment(Context context, int hour, int minute, @NonNull TimeSetter timeSetter)41     public ZenModeTimePickerFragment(Context context, int hour, int minute,
42             @NonNull TimeSetter timeSetter) {
43         super();
44         mContext = context;
45         mHour = hour;
46         mMinute = minute;
47         mTimeSetter = timeSetter;
48     }
49 
50     @Override
onCreateDialog(Bundle savedInstanceState)51     public Dialog onCreateDialog(Bundle savedInstanceState) {
52         return new TimePickerDialog(mContext, this, mHour, mMinute,
53                 DateFormat.is24HourFormat(mContext));
54     }
55 
56     /**
57      * Calls the provided TimeSetter's setTime() method when a time is set on the TimePicker.
58      */
onTimeSet(TimePicker view, int hourOfDay, int minute)59     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
60         mTimeSetter.setTime(hourOfDay, minute);
61     }
62 
63     @Override
getMetricsCategory()64     public int getMetricsCategory() {
65         // TODO: b/332937635 - set correct metrics category (or decide to keep this one?)
66         return SettingsEnums.DIALOG_ZEN_TIMEPICKER;
67     }
68 
69     /**
70      * Interface for a method to pass into the TimePickerFragment that specifies what to do when the
71      * time is updated.
72      */
73     public interface TimeSetter {
setTime(int hour, int minute)74         void setTime(int hour, int minute);
75     }
76 }
77