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