1/*
2 * Copyright (C) 2017 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.media;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.os.ServiceSpecificException;
22
23import static android.media.MediaCasException.*;
24
25/**
26 * Base class for MediaCas runtime exceptions
27 */
28public class MediaCasStateException extends IllegalStateException {
29    private final int mErrorCode;
30    private final String mDiagnosticInfo;
31
32    /** @hide */
33    public MediaCasStateException(int err, @Nullable String msg, @Nullable String diagnosticInfo) {
34        super(msg);
35        mErrorCode = err;
36        mDiagnosticInfo = diagnosticInfo;
37    }
38
39    static void throwExceptions(ServiceSpecificException e) {
40        String diagnosticInfo = "";
41        switch (e.errorCode) {
42        case ERROR_DRM_UNKNOWN:
43            diagnosticInfo = "General CAS error";
44            break;
45        case ERROR_DRM_NO_LICENSE:
46            diagnosticInfo = "No license";
47            break;
48        case ERROR_DRM_LICENSE_EXPIRED:
49            diagnosticInfo = "License expired";
50            break;
51        case ERROR_DRM_SESSION_NOT_OPENED:
52            diagnosticInfo = "Session not opened";
53            break;
54        case ERROR_DRM_DECRYPT_UNIT_NOT_INITIALIZED:
55            diagnosticInfo = "Not initialized";
56            break;
57        case ERROR_DRM_DECRYPT:
58            diagnosticInfo = "Decrypt error";
59            break;
60        case ERROR_DRM_CANNOT_HANDLE:
61            diagnosticInfo = "Unsupported scheme or data format";
62            break;
63        case ERROR_DRM_TAMPER_DETECTED:
64            diagnosticInfo = "Tamper detected";
65            break;
66        default:
67            diagnosticInfo = "Unknown CAS state exception";
68            break;
69        }
70        throw new MediaCasStateException(e.errorCode, e.getMessage(),
71                String.format("%s (err=%d)", diagnosticInfo, e.errorCode));
72    }
73
74    /**
75     * Retrieve the associated error code
76     *
77     * @hide
78     */
79    public int getErrorCode() {
80        return mErrorCode;
81    }
82
83    /**
84     * Retrieve a developer-readable diagnostic information string
85     * associated with the exception. Do not show this to end-users,
86     * since this string will not be localized or generally comprehensible
87     * to end-users.
88     */
89    @NonNull
90    public String getDiagnosticInfo() {
91        return mDiagnosticInfo;
92    }
93}
94