• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.calendar.alerts;
18 
19 import android.app.AlarmManager;
20 import android.app.PendingIntent;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.provider.CalendarContract;
27 import android.provider.CalendarContract.CalendarAlerts;
28 import android.text.TextUtils;
29 import android.text.format.DateFormat;
30 import android.text.format.DateUtils;
31 import android.text.format.Time;
32 
33 import com.android.calendar.EventInfoActivity;
34 import com.android.calendar.R;
35 import com.android.calendar.Utils;
36 
37 import java.util.Locale;
38 import java.util.TimeZone;
39 
40 public class AlertUtils {
41 
42     public static final long SNOOZE_DELAY = 5 * 60 * 1000L;
43 
44     // We use one notification id for the expired events notification.  All
45     // other notifications (the 'active' future/concurrent ones) use a unique ID.
46     public static final int EXPIRED_GROUP_NOTIFICATION_ID = 0;
47 
48     public static final String EVENT_ID_KEY = "eventid";
49     public static final String SHOW_EVENT_KEY = "showevent";
50     public static final String EVENT_START_KEY = "eventstart";
51     public static final String EVENT_END_KEY = "eventend";
52     public static final String NOTIFICATION_ID_KEY = "notificationid";
53     public static final String EVENT_IDS_KEY = "eventids";
54 
55     /**
56      * Schedules an alarm intent with the system AlarmManager that will notify
57      * listeners when a reminder should be fired. The provider will keep
58      * scheduled reminders up to date but apps may use this to implement snooze
59      * functionality without modifying the reminders table. Scheduled alarms
60      * will generate an intent using {@link #ACTION_EVENT_REMINDER}.
61      *
62      * @param context A context for referencing system resources
63      * @param manager The AlarmManager to use or null
64      * @param alarmTime The time to fire the intent in UTC millis since epoch
65      */
scheduleAlarm(Context context, AlarmManager manager, long alarmTime)66     public static void scheduleAlarm(Context context, AlarmManager manager, long alarmTime) {
67         scheduleAlarmHelper(context, manager, alarmTime, false);
68     }
69 
70     /**
71      * Schedules the next alarm to silently refresh the notifications.  Note that if there
72      * is a pending silent refresh alarm, it will be replaced with this one.
73      */
scheduleNextNotificationRefresh(Context context, AlarmManager manager, long alarmTime)74     static void scheduleNextNotificationRefresh(Context context, AlarmManager manager,
75             long alarmTime) {
76         scheduleAlarmHelper(context, manager, alarmTime, true);
77     }
78 
scheduleAlarmHelper(Context context, AlarmManager manager, long alarmTime, boolean quietUpdate)79     private static void scheduleAlarmHelper(Context context, AlarmManager manager, long alarmTime,
80             boolean quietUpdate) {
81         if (manager == null) {
82             manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
83         }
84 
85         int alarmType = AlarmManager.RTC_WAKEUP;
86         Intent intent = new Intent(CalendarContract.ACTION_EVENT_REMINDER);
87         intent.setClass(context, AlertReceiver.class);
88         if (quietUpdate) {
89             alarmType = AlarmManager.RTC;
90         } else {
91             // Set data field so we get a unique PendingIntent instance per alarm or else alarms
92             // may be dropped.
93             Uri.Builder builder = CalendarAlerts.CONTENT_URI.buildUpon();
94             ContentUris.appendId(builder, alarmTime);
95             intent.setData(builder.build());
96         }
97 
98         intent.putExtra(CalendarContract.CalendarAlerts.ALARM_TIME, alarmTime);
99         PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
100                 PendingIntent.FLAG_UPDATE_CURRENT);
101         manager.set(alarmType, alarmTime, pi);
102     }
103 
104     /**
105      * Format the second line which shows time and location for single alert or the
106      * number of events for multiple alerts
107      *     1) Show time only for non-all day events
108      *     2) No date for today
109      *     3) Show "tomorrow" for tomorrow
110      *     4) Show date for days beyond that
111      */
formatTimeLocation(Context context, long startMillis, boolean allDay, String location)112     static String formatTimeLocation(Context context, long startMillis, boolean allDay,
113             String location) {
114         String tz = Utils.getTimeZone(context, null);
115         Time time = new Time(tz);
116         time.setToNow();
117         int today = Time.getJulianDay(time.toMillis(false), time.gmtoff);
118         time.set(startMillis);
119         int eventDay = Time.getJulianDay(time.toMillis(false), time.gmtoff);
120 
121         int flags = DateUtils.FORMAT_ABBREV_ALL;
122         if (!allDay) {
123             flags |= DateUtils.FORMAT_SHOW_TIME;
124             if (DateFormat.is24HourFormat(context)) {
125                 flags |= DateUtils.FORMAT_24HOUR;
126             }
127         } else {
128             flags |= DateUtils.FORMAT_UTC;
129         }
130 
131         if (eventDay < today || eventDay > today + 1) {
132             flags |= DateUtils.FORMAT_SHOW_DATE;
133         }
134 
135         StringBuilder sb = new StringBuilder(Utils.formatDateRange(context, startMillis,
136                 startMillis, flags));
137 
138         if (!allDay && tz != Time.getCurrentTimezone()) {
139             // Assumes time was set to the current tz
140             time.set(startMillis);
141             boolean isDST = time.isDst != 0;
142             sb.append(" ").append(TimeZone.getTimeZone(tz).getDisplayName(
143                     isDST, TimeZone.SHORT, Locale.getDefault()));
144         }
145 
146         if (eventDay == today + 1) {
147             // Tomorrow
148             sb.append(", ");
149             sb.append(context.getString(R.string.tomorrow));
150         }
151 
152         String loc;
153         if (location != null && !TextUtils.isEmpty(loc = location.trim())) {
154             sb.append(", ");
155             sb.append(loc);
156         }
157         return sb.toString();
158     }
159 
makeContentValues(long eventId, long begin, long end, long alarmTime, int minutes)160     public static ContentValues makeContentValues(long eventId, long begin, long end,
161             long alarmTime, int minutes) {
162         ContentValues values = new ContentValues();
163         values.put(CalendarAlerts.EVENT_ID, eventId);
164         values.put(CalendarAlerts.BEGIN, begin);
165         values.put(CalendarAlerts.END, end);
166         values.put(CalendarAlerts.ALARM_TIME, alarmTime);
167         long currentTime = System.currentTimeMillis();
168         values.put(CalendarAlerts.CREATION_TIME, currentTime);
169         values.put(CalendarAlerts.RECEIVED_TIME, 0);
170         values.put(CalendarAlerts.NOTIFY_TIME, 0);
171         values.put(CalendarAlerts.STATE, CalendarAlerts.STATE_SCHEDULED);
172         values.put(CalendarAlerts.MINUTES, minutes);
173         return values;
174     }
175 
buildEventViewIntent(Context c, long eventId, long begin, long end)176     public static Intent buildEventViewIntent(Context c, long eventId, long begin, long end) {
177         Intent i = new Intent(Intent.ACTION_VIEW);
178         Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
179         builder.appendEncodedPath("events/" + eventId);
180         i.setData(builder.build());
181         i.setClass(c, EventInfoActivity.class);
182         i.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin);
183         i.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
184         return i;
185     }
186 
187 }
188