• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tv.settings.system;
18 
19 import android.app.AlarmManager;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.text.TextUtils;
23 import android.util.TypedValue;
24 import android.view.ContextThemeWrapper;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 
30 import androidx.leanback.preference.LeanbackPreferenceDialogFragment;
31 import androidx.leanback.widget.picker.DatePicker;
32 import androidx.leanback.widget.picker.TimePicker;
33 import androidx.preference.DialogPreference;
34 
35 import com.android.tv.settings.R;
36 
37 import java.util.Calendar;
38 
39 /**
40  * A DialogFragment started for either setting date or setting time purposes. The type of
41  * fragment launched is controlled by the type of {@link LeanbackPickerDialogPreference}
42  * that's clicked. Launching of these two fragments is done inside
43  * {@link com.android.tv.settings.BaseSettingsFragment#onPreferenceDisplayDialog}.
44  */
45 public class LeanbackPickerDialogFragment extends LeanbackPreferenceDialogFragment {
46 
47     private static final String EXTRA_PICKER_TYPE = "LeanbackPickerDialogFragment.PickerType";
48     private static final String TYPE_DATE = "date";
49     private static final String TYPE_TIME = "time";
50     private static final String SAVE_STATE_TITLE = "LeanbackPickerDialogFragment.title";
51 
52     private CharSequence mDialogTitle;
53     private Calendar mCalendar;
54 
55     /**
56      * Generated a new DialogFragment displaying a Leanback DatePicker widget.
57      * @param key The preference key starting this DialogFragment.
58      * @return The fragment to be started displaying a DatePicker widget for setting date.
59      */
newDatePickerInstance(String key)60     public static LeanbackPickerDialogFragment newDatePickerInstance(String key) {
61         final Bundle args = new Bundle(1);
62         args.putString(ARG_KEY, key);
63         args.putString(EXTRA_PICKER_TYPE, TYPE_DATE);
64 
65         final LeanbackPickerDialogFragment fragment = new LeanbackPickerDialogFragment();
66         fragment.setArguments(args);
67         return fragment;
68     }
69 
70     /**
71      * Generated a new DialogFragment displaying a Leanback TimePicker widget.
72      * @param key The preference key starting this DialogFragment.
73      * @return The fragment to be started displaying a TimePicker widget for setting time.
74      */
newTimePickerInstance(String key)75     public static LeanbackPickerDialogFragment newTimePickerInstance(String key) {
76         final Bundle args = new Bundle(1);
77         args.putString(ARG_KEY, key);
78         args.putString(EXTRA_PICKER_TYPE, TYPE_TIME);
79 
80         final LeanbackPickerDialogFragment fragment = new LeanbackPickerDialogFragment();
81         fragment.setArguments(args);
82         return fragment;
83     }
84 
85     @Override
onCreate(Bundle savedInstanceState)86     public void onCreate(Bundle savedInstanceState) {
87         super.onCreate(savedInstanceState);
88 
89         if (savedInstanceState == null) {
90             final DialogPreference preference = getPreference();
91             mDialogTitle = preference.getDialogTitle();
92         } else {
93             mDialogTitle = savedInstanceState.getCharSequence(SAVE_STATE_TITLE);
94         }
95         mCalendar = Calendar.getInstance();
96     }
97 
98     @Override
onSaveInstanceState(Bundle outState)99     public void onSaveInstanceState(Bundle outState) {
100         super.onSaveInstanceState(outState);
101         outState.putCharSequence(SAVE_STATE_TITLE, mDialogTitle);
102     }
103 
104     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)105     public View onCreateView(LayoutInflater inflater, ViewGroup container,
106                              Bundle savedInstanceState) {
107         final String pickerType = getArguments().getString(EXTRA_PICKER_TYPE);
108 
109         final TypedValue tv = new TypedValue();
110         getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, tv, true);
111         int theme = tv.resourceId;
112         if (theme == 0) {
113             // Fallback to default theme.
114             theme = R.style.PreferenceThemeOverlayLeanback;
115         }
116         Context styledContext = new ContextThemeWrapper(getActivity(), theme);
117         LayoutInflater styledInflater = inflater.cloneInContext(styledContext);
118         final View view = styledInflater.inflate(R.layout.picker_dialog_fragment, container, false);
119         ViewGroup pickerContainer = view.findViewById(R.id.picker_container);
120         if (pickerType.equals(TYPE_DATE)) {
121             styledInflater.inflate(R.layout.date_picker_widget, pickerContainer, true);
122             DatePicker datePicker = pickerContainer.findViewById(R.id.date_picker);
123             datePicker.setActivated(true);
124             datePicker.setOnClickListener(v -> {
125                 // Setting the new system date
126                 ((AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE)).setTime(
127                         datePicker.getDate()
128                 );
129                 // Finish the fragment/activity when clicked.
130                 if (!getFragmentManager().popBackStackImmediate()) {
131                     getActivity().finish();
132                 }
133             });
134 
135         } else {
136             styledInflater.inflate(R.layout.time_picker_widget, pickerContainer, true);
137             TimePicker timePicker = pickerContainer.findViewById(R.id.time_picker);
138             timePicker.setActivated(true);
139             timePicker.setOnClickListener(v -> {
140                 // Setting the new system time
141                 mCalendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
142                 mCalendar.set(Calendar.MINUTE, timePicker.getMinute());
143                 mCalendar.set(Calendar.SECOND, 0);
144                 mCalendar.set(Calendar.MILLISECOND, 0);
145                 ((AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE)).setTime(
146                         mCalendar.getTimeInMillis()
147                 );
148                 // Finish the fragment/activity when clicked.
149                 if (!getFragmentManager().popBackStackImmediate()) {
150                     getActivity().finish();
151                 }
152             });
153         }
154 
155         final CharSequence title = mDialogTitle;
156         if (!TextUtils.isEmpty(title)) {
157             final TextView titleView = view.findViewById(R.id.decor_title);
158             titleView.setText(title);
159         }
160         return view;
161     }
162 }
163