1 /* <lambda>null2 * Copyright (C) 2020 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.deskclock.alarms 18 19 import android.app.Dialog 20 import android.app.TimePickerDialog 21 import android.content.Context 22 import android.os.Bundle 23 import android.text.format.DateFormat 24 import android.widget.TimePicker 25 import androidx.appcompat.app.AlertDialog 26 import androidx.fragment.app.DialogFragment 27 import androidx.fragment.app.Fragment 28 import androidx.fragment.app.FragmentManager 29 30 import com.android.deskclock.Utils 31 32 import java.util.Calendar 33 34 /** 35 * DialogFragment used to show TimePicker. 36 */ 37 class TimePickerDialogFragment : DialogFragment() { 38 39 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 40 val listener = getParentFragment() as OnTimeSetListener 41 42 val now = Calendar.getInstance() 43 val args: Bundle = arguments ?: Bundle.EMPTY 44 val hour: Int = args.getInt(ARG_HOUR, now[Calendar.HOUR_OF_DAY]) 45 val minute: Int = args.getInt(ARG_MINUTE, now[Calendar.MINUTE]) 46 return if (Utils.isLOrLater) { 47 val context: Context = requireActivity() 48 TimePickerDialog(context, { _, hourOfDay, minuteOfHour -> 49 listener.onTimeSet(this@TimePickerDialogFragment, hourOfDay, minuteOfHour) 50 }, hour, minute, DateFormat.is24HourFormat(context)) 51 } else { 52 val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity()) 53 val context: Context = builder.getContext() 54 55 val timePicker = TimePicker(context) 56 timePicker.setCurrentHour(hour) 57 timePicker.setCurrentMinute(minute) 58 timePicker.setIs24HourView(DateFormat.is24HourFormat(context)) 59 60 builder.setView(timePicker) 61 .setPositiveButton(android.R.string.ok, { _, _ -> 62 listener.onTimeSet(this@TimePickerDialogFragment, 63 timePicker.getCurrentHour(), timePicker.getCurrentMinute()) 64 }).setNegativeButton(android.R.string.cancel, null /* listener */) 65 .create() 66 } 67 } 68 69 /** 70 * The callback interface used to indicate the user is done filling in the time (e.g. they 71 * clicked on the 'OK' button). 72 */ 73 interface OnTimeSetListener { 74 /** 75 * Called when the user is done setting a new time and the dialog has closed. 76 * 77 * @param fragment the fragment associated with this listener 78 * @param hourOfDay the hour that was set 79 * @param minute the minute that was set 80 */ 81 fun onTimeSet(fragment: TimePickerDialogFragment?, hourOfDay: Int, minute: Int) 82 } 83 84 companion object { 85 /** 86 * Tag for timer picker fragment in FragmentManager. 87 */ 88 private const val TAG = "TimePickerDialogFragment" 89 90 private const val ARG_HOUR = TAG + "_hour" 91 private const val ARG_MINUTE = TAG + "_minute" 92 93 @JvmStatic 94 fun show(fragment: Fragment) { 95 show(fragment, -1 /* hour */, -1 /* minute */) 96 } 97 98 fun show(parentFragment: Fragment, hourOfDay: Int, minute: Int) { 99 require(parentFragment is OnTimeSetListener) { 100 "Fragment must implement OnTimeSetListener" 101 } 102 103 val manager: FragmentManager = parentFragment.getChildFragmentManager() 104 if (manager == null || manager.isDestroyed()) { 105 return 106 } 107 108 // Make sure the dialog isn't already added. 109 removeTimeEditDialog(manager) 110 111 val fragment = TimePickerDialogFragment() 112 113 val args = Bundle() 114 if (hourOfDay in 0..23) { 115 args.putInt(ARG_HOUR, hourOfDay) 116 } 117 if (minute in 0..59) { 118 args.putInt(ARG_MINUTE, minute) 119 } 120 121 fragment.setArguments(args) 122 fragment.show(manager, TAG) 123 } 124 125 @JvmStatic 126 fun removeTimeEditDialog(manager: FragmentManager?) { 127 manager?.let { manager -> 128 val prev: Fragment? = manager.findFragmentByTag(TAG) 129 prev?.let { 130 manager.beginTransaction().remove(it).commit() 131 } 132 } 133 } 134 } 135 }