DrmInfo.java revision d074e30ce44b9e33da43b67a4515b8986ca72b26
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 * This is an entity class in which necessary information required to transact
25 * between device and online DRM server is described. DRM Framework achieves
26 * server registration, license acquisition and any other server related transaction
27 * by passing an instance of this class to {@link DrmManagerClient#processDrmInfo(DrmInfo)}.
28 *
29 * Caller can retrieve the {@link DrmInfo} instance by using
30 * {@link DrmManagerClient#acquireDrmInfo(DrmInfoRequest)}
31 * by passing {@link DrmInfoRequest} instance.
32 *
33 */
34public 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     * constructor to create DrmInfo object with given parameters
44     *
45     * @param infoType Type of information
46     * @param data Trigger data
47     * @param mimeType MIME type
48     */
49    public DrmInfo(int infoType, byte[] data, String mimeType) {
50        mInfoType = infoType;
51        mMimeType = mimeType;
52        mData = data;
53    }
54
55    /**
56     * constructor to create DrmInfo object with given parameters
57     *
58     * @param infoType Type of information
59     * @param path Trigger data
60     * @param mimeType MIME type
61     */
62    public DrmInfo(int infoType, String path, String mimeType) {
63        mInfoType = infoType;
64        mMimeType = mimeType;
65        try {
66            mData = DrmUtils.readBytes(path);
67        } catch (IOException e) {
68            // As the given path is invalid,
69            // set mData = null, so that further processDrmInfo()
70            // call would fail with IllegalArgumentException because of mData = null
71            mData = null;
72        }
73    }
74
75    /**
76     * Adds optional information as <key, value> pair to this object
77     *
78     * @param key Key to add
79     * @param value Value to add
80     *     To put custom object into DrmInfo, custom object has to
81     *     override toString() implementation.
82     */
83    public void put(String key, Object value) {
84        mAttributes.put(key, value);
85    }
86
87    /**
88     * Retrieves the value of given key, if not found returns null
89     *
90     * @param key Key whose value to be retrieved
91     * @return The value or null
92     */
93    public Object get(String key) {
94        return mAttributes.get(key);
95    }
96
97    /**
98     * Returns Iterator object to walk through the keys associated with this instance
99     *
100     * @return Iterator object
101     */
102    public Iterator<String> keyIterator() {
103        return mAttributes.keySet().iterator();
104    }
105
106    /**
107     * Returns Iterator object to walk through the values associated with this instance
108     *
109     * @return Iterator object
110     */
111    public Iterator<Object> iterator() {
112        return mAttributes.values().iterator();
113    }
114
115    /**
116     * Returns the trigger data associated with this object
117     *
118     * @return Trigger data
119     */
120    public byte[] getData() {
121        return mData;
122    }
123
124    /**
125     * Returns the mimetype associated with this object
126     *
127     * @return MIME type
128     */
129    public String getMimeType() {
130        return mMimeType;
131    }
132
133    /**
134     * Returns information type associated with this instance
135     *
136     * @return Information type
137     */
138    public int getInfoType() {
139        return mInfoType;
140    }
141
142    /**
143     * Returns whether this instance is valid or not
144     *
145     * @return
146     *     true if valid
147     *     false if invalid
148     */
149     boolean isValid() {
150        return (null != mMimeType && !mMimeType.equals("")
151                && null != mData && mData.length > 0 && DrmInfoRequest.isValidType(mInfoType));
152    }
153}
154
155