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 android.hardware.camera2.utils;
18
19import android.os.DeadObjectException;
20import android.os.RemoteException;
21import android.util.Log;
22
23import java.lang.reflect.Method;
24
25/**
26 * Translate camera service status_t return values into exceptions.
27 *
28 * @see android.hardware.camera2.utils.CameraBinderDecorator#newInstance
29 * @hide
30 */
31public class CameraServiceBinderDecorator extends CameraBinderDecorator {
32
33    private static final String TAG = "CameraServiceBinderDecorator";
34
35    static class CameraServiceBinderDecoratorListener
36            extends CameraBinderDecorator.CameraBinderDecoratorListener {
37
38        // Pass through remote exceptions, unlike CameraBinderDecorator
39        @Override
40        public boolean onCatchException(Method m, Object[] args, Throwable t) {
41
42            if (t instanceof DeadObjectException) {
43                // Can sometimes happen (camera service died)
44                // Pass on silently
45            } else if (t instanceof RemoteException) {
46                // Some other kind of remote exception - this is not normal, so let's at least
47                // note it before moving on
48                Log.e(TAG, "Unexpected RemoteException from camera service call.", t);
49            }
50            // All other exceptions also get sent onward
51            return false;
52        }
53
54    }
55
56    /**
57     * <p>
58     * Wraps the type T with a proxy that will check 'status_t' return codes
59     * from the native side of the camera service, and throw Java exceptions
60     * automatically based on the code.
61     * </p>
62     *
63     * @param obj object that will serve as the target for all method calls
64     * @param <T> the type of the element you want to wrap. This must be an interface.
65     * @return a proxy that will intercept all invocations to obj
66     */
67    public static <T> T newInstance(T obj) {
68        return Decorator.<T> newInstance(obj, new CameraServiceBinderDecoratorListener());
69    }
70}
71