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.content.SharedPreferences;
26 import android.net.Uri;
27 import android.provider.CalendarContract;
28 import android.provider.CalendarContract.CalendarAlerts;
29 import android.text.TextUtils;
30 import android.text.format.DateFormat;
31 import android.text.format.DateUtils;
32 import android.text.format.Time;
33 import android.util.Log;
34 
35 import com.android.calendar.EventInfoActivity;
36 import com.android.calendar.R;
37 import com.android.calendar.Utils;
38 
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.TimeZone;
42 
43 public class AlertUtils {
44     private static final String TAG = "AlertUtils";
45     static final boolean DEBUG = true;
46 
47     public static final long SNOOZE_DELAY = 5 * 60 * 1000L;
48 
49     // We use one notification id for the expired events notification.  All
50     // other notifications (the 'active' future/concurrent ones) use a unique ID.
51     public static final int EXPIRED_GROUP_NOTIFICATION_ID = 0;
52 
53     public static final String EVENT_ID_KEY = "eventid";
54     public static final String EVENT_START_KEY = "eventstart";
55     public static final String EVENT_END_KEY = "eventend";
56     public static final String NOTIFICATION_ID_KEY = "notificationid";
57     public static final String EVENT_IDS_KEY = "eventids";
58     public static final String EVENT_STARTS_KEY = "starts";
59 
60     // A flag for using local storage to save alert state instead of the alerts DB table.
61     // This allows the unbundled app to run alongside other calendar apps without eating
62     // alerts from other apps.
63     static boolean BYPASS_DB = true;
64 
65     /**
66      * Creates an AlarmManagerInterface that wraps a real AlarmManager.  The alarm code
67      * was abstracted to an interface to make it testable.
68      */
createAlarmManager(Context context)69     public static AlarmManagerInterface createAlarmManager(Context context) {
70         final AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
71         return new AlarmManagerInterface() {
72             @Override
73             public void set(int type, long triggerAtMillis, PendingIntent operation) {
74                 if (Utils.isKeyLimePieOrLater()) {
75                     mgr.setExact(type, triggerAtMillis, operation);
76                 } else {
77                     mgr.set(type, triggerAtMillis, operation);
78                 }
79             }
80         };
81     }
82 
83     /**
84      * Schedules an alarm intent with the system AlarmManager that will notify
85      * listeners when a reminder should be fired. The provider will keep
86      * scheduled reminders up to date but apps may use this to implement snooze
87      * functionality without modifying the reminders table. Scheduled alarms
88      * will generate an intent using AlertReceiver.EVENT_REMINDER_APP_ACTION.
89      *
90      * @param context A context for referencing system resources
91      * @param manager The AlarmManager to use or null
92      * @param alarmTime The time to fire the intent in UTC millis since epoch
93      */
94     public static void scheduleAlarm(Context context, AlarmManagerInterface manager,
95             long alarmTime) {
96     }
97 
98     public static Intent buildEventViewIntent(Context c, long eventId, long begin, long end) {
99         Intent i = new Intent(Intent.ACTION_VIEW);
100         Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
101         builder.appendEncodedPath("events/" + eventId);
102         i.setData(builder.build());
103         i.setClass(c, EventInfoActivity.class);
104         i.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin);
105         i.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
106         return i;
107     }
108 }
109