CalendarAlarmManager.java revision e7a04f1fe637bc1322a6b4942e0251e3831cd544
1/*
2 * Copyright (C) 2010 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.providers.calendar;
18
19import android.app.AlarmManager;
20import android.app.PendingIntent;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.provider.Calendar;
24import android.util.Log;
25
26/**
27 * We are using the CalendarAlertManager to be able to mock the AlarmManager as the AlarmManager
28 * cannot be extended.
29 *
30 * CalendarAlertManager is delegating its calls to the real AlarmService. When mocking
31 */
32public class CalendarAlarmManager {
33
34    protected static final String TAG = "CalendarAlarmManager";
35
36    private Context mContext;
37    private AlarmManager mAlarmManager;
38
39    public CalendarAlarmManager(Context context) {
40        initializeWithContext(context);
41    }
42
43    protected void initializeWithContext(Context context) {
44        mContext = context;
45        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
46    }
47
48    protected boolean checkAlarmService() {
49        if (mAlarmManager == null) {
50            if (Log.isLoggable(TAG, Log.DEBUG)) {
51                Log.d(TAG, "AlarmService is not set");
52            }
53            return false;
54        }
55        return true;
56    }
57
58    public void set(int type, long triggerAtTime, PendingIntent operation) {
59        if (!checkAlarmService()) {
60            return;
61        }
62        mAlarmManager.set(type, triggerAtTime, operation);
63    }
64
65    public void setRepeating(int type, long triggerAtTime, long interval,
66            PendingIntent operation) {
67        if (!checkAlarmService()) {
68            return;
69        }
70        mAlarmManager.setRepeating(type, triggerAtTime, interval, operation);
71    }
72
73    public void setInexactRepeating(int type, long triggerAtTime, long interval,
74            PendingIntent operation) {
75        if (!checkAlarmService()) {
76            return;
77        }
78        mAlarmManager.setInexactRepeating(type, triggerAtTime, interval, operation);
79    }
80
81    public void cancel(PendingIntent operation) {
82        if (!checkAlarmService()) {
83            return;
84        }
85        mAlarmManager.cancel(operation);
86    }
87
88    public void setTime(long millis) {
89        if (!checkAlarmService()) {
90            return;
91        }
92        mAlarmManager.setTime(millis);
93    }
94
95    public void setTimeZone(String timeZone) {
96        if (!checkAlarmService()) {
97            return;
98        }
99        mAlarmManager.setTimeZone(timeZone);
100    }
101
102    public void scheduleAlarm(long alarmTime) {
103        if (!checkAlarmService()) {
104            return;
105        }
106        Calendar.CalendarAlerts.scheduleAlarm(mContext, mAlarmManager, alarmTime);
107    }
108
109    public void rescheduleMissedAlarms(ContentResolver cr) {
110        if (!checkAlarmService()) {
111            return;
112        }
113        Calendar.CalendarAlerts.rescheduleMissedAlarms(cr, mContext, mAlarmManager);
114    }
115}
116