1 /*
2  * Copyright (C) 2017 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 android.alarmmanager.alarmtestapp.cts;
18 
19 import android.app.Activity;
20 import android.app.AlarmManager;
21 import android.app.PendingIntent;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.SystemClock;
26 import android.util.Log;
27 
28 /**
29  * This receiver is to be used to schedule alarms as part of tests in
30  * {@link android.alarmmanager.cts}
31  */
32 public class TestAlarmScheduler extends BroadcastReceiver {
33     private static final String TAG = TestAlarmScheduler.class.getSimpleName();
34     private static final String PACKAGE_NAME = "android.alarmmanager.alarmtestapp.cts";
35 
36     public static final String ACTION_SET_ALARM = PACKAGE_NAME + ".action.SET_ALARM";
37     public static final String EXTRA_TRIGGER_TIME = PACKAGE_NAME + ".extra.TRIGGER_TIME";
38     public static final String EXTRA_REPEAT_INTERVAL = PACKAGE_NAME + ".extra.REPEAT_INTERVAL";
39     public static final String EXTRA_WINDOW_LENGTH = PACKAGE_NAME + ".extra.WINDOW_LENGTH";
40     public static final String EXTRA_TYPE = PACKAGE_NAME + ".extra.TYPE";
41     public static final String ACTION_SET_ALARM_CLOCK = PACKAGE_NAME + ".action.SET_ALARM_CLOCK";
42     public static final String EXTRA_ALARM_CLOCK_INFO = PACKAGE_NAME + ".extra.ALARM_CLOCK_INFO";
43     public static final String ACTION_CANCEL_ALL_ALARMS = PACKAGE_NAME + ".action.CANCEL_ALARMS";
44 
45     @Override
onReceive(Context context, Intent intent)46     public void onReceive(Context context, Intent intent) {
47         final AlarmManager am = context.getSystemService(AlarmManager.class);
48         final Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
49         receiverIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
50         final long id = SystemClock.elapsedRealtime();
51         receiverIntent.putExtra(TestAlarmReceiver.EXTRA_ID, id);
52         final PendingIntent alarmClockSender = PendingIntent.getBroadcast(context, 0,
53                 receiverIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
54         final PendingIntent alarmSender = PendingIntent.getBroadcast(context, 1, receiverIntent,
55                 PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
56         switch (intent.getAction()) {
57             case ACTION_SET_ALARM_CLOCK:
58                 if (!intent.hasExtra(EXTRA_ALARM_CLOCK_INFO)) {
59                     Log.e(TAG, "No alarm clock supplied");
60                     break;
61                 }
62                 final AlarmManager.AlarmClockInfo alarmClockInfo =
63                         intent.getParcelableExtra(EXTRA_ALARM_CLOCK_INFO);
64                 Log.d(TAG, "Setting alarm clock " + alarmClockInfo + " id: " + id);
65                 am.setAlarmClock(alarmClockInfo, alarmClockSender);
66                 setResult(Activity.RESULT_OK, null, null);
67                 break;
68             case ACTION_SET_ALARM:
69                 if (!intent.hasExtra(EXTRA_TYPE) || !intent.hasExtra(EXTRA_TRIGGER_TIME)) {
70                     Log.e(TAG, "Alarm type or trigger time not supplied");
71                     break;
72                 }
73                 final int type = intent.getIntExtra(EXTRA_TYPE, 0);
74                 final long triggerTime = intent.getLongExtra(EXTRA_TRIGGER_TIME, 0);
75                 final long interval = intent.getLongExtra(EXTRA_REPEAT_INTERVAL, 0);
76                 final long window = intent.getLongExtra(EXTRA_WINDOW_LENGTH, 1);
77 
78                 Log.d(TAG, "Setting alarm: id=" + id + " type=" + type + ", triggerTime="
79                         + triggerTime + ", interval=" + interval + " window=" + window);
80                 if (interval > 0) {
81                     am.setRepeating(type, triggerTime, interval, alarmSender);
82                 } else {
83                     am.setWindow(type, triggerTime, window, alarmSender);
84                 }
85                 setResult(Activity.RESULT_OK, null, null);
86                 break;
87             case ACTION_CANCEL_ALL_ALARMS:
88                 Log.d(TAG, "Cancelling all alarms");
89                 am.cancel(alarmClockSender);
90                 am.cancel(alarmSender);
91                 setResult(Activity.RESULT_OK, null, null);
92                 break;
93             default:
94                 Log.e(TAG, "Unspecified action " + intent.getAction());
95                 setResult(Activity.RESULT_CANCELED, null, null);
96                 break;
97         }
98     }
99 }