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