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 R* limitations under the License.
15 */
16
17package android.telecom;
18
19import com.android.internal.os.SomeArgs;
20import com.android.internal.telecom.IVideoCallback;
21
22import android.os.Handler;
23import android.os.Message;
24import android.os.RemoteException;
25
26/**
27 * A component that provides an RPC servant implementation of {@link IVideoCallback},
28 * posting incoming messages on the main thread on a client-supplied delegate object.
29 *
30 * TODO: Generate this and similar classes using a compiler starting from AIDL interfaces.
31 *
32 * @hide
33 */
34final class VideoCallbackServant {
35    private static final int MSG_RECEIVE_SESSION_MODIFY_REQUEST = 0;
36    private static final int MSG_RECEIVE_SESSION_MODIFY_RESPONSE = 1;
37    private static final int MSG_HANDLE_CALL_SESSION_EVENT = 2;
38    private static final int MSG_CHANGE_PEER_DIMENSIONS = 3;
39    private static final int MSG_CHANGE_CALL_DATA_USAGE = 4;
40    private static final int MSG_CHANGE_CAMERA_CAPABILITIES = 5;
41    private static final int MSG_CHANGE_VIDEO_QUALITY = 6;
42
43    private final IVideoCallback mDelegate;
44
45    private final Handler mHandler = new Handler() {
46        @Override
47        public void handleMessage(Message msg) {
48            try {
49                internalHandleMessage(msg);
50            } catch (RemoteException e) {
51            }
52        }
53
54        // Internal method defined to centralize handling of RemoteException
55        private void internalHandleMessage(Message msg) throws RemoteException {
56            switch (msg.what) {
57                case MSG_RECEIVE_SESSION_MODIFY_REQUEST: {
58                    mDelegate.receiveSessionModifyRequest((VideoProfile) msg.obj);
59                    break;
60                }
61                case MSG_RECEIVE_SESSION_MODIFY_RESPONSE: {
62                    SomeArgs args = (SomeArgs) msg.obj;
63                    try {
64                        mDelegate.receiveSessionModifyResponse(
65                                args.argi1,
66                                (VideoProfile) args.arg1,
67                                (VideoProfile) args.arg2);
68                    } finally {
69                        args.recycle();
70                    }
71                    break;
72                }
73                case MSG_HANDLE_CALL_SESSION_EVENT: {
74                    SomeArgs args = (SomeArgs) msg.obj;
75                    try {
76                        mDelegate.handleCallSessionEvent(args.argi1);
77                    } finally {
78                        args.recycle();
79                    }
80                    break;
81                }
82                case MSG_CHANGE_PEER_DIMENSIONS: {
83                    SomeArgs args = (SomeArgs) msg.obj;
84                    try {
85                        mDelegate.changePeerDimensions(args.argi1, args.argi2);
86                    } finally {
87                        args.recycle();
88                    }
89                    break;
90                }
91                case MSG_CHANGE_CALL_DATA_USAGE: {
92                    SomeArgs args = (SomeArgs) msg.obj;
93                    try {
94                        mDelegate.changeCallDataUsage((long) args.arg1);
95                    } finally {
96                        args.recycle();
97                    }
98                    break;
99                }
100                case MSG_CHANGE_CAMERA_CAPABILITIES: {
101                    mDelegate.changeCameraCapabilities((VideoProfile.CameraCapabilities) msg.obj);
102                    break;
103                }
104                case MSG_CHANGE_VIDEO_QUALITY: {
105                    mDelegate.changeVideoQuality(msg.arg1);
106                    break;
107                }
108            }
109        }
110    };
111
112    private final IVideoCallback mStub = new IVideoCallback.Stub() {
113        @Override
114        public void receiveSessionModifyRequest(VideoProfile videoProfile) throws RemoteException {
115            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_REQUEST, videoProfile).sendToTarget();
116        }
117
118        @Override
119        public void receiveSessionModifyResponse(int status, VideoProfile requestedProfile,
120                VideoProfile responseProfile) throws RemoteException {
121            SomeArgs args = SomeArgs.obtain();
122            args.argi1 = status;
123            args.arg1 = requestedProfile;
124            args.arg2 = responseProfile;
125            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_RESPONSE, args).sendToTarget();
126        }
127
128        @Override
129        public void handleCallSessionEvent(int event) throws RemoteException {
130            SomeArgs args = SomeArgs.obtain();
131            args.argi1 = event;
132            mHandler.obtainMessage(MSG_HANDLE_CALL_SESSION_EVENT, args).sendToTarget();
133        }
134
135        @Override
136        public void changePeerDimensions(int width, int height) throws RemoteException {
137            SomeArgs args = SomeArgs.obtain();
138            args.argi1 = width;
139            args.argi2 = height;
140            mHandler.obtainMessage(MSG_CHANGE_PEER_DIMENSIONS, args).sendToTarget();
141        }
142
143        @Override
144        public void changeCallDataUsage(long dataUsage) throws RemoteException {
145            SomeArgs args = SomeArgs.obtain();
146            args.arg1 = dataUsage;
147            mHandler.obtainMessage(MSG_CHANGE_CALL_DATA_USAGE, args).sendToTarget();
148        }
149
150        @Override
151        public void changeCameraCapabilities(
152                VideoProfile.CameraCapabilities cameraCapabilities)
153                throws RemoteException {
154            mHandler.obtainMessage(MSG_CHANGE_CAMERA_CAPABILITIES, cameraCapabilities)
155                    .sendToTarget();
156        }
157
158        @Override
159        public void changeVideoQuality(int videoQuality) throws RemoteException {
160            mHandler.obtainMessage(MSG_CHANGE_VIDEO_QUALITY, videoQuality, 0).sendToTarget();
161        }
162    };
163
164    public VideoCallbackServant(IVideoCallback delegate) {
165        mDelegate = delegate;
166    }
167
168    public IVideoCallback getStub() {
169        return mStub;
170    }
171}
172