1 /*
2  * Copyright (C) 2024 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 package com.android.systemui.car.telecom;
17 
18 import android.telecom.Call;
19 import android.telecom.InCallService;
20 import android.util.Log;
21 
22 import com.android.car.telephony.calling.InCallServiceManager;
23 
24 import java.util.ArrayList;
25 
26 import javax.inject.Inject;
27 
28 /**
29  * Implementation of {@link InCallService}, an {@link android.telecom} service which must be
30  * implemented by an app that wishes to provide functionality for managing phone calls. This service
31  * is bound by android telecom.
32  */
33 public class InCallServiceImpl extends InCallService {
34     private static final String TAG = "SysUI.InCallServiceImpl";
35     private static final boolean DEBUG = false;
36 
37     private final InCallServiceManager mServiceManager;
38     private final ArrayList<InCallListener> mInCallListeners = new ArrayList<>();
39 
40     @Inject
InCallServiceImpl(InCallServiceManager serviceManager)41     public InCallServiceImpl(InCallServiceManager serviceManager) {
42         mServiceManager = serviceManager;
43     }
44 
45     @Override
onCreate()46     public void onCreate() {
47         super.onCreate();
48         if (DEBUG) Log.d(TAG, "onCreate service");
49         mServiceManager.setInCallService(this);
50     }
51 
52     @Override
onDestroy()53     public void onDestroy() {
54         if (DEBUG) Log.d(TAG, "onDestroy service");
55         mServiceManager.setInCallService(null);
56         super.onDestroy();
57     }
58 
59     @Override
onCallAdded(Call call)60     public void onCallAdded(Call call) {
61         if (DEBUG) Log.d(TAG, "onCallAdded: " + call);
62         for (InCallListener listener : mInCallListeners) {
63             listener.onCallAdded(call);
64         }
65     }
66 
67     @Override
onCallRemoved(Call call)68     public void onCallRemoved(Call call) {
69         if (DEBUG) Log.d(TAG, "onCallRemoved: " + call);
70         for (InCallListener listener : mInCallListeners) {
71             listener.onCallRemoved(call);
72         }
73     }
74 
75     /**
76      * Adds a listener for {@link InCallService} events
77      */
addListener(InCallListener listener)78     public void addListener(InCallListener listener) {
79         mInCallListeners.add(listener);
80     }
81 
82     /**
83      * Removes a listener for {@link InCallService} events
84      */
removeListener(InCallListener listener)85     public void removeListener(InCallListener listener) {
86         if (!mInCallListeners.isEmpty()) mInCallListeners.remove(listener);
87     }
88 
89     /**
90      * Listens for {@link #onCallAdded(Call)} and {@link #onCallRemoved(Call)} events
91      */
92     public interface InCallListener {
93         /**
94          * Called when a {@link Call} has been added to this in-call session, generally indicating
95          * that the call has been received.
96          */
onCallAdded(Call call)97         void onCallAdded(Call call);
98 
99         /**
100          * Called when a {@link Call} has been removed from this in-call session, generally
101          * indicating that the call has ended.
102          */
onCallRemoved(Call call)103         void onCallRemoved(Call call);
104     }
105 }
106