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
42    private final IVideoCallback mDelegate;
43
44    private final Handler mHandler = new Handler() {
45        @Override
46        public void handleMessage(Message msg) {
47            try {
48                internalHandleMessage(msg);
49            } catch (RemoteException e) {
50            }
51        }
52
53        // Internal method defined to centralize handling of RemoteException
54        private void internalHandleMessage(Message msg) throws RemoteException {
55            switch (msg.what) {
56                case MSG_RECEIVE_SESSION_MODIFY_REQUEST: {
57                    mDelegate.receiveSessionModifyRequest((VideoProfile) msg.obj);
58                    break;
59                }
60                case MSG_RECEIVE_SESSION_MODIFY_RESPONSE: {
61                    SomeArgs args = (SomeArgs) msg.obj;
62                    try {
63                        mDelegate.receiveSessionModifyResponse(
64                                args.argi1,
65                                (VideoProfile) args.arg1,
66                                (VideoProfile) args.arg2);
67                    } finally {
68                        args.recycle();
69                    }
70                    break;
71                }
72                case MSG_HANDLE_CALL_SESSION_EVENT: {
73                    SomeArgs args = (SomeArgs) msg.obj;
74                    try {
75                        mDelegate.handleCallSessionEvent(args.argi1);
76                    } finally {
77                        args.recycle();
78                    }
79                    break;
80                }
81                case MSG_CHANGE_PEER_DIMENSIONS: {
82                    SomeArgs args = (SomeArgs) msg.obj;
83                    try {
84                        mDelegate.changePeerDimensions(args.argi1, args.argi2);
85                    } finally {
86                        args.recycle();
87                    }
88                    break;
89                }
90                case MSG_CHANGE_CALL_DATA_USAGE: {
91                    SomeArgs args = (SomeArgs) msg.obj;
92                    try {
93                        mDelegate.changeCallDataUsage(args.argi1);
94                    } finally {
95                        args.recycle();
96                    }
97                    break;
98                }
99                case MSG_CHANGE_CAMERA_CAPABILITIES: {
100                    mDelegate.changeCameraCapabilities((CameraCapabilities) msg.obj);
101                    break;
102                }
103            }
104        }
105    };
106
107    private final IVideoCallback mStub = new IVideoCallback.Stub() {
108        @Override
109        public void receiveSessionModifyRequest(VideoProfile videoProfile) throws RemoteException {
110            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_REQUEST, videoProfile).sendToTarget();
111        }
112
113        @Override
114        public void receiveSessionModifyResponse(int status, VideoProfile requestedProfile,
115                VideoProfile responseProfile) throws RemoteException {
116            SomeArgs args = SomeArgs.obtain();
117            args.argi1 = status;
118            args.arg1 = requestedProfile;
119            args.arg2 = responseProfile;
120            mHandler.obtainMessage(MSG_RECEIVE_SESSION_MODIFY_RESPONSE, args).sendToTarget();
121        }
122
123        @Override
124        public void handleCallSessionEvent(int event) throws RemoteException {
125            SomeArgs args = SomeArgs.obtain();
126            args.argi1 = event;
127            mHandler.obtainMessage(MSG_HANDLE_CALL_SESSION_EVENT, args).sendToTarget();
128        }
129
130        @Override
131        public void changePeerDimensions(int width, int height) throws RemoteException {
132            SomeArgs args = SomeArgs.obtain();
133            args.argi1 = width;
134            args.argi2 = height;
135            mHandler.obtainMessage(MSG_CHANGE_PEER_DIMENSIONS, args).sendToTarget();
136        }
137
138        @Override
139        public void changeCallDataUsage(int dataUsage) throws RemoteException {
140            SomeArgs args = SomeArgs.obtain();
141            args.argi1 = dataUsage;
142            mHandler.obtainMessage(MSG_CHANGE_CALL_DATA_USAGE, args).sendToTarget();
143        }
144
145        @Override
146        public void changeCameraCapabilities(CameraCapabilities cameraCapabilities)
147                throws RemoteException {
148            mHandler.obtainMessage(MSG_CHANGE_CAMERA_CAPABILITIES, cameraCapabilities)
149                    .sendToTarget();
150        }
151    };
152
153    public VideoCallbackServant(IVideoCallback delegate) {
154        mDelegate = delegate;
155    }
156
157    public IVideoCallback getStub() {
158        return mStub;
159    }
160}
161