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
17package com.android.deskclock;
18
19import android.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.text.format.DateFormat;
24import android.widget.Toast;
25
26import com.android.datetimepicker.time.TimePickerDialog;
27import com.android.deskclock.provider.Alarm;
28import com.android.deskclock.provider.AlarmInstance;
29
30import java.util.Calendar;
31import java.util.Locale;
32
33/**
34 * Static utility methods for Alarms.
35 */
36public class AlarmUtils {
37    public static final String FRAG_TAG_TIME_PICKER = "time_dialog";
38
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
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    public static void showTimeEditDialog(FragmentManager manager, final Alarm alarm,
52            TimePickerDialog.OnTimeSetListener listener, boolean is24HourMode) {
53
54        int hour, minutes;
55        if (alarm == null) {
56            hour = 0; minutes = 0;
57        } else {
58            hour = alarm.hour;
59            minutes = alarm.minutes;
60        }
61        TimePickerDialog dialog = TimePickerDialog.newInstance(listener,
62                hour, minutes, is24HourMode);
63        dialog.setThemeDark(true);
64
65        // Make sure the dialog isn't already added.
66        manager.executePendingTransactions();
67        final FragmentTransaction ft = manager.beginTransaction();
68        final Fragment prev = manager.findFragmentByTag(FRAG_TAG_TIME_PICKER);
69        if (prev != null) {
70            ft.remove(prev);
71        }
72        ft.commit();
73
74        if (dialog != null && !dialog.isAdded()) {
75            dialog.show(manager, FRAG_TAG_TIME_PICKER);
76        }
77    }
78
79    /**
80     * format "Alarm set for 2 days 7 hours and 53 minutes from
81     * now"
82     */
83    private static String formatToast(Context context, long timeInMillis) {
84        long delta = timeInMillis - System.currentTimeMillis();
85        long hours = delta / (1000 * 60 * 60);
86        long minutes = delta / (1000 * 60) % 60;
87        long days = hours / 24;
88        hours = hours % 24;
89
90        String daySeq = (days == 0) ? "" :
91                (days == 1) ? context.getString(R.string.day) :
92                        context.getString(R.string.days, Long.toString(days));
93
94        String minSeq = (minutes == 0) ? "" :
95                (minutes == 1) ? context.getString(R.string.minute) :
96                        context.getString(R.string.minutes, Long.toString(minutes));
97
98        String hourSeq = (hours == 0) ? "" :
99                (hours == 1) ? context.getString(R.string.hour) :
100                        context.getString(R.string.hours, Long.toString(hours));
101
102        boolean dispDays = days > 0;
103        boolean dispHour = hours > 0;
104        boolean dispMinute = minutes > 0;
105
106        int index = (dispDays ? 1 : 0) |
107                (dispHour ? 2 : 0) |
108                (dispMinute ? 4 : 0);
109
110        String[] formats = context.getResources().getStringArray(R.array.alarm_set);
111        return String.format(formats[index], daySeq, hourSeq, minSeq);
112    }
113
114    public static void popAlarmSetToast(Context context, long timeInMillis) {
115        String toastText = formatToast(context, timeInMillis);
116        Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG);
117        ToastMaster.setToast(toast);
118        toast.show();
119    }
120}
121