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.Arrays; 21 import java.util.HashMap; 22 import java.util.Iterator; 23 24 /** 25 * An entity class that describes the information required to send transactions 26 * between a device and an online DRM server. The DRM framework achieves 27 * server registration, license acquisition, and any other server-related transactions 28 * by passing an instance of this class to {@link DrmManagerClient#processDrmInfo}. 29 *<p> 30 * The caller can retrieve the {@link DrmInfo} instance by passing a {@link DrmInfoRequest} 31 * instance to {@link DrmManagerClient#acquireDrmInfo}. 32 * 33 * @deprecated Please use {@link android.media.MediaDrm} 34 */ 35 @Deprecated 36 public class DrmInfo { 37 private byte[] mData; 38 private final String mMimeType; 39 private final int mInfoType; 40 // It would be used to add attributes specific to 41 // DRM scheme such as account id, path or multiple path's 42 private final HashMap<String, Object> mAttributes = new HashMap<String, Object>(); 43 44 /** 45 * Creates a <code>DrmInfo</code> object with the given parameters. 46 * 47 * @param infoType The type of information. 48 * @param data The trigger data. 49 * @param mimeType The MIME type. 50 */ DrmInfo(int infoType, byte[] data, String mimeType)51 public DrmInfo(int infoType, byte[] data, String mimeType) { 52 mInfoType = infoType; 53 mMimeType = mimeType; 54 mData = data; 55 if (!isValid()) { 56 final String msg = "infoType: " + infoType + "," + 57 "mimeType: " + mimeType + "," + 58 "data: " + Arrays.toString(data); 59 60 throw new IllegalArgumentException(msg); 61 } 62 } 63 64 /** 65 * Creates a <code>DrmInfo</code> object with the given parameters. 66 * 67 * @param infoType The type of information. 68 * @param path The trigger data. 69 * @param mimeType The MIME type. 70 */ DrmInfo(int infoType, String path, String mimeType)71 public DrmInfo(int infoType, String path, String mimeType) { 72 mInfoType = infoType; 73 mMimeType = mimeType; 74 try { 75 mData = DrmUtils.readBytes(path); 76 } catch (IOException e) { 77 // As the given path is invalid, 78 // set mData = null, so that further processDrmInfo() 79 // call would fail with IllegalArgumentException because of mData = null 80 mData = null; 81 } 82 if (!isValid()) { 83 final String msg = "infoType: " + infoType + "," + 84 "mimeType: " + mimeType + "," + 85 "data: " + Arrays.toString(mData); 86 87 throw new IllegalArgumentException(); 88 } 89 } 90 91 /** 92 * Adds optional information as key-value pairs to this object. To add a custom object 93 * to the <code>DrmInfo</code> object, you must override the {@link #toString} implementation. 94 * 95 * @param key Key to add. 96 * @param value Value to add. 97 * 98 */ put(String key, Object value)99 public void put(String key, Object value) { 100 mAttributes.put(key, value); 101 } 102 103 /** 104 * Retrieves the value of a given key. 105 * 106 * @param key The key whose value is being retrieved. 107 * 108 * @return The value of the key being retrieved. Returns null if the key cannot be found. 109 */ get(String key)110 public Object get(String key) { 111 return mAttributes.get(key); 112 } 113 114 /** 115 * Retrieves an iterator object that you can use to iterate over the keys associated with 116 * this <code>DrmInfo</code> object. 117 * 118 * @return The iterator object. 119 */ keyIterator()120 public Iterator<String> keyIterator() { 121 return mAttributes.keySet().iterator(); 122 } 123 124 /** 125 * Retrieves an iterator object that you can use to iterate over the values associated with 126 * this <code>DrmInfo</code> object. 127 * 128 * @return The iterator object. 129 */ iterator()130 public Iterator<Object> iterator() { 131 return mAttributes.values().iterator(); 132 } 133 134 /** 135 * Retrieves the trigger data associated with this object. 136 * 137 * @return The trigger data. 138 */ getData()139 public byte[] getData() { 140 return mData; 141 } 142 143 /** 144 * Retrieves the MIME type associated with this object. 145 * 146 * @return The MIME type. 147 */ getMimeType()148 public String getMimeType() { 149 return mMimeType; 150 } 151 152 /** 153 * Retrieves the information type associated with this object. 154 * 155 * @return The information type. 156 */ getInfoType()157 public int getInfoType() { 158 return mInfoType; 159 } 160 161 /** 162 * Returns whether this instance is valid or not 163 * 164 * @return 165 * true if valid 166 * false if invalid 167 */ isValid()168 boolean isValid() { 169 return (null != mMimeType && !mMimeType.equals("") 170 && null != mData && mData.length > 0 && DrmInfoRequest.isValidType(mInfoType)); 171 } 172 } 173 174