DrmInfo.java revision 0e092f806b0a4b81785a52da8ba22d2d47087de5
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
17package android.drm;
18
19import java.io.IOException;
20import java.util.HashMap;
21import java.util.Iterator;
22
23/**
24 * 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}.
31 *
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    /**
42     * Creates a <code>DrmInfo</code> object with the given parameters.
43     *
44     * @param infoType The type of information.
45     * @param data The trigger data.
46     * @param mimeType The MIME type.
47     */
48    public DrmInfo(int infoType, byte[] data, String mimeType) {
49        mInfoType = infoType;
50        mMimeType = mimeType;
51        mData = data;
52    }
53
54    /**
55     * Creates a <code>DrmInfo</code> object with the given parameters.
56     *
57     * @param infoType The type of information.
58     * @param path The trigger data.
59     * @param mimeType The MIME type.
60     */
61    public DrmInfo(int infoType, String path, String mimeType) {
62        mInfoType = infoType;
63        mMimeType = mimeType;
64        try {
65            mData = DrmUtils.readBytes(path);
66        } catch (IOException e) {
67            // As the given path is invalid,
68            // set mData = null, so that further processDrmInfo()
69            // call would fail with IllegalArgumentException because of mData = null
70            mData = null;
71        }
72    }
73
74    /**
75     * Adds optional information as key-value pairs to this object. To add a custom object
76     * to the <code>DrmInfo</code> object, you must override the {@link #toString} implementation.
77     *
78     * @param key Key to add.
79     * @param value Value to add.
80     *
81     */
82    public void put(String key, Object value) {
83        mAttributes.put(key, value);
84    }
85
86    /**
87     * Retrieves the value of a given key.
88     *
89     * @param key The key whose value is being retrieved.
90     *
91     * @return The value of the key being retrieved. Returns null if the key cannot be found.
92     */
93    public Object get(String key) {
94        return mAttributes.get(key);
95    }
96
97    /**
98     * Retrieves an iterator object that you can use to iterate over the keys associated with
99     * this <code>DrmInfo</code> object.
100     *
101     * @return The iterator object.
102     */
103    public Iterator<String> keyIterator() {
104        return mAttributes.keySet().iterator();
105    }
106
107    /**
108     * Retrieves an iterator object that you can use to iterate over the values associated with
109     * this <code>DrmInfo</code> object.
110     *
111     * @return The iterator object.
112     */
113    public Iterator<Object> iterator() {
114        return mAttributes.values().iterator();
115    }
116
117    /**
118     * Retrieves the trigger data associated with this object.
119     *
120     * @return The trigger data.
121     */
122    public byte[] getData() {
123        return mData;
124    }
125
126    /**
127     * Retrieves the MIME type associated with this object.
128     *
129     * @return The MIME type.
130     */
131    public String getMimeType() {
132        return mMimeType;
133    }
134
135    /**
136     * Retrieves the information type associated with this object.
137     *
138     * @return The information type.
139     */
140    public int getInfoType() {
141        return mInfoType;
142    }
143
144    /**
145     * Returns whether this instance is valid or not
146     *
147     * @return
148     *     true if valid
149     *     false if invalid
150     */
151     boolean isValid() {
152        return (null != mMimeType && !mMimeType.equals("")
153                && null != mData && mData.length > 0 && DrmInfoRequest.isValidType(mInfoType));
154    }
155}
156
157