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.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.IBinder;
23 import android.telecom.Call;
24 import android.text.TextUtils;
25 
26 import com.android.car.dialer.Constants;
27 import com.android.car.dialer.telecom.UiCallManager;
28 import com.android.car.dialer.ui.activecall.InCallActivity;
29 import com.android.car.telephony.common.TelecomUtils;
30 
31 import java.util.List;
32 
33 /**
34  * A {@link Service} that is used to handle actions from notifications to:
35  * <ul><li>answer or inject an incoming call.
36  * <li>call back or message to a missed call.
37  */
38 public class NotificationService extends Service {
39     static final String ACTION_ANSWER_CALL = "CD.ACTION_ANSWER_CALL";
40     static final String ACTION_DECLINE_CALL = "CD.ACTION_DECLINE_CALL";
41     static final String ACTION_SHOW_FULLSCREEN_UI = "CD.ACTION_SHOW_FULLSCREEN_UI";
42     static final String ACTION_CALL_BACK_MISSED = "CD.ACTION_CALL_BACK_MISSED";
43     static final String ACTION_MESSAGE_MISSED = "CD.ACTION_MESSAGE_MISSED";
44     static final String ACTION_READ_ALL_MISSED = "CD.ACTION_READ_ALL_MISSED";
45     static final String EXTRA_CALL_ID = "CD.EXTRA_CALL_ID";
46 
47     @Override
onBind(Intent intent)48     public IBinder onBind(Intent intent) {
49         return null;
50     }
51 
52     /** Create an intent to handle reading all missed call action and schedule for executing. */
readAllMissedCall(Context context)53     public static void readAllMissedCall(Context context) {
54         Intent readAllMissedCallIntent = new Intent(context, NotificationService.class);
55         readAllMissedCallIntent.setAction(ACTION_READ_ALL_MISSED);
56         context.startService(readAllMissedCallIntent);
57     }
58 
59     @Override
onStartCommand(Intent intent, int flags, int startId)60     public int onStartCommand(Intent intent, int flags, int startId) {
61         String action = intent.getAction();
62         String callId = intent.getStringExtra(EXTRA_CALL_ID);
63         switch (action) {
64             case ACTION_ANSWER_CALL:
65                 answerCall(callId);
66                 InCallNotificationController.get().cancelInCallNotification(callId);
67                 break;
68             case ACTION_DECLINE_CALL:
69                 declineCall(callId);
70                 InCallNotificationController.get().cancelInCallNotification(callId);
71                 break;
72             case ACTION_SHOW_FULLSCREEN_UI:
73                 Intent inCallActivityIntent = new Intent(getApplicationContext(),
74                         InCallActivity.class);
75                 inCallActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
76                 inCallActivityIntent.putExtra(Constants.Intents.EXTRA_SHOW_INCOMING_CALL, true);
77                 startActivity(inCallActivityIntent);
78                 InCallNotificationController.get().cancelInCallNotification(callId);
79                 break;
80             case ACTION_CALL_BACK_MISSED:
81                 UiCallManager.get().placeCall(callId);
82                 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
83                 break;
84             case ACTION_MESSAGE_MISSED:
85                 // TODO: call assistant to send message
86                 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
87                 break;
88             case ACTION_READ_ALL_MISSED:
89                 TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
90                 break;
91             default:
92                 break;
93         }
94 
95         return START_NOT_STICKY;
96     }
97 
answerCall(String callId)98     private void answerCall(String callId) {
99         List<Call> callList = UiCallManager.get().getCallList();
100         for (Call call : callList) {
101             if (call.getDetails() != null
102                     && TextUtils.equals(call.getDetails().getTelecomCallId(), callId)) {
103                 call.answer(/* videoState= */0);
104                 return;
105             }
106         }
107     }
108 
declineCall(String callId)109     private void declineCall(String callId) {
110         List<Call> callList = UiCallManager.get().getCallList();
111         for (Call call : callList) {
112             if (call.getDetails() != null
113                     && TextUtils.equals(call.getDetails().getTelecomCallId(), callId)) {
114                 call.reject(false, /* textMessage= */"");
115                 return;
116             }
117         }
118     }
119 }
120