1 /*
2  * Copyright (C) 2015 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.server.telecom.testapps;
18 
19 import android.telecom.Call;
20 import android.telecom.InCallService;
21 import android.telecom.VideoProfile;
22 import android.telecom.VideoProfile.CameraCapabilities;
23 import android.util.ArrayMap;
24 import android.util.ArraySet;
25 import android.util.Log;
26 
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 
32 /**
33  * Maintains a list of calls received via the {@link TestInCallServiceImpl}.
34  */
35 public class TestCallList extends Call.Listener {
36 
37     public static abstract class Listener {
onCallAdded(Call call)38         public void onCallAdded(Call call) {}
onCallRemoved(Call call)39         public void onCallRemoved(Call call) {}
40     }
41 
42     private static final TestCallList INSTANCE = new TestCallList();
43     private static final String TAG = "TestCallList";
44 
45     private class TestVideoCallListener extends InCallService.VideoCall.Callback {
46         private Call mCall;
47 
TestVideoCallListener(Call call)48         public TestVideoCallListener(Call call) {
49             mCall = call;
50         }
51 
52         @Override
onSessionModifyRequestReceived(VideoProfile videoProfile)53         public void onSessionModifyRequestReceived(VideoProfile videoProfile) {
54             Log.v(TAG,
55                     "onSessionModifyRequestReceived: videoState = " + videoProfile.getVideoState()
56                             + " call = " + mCall);
57         }
58 
59         @Override
onSessionModifyResponseReceived(int status, VideoProfile requestedProfile, VideoProfile responseProfile)60         public void onSessionModifyResponseReceived(int status, VideoProfile requestedProfile,
61                 VideoProfile responseProfile) {
62             Log.v(TAG,
63                     "onSessionModifyResponseReceived: status = " + status + " videoState = "
64                             + responseProfile.getVideoState()
65                             + " call = " + mCall);
66         }
67 
68         @Override
onCallSessionEvent(int event)69         public void onCallSessionEvent(int event) {
70 
71         }
72 
73         @Override
onPeerDimensionsChanged(int width, int height)74         public void onPeerDimensionsChanged(int width, int height) {
75 
76         }
77 
78         @Override
onVideoQualityChanged(int videoQuality)79         public void onVideoQualityChanged(int videoQuality) {
80             Log.v(TAG,
81                     "onVideoQualityChanged: videoQuality = " + videoQuality + " call = " + mCall);
82         }
83 
84         @Override
onCallDataUsageChanged(long dataUsage)85         public void onCallDataUsageChanged(long dataUsage) {
86 
87         }
88 
89         @Override
onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities)90         public void onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities) {
91 
92         }
93     }
94 
95     // The calls the call list knows about.
96     private List<Call> mCalls = new LinkedList<Call>();
97     private Map<Call, TestVideoCallListener> mVideoCallListeners =
98             new ArrayMap<Call, TestVideoCallListener>();
99     private Set<Listener> mListeners = new ArraySet<Listener>();
100 
101     /**
102      * Singleton accessor.
103      */
getInstance()104     public static TestCallList getInstance() {
105         return INSTANCE;
106     }
107 
addListener(Listener listener)108     public void addListener(Listener listener) {
109         if (listener != null) {
110             mListeners.add(listener);
111         }
112     }
113 
removeListener(Listener listener)114     public boolean removeListener(Listener listener) {
115         return mListeners.remove(listener);
116     }
117 
getCall(int position)118     public Call getCall(int position) {
119         return mCalls.get(position);
120     }
121 
addCall(Call call)122     public void addCall(Call call) {
123         if (mCalls.contains(call)) {
124             Log.e(TAG, "addCall: Call already added.");
125             return;
126         }
127         Log.i(TAG, "addCall: " + call + " " + System.identityHashCode(this));
128         mCalls.add(call);
129         call.addListener(this);
130 
131         for (Listener l : mListeners) {
132             l.onCallAdded(call);
133         }
134     }
135 
removeCall(Call call)136     public void removeCall(Call call) {
137         if (!mCalls.contains(call)) {
138             Log.e(TAG, "removeCall: Call cannot be removed -- doesn't exist.");
139             return;
140         }
141         Log.i(TAG, "removeCall: " + call);
142         mCalls.remove(call);
143         call.removeListener(this);
144 
145         for (Listener l : mListeners) {
146             l.onCallRemoved(call);
147         }
148     }
149 
clearCalls()150     public void clearCalls() {
151         for (Call call : new LinkedList<Call>(mCalls)) {
152             removeCall(call);
153         }
154 
155         for (Call call : mVideoCallListeners.keySet()) {
156             if (call.getVideoCall() != null) {
157                 call.getVideoCall().destroy();
158             }
159         }
160         mVideoCallListeners.clear();
161     }
162 
size()163     public int size() {
164         return mCalls.size();
165     }
166 
167     /**
168      * For any video calls tracked, sends an upgrade to video request.
169      */
sendUpgradeToVideoRequest(int videoState)170     public void sendUpgradeToVideoRequest(int videoState) {
171         Log.v(TAG, "sendUpgradeToVideoRequest : videoState = " + videoState);
172 
173         for (Call call : mCalls) {
174             InCallService.VideoCall videoCall = call.getVideoCall();
175             Log.v(TAG, "sendUpgradeToVideoRequest: checkCall "+call);
176             if (videoCall == null) {
177                 continue;
178             }
179 
180             Log.v(TAG, "send upgrade to video request for call: " + call);
181             videoCall.sendSessionModifyRequest(new VideoProfile(videoState));
182         }
183     }
184 
185     /**
186      * For any video calls which are active, sends an upgrade to video response with the specified
187      * video state.
188      *
189      * @param videoState The video state to respond with.
190      */
sendUpgradeToVideoResponse(int videoState)191     public void sendUpgradeToVideoResponse(int videoState) {
192         Log.v(TAG, "sendUpgradeToVideoResponse : videoState = " + videoState);
193 
194         for (Call call : mCalls) {
195             InCallService.VideoCall videoCall = call.getVideoCall();
196             if (videoCall == null) {
197                 continue;
198             }
199 
200             Log.v(TAG, "send upgrade to video response for call: " + call);
201             videoCall.sendSessionModifyResponse(new VideoProfile(videoState));
202         }
203     }
204 
205     @Override
onVideoCallChanged(Call call, InCallService.VideoCall videoCall)206     public void onVideoCallChanged(Call call, InCallService.VideoCall videoCall) {
207         Log.v(TAG, "onVideoCallChanged: call = " + call + " " + System.identityHashCode(this));
208         if (videoCall != null) {
209             if (!mVideoCallListeners.containsKey(call)) {
210                 TestVideoCallListener listener = new TestVideoCallListener(call);
211                 videoCall.registerCallback(listener);
212                 mVideoCallListeners.put(call, listener);
213                 Log.v(TAG, "onVideoCallChanged: added new listener");
214             }
215         }
216     }
217 }
218