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 EXTRA_TEST_FGS = PACKAGE_NAME + ".extra.TEST_FGS";
44     public static final String ACTION_CANCEL_ALL_ALARMS = PACKAGE_NAME + ".action.CANCEL_ALARMS";
45 
46     @Override
onReceive(Context context, Intent intent)47     public void onReceive(Context context, Intent intent) {
48         final AlarmManager am = context.getSystemService(AlarmManager.class);
49         final Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
50         receiverIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
51         final long id = SystemClock.elapsedRealtime();
52         receiverIntent.putExtra(TestAlarmReceiver.EXTRA_ID, id);
53         receiverIntent.putExtra(EXTRA_TEST_FGS, intent.getBooleanExtra(EXTRA_TEST_FGS, false));
54         final PendingIntent alarmClockSender = PendingIntent.getBroadcast(context, 0,
55                 receiverIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
56         final PendingIntent alarmSender = PendingIntent.getBroadcast(context, 1, receiverIntent,
57                 PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
58         switch (intent.getAction()) {
59             case ACTION_SET_ALARM_CLOCK:
60                 if (!intent.hasExtra(EXTRA_ALARM_CLOCK_INFO)) {
61                     Log.e(TAG, "No alarm clock supplied");
62                     break;
63                 }
64                 final AlarmManager.AlarmClockInfo alarmClockInfo =
65                         intent.getParcelableExtra(EXTRA_ALARM_CLOCK_INFO);
66                 Log.d(TAG, "Setting alarm clock " + alarmClockInfo + " id: " + id);
67                 am.setAlarmClock(alarmClockInfo, alarmClockSender);
68                 setResult(Activity.RESULT_OK, null, null);
69                 break;
70             case ACTION_SET_ALARM:
71                 if (!intent.hasExtra(EXTRA_TYPE) || !intent.hasExtra(EXTRA_TRIGGER_TIME)) {
72                     Log.e(TAG, "Alarm type or trigger time not supplied");
73                     break;
74                 }
75                 final int type = intent.getIntExtra(EXTRA_TYPE, 0);
76                 final long triggerTime = intent.getLongExtra(EXTRA_TRIGGER_TIME, 0);
77                 final long interval = intent.getLongExtra(EXTRA_REPEAT_INTERVAL, 0);
78                 final long window = intent.getLongExtra(EXTRA_WINDOW_LENGTH, 1);
79 
80                 Log.d(TAG, "Setting alarm: id=" + id + " type=" + type + ", triggerTime="
81                         + triggerTime + ", interval=" + interval + " window=" + window);
82                 if (interval > 0) {
83                     am.setRepeating(type, triggerTime, interval, alarmSender);
84                 } else {
85                     am.setWindow(type, triggerTime, window, alarmSender);
86                 }
87                 setResult(Activity.RESULT_OK, null, null);
88                 break;
89             case ACTION_CANCEL_ALL_ALARMS:
90                 Log.d(TAG, "Cancelling all alarms");
91                 am.cancel(alarmClockSender);
92                 am.cancel(alarmSender);
93                 setResult(Activity.RESULT_OK, null, null);
94                 break;
95             default:
96                 Log.e(TAG, "Unspecified action " + intent.getAction());
97                 setResult(Activity.RESULT_CANCELED, null, null);
98                 break;
99         }
100     }
101 }