Home | History | Annotate | Download | only in deskclock
      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 android.support.annotation.VisibleForTesting;
     21 import android.support.design.widget.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 
     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 
     45     public static String getAlarmText(Context context, AlarmInstance instance,
     46             boolean includeLabel) {
     47         String alarmTimeStr = getFormattedTime(context, instance.getAlarmTime());
     48         return (instance.mLabel.isEmpty() || !includeLabel)
     49                 ? alarmTimeStr
     50                 : alarmTimeStr + " - " + instance.mLabel;
     51     }
     52 
     53     /**
     54      * format "Alarm set for 2 days, 7 hours, and 53 minutes from now."
     55      */
     56     @VisibleForTesting
     57     static String formatElapsedTimeUntilAlarm(Context context, long delta) {
     58         // If the alarm will ring within 60 seconds, just report "less than a minute."
     59         final String[] formats = context.getResources().getStringArray(R.array.alarm_set);
     60         if (delta < DateUtils.MINUTE_IN_MILLIS) {
     61             return formats[0];
     62         }
     63 
     64         // Otherwise, format the remaining time until the alarm rings.
     65 
     66         // Round delta upwards to the nearest whole minute. (e.g. 7m 58s -> 8m)
     67         final long remainder = delta % DateUtils.MINUTE_IN_MILLIS;
     68         delta += remainder == 0 ? 0 : (DateUtils.MINUTE_IN_MILLIS - remainder);
     69 
     70         int hours = (int) delta / (1000 * 60 * 60);
     71         final int minutes = (int) delta / (1000 * 60) % 60;
     72         final int days = hours / 24;
     73         hours = hours % 24;
     74 
     75         String daySeq = Utils.getNumberFormattedQuantityString(context, R.plurals.days, days);
     76         String minSeq = Utils.getNumberFormattedQuantityString(context, R.plurals.minutes, minutes);
     77         String hourSeq = Utils.getNumberFormattedQuantityString(context, R.plurals.hours, hours);
     78 
     79         final boolean showDays = days > 0;
     80         final boolean showHours = hours > 0;
     81         final boolean showMinutes = minutes > 0;
     82 
     83         // Compute the index of the most appropriate time format based on the time delta.
     84         final int index = (showDays ? 1 : 0) | (showHours ? 2 : 0) | (showMinutes ? 4 : 0);
     85 
     86         return String.format(formats[index], daySeq, hourSeq, minSeq);
     87     }
     88 
     89     public static void popAlarmSetToast(Context context, long alarmTime) {
     90         final long alarmTimeDelta = alarmTime - System.currentTimeMillis();
     91         final String text = formatElapsedTimeUntilAlarm(context, alarmTimeDelta);
     92         Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
     93         ToastManager.setToast(toast);
     94         toast.show();
     95     }
     96 
     97     public static void popAlarmSetSnackbar(View snackbarAnchor, long alarmTime) {
     98         final long alarmTimeDelta = alarmTime - System.currentTimeMillis();
     99         final String text = formatElapsedTimeUntilAlarm(
    100                 snackbarAnchor.getContext(), alarmTimeDelta);
    101         SnackbarManager.show(Snackbar.make(snackbarAnchor, text, Snackbar.LENGTH_SHORT));
    102         snackbarAnchor.announceForAccessibility(text);
    103     }
    104 }
    105