1 /* 2 * Copyright (C) 2020 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 com.android.emergency.action.broadcast; 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 import com.android.emergency.action.EmergencyActionUtils; 27 import com.android.emergency.action.service.EmergencyActionForegroundService; 28 29 /** 30 * Broadcast receiver to handle actions for emergency gesture foreground service and notification 31 */ 32 public class EmergencyActionBroadcastReceiver extends BroadcastReceiver { 33 private static final String TAG = "EmergencyActionRcvr"; 34 35 36 private static final String ACTION_START_EMERGENCY_CALL = 37 "com.android.emergency.broadcast.MAKE_EMERGENCY_CALL"; 38 private static final String ACTION_CANCEL_COUNTDOWN = 39 "com.android.emergency.broadcast.CANCEL_EMERGENCY_COUNTDOWN"; 40 newCallEmergencyIntent(Context context)41 public static Intent newCallEmergencyIntent(Context context) { 42 return new Intent(ACTION_START_EMERGENCY_CALL).setClass(context, 43 EmergencyActionBroadcastReceiver.class); 44 } 45 newCallEmergencyPendingIntent(Context context)46 public static PendingIntent newCallEmergencyPendingIntent(Context context) { 47 return PendingIntent.getBroadcast(context, 0, newCallEmergencyIntent(context), 48 PendingIntent.FLAG_IMMUTABLE); 49 } 50 newCancelCountdownPendingIntent(Context context)51 public static PendingIntent newCancelCountdownPendingIntent(Context context) { 52 return PendingIntent.getBroadcast(context, 0, 53 new Intent(ACTION_CANCEL_COUNTDOWN).setClass(context, 54 EmergencyActionBroadcastReceiver.class), 55 PendingIntent.FLAG_IMMUTABLE); 56 } 57 58 @Override onReceive(Context context, Intent intent)59 public void onReceive(Context context, Intent intent) { 60 AlarmManager alarmManager = context.getSystemService(AlarmManager.class); 61 String action = intent.getAction(); 62 switch (action) { 63 case ACTION_START_EMERGENCY_CALL: 64 Log.i(TAG, "Starting to call emergency number"); 65 EmergencyActionUtils.placeEmergencyCall(context); 66 // Intentionally fall through to cancel service 67 case ACTION_CANCEL_COUNTDOWN: 68 Log.i(TAG, "Cancelling scheduled emergency calls and foreground service"); 69 alarmManager.cancel(newCallEmergencyPendingIntent(context)); 70 EmergencyActionForegroundService.stopService(context); 71 break; 72 default: 73 Log.w(TAG, "Unknown action received, skipping: " + action); 74 } 75 } 76 } 77