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 androidx.annotation.VisibleForTesting; 23 24 import com.android.car.telephony.calling.InCallServiceManager; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 import javax.inject.Inject; 30 31 /** 32 * Implementation of {@link InCallService}, an {@link android.telecom} service which must be 33 * implemented by an app that wishes to provide functionality for managing phone calls. This service 34 * is bound by android telecom. 35 */ 36 public class InCallServiceImpl extends InCallService { 37 private static final String TAG = "SysUI.InCallServiceImpl"; 38 private static final boolean DEBUG = false; 39 40 private final InCallServiceManager mServiceManager; 41 private final ArrayList<InCallListener> mInCallListeners = new ArrayList<>(); 42 43 @VisibleForTesting 44 final Call.Callback mCallStateChangedCallback = new Call.Callback() { 45 @Override 46 public void onStateChanged(Call call, int state) { 47 Log.d(TAG, "onStateChanged: " + call); 48 for (InCallListener listener : mInCallListeners) { 49 listener.onStateChanged(call, state); 50 } 51 } 52 53 @Override 54 public void onParentChanged(Call call, Call parent) { 55 Log.d(TAG, "onParentChanged: " + call); 56 for (InCallListener listener : mInCallListeners) { 57 listener.onParentChanged(call, parent); 58 } 59 } 60 61 @Override 62 public void onChildrenChanged(Call call, List<Call> children) { 63 Log.d(TAG, "onChildrenChanged: " + call); 64 for (InCallListener listener : mInCallListeners) { 65 listener.onChildrenChanged(call, children); 66 } 67 } 68 }; 69 70 @Inject InCallServiceImpl(InCallServiceManager serviceManager)71 public InCallServiceImpl(InCallServiceManager serviceManager) { 72 mServiceManager = serviceManager; 73 } 74 75 @Override onCreate()76 public void onCreate() { 77 super.onCreate(); 78 if (DEBUG) Log.d(TAG, "onCreate service"); 79 mServiceManager.setInCallService(this); 80 } 81 82 @Override onDestroy()83 public void onDestroy() { 84 if (DEBUG) Log.d(TAG, "onDestroy service"); 85 mServiceManager.setInCallService(null); 86 super.onDestroy(); 87 } 88 89 @Override onCallAdded(Call call)90 public void onCallAdded(Call call) { 91 Log.d(TAG, "onCallAdded: " + call); 92 call.registerCallback(mCallStateChangedCallback); 93 for (InCallListener listener : mInCallListeners) { 94 listener.onCallAdded(call); 95 } 96 } 97 98 @Override onCallRemoved(Call call)99 public void onCallRemoved(Call call) { 100 Log.d(TAG, "onCallRemoved: " + call); 101 call.unregisterCallback(mCallStateChangedCallback); 102 for (InCallListener listener : mInCallListeners) { 103 listener.onCallRemoved(call); 104 } 105 } 106 107 /** 108 * Adds a listener for {@link InCallService} events 109 */ addListener(InCallListener listener)110 public void addListener(InCallListener listener) { 111 mInCallListeners.add(listener); 112 } 113 114 /** 115 * Removes a listener for {@link InCallService} events 116 */ removeListener(InCallListener listener)117 public void removeListener(InCallListener listener) { 118 if (!mInCallListeners.isEmpty()) mInCallListeners.remove(listener); 119 } 120 121 /** 122 * Listens for {@link #onCallAdded(Call)} and {@link #onCallRemoved(Call)} events 123 */ 124 public interface InCallListener { 125 /** 126 * Called when a {@link Call} has been added to this in-call session, generally indicating 127 * that the call has been received. 128 */ onCallAdded(Call call)129 void onCallAdded(Call call); 130 131 /** 132 * Called when a {@link Call} has been removed from this in-call session, generally 133 * indicating that the call has ended. 134 */ onCallRemoved(Call call)135 void onCallRemoved(Call call); 136 137 /** 138 * Called when the state of a {@link Call} has changed. 139 */ onStateChanged(Call call, int state)140 void onStateChanged(Call call, int state); 141 142 /** 143 * Called when a {@link Call} has been added to a conference. 144 */ onParentChanged(Call call, Call parent)145 void onParentChanged(Call call, Call parent); 146 147 /** 148 * Called when a conference {@link Call} has children calls added or removed. 149 */ onChildrenChanged(Call call, List<Call> children)150 void onChildrenChanged(Call call, List<Call> children); 151 } 152 } 153