1 /* 2 * 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 package com.android.wallpaper.util; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.text.format.DateFormat; 23 24 import androidx.annotation.Nullable; 25 26 import java.util.Calendar; 27 28 /** Utility class for clock time preview. */ 29 public final class TimeUtils { 30 31 private static final CharSequence CLOCK_FORMAT_12HOUR = "h:mm"; 32 private static final CharSequence CLOCK_FORMAT_24HOUR = "H:mm"; 33 private static final CharSequence CLOCK_DOUBLE_LINE_FORMAT_12_HOUR = "hh\nmm"; 34 private static final CharSequence CLOCK_DOUBLE_LINE_FORMAT_24_HOUR = "HH\nmm"; 35 36 /** 37 * Returns the default clock formatted time. For example: 4:35 or 16:35. 38 * 39 * <p> For 12-hour format, there's no AM/PM field displayed. 40 */ getFormattedTime(Context context, Calendar calendar)41 public static CharSequence getFormattedTime(Context context, Calendar calendar) { 42 return DateFormat.format( 43 DateFormat.is24HourFormat(context) 44 ? CLOCK_FORMAT_24HOUR 45 : CLOCK_FORMAT_12HOUR, 46 calendar); 47 } 48 49 /** 50 * Returns the double line clock formatted time. 51 * 52 * <p> For 12-hour format, there's no AM/PM field displayed. 53 */ getDoubleLineFormattedTime(Context context, Calendar calendar)54 public static CharSequence getDoubleLineFormattedTime(Context context, 55 Calendar calendar) { 56 return DateFormat.format( 57 DateFormat.is24HourFormat(context) 58 ? CLOCK_DOUBLE_LINE_FORMAT_24_HOUR 59 : CLOCK_DOUBLE_LINE_FORMAT_12_HOUR, 60 calendar); 61 } 62 63 /** 64 * BroadcastReceiver that can notify a listener when the system time (minutes) changes. 65 * Use {@link #registerNewReceiver(Context, TimeListener)} to create a new instance that will be 66 * automatically registered using the given Context. 67 */ 68 public static class TimeTicker extends BroadcastReceiver { 69 70 /** 71 * Listener for the system time's change. 72 */ 73 public interface TimeListener { 74 /** 75 * Called when the system time (minutes) changes. 76 */ onCurrentTimeChanged()77 void onCurrentTimeChanged(); 78 } 79 80 /** 81 * Registers a broadcast receiver for time tick. 82 */ registerNewReceiver(Context context, @Nullable TimeListener listener)83 public static TimeTicker registerNewReceiver(Context context, 84 @Nullable TimeListener listener) { 85 TimeTicker receiver = new TimeTicker(listener); 86 // Register broadcast receiver for time tick 87 final IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); 88 context.registerReceiver(receiver, filter); 89 return receiver; 90 } 91 92 private TimeListener mListener; 93 TimeTicker(@ullable TimeListener listener)94 private TimeTicker(@Nullable TimeListener listener) { 95 mListener = listener; 96 } 97 98 @Override onReceive(Context context, Intent intent)99 public void onReceive(Context context, Intent intent) { 100 if (mListener != null) { 101 mListener.onCurrentTimeChanged(); 102 } 103 } 104 } 105 } 106