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.content.Context; 20 import androidx.annotation.VisibleForTesting; 21 import com.google.android.material.snackbar.Snackbar; 22 import android.text.format.DateFormat; 23 import android.text.format.DateUtils; 24 import android.view.View; 25 import android.widget.Toast; 26 27 import com.android.deskclock.provider.AlarmInstance; 28 import com.android.deskclock.widget.toast.SnackbarManager; 29 import com.android.deskclock.widget.toast.ToastManager; 30 31 import java.util.Calendar; 32 import java.util.Locale; 33 34 /** 35 * Static utility methods for Alarms. 36 */ 37 public class AlarmUtils { 38 getFormattedTime(Context context, Calendar time)39 public static String getFormattedTime(Context context, Calendar time) { 40 final String skeleton = DateFormat.is24HourFormat(context) ? "EHm" : "Ehma"; 41 final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); 42 return (String) DateFormat.format(pattern, time); 43 } 44 getFormattedTime(Context context, long timeInMillis)45 public static String getFormattedTime(Context context, long timeInMillis) { 46 final Calendar c = Calendar.getInstance(); 47 c.setTimeInMillis(timeInMillis); 48 return getFormattedTime(context, c); 49 } 50 getAlarmText(Context context, AlarmInstance instance, boolean includeLabel)51 public static String getAlarmText(Context context, AlarmInstance instance, 52 boolean includeLabel) { 53 String alarmTimeStr = getFormattedTime(context, instance.getAlarmTime()); 54 return (instance.mLabel.isEmpty() || !includeLabel) 55 ? alarmTimeStr 56 : alarmTimeStr + " - " + instance.mLabel; 57 } 58 59 /** 60 * format "Alarm set for 2 days, 7 hours, and 53 minutes from now." 61 */ 62 @VisibleForTesting formatElapsedTimeUntilAlarm(Context context, long delta)63 static String formatElapsedTimeUntilAlarm(Context context, long delta) { 64 // If the alarm will ring within 60 seconds, just report "less than a minute." 65 final String[] formats = context.getResources().getStringArray(R.array.alarm_set); 66 if (delta < DateUtils.MINUTE_IN_MILLIS) { 67 return formats[0]; 68 } 69 70 // Otherwise, format the remaining time until the alarm rings. 71 72 // Round delta upwards to the nearest whole minute. (e.g. 7m 58s -> 8m) 73 final long remainder = delta % DateUtils.MINUTE_IN_MILLIS; 74 delta += remainder == 0 ? 0 : (DateUtils.MINUTE_IN_MILLIS - remainder); 75 76 int hours = (int) delta / (1000 * 60 * 60); 77 final int minutes = (int) delta / (1000 * 60) % 60; 78 final int days = hours / 24; 79 hours = hours % 24; 80 81 String daySeq = Utils.getNumberFormattedQuantityString(context, R.plurals.days, days); 82 String minSeq = Utils.getNumberFormattedQuantityString(context, R.plurals.minutes, minutes); 83 String hourSeq = Utils.getNumberFormattedQuantityString(context, R.plurals.hours, hours); 84 85 final boolean showDays = days > 0; 86 final boolean showHours = hours > 0; 87 final boolean showMinutes = minutes > 0; 88 89 // Compute the index of the most appropriate time format based on the time delta. 90 final int index = (showDays ? 1 : 0) | (showHours ? 2 : 0) | (showMinutes ? 4 : 0); 91 92 return String.format(formats[index], daySeq, hourSeq, minSeq); 93 } 94 popAlarmSetToast(Context context, long alarmTime)95 public static void popAlarmSetToast(Context context, long alarmTime) { 96 final long alarmTimeDelta = alarmTime - System.currentTimeMillis(); 97 final String text = formatElapsedTimeUntilAlarm(context, alarmTimeDelta); 98 Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG); 99 ToastManager.setToast(toast); 100 toast.show(); 101 } 102 popAlarmSetSnackbar(View snackbarAnchor, long alarmTime)103 public static void popAlarmSetSnackbar(View snackbarAnchor, long alarmTime) { 104 final long alarmTimeDelta = alarmTime - System.currentTimeMillis(); 105 final String text = formatElapsedTimeUntilAlarm( 106 snackbarAnchor.getContext(), alarmTimeDelta); 107 SnackbarManager.show(Snackbar.make(snackbarAnchor, text, Snackbar.LENGTH_SHORT)); 108 snackbarAnchor.announceForAccessibility(text); 109 } 110 } 111