AlertUtils.java revision 0ef732f240516f9f8b8add982f82244e01ec509b
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.calendar.alerts; 18 19import android.app.AlarmManager; 20import android.app.PendingIntent; 21import android.content.ContentValues; 22import android.content.Context; 23import android.content.Intent; 24import android.net.Uri; 25import android.provider.CalendarContract; 26import android.provider.CalendarContract.CalendarAlerts; 27 28import com.android.calendar.EventInfoActivity; 29 30public class AlertUtils { 31 32 public static final long SNOOZE_DELAY = 5 * 60 * 1000L; 33 34 // We use one notification id for all events so that we don't clutter 35 // the notification screen. It doesn't matter what the id is, as long 36 // as it is used consistently everywhere. 37 public static final int NOTIFICATION_ID = 0; 38 39 public static final String EVENT_ID_KEY = "eventid"; 40 public static final String SHOW_EVENT_KEY = "showevent"; 41 public static final String EVENT_START_KEY = "eventstart"; 42 public static final String EVENT_END_KEY = "eventend"; 43 44 /** 45 * Schedules an alarm intent with the system AlarmManager that will notify 46 * listeners when a reminder should be fired. The provider will keep 47 * scheduled reminders up to date but apps may use this to implement snooze 48 * functionality without modifying the reminders table. Scheduled alarms 49 * will generate an intent using {@link #ACTION_EVENT_REMINDER}. 50 * 51 * @param context A context for referencing system resources 52 * @param manager The AlarmManager to use or null 53 * @param alarmTime The time to fire the intent in UTC millis since epoch 54 */ 55 public static void scheduleAlarm(Context context, AlarmManager manager, long alarmTime) { 56 57 // The default snooze delay: 5 minutes 58 59 if (manager == null) { 60 manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 61 } 62 63 Intent intent = new Intent(CalendarContract.ACTION_EVENT_REMINDER); 64 intent.setData(CalendarAlerts.CONTENT_URI); 65 intent.putExtra(CalendarContract.CalendarAlerts.ALARM_TIME, alarmTime); 66 PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 67 PendingIntent.FLAG_UPDATE_CURRENT); 68 manager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi); 69 } 70 71 public static ContentValues makeContentValues(long eventId, long begin, long end, 72 long alarmTime, int minutes) { 73 ContentValues values = new ContentValues(); 74 values.put(CalendarAlerts.EVENT_ID, eventId); 75 values.put(CalendarAlerts.BEGIN, begin); 76 values.put(CalendarAlerts.END, end); 77 values.put(CalendarAlerts.ALARM_TIME, alarmTime); 78 long currentTime = System.currentTimeMillis(); 79 values.put(CalendarAlerts.CREATION_TIME, currentTime); 80 values.put(CalendarAlerts.RECEIVED_TIME, 0); 81 values.put(CalendarAlerts.NOTIFY_TIME, 0); 82 values.put(CalendarAlerts.STATE, CalendarAlerts.STATE_SCHEDULED); 83 values.put(CalendarAlerts.MINUTES, minutes); 84 return values; 85 } 86 87 public static Intent buildEventViewIntent(Context c, long eventId, long begin, long end) { 88 Intent i = new Intent(Intent.ACTION_VIEW); 89 Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); 90 builder.appendEncodedPath("events/" + eventId); 91 i.setData(builder.build()); 92 i.setClass(c, EventInfoActivity.class); 93 i.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin); 94 i.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end); 95 return i; 96 } 97 98} 99