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