1/*
2 * Copyright (C) 2014 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.incallui;
18
19import android.telecom.CameraCapabilities;
20import android.telecom.Connection;
21import android.telecom.InCallService.VideoCall;
22import android.telecom.VideoProfile;
23
24/**
25 * Implements the InCallUI Video Call Listener.
26 */
27public class InCallVideoCallListener extends VideoCall.Listener {
28
29    /**
30     * The call associated with this {@link InCallVideoClient}.
31     */
32    private Call mCall;
33
34    /**
35     * Creates an instance of the call video client, specifying the call it is related to.
36     *
37     * @param call The call.
38     */
39    public InCallVideoCallListener(Call call) {
40        mCall = call;
41    }
42
43    /**
44     * Handles an incoming session modification request.
45     *
46     * @param videoProfile The requested video call profile.
47     */
48    @Override
49    public void onSessionModifyRequestReceived(VideoProfile videoProfile) {
50        int previousVideoState = mCall.getVideoState();
51        int newVideoState = videoProfile.getVideoState();
52
53        boolean wasVideoCall = VideoProfile.VideoState.isBidirectional(previousVideoState);
54        boolean isVideoCall = VideoProfile.VideoState.isBidirectional(newVideoState);
55
56        boolean wasPaused = VideoProfile.VideoState.isPaused(previousVideoState);
57        boolean isPaused = VideoProfile.VideoState.isPaused(newVideoState);
58
59        // Check for upgrades to video and downgrades to audio.
60        if (!wasVideoCall && isVideoCall) {
61            InCallVideoCallListenerNotifier.getInstance().upgradeToVideoRequest(mCall);
62        } else if (wasVideoCall && !isVideoCall) {
63            InCallVideoCallListenerNotifier.getInstance().downgradeToAudio(mCall);
64        }
65
66        boolean pause = !wasPaused && isPaused;
67        InCallVideoCallListenerNotifier.getInstance().peerPausedStateChanged(mCall, pause);
68    }
69
70    /**
71     * Handles a session modification response.
72     *
73     * @param status Status of the session modify request.  Valid values are
74     *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
75     *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
76     *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
77     * @param requestedProfile
78     * @param responseProfile The actual profile changes made by the peer device.
79     */
80    @Override
81    public void onSessionModifyResponseReceived(
82            int status, VideoProfile requestedProfile, VideoProfile responseProfile) {
83        boolean modifySucceeded =
84                requestedProfile.getVideoState() == responseProfile.getVideoState();
85        boolean isVideoCall =
86                VideoProfile.VideoState.isBidirectional(responseProfile.getVideoState());
87
88        if (modifySucceeded && isVideoCall) {
89            InCallVideoCallListenerNotifier.getInstance().upgradeToVideoSuccess(mCall);
90        } else if (!modifySucceeded || status != Connection.VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS) {
91            InCallVideoCallListenerNotifier.getInstance().upgradeToVideoFail(mCall);
92        }
93    }
94
95    /**
96     * Handles a call session event.
97     *
98     * @param event The event.
99     */
100    @Override
101    public void onCallSessionEvent(int event) {
102    }
103
104    /**
105     * Handles a change to the peer video dimensions.
106     *
107     * @param width  The updated peer video width.
108     * @param height The updated peer video height.
109     */
110    @Override
111    public void onPeerDimensionsChanged(int width, int height) {
112        InCallVideoCallListenerNotifier.getInstance().peerDimensionsChanged(mCall, width, height);
113    }
114
115    /**
116     * Handles a change to the call data usage.  No implementation as the in-call UI does not
117     * display data usage.
118     *
119     * @param dataUsage The updated data usage.
120     */
121    @Override
122    public void onCallDataUsageChanged(int dataUsage) {
123    }
124
125    /**
126     * Handles changes to the camera capabilities.  No implementation as the in-call UI does not
127     * make use of camera capabilities.
128     *
129     * @param cameraCapabilities The changed camera capabilities.
130     */
131    @Override
132    public void onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities) {
133        InCallVideoCallListenerNotifier.getInstance().cameraDimensionsChanged(
134                mCall, cameraCapabilities.getWidth(), cameraCapabilities.getHeight());
135    }
136}
137