1 /*
2  * Copyright (C) 2012 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;
18 
19 import android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.app.FragmentTransaction;
22 import android.content.Context;
23 import android.text.format.DateFormat;
24 import android.widget.Toast;
25 
26 import android.app.TimePickerDialog;
27 import com.android.deskclock.provider.Alarm;
28 import com.android.deskclock.provider.AlarmInstance;
29 
30 import java.util.Calendar;
31 import java.util.Locale;
32 
33 /**
34  * Static utility methods for Alarms.
35  */
36 public class AlarmUtils {
37     public static final String FRAG_TAG_TIME_PICKER = "time_dialog";
38 
getFormattedTime(Context context, Calendar time)39     public static String getFormattedTime(Context context, Calendar time) {
40         String skeleton = DateFormat.is24HourFormat(context) ? "EHm" : "Ehma";
41         String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
42         return (String) DateFormat.format(pattern, time);
43     }
44 
getAlarmText(Context context, AlarmInstance instance)45     public static String getAlarmText(Context context, AlarmInstance instance) {
46         String alarmTimeStr = getFormattedTime(context, instance.getAlarmTime());
47         return !instance.mLabel.isEmpty() ? alarmTimeStr + " - " + instance.mLabel
48                 : alarmTimeStr;
49     }
50 
51     /**
52      * Show the time picker dialog. This is called from AlarmClockFragment to set alarm.
53      * @param fragment The calling fragment (which is also a onTimeSetListener),
54      *                 we use it as the target fragment of the TimePickerFragment, so later the
55      *                 latter can retrieve it and set it as its onTimeSetListener when the fragment
56      *                 is recreated.
57      * @param alarm The clicked alarm, it can be null if user was clicking the fab instead.
58      */
showTimeEditDialog(Fragment fragment, final Alarm alarm)59     public static void showTimeEditDialog(Fragment fragment, final Alarm alarm) {
60         final FragmentManager manager = fragment.getFragmentManager();
61         final FragmentTransaction ft = manager.beginTransaction();
62         final Fragment prev = manager.findFragmentByTag(FRAG_TAG_TIME_PICKER);
63         if (prev != null) {
64             ft.remove(prev);
65         }
66         ft.commit();
67         final TimePickerFragment timePickerFragment = new TimePickerFragment();
68         timePickerFragment.setTargetFragment(fragment, 0);
69         timePickerFragment.setOnTimeSetListener((TimePickerDialog.OnTimeSetListener) fragment);
70         timePickerFragment.setAlarm(alarm);
71         timePickerFragment.show(manager, FRAG_TAG_TIME_PICKER);
72     }
73 
74     /**
75      * format "Alarm set for 2 days 7 hours and 53 minutes from
76      * now"
77      */
formatToast(Context context, long timeInMillis)78     private static String formatToast(Context context, long timeInMillis) {
79         long delta = timeInMillis - System.currentTimeMillis();
80         long hours = delta / (1000 * 60 * 60);
81         long minutes = delta / (1000 * 60) % 60;
82         long days = hours / 24;
83         hours = hours % 24;
84 
85         String daySeq = (days == 0) ? "" :
86                 (days == 1) ? context.getString(R.string.day) :
87                         context.getString(R.string.days, Long.toString(days));
88 
89         String minSeq = (minutes == 0) ? "" :
90                 (minutes == 1) ? context.getString(R.string.minute) :
91                         context.getString(R.string.minutes, Long.toString(minutes));
92 
93         String hourSeq = (hours == 0) ? "" :
94                 (hours == 1) ? context.getString(R.string.hour) :
95                         context.getString(R.string.hours, Long.toString(hours));
96 
97         boolean dispDays = days > 0;
98         boolean dispHour = hours > 0;
99         boolean dispMinute = minutes > 0;
100 
101         int index = (dispDays ? 1 : 0) |
102                 (dispHour ? 2 : 0) |
103                 (dispMinute ? 4 : 0);
104 
105         String[] formats = context.getResources().getStringArray(R.array.alarm_set);
106         return String.format(formats[index], daySeq, hourSeq, minSeq);
107     }
108 
popAlarmSetToast(Context context, long timeInMillis)109     public static void popAlarmSetToast(Context context, long timeInMillis) {
110         String toastText = formatToast(context, timeInMillis);
111         Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG);
112         ToastMaster.setToast(toast);
113         toast.show();
114     }
115 }
116