blob: 22d06c7bd20e7ad05462b0acc3827c993a738ec9 [file] [log] [blame]
aimitakeshid074e302010-07-29 10:12:27 +09001/*
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
17package android.drm;
18
19import java.io.IOException;
20import java.util.HashMap;
21import java.util.Iterator;
22
23/**
Bill Gruber0e092f82011-03-17 16:04:18 -070024 * An entity class that describes the information required to send transactions
25 * between a device and an online DRM server. The DRM framework achieves
26 * server registration, license acquisition, and any other server-related transactions
27 * by passing an instance of this class to {@link DrmManagerClient#processDrmInfo}.
28 *<p>
29 * The caller can retrieve the {@link DrmInfo} instance by passing a {@link DrmInfoRequest}
30 * instance to {@link DrmManagerClient#acquireDrmInfo}.
aimitakeshid074e302010-07-29 10:12:27 +090031 *
32 */
33public class DrmInfo {
34 private byte[] mData;
35 private final String mMimeType;
36 private final int mInfoType;
37 // It would be used to add attributes specific to
38 // DRM scheme such as account id, path or multiple path's
39 private final HashMap<String, Object> mAttributes = new HashMap<String, Object>();
40
41 /**
Bill Gruber0e092f82011-03-17 16:04:18 -070042 * Creates a <code>DrmInfo</code> object with the given parameters.
aimitakeshid074e302010-07-29 10:12:27 +090043 *
Bill Gruber0e092f82011-03-17 16:04:18 -070044 * @param infoType The type of information.
45 * @param data The trigger data.
46 * @param mimeType The MIME type.
aimitakeshid074e302010-07-29 10:12:27 +090047 */
48 public DrmInfo(int infoType, byte[] data, String mimeType) {
49 mInfoType = infoType;
50 mMimeType = mimeType;
51 mData = data;
James Dong6c95d4f2012-02-14 16:27:38 -080052 if (!isValid()) {
53 final String msg = "infoType: " + infoType + "," +
54 "mimeType: " + mimeType + "," +
55 "data: " + data;
56
57 throw new IllegalArgumentException(msg);
58 }
aimitakeshid074e302010-07-29 10:12:27 +090059 }
60
61 /**
Bill Gruber0e092f82011-03-17 16:04:18 -070062 * Creates a <code>DrmInfo</code> object with the given parameters.
aimitakeshid074e302010-07-29 10:12:27 +090063 *
Bill Gruber0e092f82011-03-17 16:04:18 -070064 * @param infoType The type of information.
65 * @param path The trigger data.
66 * @param mimeType The MIME type.
aimitakeshid074e302010-07-29 10:12:27 +090067 */
68 public DrmInfo(int infoType, String path, String mimeType) {
69 mInfoType = infoType;
70 mMimeType = mimeType;
71 try {
72 mData = DrmUtils.readBytes(path);
73 } catch (IOException e) {
74 // As the given path is invalid,
75 // set mData = null, so that further processDrmInfo()
76 // call would fail with IllegalArgumentException because of mData = null
77 mData = null;
78 }
James Dong6c95d4f2012-02-14 16:27:38 -080079 if (!isValid()) {
80 final String msg = "infoType: " + infoType + "," +
81 "mimeType: " + mimeType + "," +
82 "data: " + mData;
83
84 throw new IllegalArgumentException();
85 }
aimitakeshid074e302010-07-29 10:12:27 +090086 }
87
88 /**
Bill Gruber0e092f82011-03-17 16:04:18 -070089 * Adds optional information as key-value pairs to this object. To add a custom object
90 * to the <code>DrmInfo</code> object, you must override the {@link #toString} implementation.
aimitakeshid074e302010-07-29 10:12:27 +090091 *
Bill Gruber0e092f82011-03-17 16:04:18 -070092 * @param key Key to add.
93 * @param value Value to add.
94 *
aimitakeshid074e302010-07-29 10:12:27 +090095 */
96 public void put(String key, Object value) {
97 mAttributes.put(key, value);
98 }
99
100 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700101 * Retrieves the value of a given key.
aimitakeshid074e302010-07-29 10:12:27 +0900102 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700103 * @param key The key whose value is being retrieved.
104 *
105 * @return The value of the key being retrieved. Returns null if the key cannot be found.
aimitakeshid074e302010-07-29 10:12:27 +0900106 */
107 public Object get(String key) {
108 return mAttributes.get(key);
109 }
110
111 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700112 * Retrieves an iterator object that you can use to iterate over the keys associated with
113 * this <code>DrmInfo</code> object.
aimitakeshid074e302010-07-29 10:12:27 +0900114 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700115 * @return The iterator object.
aimitakeshid074e302010-07-29 10:12:27 +0900116 */
117 public Iterator<String> keyIterator() {
118 return mAttributes.keySet().iterator();
119 }
120
121 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700122 * Retrieves an iterator object that you can use to iterate over the values associated with
123 * this <code>DrmInfo</code> object.
aimitakeshid074e302010-07-29 10:12:27 +0900124 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700125 * @return The iterator object.
aimitakeshid074e302010-07-29 10:12:27 +0900126 */
127 public Iterator<Object> iterator() {
128 return mAttributes.values().iterator();
129 }
130
131 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700132 * Retrieves the trigger data associated with this object.
aimitakeshid074e302010-07-29 10:12:27 +0900133 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700134 * @return The trigger data.
aimitakeshid074e302010-07-29 10:12:27 +0900135 */
136 public byte[] getData() {
137 return mData;
138 }
139
140 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700141 * Retrieves the MIME type associated with this object.
aimitakeshid074e302010-07-29 10:12:27 +0900142 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700143 * @return The MIME type.
aimitakeshid074e302010-07-29 10:12:27 +0900144 */
145 public String getMimeType() {
146 return mMimeType;
147 }
148
149 /**
Bill Gruber0e092f82011-03-17 16:04:18 -0700150 * Retrieves the information type associated with this object.
aimitakeshid074e302010-07-29 10:12:27 +0900151 *
Bill Gruber0e092f82011-03-17 16:04:18 -0700152 * @return The information type.
aimitakeshid074e302010-07-29 10:12:27 +0900153 */
154 public int getInfoType() {
155 return mInfoType;
156 }
157
158 /**
159 * Returns whether this instance is valid or not
160 *
161 * @return
162 * true if valid
163 * false if invalid
164 */
165 boolean isValid() {
166 return (null != mMimeType && !mMimeType.equals("")
167 && null != mData && mData.length > 0 && DrmInfoRequest.isValidType(mInfoType));
168 }
169}
170