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