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