1 /*
2  * Copyright (C) 2022 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.common;
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.util.Log;
26 
27 /**
28  * This receiver is to be used to communicate with tests in {@link android.alarmmanager.cts}.
29  */
30 public class RequestReceiver extends BroadcastReceiver {
31     private static final String TAG = RequestReceiver.class.getSimpleName();
32     public static final String PACKAGE_NAME = "android.alarmmanager.alarmtestapp.cts.common";
33 
34     public static final String ACTION_GET_CAN_SCHEDULE_EXACT_ALARM =
35             PACKAGE_NAME + ".action.GET_CAN_SCHEDULE_EXACT_ALARM";
36     public static final String ACTION_SET_EXACT_PI =
37             PACKAGE_NAME + ".action.SET_EXACT_PI";
38     public static final String ACTION_SET_EXACT_CALLBACK =
39             PACKAGE_NAME + ".action.SET_EXACT_CALLBACK";
40     public static final String ACTION_SET_EXACT_AND_AWI =
41             PACKAGE_NAME + ".action.SET_EXACT_AND_AWI";
42     public static final String ACTION_SET_ALARM_CLOCK =
43             PACKAGE_NAME + ".action.SET_ALARM_CLOCK";
44 
45     public static final int RESULT_SECURITY_EXCEPTION = Activity.RESULT_FIRST_USER + 12;
46 
getAlarmSender(Context context)47     private static PendingIntent getAlarmSender(Context context) {
48         final Intent alarmAction = new Intent(context, RequestReceiver.class)
49                 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
50         return PendingIntent.getBroadcast(context, 0, alarmAction,
51                 PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
52     }
53 
54     @Override
onReceive(Context context, Intent intent)55     public void onReceive(Context context, Intent intent) {
56         final AlarmManager am = context.getSystemService(AlarmManager.class);
57         switch (intent.getAction()) {
58             case ACTION_GET_CAN_SCHEDULE_EXACT_ALARM:
59                 final boolean result = am.canScheduleExactAlarms();
60                 setResult(Activity.RESULT_OK, String.valueOf(result), null);
61                 break;
62             case ACTION_SET_EXACT_AND_AWI:
63                 try {
64                     am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1234,
65                             getAlarmSender(context));
66                     setResult(Activity.RESULT_OK, null, null);
67                 } catch (SecurityException se) {
68                     setResult(RESULT_SECURITY_EXCEPTION, se.getMessage(), null);
69                 }
70                 break;
71             case ACTION_SET_ALARM_CLOCK:
72                 final AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(1234,
73                         getAlarmSender(context));
74                 try {
75                     am.setAlarmClock(info, getAlarmSender(context));
76                     setResult(Activity.RESULT_OK, null, null);
77                 } catch (SecurityException se) {
78                     setResult(RESULT_SECURITY_EXCEPTION, se.getMessage(), null);
79                 }
80                 break;
81             case ACTION_SET_EXACT_PI:
82                 try {
83                     am.setExact(AlarmManager.ELAPSED_REALTIME, 1234, getAlarmSender(context));
84                     setResult(Activity.RESULT_OK, null, null);
85                 } catch (SecurityException se) {
86                     setResult(RESULT_SECURITY_EXCEPTION, se.getMessage(), null);
87                 }
88                 break;
89             case ACTION_SET_EXACT_CALLBACK:
90                 try {
91                     am.setExact(AlarmManager.ELAPSED_REALTIME, 1234, TAG,
92                             () -> Log.i(TAG, "Listener alarm fired!"), null);
93                     setResult(Activity.RESULT_OK, null, null);
94                 } catch (SecurityException se) {
95                     setResult(RESULT_SECURITY_EXCEPTION, se.getMessage(), null);
96                 }
97                 break;
98             default:
99                 Log.e(TAG, "Unspecified action " + intent.getAction());
100                 setResult(Activity.RESULT_CANCELED, null, null);
101                 break;
102         }
103     }
104 }
105