blob: 1bc36ec8b22c315b3a19e40bc50d19154a3a13fb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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.mobile1;
18
19import java.io.*;
20import java.util.*;
21
22/**
23 * This class provides interfaces to access the DRM right manager.
24 */
25public class DrmRightsManager {
26 /**
27 * The "application/vnd.oma.drm.rights+xml" mime type.
28 */
29 public static final String DRM_MIMETYPE_RIGHTS_XML_STRING = "application/vnd.oma.drm.rights+xml";
30
31 /**
32 * The "application/vnd.oma.drm.rights+wbxml" mime type.
33 */
34 public static final String DRM_MIMETYPE_RIGHTS_WBXML_STRING = "application/vnd.oma.drm.rights+wbxml";
35
36 /**
37 * The id of "application/vnd.oma.drm.rights+xml" mime type.
38 */
39 private static final int DRM_MIMETYPE_RIGHTS_XML = 3;
40
41 /**
42 * The id of "application/vnd.oma.drm.rights+wbxml" mime type.
43 */
44 private static final int DRM_MIMETYPE_RIGHTS_WBXML = 4;
45
46 /**
47 * The id of "application/vnd.oma.drm.message" mime type.
48 */
49 private static final int DRM_MIMETYPE_MESSAGE = 1;
50
51 /**
52 * Successful operation.
53 */
54 private static final int JNI_DRM_SUCCESS = 0;
55
56 /**
57 * General failure.
58 */
59 private static final int JNI_DRM_FAILURE = -1;
60
61 /**
62 * The instance of the rights manager.
63 */
64 private static DrmRightsManager singleton = null;
65
66
67 /**
68 * Construct a DrmRightsManager
69 */
70 protected DrmRightsManager() {
71 }
72
73 /**
74 * Get the DrmRightsManager instance.
75 *
76 * @return the instance of DrmRightsManager.
77 */
78 public static synchronized DrmRightsManager getInstance() {
79 if (singleton == null) {
80 singleton = new DrmRightsManager();
81 }
82
83 return singleton;
84 }
85
86 /**
87 * Install one DRM rights and return one instance of DrmRights.
88 *
89 * @param rightsData raw rights data.
90 * @param mimeTypeStr the mime type of the rights object.
91 *
92 * @return the instance of the installed DrmRights.
93 */
94 public synchronized DrmRights installRights(InputStream rightsData, int len, String mimeTypeStr) throws DrmException, IOException {
95 int mimeType = 0;
96
97 if (DRM_MIMETYPE_RIGHTS_XML_STRING.equals(mimeTypeStr))
98 mimeType = DRM_MIMETYPE_RIGHTS_XML;
99 else if (DRM_MIMETYPE_RIGHTS_WBXML_STRING.equals(mimeTypeStr))
100 mimeType = DRM_MIMETYPE_RIGHTS_WBXML;
101 else if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equals(mimeTypeStr))
102 mimeType = DRM_MIMETYPE_MESSAGE;
103 else
104 throw new IllegalArgumentException("mimeType must be DRM_MIMETYPE_RIGHTS_XML or DRM_MIMETYPE_RIGHTS_WBXML or DRM_MIMETYPE_MESSAGE");
105
106 if (len <= 0)
107 return null;
108
109 DrmRights rights = new DrmRights();
110
111 /* call native method to install this rights object. */
112 int res = nativeInstallDrmRights(rightsData, len, mimeType, rights);
113
114 if (JNI_DRM_FAILURE == res)
115 throw new DrmException("nativeInstallDrmRights() returned JNI_DRM_FAILURE");
116
117 return rights;
118 }
119
120 /**
121 * Query DRM rights of specified DRM raw content.
122 *
123 * @param content raw content object.
124 *
125 * @return the instance of DrmRights, or null if there is no rights.
126 */
127 public synchronized DrmRights queryRights(DrmRawContent content) {
128 DrmRights rights = new DrmRights();
129
130 /* call native method to query the rights */
131 int res = nativeQueryRights(content, rights);
132
133 if (JNI_DRM_FAILURE == res)
134 return null;
135
136 return rights;
137 }
138
139 /**
140 * Get the list of all DRM rights saved in local client.
141 *
142 * @return the list of all the rights object.
143 */
144 public synchronized List getRightsList() {
145 List rightsList = new ArrayList();
146
147 /* call native method to get how many rights object in current agent */
148 int num = nativeGetNumOfRights();
149
150 if (JNI_DRM_FAILURE == num)
151 return null;
152
153 if (num > 0) {
154 DrmRights[] rightsArray = new DrmRights[num];
155 int i;
156
157 for (i = 0; i < num; i++)
158 rightsArray[i] = new DrmRights();
159
160 /* call native method to get all the rights information */
161 num = nativeGetRightsList(rightsArray, num);
162
163 if (JNI_DRM_FAILURE == num)
164 return null;
165
166 /* add all rights informations to ArrayList */
167 for (i = 0; i < num; i++)
168 rightsList.add(rightsArray[i]);
169 }
170
171 return rightsList;
172 }
173
174 /**
175 * Delete the specified DRM rights object.
176 *
177 * @param rights the specified rights object to be deleted.
178 */
179 public synchronized void deleteRights(DrmRights rights) {
180 /* call native method to delete the specified rights object */
181 int res = nativeDeleteRights(rights);
182
183 if (JNI_DRM_FAILURE == res)
184 return;
185 }
186
187
188 /**
189 * native method: install rights object to local client.
190 *
191 * @param data input DRM rights object data to be installed.
192 * @param len the length of the data.
193 * @param mimeType the mime type of this DRM rights object. the value of this field includes:
194 * #DRM_MIMETYPE_RIGHTS_XML
195 * #DRM_MIMETYPE_RIGHTS_WBXML
196 * @parma rights the instance of DRMRights to be filled.
197 *
198 * @return #JNI_DRM_SUCCESS if succeed.
199 * #JNI_DRM_FAILURE if fail.
200 */
201 private native int nativeInstallDrmRights(InputStream data, int len, int mimeType, DrmRights rights);
202
203 /**
204 * native method: query the given DRM content's rights object.
205 *
206 * @param content the given DRM content.
207 * @param rights the instance of rights to set if have.
208 *
209 * @return #JNI_DRM_SUCCESS if succeed.
210 * #JNI_DRM_FAILURE if fail.
211 */
212 private native int nativeQueryRights(DrmRawContent content, DrmRights rights);
213
214 /**
215 * native method: get how many rights object in current DRM agent.
216 *
217 * @return the number of the rights object.
218 * #JNI_DRM_FAILURE if fail.
219 */
220 private native int nativeGetNumOfRights();
221
222 /**
223 * native method: get all the rights object in current local agent.
224 *
225 * @param rights the array instance of rights object.
226 * @param numRights how many rights can be saved.
227 *
228 * @return the number of the rights object has been gotten.
229 * #JNI_DRM_FAILURE if fail.
230 */
231 private native int nativeGetRightsList(DrmRights[] rights, int numRights);
232
233 /**
234 * native method: delete a specified rights object.
235 *
236 * @param rights the specified rights object to be deleted.
237 *
238 * @return #JNI_DRM_SUCCESS if succeed.
239 * #JNI_DRM_FAILURE if fail.
240 */
241 private native int nativeDeleteRights(DrmRights rights);
242
243
244 /**
245 * Load the shared library to link the native methods.
246 */
247 static {
248 try {
249 System.loadLibrary("drm1_jni");
250 }
251 catch (UnsatisfiedLinkError ule) {
252 System.err.println("WARNING: Could not load libdrm1_jni.so");
253 }
254 }
255}