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        if (!isValid()) {
53            final String msg = "infoType: " + infoType + "," +
54                               "mimeType: " + mimeType + "," +
55                               "data: " + data;
56
57            throw new IllegalArgumentException(msg);
58        }
59    }
60
61    /**
62     * Creates a <code>DrmInfo</code> object with the given parameters.
63     *
64     * @param infoType The type of information.
65     * @param path The trigger data.
66     * @param mimeType The MIME type.
67     */
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        }
79        if (!isValid()) {
80            final String msg = "infoType: " + infoType + "," +
81                               "mimeType: " + mimeType + "," +
82                               "data: " + mData;
83
84            throw new IllegalArgumentException();
85        }
86    }
87
88    /**
89     * 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.
91     *
92     * @param key Key to add.
93     * @param value Value to add.
94     *
95     */
96    public void put(String key, Object value) {
97        mAttributes.put(key, value);
98    }
99
100    /**
101     * Retrieves the value of a given key.
102     *
103     * @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.
106     */
107    public Object get(String key) {
108        return mAttributes.get(key);
109    }
110
111    /**
112     * Retrieves an iterator object that you can use to iterate over the keys associated with
113     * this <code>DrmInfo</code> object.
114     *
115     * @return The iterator object.
116     */
117    public Iterator<String> keyIterator() {
118        return mAttributes.keySet().iterator();
119    }
120
121    /**
122     * Retrieves an iterator object that you can use to iterate over the values associated with
123     * this <code>DrmInfo</code> object.
124     *
125     * @return The iterator object.
126     */
127    public Iterator<Object> iterator() {
128        return mAttributes.values().iterator();
129    }
130
131    /**
132     * Retrieves the trigger data associated with this object.
133     *
134     * @return The trigger data.
135     */
136    public byte[] getData() {
137        return mData;
138    }
139
140    /**
141     * Retrieves the MIME type associated with this object.
142     *
143     * @return The MIME type.
144     */
145    public String getMimeType() {
146        return mMimeType;
147    }
148
149    /**
150     * Retrieves the information type associated with this object.
151     *
152     * @return The information type.
153     */
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
171