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.legacy;
18
19import android.hardware.camera2.utils.CameraBinderDecorator;
20import android.util.AndroidException;
21
22/**
23 * Utility class containing exception handling used solely by the compatibility mode shim.
24 */
25public class LegacyExceptionUtils {
26    private static final String TAG = "LegacyExceptionUtils";
27
28    /**
29     * Checked exception thrown when a BufferQueue has been abandoned by its consumer.
30     */
31    public static class BufferQueueAbandonedException extends AndroidException {
32        public BufferQueueAbandonedException () {}
33
34        public BufferQueueAbandonedException(String name) {
35            super(name);
36        }
37
38        public BufferQueueAbandonedException(String name, Throwable cause) {
39            super(name, cause);
40        }
41
42        public BufferQueueAbandonedException(Exception cause) {
43            super(cause);
44        }
45    }
46
47    /**
48     * Throw error codes used by legacy device methods as exceptions.
49     *
50     * <p>Non-negative return values are passed through, negative return values are thrown as
51     * exceptions.</p>
52     *
53     * @param errorFlag error to throw as an exception.
54     * @throws {@link BufferQueueAbandonedException} for {@link CameraBinderDecorator#ENODEV}.
55     * @throws {@link UnsupportedOperationException} for an unknown negative error code.
56     * @return {@code errorFlag} if the value was non-negative, throws otherwise.
57     */
58    public static int throwOnError(int errorFlag) throws BufferQueueAbandonedException {
59        switch (errorFlag) {
60            case CameraBinderDecorator.NO_ERROR: {
61                return CameraBinderDecorator.NO_ERROR;
62            }
63            case CameraBinderDecorator.ENODEV: {
64                throw new BufferQueueAbandonedException();
65            }
66        }
67
68        if (errorFlag < 0) {
69            throw new UnsupportedOperationException("Unknown error " + errorFlag);
70        }
71        return errorFlag;
72    }
73
74    private LegacyExceptionUtils() {
75        throw new AssertionError();
76    }
77}
78