1 /*
2  * Copyright (C) 2010 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.drm;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Arrays;
22 
23 /**
24  * An entity class that wraps the license information retrieved from the online DRM server.
25  *<p>
26  * A caller can instantiate a {@link DrmRights} object by first invoking the
27  * {@link DrmManagerClient#processDrmInfo(DrmInfo)} method and then using the resulting
28  * {@link ProcessedData} object to invoke the {@link DrmRights#DrmRights(ProcessedData, String)}
29  * constructor.
30  *<p>
31  * A caller can also instantiate a {@link DrmRights} object by using the
32  * {@link DrmRights#DrmRights(String, String)} constructor, which takes a path to a file
33  * containing rights information instead of a <code>ProcessedData</code>.
34  *<p>
35  * Please note that the account id and subscription id is not mandatory by all DRM agents
36  * or plugins. When account id or subscription id is not required by the specific DRM
37  * agent or plugin, they can be either null, or an empty string, or any other don't-care
38  * string value.
39  *
40  * @deprecated Please use {@link android.media.MediaDrm}
41  */
42 @Deprecated
43 public class DrmRights {
44     private byte[] mData;
45     private String mMimeType;
46     private String mAccountId;
47     private String mSubscriptionId;
48 
49     /**
50      * Creates a <code>DrmRights</code> object with the given parameters.
51      *
52      * @param rightsFilePath Path to the file containing rights information.
53      * @param mimeType MIME type. Must not be null or an empty string.
54      */
DrmRights(String rightsFilePath, String mimeType)55     public DrmRights(String rightsFilePath, String mimeType) {
56         File file = new File(rightsFilePath);
57         instantiate(file, mimeType);
58     }
59 
60     /**
61      * Creates a <code>DrmRights</code> object with the given parameters.
62      *
63      * @param rightsFilePath Path to the file containing rights information.
64      * @param mimeType MIME type. Must not be null or an empty string.
65      * @param accountId Account ID of the user.
66      */
DrmRights(String rightsFilePath, String mimeType, String accountId)67     public DrmRights(String rightsFilePath, String mimeType, String accountId) {
68         this(rightsFilePath, mimeType);
69 
70         mAccountId = accountId;
71     }
72 
73     /**
74      * Creates a <code>DrmRights</code> object with the given parameters.
75      *
76      * @param rightsFilePath Path to the file containing rights information.
77      * @param mimeType MIME type. Must not be null or an empty string.
78      * @param accountId Account ID of the user.
79      * @param subscriptionId Subscription ID of the user.
80      */
DrmRights( String rightsFilePath, String mimeType, String accountId, String subscriptionId)81     public DrmRights(
82             String rightsFilePath, String mimeType, String accountId, String subscriptionId) {
83         this(rightsFilePath, mimeType);
84 
85         mAccountId = accountId;
86         mSubscriptionId = subscriptionId;
87     }
88 
89     /**
90      * Creates a <code>DrmRights</code> object with the given parameters.
91      *
92      * @param rightsFile File containing rights information.
93      * @param mimeType MIME type. Must not be null or an empty string.
94      */
DrmRights(File rightsFile, String mimeType)95     public DrmRights(File rightsFile, String mimeType) {
96         instantiate(rightsFile, mimeType);
97     }
98 
instantiate(File rightsFile, String mimeType)99     private void instantiate(File rightsFile, String mimeType) {
100         try {
101             mData = DrmUtils.readBytes(rightsFile);
102         } catch (IOException e) {
103             e.printStackTrace();
104         }
105 
106         mMimeType = mimeType;
107         if (!isValid()) {
108             final String msg = "mimeType: " + mMimeType + "," +
109                                "data: " + Arrays.toString(mData);
110             throw new IllegalArgumentException(msg);
111         }
112     }
113 
114     /**
115      * Creates a <code>DrmRights</code> object with the given parameters.
116      *
117      * @param data A {@link ProcessedData} object containing rights information.
118      *             Must not be null.
119      * @param mimeType The MIME type. It must not be null or an empty string.
120      */
DrmRights(ProcessedData data, String mimeType)121     public DrmRights(ProcessedData data, String mimeType) {
122         if (data == null) {
123             throw new IllegalArgumentException("data is null");
124         }
125 
126         mData = data.getData();
127         mAccountId = data.getAccountId();
128         mSubscriptionId = data.getSubscriptionId();
129         mMimeType = mimeType;
130 
131         if (!isValid()) {
132             final String msg = "mimeType: " + mMimeType + "," +
133                                "data: " + Arrays.toString(mData);
134             throw new IllegalArgumentException(msg);
135         }
136     }
137 
138     /**
139      * Retrieves the rights data associated with this <code>DrmRights</code> object.
140      *
141      * @return A <code>byte</code> array representing the rights data.
142      */
getData()143     public byte[] getData() {
144         return mData;
145     }
146 
147     /**
148      * Retrieves the MIME type associated with this <code>DrmRights</code> object.
149      *
150      * @return The MIME type.
151      */
getMimeType()152     public String getMimeType() {
153         return mMimeType;
154     }
155 
156     /**
157      * Retrieves the account ID associated with this <code>DrmRights</code> object.
158      *
159      * @return The account ID.
160      */
getAccountId()161     public String getAccountId() {
162         return mAccountId;
163     }
164 
165     /**
166      * Retrieves the subscription ID associated with this <code>DrmRights</code> object.
167      *
168      * @return The subscription ID.
169      */
getSubscriptionId()170     public String getSubscriptionId() {
171         return mSubscriptionId;
172     }
173 
174     /**
175      * Determines whether this instance is valid or not.
176      *
177      * @return True if valid; false if invalid.
178      */
isValid()179     /*package*/ boolean isValid() {
180         return (null != mMimeType && !mMimeType.equals("")
181                 && null != mData && mData.length > 0);
182     }
183 }
184 
185