| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 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.HashMap; |
| 21 | import java.util.Iterator; |
| 22 | |
| 23 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 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}. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 31 | * |
| 32 | */ |
| 33 | public 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 Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 42 | * Creates a <code>DrmInfo</code> object with the given parameters. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 43 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 44 | * @param infoType The type of information. |
| 45 | * @param data The trigger data. |
| 46 | * @param mimeType The MIME type. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 47 | */ |
| 48 | public DrmInfo(int infoType, byte[] data, String mimeType) { |
| 49 | mInfoType = infoType; |
| 50 | mMimeType = mimeType; |
| 51 | mData = data; |
| James Dong | 6c95d4f | 2012-02-14 16:27:38 -0800 | [diff] [blame] | 52 | if (!isValid()) { |
| 53 | final String msg = "infoType: " + infoType + "," + |
| 54 | "mimeType: " + mimeType + "," + |
| 55 | "data: " + data; |
| 56 | |
| 57 | throw new IllegalArgumentException(msg); |
| 58 | } |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 62 | * Creates a <code>DrmInfo</code> object with the given parameters. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 63 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 64 | * @param infoType The type of information. |
| 65 | * @param path The trigger data. |
| 66 | * @param mimeType The MIME type. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 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 | } |
| James Dong | 6c95d4f | 2012-02-14 16:27:38 -0800 | [diff] [blame] | 79 | if (!isValid()) { |
| 80 | final String msg = "infoType: " + infoType + "," + |
| 81 | "mimeType: " + mimeType + "," + |
| 82 | "data: " + mData; |
| 83 | |
| 84 | throw new IllegalArgumentException(); |
| 85 | } |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 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. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 91 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 92 | * @param key Key to add. |
| 93 | * @param value Value to add. |
| 94 | * |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 95 | */ |
| 96 | public void put(String key, Object value) { |
| 97 | mAttributes.put(key, value); |
| 98 | } |
| 99 | |
| 100 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 101 | * Retrieves the value of a given key. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 102 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 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. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 106 | */ |
| 107 | public Object get(String key) { |
| 108 | return mAttributes.get(key); |
| 109 | } |
| 110 | |
| 111 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 112 | * Retrieves an iterator object that you can use to iterate over the keys associated with |
| 113 | * this <code>DrmInfo</code> object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 114 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 115 | * @return The iterator object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 116 | */ |
| 117 | public Iterator<String> keyIterator() { |
| 118 | return mAttributes.keySet().iterator(); |
| 119 | } |
| 120 | |
| 121 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 122 | * Retrieves an iterator object that you can use to iterate over the values associated with |
| 123 | * this <code>DrmInfo</code> object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 124 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 125 | * @return The iterator object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 126 | */ |
| 127 | public Iterator<Object> iterator() { |
| 128 | return mAttributes.values().iterator(); |
| 129 | } |
| 130 | |
| 131 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 132 | * Retrieves the trigger data associated with this object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 133 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 134 | * @return The trigger data. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 135 | */ |
| 136 | public byte[] getData() { |
| 137 | return mData; |
| 138 | } |
| 139 | |
| 140 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 141 | * Retrieves the MIME type associated with this object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 142 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 143 | * @return The MIME type. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 144 | */ |
| 145 | public String getMimeType() { |
| 146 | return mMimeType; |
| 147 | } |
| 148 | |
| 149 | /** |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 150 | * Retrieves the information type associated with this object. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 151 | * |
| Bill Gruber | 0e092f8 | 2011-03-17 16:04:18 -0700 | [diff] [blame] | 152 | * @return The information type. |
| aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 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 | |