1 /*
2  * Copyright (c) 2016, 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.car.stream.telecom;
17 
18 import android.content.Intent;
19 import android.os.Binder;
20 import android.os.IBinder;
21 import android.telecom.Call;
22 import android.telecom.CallAudioState;
23 import android.telecom.InCallService;
24 import android.util.Log;
25 
26 /**
27  * {@link InCallService} to listen for incoming calls and changes in call state.
28  */
29 public class StreamInCallService extends InCallService {
30     public static final String LOCAL_INCALL_SERVICE_BIND_ACTION = "stream_incall_service_action";
31     private static final String TAG = "StreamInCallService";
32     private final IBinder mBinder = new StreamInCallServiceBinder();
33 
34     private InCallServiceCallback mCallback;
35 
36     /**
37      * Callback interface to receive changes in the call state.
38      */
39     public interface InCallServiceCallback {
onCallAdded(Call call)40         void onCallAdded(Call call);
41 
onCallRemoved(Call call)42         void onCallRemoved(Call call);
43 
onCallAudioStateChanged(CallAudioState audioState)44         void onCallAudioStateChanged(CallAudioState audioState);
45     }
46 
47     public class StreamInCallServiceBinder extends Binder {
getService()48         StreamInCallService getService() {
49             return StreamInCallService.this;
50         }
51     }
52 
setCallback(InCallServiceCallback callback)53     public void setCallback(InCallServiceCallback callback) {
54         mCallback = callback;
55     }
56 
57     @Override
onBind(Intent intent)58     public IBinder onBind(Intent intent) {
59         // This service can be bound by the framework or a local stream producer.
60         // Check the action and return the appropriate IBinder.
61         if (LOCAL_INCALL_SERVICE_BIND_ACTION.equals(intent.getAction())) {
62             if (Log.isLoggable(TAG, Log.DEBUG)) {
63                 Log.d(TAG, "onBind with action: LOCAL_INCALL_SERVICE_BIND_ACTION," +
64                         " returning StreamInCallServiceBinder");
65             }
66             return mBinder;
67         }
68         if (Log.isLoggable(TAG, Log.DEBUG)) {
69             Log.d(TAG, "onBind without action specified, returning InCallService");
70         }
71         return super.onBind(intent);
72     }
73 
74     @Override
onCallAdded(Call call)75     public void onCallAdded(Call call) {
76         if (mCallback != null) {
77             mCallback.onCallAdded(call);
78         }
79     }
80 
81     @Override
onCallRemoved(Call call)82     public void onCallRemoved(Call call) {
83         if (mCallback != null) {
84             mCallback.onCallRemoved(call);
85         }
86     }
87 
88     @Override
onCallAudioStateChanged(CallAudioState audioState)89     public void onCallAudioStateChanged(CallAudioState audioState) {
90         if (mCallback != null) {
91             mCallback.onCallAudioStateChanged(audioState);
92         }
93         super.onCallAudioStateChanged(audioState);
94     }
95 
isMuted()96     public boolean isMuted() {
97         CallAudioState audioState = getCallAudioState();
98         return audioState != null && audioState.isMuted();
99     }
100 }
101