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.IOException;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 
24 /**
25  * An entity class that describes the information required to send transactions
26  * between a device and an online DRM server. The DRM framework achieves
27  * server registration, license acquisition, and any other server-related transactions
28  * by passing an instance of this class to {@link DrmManagerClient#processDrmInfo}.
29  *<p>
30  * The caller can retrieve the {@link DrmInfo} instance by passing a {@link DrmInfoRequest}
31  * instance to {@link DrmManagerClient#acquireDrmInfo}.
32  *
33  */
34 public class DrmInfo {
35     private byte[] mData;
36     private final String mMimeType;
37     private final int mInfoType;
38     // It would be used to add attributes specific to
39     // DRM scheme such as account id, path or multiple path's
40     private final HashMap<String, Object> mAttributes = new HashMap<String, Object>();
41 
42     /**
43      * Creates a <code>DrmInfo</code> object with the given parameters.
44      *
45      * @param infoType The type of information.
46      * @param data The trigger data.
47      * @param mimeType The MIME type.
48      */
DrmInfo(int infoType, byte[] data, String mimeType)49     public DrmInfo(int infoType, byte[] data, String mimeType) {
50         mInfoType = infoType;
51         mMimeType = mimeType;
52         mData = data;
53         if (!isValid()) {
54             final String msg = "infoType: " + infoType + "," +
55                                "mimeType: " + mimeType + "," +
56                                "data: " + Arrays.toString(data);
57 
58             throw new IllegalArgumentException(msg);
59         }
60     }
61 
62     /**
63      * Creates a <code>DrmInfo</code> object with the given parameters.
64      *
65      * @param infoType The type of information.
66      * @param path The trigger data.
67      * @param mimeType The MIME type.
68      */
DrmInfo(int infoType, String path, String mimeType)69     public DrmInfo(int infoType, String path, String mimeType) {
70         mInfoType = infoType;
71         mMimeType = mimeType;
72         try {
73             mData = DrmUtils.readBytes(path);
74         } catch (IOException e) {
75             // As the given path is invalid,
76             // set mData = null, so that further processDrmInfo()
77             // call would fail with IllegalArgumentException because of mData = null
78             mData = null;
79         }
80         if (!isValid()) {
81             final String msg = "infoType: " + infoType + "," +
82                                "mimeType: " + mimeType + "," +
83                                "data: " + Arrays.toString(mData);
84 
85             throw new IllegalArgumentException();
86         }
87     }
88 
89     /**
90      * Adds optional information as key-value pairs to this object. To add a custom object
91      * to the <code>DrmInfo</code> object, you must override the {@link #toString} implementation.
92      *
93      * @param key Key to add.
94      * @param value Value to add.
95      *
96      */
put(String key, Object value)97     public void put(String key, Object value) {
98         mAttributes.put(key, value);
99     }
100 
101     /**
102      * Retrieves the value of a given key.
103      *
104      * @param key The key whose value is being retrieved.
105      *
106      * @return The value of the key being retrieved. Returns null if the key cannot be found.
107      */
get(String key)108     public Object get(String key) {
109         return mAttributes.get(key);
110     }
111 
112     /**
113      * Retrieves an iterator object that you can use to iterate over the keys associated with
114      * this <code>DrmInfo</code> object.
115      *
116      * @return The iterator object.
117      */
keyIterator()118     public Iterator<String> keyIterator() {
119         return mAttributes.keySet().iterator();
120     }
121 
122     /**
123      * Retrieves an iterator object that you can use to iterate over the values associated with
124      * this <code>DrmInfo</code> object.
125      *
126      * @return The iterator object.
127      */
iterator()128     public Iterator<Object> iterator() {
129         return mAttributes.values().iterator();
130     }
131 
132     /**
133      * Retrieves the trigger data associated with this object.
134      *
135      * @return The trigger data.
136      */
getData()137     public byte[] getData() {
138         return mData;
139     }
140 
141     /**
142      * Retrieves the MIME type associated with this object.
143      *
144      * @return The MIME type.
145      */
getMimeType()146     public String getMimeType() {
147         return mMimeType;
148     }
149 
150     /**
151      * Retrieves the information type associated with this object.
152      *
153      * @return The information type.
154      */
getInfoType()155     public int getInfoType() {
156         return mInfoType;
157     }
158 
159     /**
160      * Returns whether this instance is valid or not
161      *
162      * @return
163      *     true if valid
164      *     false if invalid
165      */
isValid()166      boolean isValid() {
167         return (null != mMimeType && !mMimeType.equals("")
168                 && null != mData && mData.length > 0 && DrmInfoRequest.isValidType(mInfoType));
169     }
170 }
171 
172