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 
17 package android.media;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import android.hardware.cas.V1_0.Status;
23 
24 /**
25  * Base class for MediaCas runtime exceptions
26  */
27 public class MediaCasStateException extends IllegalStateException {
28     private final int mErrorCode;
29     private final String mDiagnosticInfo;
30 
MediaCasStateException(int err, @Nullable String msg, @Nullable String diagnosticInfo)31     private MediaCasStateException(int err, @Nullable String msg, @Nullable String diagnosticInfo) {
32         super(msg);
33         mErrorCode = err;
34         mDiagnosticInfo = diagnosticInfo;
35     }
36 
throwExceptionIfNeeded(int err)37     static void throwExceptionIfNeeded(int err) {
38         throwExceptionIfNeeded(err, null /* msg */);
39     }
40 
throwExceptionIfNeeded(int err, @Nullable String msg)41     static void throwExceptionIfNeeded(int err, @Nullable String msg) {
42         if (err == Status.OK) {
43             return;
44         }
45         if (err == Status.BAD_VALUE) {
46             throw new IllegalArgumentException();
47         }
48 
49         String diagnosticInfo = "";
50         switch (err) {
51         case Status.ERROR_CAS_UNKNOWN:
52             diagnosticInfo = "General CAS error";
53             break;
54         case Status.ERROR_CAS_NO_LICENSE:
55             diagnosticInfo = "No license";
56             break;
57         case Status.ERROR_CAS_LICENSE_EXPIRED:
58             diagnosticInfo = "License expired";
59             break;
60         case Status.ERROR_CAS_SESSION_NOT_OPENED:
61             diagnosticInfo = "Session not opened";
62             break;
63         case Status.ERROR_CAS_CANNOT_HANDLE:
64             diagnosticInfo = "Unsupported scheme or data format";
65             break;
66         case Status.ERROR_CAS_INVALID_STATE:
67             diagnosticInfo = "Invalid CAS state";
68             break;
69         case Status.ERROR_CAS_INSUFFICIENT_OUTPUT_PROTECTION:
70             diagnosticInfo = "Insufficient output protection";
71             break;
72         case Status.ERROR_CAS_TAMPER_DETECTED:
73             diagnosticInfo = "Tamper detected";
74             break;
75         case Status.ERROR_CAS_DECRYPT_UNIT_NOT_INITIALIZED:
76             diagnosticInfo = "Not initialized";
77             break;
78         case Status.ERROR_CAS_DECRYPT:
79             diagnosticInfo = "Decrypt error";
80             break;
81         default:
82             diagnosticInfo = "Unknown CAS state exception";
83             break;
84         }
85         throw new MediaCasStateException(err, msg,
86                 String.format("%s (err=%d)", diagnosticInfo, err));
87     }
88 
89     /**
90      * Retrieve the associated error code
91      *
92      * @hide
93      */
getErrorCode()94     public int getErrorCode() {
95         return mErrorCode;
96     }
97 
98     /**
99      * Retrieve a developer-readable diagnostic information string
100      * associated with the exception. Do not show this to end-users,
101      * since this string will not be localized or generally comprehensible
102      * to end-users.
103      */
104     @NonNull
getDiagnosticInfo()105     public String getDiagnosticInfo() {
106         return mDiagnosticInfo;
107     }
108 }
109