1 /* 2 * Copyright 2016 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.example.android.directboot; 18 19 import com.example.android.directboot.alarms.Alarm; 20 import com.example.android.directboot.alarms.AlarmStorage; 21 import com.example.android.directboot.alarms.AlarmUtil; 22 23 import android.os.Bundle; 24 import android.support.annotation.Nullable; 25 import android.support.v4.app.DialogFragment; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.Button; 30 import android.widget.TimePicker; 31 import android.widget.Toast; 32 33 import java.util.Calendar; 34 35 /** 36 * DialogFragment for showing a TimePicker. 37 */ 38 public class TimePickerFragment extends DialogFragment { 39 40 private TimePicker mTimePicker; 41 private AlarmStorage mAlarmStorage; 42 private AlarmAddListener mAlarmAddListener; 43 private AlarmUtil mAlarmUtil; 44 TimePickerFragment()45 public TimePickerFragment() {} 46 newInstance()47 public static TimePickerFragment newInstance() { 48 return new TimePickerFragment(); 49 } 50 setAlarmAddListener(AlarmAddListener listener)51 public void setAlarmAddListener(AlarmAddListener listener) { 52 mAlarmAddListener = listener; 53 } 54 55 @Override onCreate(@ullable Bundle savedInstanceState)56 public void onCreate(@Nullable Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog); 59 } 60 61 @Override onActivityCreated(Bundle savedInstanceState)62 public void onActivityCreated(Bundle savedInstanceState) { 63 super.onActivityCreated(savedInstanceState); 64 mAlarmStorage = new AlarmStorage(getActivity()); 65 mAlarmUtil = new AlarmUtil(getActivity()); 66 } 67 68 @Nullable 69 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)70 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 71 @Nullable Bundle savedInstanceState) { 72 View view = inflater.inflate(R.layout.fragment_time_picker, container, false); 73 mTimePicker = (TimePicker) view.findViewById(R.id.time_picker_alarm); 74 Button buttonOk = (Button) view.findViewById(R.id.button_ok_time_picker); 75 buttonOk.setOnClickListener(new View.OnClickListener() { 76 @Override 77 public void onClick(View view) { 78 Calendar alarmTime = mAlarmUtil 79 .getNextAlarmTime(mTimePicker.getHour(), mTimePicker.getMinute()); 80 Alarm alarm = mAlarmStorage 81 .saveAlarm(alarmTime.get(Calendar.MONTH), alarmTime.get(Calendar.DATE), 82 alarmTime.get(Calendar.HOUR_OF_DAY), alarmTime.get(Calendar.MINUTE)); 83 String alarmSavedString = getActivity() 84 .getString(R.string.alarm_saved, alarm.hour, alarm.minute); 85 Toast.makeText(getActivity(), alarmSavedString, Toast.LENGTH_SHORT).show(); 86 if (mAlarmAddListener != null) { 87 mAlarmAddListener.onAlarmAdded(alarm); 88 } 89 dismiss(); 90 } 91 }); 92 Button buttonCancel = (Button) view.findViewById(R.id.button_cancel_time_picker); 93 buttonCancel.setOnClickListener(new View.OnClickListener() { 94 @Override 95 public void onClick(View view) { 96 dismiss(); 97 } 98 }); 99 return view; 100 } 101 102 public interface AlarmAddListener { onAlarmAdded(Alarm alarm)103 void onAlarmAdded(Alarm alarm); 104 } 105 } 106