DrmManagerClient.java revision 6225df010365d00b1d0f3b1ca13f18cea537c8e3
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 android.content.ContentResolver; 20import android.content.ContentValues; 21import android.content.Context; 22import android.database.Cursor; 23import android.database.sqlite.SQLiteException; 24import android.net.Uri; 25import android.os.Handler; 26import android.os.HandlerThread; 27import android.os.Looper; 28import android.os.Message; 29import android.provider.MediaStore; 30import android.util.Log; 31 32import java.io.IOException; 33import java.lang.ref.WeakReference; 34import java.util.ArrayList; 35import java.util.HashMap; 36 37/** 38 * The main programming interface for the DRM framework. An application must instantiate this class 39 * to access DRM agents through the DRM framework. 40 * 41 */ 42public class DrmManagerClient { 43 /** 44 * Indicates that a request was successful or that no error occurred. 45 */ 46 public static final int ERROR_NONE = 0; 47 /** 48 * Indicates that an error occurred and the reason is not known. 49 */ 50 public static final int ERROR_UNKNOWN = -2000; 51 52 HandlerThread mInfoThread; 53 HandlerThread mEventThread; 54 private static final String TAG = "DrmManagerClient"; 55 56 static { 57 // Load the respective library 58 System.loadLibrary("drmframework_jni"); 59 } 60 61 /** 62 * Interface definition for a callback that receives status messages and warnings 63 * during registration and rights acquisition. 64 */ 65 public interface OnInfoListener { 66 /** 67 * Called when the DRM framework sends status or warning information during registration 68 * and rights acquisition. 69 * 70 * @param client The <code>DrmManagerClient</code> instance. 71 * @param event The {@link DrmInfoEvent} instance that wraps the status information or 72 * warnings. 73 */ 74 public void onInfo(DrmManagerClient client, DrmInfoEvent event); 75 } 76 77 /** 78 * Interface definition for a callback that receives information 79 * about DRM processing events. 80 */ 81 public interface OnEventListener { 82 /** 83 * Called when the DRM framework sends information about a DRM processing request. 84 * 85 * @param client The <code>DrmManagerClient</code> instance. 86 * @param event The {@link DrmEvent} instance that wraps the information being 87 * conveyed, such as the information type and message. 88 */ 89 public void onEvent(DrmManagerClient client, DrmEvent event); 90 } 91 92 /** 93 * Interface definition for a callback that receives information about DRM framework errors. 94 */ 95 public interface OnErrorListener { 96 /** 97 * Called when the DRM framework sends error information. 98 * 99 * @param client The <code>DrmManagerClient</code> instance. 100 * @param event The {@link DrmErrorEvent} instance that wraps the error type and message. 101 */ 102 public void onError(DrmManagerClient client, DrmErrorEvent event); 103 } 104 105 private static final int ACTION_REMOVE_ALL_RIGHTS = 1001; 106 private static final int ACTION_PROCESS_DRM_INFO = 1002; 107 108 private int mUniqueId; 109 private int mNativeContext; 110 private boolean mReleased; 111 private Context mContext; 112 private InfoHandler mInfoHandler; 113 private EventHandler mEventHandler; 114 private OnInfoListener mOnInfoListener; 115 private OnEventListener mOnEventListener; 116 private OnErrorListener mOnErrorListener; 117 118 private class EventHandler extends Handler { 119 120 public EventHandler(Looper looper) { 121 super(looper); 122 } 123 124 public void handleMessage(Message msg) { 125 DrmEvent event = null; 126 DrmErrorEvent error = null; 127 HashMap<String, Object> attributes = new HashMap<String, Object>(); 128 129 switch(msg.what) { 130 case ACTION_PROCESS_DRM_INFO: { 131 final DrmInfo drmInfo = (DrmInfo) msg.obj; 132 DrmInfoStatus status = _processDrmInfo(mUniqueId, drmInfo); 133 134 attributes.put(DrmEvent.DRM_INFO_STATUS_OBJECT, status); 135 attributes.put(DrmEvent.DRM_INFO_OBJECT, drmInfo); 136 137 if (null != status && DrmInfoStatus.STATUS_OK == status.statusCode) { 138 event = new DrmEvent(mUniqueId, 139 getEventType(status.infoType), null, attributes); 140 } else { 141 int infoType = (null != status) ? status.infoType : drmInfo.getInfoType(); 142 error = new DrmErrorEvent(mUniqueId, 143 getErrorType(infoType), null, attributes); 144 } 145 break; 146 } 147 case ACTION_REMOVE_ALL_RIGHTS: { 148 if (ERROR_NONE == _removeAllRights(mUniqueId)) { 149 event = new DrmEvent(mUniqueId, DrmEvent.TYPE_ALL_RIGHTS_REMOVED, null); 150 } else { 151 error = new DrmErrorEvent(mUniqueId, 152 DrmErrorEvent.TYPE_REMOVE_ALL_RIGHTS_FAILED, null); 153 } 154 break; 155 } 156 default: 157 Log.e(TAG, "Unknown message type " + msg.what); 158 return; 159 } 160 if (null != mOnEventListener && null != event) { 161 mOnEventListener.onEvent(DrmManagerClient.this, event); 162 } 163 if (null != mOnErrorListener && null != error) { 164 mOnErrorListener.onError(DrmManagerClient.this, error); 165 } 166 } 167 } 168 169 /** 170 * {@hide} 171 */ 172 public static void notify( 173 Object thisReference, int uniqueId, int infoType, String message) { 174 DrmManagerClient instance = (DrmManagerClient)((WeakReference)thisReference).get(); 175 176 if (null != instance && null != instance.mInfoHandler) { 177 Message m = instance.mInfoHandler.obtainMessage( 178 InfoHandler.INFO_EVENT_TYPE, uniqueId, infoType, message); 179 instance.mInfoHandler.sendMessage(m); 180 } 181 } 182 183 private class InfoHandler extends Handler { 184 public static final int INFO_EVENT_TYPE = 1; 185 186 public InfoHandler(Looper looper) { 187 super(looper); 188 } 189 190 public void handleMessage(Message msg) { 191 DrmInfoEvent info = null; 192 DrmErrorEvent error = null; 193 194 switch (msg.what) { 195 case InfoHandler.INFO_EVENT_TYPE: 196 int uniqueId = msg.arg1; 197 int infoType = msg.arg2; 198 String message = msg.obj.toString(); 199 200 switch (infoType) { 201 case DrmInfoEvent.TYPE_REMOVE_RIGHTS: { 202 try { 203 DrmUtils.removeFile(message); 204 } catch (IOException e) { 205 e.printStackTrace(); 206 } 207 info = new DrmInfoEvent(uniqueId, infoType, message); 208 break; 209 } 210 case DrmInfoEvent.TYPE_ALREADY_REGISTERED_BY_ANOTHER_ACCOUNT: 211 case DrmInfoEvent.TYPE_RIGHTS_INSTALLED: 212 case DrmInfoEvent.TYPE_WAIT_FOR_RIGHTS: 213 case DrmInfoEvent.TYPE_ACCOUNT_ALREADY_REGISTERED: 214 case DrmInfoEvent.TYPE_RIGHTS_REMOVED: { 215 info = new DrmInfoEvent(uniqueId, infoType, message); 216 break; 217 } 218 default: 219 error = new DrmErrorEvent(uniqueId, infoType, message); 220 break; 221 } 222 223 if (null != mOnInfoListener && null != info) { 224 mOnInfoListener.onInfo(DrmManagerClient.this, info); 225 } 226 if (null != mOnErrorListener && null != error) { 227 mOnErrorListener.onError(DrmManagerClient.this, error); 228 } 229 return; 230 default: 231 Log.e(TAG, "Unknown message type " + msg.what); 232 return; 233 } 234 } 235 } 236 237 /** 238 * Creates a <code>DrmManagerClient</code>. 239 * 240 * @param context Context of the caller. 241 */ 242 public DrmManagerClient(Context context) { 243 mContext = context; 244 mReleased = false; 245 246 // save the unique id 247 mUniqueId = _initialize(); 248 } 249 250 protected void finalize() { 251 if (!mReleased) { 252 Log.w(TAG, "You should have called release()"); 253 release(); 254 } 255 } 256 257 /** 258 * Releases resources associated with the current session of DrmManagerClient. 259 * 260 * It is considered good practice to call this method when the {@link DrmManagerClient} object 261 * is no longer needed in your application. After release() is called, 262 * {@link DrmManagerClient} is no longer usable since it has lost all of its required resource. 263 */ 264 public void release() { 265 if (mReleased) { 266 Log.w(TAG, "You have already called release()"); 267 return; 268 } 269 mReleased = true; 270 if (mEventHandler != null) { 271 mEventThread.quit(); 272 mEventThread = null; 273 } 274 if (mInfoHandler != null) { 275 mInfoThread.quit(); 276 mInfoThread = null; 277 } 278 mEventHandler = null; 279 mInfoHandler = null; 280 mOnEventListener = null; 281 mOnInfoListener = null; 282 mOnErrorListener = null; 283 _release(mUniqueId); 284 } 285 286 287 private void createListeners() { 288 if (mEventHandler == null && mInfoHandler == null) { 289 mInfoThread = new HandlerThread("DrmManagerClient.InfoHandler"); 290 mInfoThread.start(); 291 mInfoHandler = new InfoHandler(mInfoThread.getLooper()); 292 293 mEventThread = new HandlerThread("DrmManagerClient.EventHandler"); 294 mEventThread.start(); 295 mEventHandler = new EventHandler(mEventThread.getLooper()); 296 _setListeners(mUniqueId, new WeakReference<DrmManagerClient>(this)); 297 } 298 } 299 300 301 /** 302 * Registers an {@link DrmManagerClient.OnInfoListener} callback, which is invoked when the 303 * DRM framework sends status or warning information during registration or rights acquisition. 304 * 305 * @param infoListener Interface definition for the callback. 306 */ 307 public synchronized void setOnInfoListener(OnInfoListener infoListener) { 308 mOnInfoListener = infoListener; 309 if (null != infoListener) { 310 createListeners(); 311 } 312 } 313 314 /** 315 * Registers an {@link DrmManagerClient.OnEventListener} callback, which is invoked when the 316 * DRM framework sends information about DRM processing. 317 * 318 * @param eventListener Interface definition for the callback. 319 */ 320 public synchronized void setOnEventListener(OnEventListener eventListener) { 321 mOnEventListener = eventListener; 322 if (null != eventListener) { 323 createListeners(); 324 } 325 } 326 327 /** 328 * Registers an {@link DrmManagerClient.OnErrorListener} callback, which is invoked when 329 * the DRM framework sends error information. 330 * 331 * @param errorListener Interface definition for the callback. 332 */ 333 public synchronized void setOnErrorListener(OnErrorListener errorListener) { 334 mOnErrorListener = errorListener; 335 if (null != errorListener) { 336 createListeners(); 337 } 338 } 339 340 /** 341 * Retrieves information about all the DRM plug-ins (agents) that are registered with 342 * the DRM framework. 343 * 344 * @return A <code>String</code> array of DRM plug-in descriptions. 345 */ 346 public String[] getAvailableDrmEngines() { 347 DrmSupportInfo[] supportInfos = _getAllSupportInfo(mUniqueId); 348 ArrayList<String> descriptions = new ArrayList<String>(); 349 350 for (int i = 0; i < supportInfos.length; i++) { 351 descriptions.add(supportInfos[i].getDescriprition()); 352 } 353 354 String[] drmEngines = new String[descriptions.size()]; 355 return descriptions.toArray(drmEngines); 356 } 357 358 /** 359 * Retrieves constraint information for rights-protected content. 360 * 361 * @param path Path to the content from which you are retrieving DRM constraints. 362 * @param action Action defined in {@link DrmStore.Action}. 363 * 364 * @return A {@link android.content.ContentValues} instance that contains 365 * key-value pairs representing the constraints. Null in case of failure. 366 */ 367 public ContentValues getConstraints(String path, int action) { 368 if (null == path || path.equals("") || !DrmStore.Action.isValid(action)) { 369 throw new IllegalArgumentException("Given usage or path is invalid/null"); 370 } 371 return _getConstraints(mUniqueId, path, action); 372 } 373 374 /** 375 * Retrieves metadata information for rights-protected content. 376 * 377 * @param path Path to the content from which you are retrieving metadata information. 378 * 379 * @return A {@link android.content.ContentValues} instance that contains 380 * key-value pairs representing the metadata. Null in case of failure. 381 */ 382 public ContentValues getMetadata(String path) { 383 if (null == path || path.equals("")) { 384 throw new IllegalArgumentException("Given path is invalid/null"); 385 } 386 return _getMetadata(mUniqueId, path); 387 } 388 389 /** 390 * Retrieves constraint information for rights-protected content. 391 * 392 * @param uri URI for the content from which you are retrieving DRM constraints. 393 * @param action Action defined in {@link DrmStore.Action}. 394 * 395 * @return A {@link android.content.ContentValues} instance that contains 396 * key-value pairs representing the constraints. Null in case of failure. 397 */ 398 public ContentValues getConstraints(Uri uri, int action) { 399 if (null == uri || Uri.EMPTY == uri) { 400 throw new IllegalArgumentException("Uri should be non null"); 401 } 402 return getConstraints(convertUriToPath(uri), action); 403 } 404 405 /** 406 * Retrieves metadata information for rights-protected content. 407 * 408 * @param uri URI for the content from which you are retrieving metadata information. 409 * 410 * @return A {@link android.content.ContentValues} instance that contains 411 * key-value pairs representing the constraints. Null in case of failure. 412 */ 413 public ContentValues getMetadata(Uri uri) { 414 if (null == uri || Uri.EMPTY == uri) { 415 throw new IllegalArgumentException("Uri should be non null"); 416 } 417 return getMetadata(convertUriToPath(uri)); 418 } 419 420 /** 421 * Saves rights to a specified path and associates that path with the content path. 422 * 423 * <p class="note"><strong>Note:</strong> For OMA or WM-DRM, <code>rightsPath</code> and 424 * <code>contentPath</code> can be null.</p> 425 * 426 * @param drmRights The {@link DrmRights} to be saved. 427 * @param rightsPath File path where rights will be saved. 428 * @param contentPath File path where content is saved. 429 * 430 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 431 * 432 * @throws IOException If the call failed to save rights information at the given 433 * <code>rightsPath</code>. 434 */ 435 public int saveRights( 436 DrmRights drmRights, String rightsPath, String contentPath) throws IOException { 437 if (null == drmRights || !drmRights.isValid()) { 438 throw new IllegalArgumentException("Given drmRights or contentPath is not valid"); 439 } 440 if (null != rightsPath && !rightsPath.equals("")) { 441 DrmUtils.writeToFile(rightsPath, drmRights.getData()); 442 } 443 return _saveRights(mUniqueId, drmRights, rightsPath, contentPath); 444 } 445 446 /** 447 * Installs a new DRM plug-in (agent) at runtime. 448 * 449 * @param engineFilePath File path to the plug-in file to be installed. 450 * 451 * {@hide} 452 */ 453 public void installDrmEngine(String engineFilePath) { 454 if (null == engineFilePath || engineFilePath.equals("")) { 455 throw new IllegalArgumentException( 456 "Given engineFilePath: "+ engineFilePath + "is not valid"); 457 } 458 _installDrmEngine(mUniqueId, engineFilePath); 459 } 460 461 /** 462 * Checks whether the given MIME type or path can be handled. 463 * 464 * @param path Path of the content to be handled. 465 * @param mimeType MIME type of the object to be handled. 466 * 467 * @return True if the given MIME type or path can be handled; false if they cannot be handled. 468 */ 469 public boolean canHandle(String path, String mimeType) { 470 if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) { 471 throw new IllegalArgumentException("Path or the mimetype should be non null"); 472 } 473 return _canHandle(mUniqueId, path, mimeType); 474 } 475 476 /** 477 * Checks whether the given MIME type or URI can be handled. 478 * 479 * @param uri URI for the content to be handled. 480 * @param mimeType MIME type of the object to be handled 481 * 482 * @return True if the given MIME type or URI can be handled; false if they cannot be handled. 483 */ 484 public boolean canHandle(Uri uri, String mimeType) { 485 if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) { 486 throw new IllegalArgumentException("Uri or the mimetype should be non null"); 487 } 488 return canHandle(convertUriToPath(uri), mimeType); 489 } 490 491 /** 492 * Processes the given DRM information based on the information type. 493 * 494 * @param drmInfo The {@link DrmInfo} to be processed. 495 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 496 */ 497 public int processDrmInfo(DrmInfo drmInfo) { 498 if (null == drmInfo || !drmInfo.isValid()) { 499 throw new IllegalArgumentException("Given drmInfo is invalid/null"); 500 } 501 int result = ERROR_UNKNOWN; 502 if (null != mEventHandler) { 503 Message msg = mEventHandler.obtainMessage(ACTION_PROCESS_DRM_INFO, drmInfo); 504 result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result; 505 } 506 return result; 507 } 508 509 /** 510 * Retrieves information for registering, unregistering, or acquiring rights. 511 * 512 * @param drmInfoRequest The {@link DrmInfoRequest} that specifies the type of DRM 513 * information being retrieved. 514 * 515 * @return A {@link DrmInfo} instance. 516 */ 517 public DrmInfo acquireDrmInfo(DrmInfoRequest drmInfoRequest) { 518 if (null == drmInfoRequest || !drmInfoRequest.isValid()) { 519 throw new IllegalArgumentException("Given drmInfoRequest is invalid/null"); 520 } 521 return _acquireDrmInfo(mUniqueId, drmInfoRequest); 522 } 523 524 /** 525 * Processes a given {@link DrmInfoRequest} and returns the rights information asynchronously. 526 *<p> 527 * This is a utility method that consists of an 528 * {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and a 529 * {@link #processDrmInfo(DrmInfo) processDrmInfo()} method call. This utility method can be 530 * used only if the selected DRM plug-in (agent) supports this sequence of calls. Some DRM 531 * agents, such as OMA, do not support this utility method, in which case an application must 532 * invoke {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and 533 * {@link #processDrmInfo(DrmInfo) processDrmInfo()} separately. 534 * 535 * @param drmInfoRequest The {@link DrmInfoRequest} used to acquire the rights. 536 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 537 */ 538 public int acquireRights(DrmInfoRequest drmInfoRequest) { 539 DrmInfo drmInfo = acquireDrmInfo(drmInfoRequest); 540 if (null == drmInfo) { 541 return ERROR_UNKNOWN; 542 } 543 return processDrmInfo(drmInfo); 544 } 545 546 /** 547 * Retrieves the type of rights-protected object (for example, content object, rights 548 * object, and so on) using the specified path or MIME type. At least one parameter must 549 * be specified to retrieve the DRM object type. 550 * 551 * @param path Path to the content or null. 552 * @param mimeType MIME type of the content or null. 553 * 554 * @return An <code>int</code> that corresponds to a {@link DrmStore.DrmObjectType}. 555 */ 556 public int getDrmObjectType(String path, String mimeType) { 557 if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) { 558 throw new IllegalArgumentException("Path or the mimetype should be non null"); 559 } 560 return _getDrmObjectType(mUniqueId, path, mimeType); 561 } 562 563 /** 564 * Retrieves the type of rights-protected object (for example, content object, rights 565 * object, and so on) using the specified URI or MIME type. At least one parameter must 566 * be specified to retrieve the DRM object type. 567 * 568 * @param uri URI for the content or null. 569 * @param mimeType MIME type of the content or null. 570 * 571 * @return An <code>int</code> that corresponds to a {@link DrmStore.DrmObjectType}. 572 */ 573 public int getDrmObjectType(Uri uri, String mimeType) { 574 if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) { 575 throw new IllegalArgumentException("Uri or the mimetype should be non null"); 576 } 577 String path = ""; 578 try { 579 path = convertUriToPath(uri); 580 } catch (Exception e) { 581 // Even uri is invalid the mimetype shall be valid, so allow to proceed further. 582 Log.w(TAG, "Given Uri could not be found in media store"); 583 } 584 return getDrmObjectType(path, mimeType); 585 } 586 587 /** 588 * Retrieves the MIME type embedded in the original content. 589 * 590 * @param path Path to the rights-protected content. 591 * 592 * @return The MIME type of the original content, such as <code>video/mpeg</code>. 593 */ 594 public String getOriginalMimeType(String path) { 595 if (null == path || path.equals("")) { 596 throw new IllegalArgumentException("Given path should be non null"); 597 } 598 return _getOriginalMimeType(mUniqueId, path); 599 } 600 601 /** 602 * Retrieves the MIME type embedded in the original content. 603 * 604 * @param uri URI of the rights-protected content. 605 * 606 * @return MIME type of the original content, such as <code>video/mpeg</code>. 607 */ 608 public String getOriginalMimeType(Uri uri) { 609 if (null == uri || Uri.EMPTY == uri) { 610 throw new IllegalArgumentException("Given uri is not valid"); 611 } 612 return getOriginalMimeType(convertUriToPath(uri)); 613 } 614 615 /** 616 * Checks whether the given content has valid rights. 617 * 618 * @param path Path to the rights-protected content. 619 * 620 * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content. 621 */ 622 public int checkRightsStatus(String path) { 623 return checkRightsStatus(path, DrmStore.Action.DEFAULT); 624 } 625 626 /** 627 * Check whether the given content has valid rights. 628 * 629 * @param uri URI of the rights-protected content. 630 * 631 * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content. 632 */ 633 public int checkRightsStatus(Uri uri) { 634 if (null == uri || Uri.EMPTY == uri) { 635 throw new IllegalArgumentException("Given uri is not valid"); 636 } 637 return checkRightsStatus(convertUriToPath(uri)); 638 } 639 640 /** 641 * Checks whether the given rights-protected content has valid rights for the specified 642 * {@link DrmStore.Action}. 643 * 644 * @param path Path to the rights-protected content. 645 * @param action The {@link DrmStore.Action} to perform. 646 * 647 * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content. 648 */ 649 public int checkRightsStatus(String path, int action) { 650 if (null == path || path.equals("") || !DrmStore.Action.isValid(action)) { 651 throw new IllegalArgumentException("Given path or action is not valid"); 652 } 653 return _checkRightsStatus(mUniqueId, path, action); 654 } 655 656 /** 657 * Checks whether the given rights-protected content has valid rights for the specified 658 * {@link DrmStore.Action}. 659 * 660 * @param uri URI for the rights-protected content. 661 * @param action The {@link DrmStore.Action} to perform. 662 * 663 * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content. 664 */ 665 public int checkRightsStatus(Uri uri, int action) { 666 if (null == uri || Uri.EMPTY == uri) { 667 throw new IllegalArgumentException("Given uri is not valid"); 668 } 669 return checkRightsStatus(convertUriToPath(uri), action); 670 } 671 672 /** 673 * Removes the rights associated with the given rights-protected content. 674 * 675 * @param path Path to the rights-protected content. 676 * 677 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 678 */ 679 public int removeRights(String path) { 680 if (null == path || path.equals("")) { 681 throw new IllegalArgumentException("Given path should be non null"); 682 } 683 return _removeRights(mUniqueId, path); 684 } 685 686 /** 687 * Removes the rights associated with the given rights-protected content. 688 * 689 * @param uri URI for the rights-protected content. 690 * 691 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 692 */ 693 public int removeRights(Uri uri) { 694 if (null == uri || Uri.EMPTY == uri) { 695 throw new IllegalArgumentException("Given uri is not valid"); 696 } 697 return removeRights(convertUriToPath(uri)); 698 } 699 700 /** 701 * Removes all the rights information of every DRM plug-in (agent) associated with 702 * the DRM framework. Will be used during a master reset. 703 * 704 * @return ERROR_NONE for success; ERROR_UNKNOWN for failure. 705 */ 706 public int removeAllRights() { 707 int result = ERROR_UNKNOWN; 708 if (null != mEventHandler) { 709 Message msg = mEventHandler.obtainMessage(ACTION_REMOVE_ALL_RIGHTS); 710 result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result; 711 } 712 return result; 713 } 714 715 /** 716 * Initiates a new conversion session. An application must initiate a conversion session 717 * with this method each time it downloads a rights-protected file that needs to be converted. 718 *<p> 719 * This method applies only to forward-locking (copy protection) DRM schemes. 720 * 721 * @param mimeType MIME type of the input data packet. 722 * 723 * @return A convert ID that is used used to maintain the conversion session. 724 */ 725 public int openConvertSession(String mimeType) { 726 if (null == mimeType || mimeType.equals("")) { 727 throw new IllegalArgumentException("Path or the mimeType should be non null"); 728 } 729 return _openConvertSession(mUniqueId, mimeType); 730 } 731 732 /** 733 * Converts the input data (content) that is part of a rights-protected file. The converted 734 * data and status is returned in a {@link DrmConvertedStatus} object. This method should be 735 * called each time there is a new block of data received by the application. 736 * 737 * @param convertId Handle for the conversion session. 738 * @param inputData Input data that needs to be converted. 739 * 740 * @return A {@link DrmConvertedStatus} object that contains the status of the data conversion, 741 * the converted data, and offset for the header and body signature. An application can 742 * ignore the offset because it is only relevant to the 743 * {@link #closeConvertSession closeConvertSession()} method. 744 */ 745 public DrmConvertedStatus convertData(int convertId, byte[] inputData) { 746 if (null == inputData || 0 >= inputData.length) { 747 throw new IllegalArgumentException("Given inputData should be non null"); 748 } 749 return _convertData(mUniqueId, convertId, inputData); 750 } 751 752 /** 753 * Informs the DRM plug-in (agent) that there is no more data to convert or that an error 754 * has occurred. Upon successful conversion of the data, the DRM agent will provide an offset 755 * value indicating where the header and body signature should be added. Appending the 756 * signature is necessary to protect the integrity of the converted file. 757 * 758 * @param convertId Handle for the conversion session. 759 * 760 * @return A {@link DrmConvertedStatus} object that contains the status of the data conversion, 761 * the converted data, and the offset for the header and body signature. 762 */ 763 public DrmConvertedStatus closeConvertSession(int convertId) { 764 return _closeConvertSession(mUniqueId, convertId); 765 } 766 767 private int getEventType(int infoType) { 768 int eventType = -1; 769 770 switch (infoType) { 771 case DrmInfoRequest.TYPE_REGISTRATION_INFO: 772 case DrmInfoRequest.TYPE_UNREGISTRATION_INFO: 773 case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO: 774 eventType = DrmEvent.TYPE_DRM_INFO_PROCESSED; 775 break; 776 } 777 return eventType; 778 } 779 780 private int getErrorType(int infoType) { 781 int error = -1; 782 783 switch (infoType) { 784 case DrmInfoRequest.TYPE_REGISTRATION_INFO: 785 case DrmInfoRequest.TYPE_UNREGISTRATION_INFO: 786 case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO: 787 error = DrmErrorEvent.TYPE_PROCESS_DRM_INFO_FAILED; 788 break; 789 } 790 return error; 791 } 792 793 /** 794 * This method expects uri in the following format 795 * content://media/<table_name>/<row_index> (or) 796 * file://sdcard/test.mp4 797 * http://test.com/test.mp4 798 * 799 * Here <table_name> shall be "video" or "audio" or "images" 800 * <row_index> the index of the content in given table 801 */ 802 private String convertUriToPath(Uri uri) { 803 String path = null; 804 if (null != uri) { 805 String scheme = uri.getScheme(); 806 if (null == scheme || scheme.equals("") || 807 scheme.equals(ContentResolver.SCHEME_FILE)) { 808 path = uri.getPath(); 809 810 } else if (scheme.equals("http")) { 811 path = uri.toString(); 812 813 } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) { 814 String[] projection = new String[] {MediaStore.MediaColumns.DATA}; 815 Cursor cursor = null; 816 try { 817 cursor = mContext.getContentResolver().query(uri, projection, null, 818 null, null); 819 if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) { 820 throw new IllegalArgumentException("Given Uri could not be found" + 821 " in media store"); 822 } 823 int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 824 path = cursor.getString(pathIndex); 825 } catch (SQLiteException e) { 826 throw new IllegalArgumentException("Given Uri is not formatted in a way " + 827 "so that it can be found in media store."); 828 } finally { 829 if (null != cursor) { 830 cursor.close(); 831 } 832 } 833 } else { 834 throw new IllegalArgumentException("Given Uri scheme is not supported"); 835 } 836 } 837 return path; 838 } 839 840 // private native interfaces 841 private native int _initialize(); 842 843 private native void _setListeners(int uniqueId, Object weak_this); 844 845 private native void _release(int uniqueId); 846 847 private native void _installDrmEngine(int uniqueId, String engineFilepath); 848 849 private native ContentValues _getConstraints(int uniqueId, String path, int usage); 850 851 private native ContentValues _getMetadata(int uniqueId, String path); 852 853 private native boolean _canHandle(int uniqueId, String path, String mimeType); 854 855 private native DrmInfoStatus _processDrmInfo(int uniqueId, DrmInfo drmInfo); 856 857 private native DrmInfo _acquireDrmInfo(int uniqueId, DrmInfoRequest drmInfoRequest); 858 859 private native int _saveRights( 860 int uniqueId, DrmRights drmRights, String rightsPath, String contentPath); 861 862 private native int _getDrmObjectType(int uniqueId, String path, String mimeType); 863 864 private native String _getOriginalMimeType(int uniqueId, String path); 865 866 private native int _checkRightsStatus(int uniqueId, String path, int action); 867 868 private native int _removeRights(int uniqueId, String path); 869 870 private native int _removeAllRights(int uniqueId); 871 872 private native int _openConvertSession(int uniqueId, String mimeType); 873 874 private native DrmConvertedStatus _convertData( 875 int uniqueId, int convertId, byte[] inputData); 876 877 private native DrmConvertedStatus _closeConvertSession(int uniqueId, int convertId); 878 879 private native DrmSupportInfo[] _getAllSupportInfo(int uniqueId); 880} 881 882