TestCallList.java revision dd68bc36a3278557b1c4d9183ed9e3dee077eb20
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
17package com.android.server.telecom.testapps;
18
19import android.telecom.Call;
20import android.telecom.InCallService;
21import android.telecom.VideoProfile;
22import android.telecom.VideoProfile.CameraCapabilities;
23import android.util.ArrayMap;
24import android.util.ArraySet;
25import android.util.Log;
26
27import java.util.LinkedList;
28import java.util.List;
29import java.util.Map;
30import java.util.Set;
31
32/**
33 * Maintains a list of calls received via the {@link TestInCallServiceImpl}.
34 */
35public class TestCallList extends Call.Callback {
36
37    public static abstract class Listener {
38        public void onCallAdded(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
48        public TestVideoCallListener(Call call) {
49            mCall = call;
50        }
51
52        @Override
53        public void onSessionModifyRequestReceived(VideoProfile videoProfile) {
54            Log.v(TAG,
55                    "onSessionModifyRequestReceived: videoState = " + videoProfile.getVideoState()
56                            + " call = " + mCall);
57        }
58
59        @Override
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
69        public void onCallSessionEvent(int event) {
70
71        }
72
73        @Override
74        public void onPeerDimensionsChanged(int width, int height) {
75
76        }
77
78        @Override
79        public void onVideoQualityChanged(int videoQuality) {
80            Log.v(TAG,
81                    "onVideoQualityChanged: videoQuality = " + videoQuality + " call = " + mCall);
82        }
83
84        @Override
85        public void onCallDataUsageChanged(long dataUsage) {
86
87        }
88
89        @Override
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     */
104    public static TestCallList getInstance() {
105        return INSTANCE;
106    }
107
108    public void addListener(Listener listener) {
109        if (listener != null) {
110            mListeners.add(listener);
111        }
112    }
113
114    public boolean removeListener(Listener listener) {
115        return mListeners.remove(listener);
116    }
117
118    public Call getCall(int position) {
119        return mCalls.get(position);
120    }
121
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.registerCallback(this);
130
131        for (Listener l : mListeners) {
132            l.onCallAdded(call);
133        }
134    }
135
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.unregisterCallback(this);
144
145        for (Listener l : mListeners) {
146            l.onCallRemoved(call);
147        }
148    }
149
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
163    public int size() {
164        return mCalls.size();
165    }
166
167    /**
168     * For any video calls tracked, sends an upgrade to video request.
169     */
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     */
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
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    @Override
219    public void onRttStatusChanged(Call call, boolean enabled, Call.RttCall rttCall) {
220        Log.v(TAG, "onRttStatusChanged: call = " + call + " " + System.identityHashCode(this));
221
222        if (call != null) {
223            // Did you have another call? Well too bad, this class isn't gonna handle it.
224            mCalls.clear();
225            mCalls.add(call);
226        }
227    }
228}
229