1 /* 2 * Copyright (C) 2019 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.car.dialer.notification; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.telecom.Call; 22 import android.text.TextUtils; 23 24 import androidx.core.app.JobIntentService; 25 26 import com.android.car.dialer.Constants; 27 import com.android.car.dialer.telecom.UiCallManager; 28 import com.android.car.telephony.common.TelecomUtils; 29 30 import java.util.List; 31 32 /** 33 * A {@link JobIntentService} that is used to handle actions from notifications to: 34 * <ul><li>answer or inject an incoming call. 35 * <li>call back or message to a missed call. 36 */ 37 public class NotificationService extends JobIntentService { 38 static final String ACTION_ANSWER_CALL = "CD.ACTION_ANSWER_CALL"; 39 static final String ACTION_DECLINE_CALL = "CD.ACTION_DECLINE_CALL"; 40 static final String ACTION_CALL_BACK_MISSED = "CD.ACTION_CALL_BACK_MISSED"; 41 static final String ACTION_MESSAGE_MISSED = "CD.ACTION_MESSAGE_MISSED"; 42 static final String ACTION_READ_ALL_MISSED = "CD.ACTION_READ_ALL_MISSED"; 43 static final String EXTRA_CALL_ID = "CD.EXTRA_CALL_ID"; 44 45 /** Create an intent to handle reading all missed call action and schedule for executing. */ readAllMissedCall(Context context)46 public static void readAllMissedCall(Context context) { 47 Intent readAllMissedCallIntent = new Intent(context, NotificationReceiver.class); 48 readAllMissedCallIntent.setAction(ACTION_READ_ALL_MISSED); 49 enqueueWork(context, readAllMissedCallIntent); 50 } 51 52 /** Enqueue the intent. */ enqueueWork(Context context, Intent intent)53 static void enqueueWork(Context context, Intent intent) { 54 enqueueWork( 55 context, NotificationService.class, Constants.JobIds.NOTIFICATION_SERVICE, intent); 56 } 57 58 @Override onHandleWork(Intent intent)59 protected void onHandleWork(Intent intent) { 60 String action = intent.getAction(); 61 String callId = intent.getStringExtra(EXTRA_CALL_ID); 62 switch (action) { 63 case ACTION_ANSWER_CALL: 64 answerCall(callId); 65 break; 66 case ACTION_DECLINE_CALL: 67 declineCall(callId); 68 break; 69 case ACTION_CALL_BACK_MISSED: 70 UiCallManager.get().placeCall(callId); 71 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId); 72 break; 73 case ACTION_MESSAGE_MISSED: 74 // TODO: call assistant to send message 75 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId); 76 break; 77 case ACTION_READ_ALL_MISSED: 78 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId); 79 break; 80 default: 81 break; 82 } 83 } 84 answerCall(String callId)85 private void answerCall(String callId) { 86 List<Call> callList = UiCallManager.get().getCallList(); 87 for (Call call : callList) { 88 if (call.getDetails() != null 89 && TextUtils.equals(call.getDetails().getTelecomCallId(), callId)) { 90 call.answer(/* videoState= */0); 91 return; 92 } 93 } 94 } 95 declineCall(String callId)96 private void declineCall(String callId) { 97 List<Call> callList = UiCallManager.get().getCallList(); 98 for (Call call : callList) { 99 if (call.getDetails() != null 100 && TextUtils.equals(call.getDetails().getTelecomCallId(), callId)) { 101 call.reject(false, /* textMessage= */""); 102 return; 103 } 104 } 105 } 106 } 107