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     @Override
close()280     public void close() {
281         mCloseGuard.close();
282         if (mClosed.compareAndSet(false, true)) {
283             if (mEventHandler != null) {
284                 mEventThread.quit();
285                 mEventThread = null;
286             }
287             if (mInfoHandler != null) {
288                 mInfoThread.quit();
289                 mInfoThread = null;
290             }
291             mEventHandler = null;
292             mInfoHandler = null;
293             mOnEventListener = null;
294             mOnInfoListener = null;
295             mOnErrorListener = null;
296             _release(mUniqueId);
297         }
298     }
299 
300     /**
301      * @deprecated replaced by {@link #close()}.
302      */
303     @Deprecated
release()304     public void release() {
305         close();
306     }
307 
308     /**
309      * Registers an {@link DrmManagerClient.OnInfoListener} callback, which is invoked when the
310      * DRM framework sends status or warning information during registration or rights acquisition.
311      *
312      * @param infoListener Interface definition for the callback.
313      */
setOnInfoListener(OnInfoListener infoListener)314     public synchronized void setOnInfoListener(OnInfoListener infoListener) {
315         mOnInfoListener = infoListener;
316         if (null != infoListener) {
317             createListeners();
318         }
319     }
320 
321     /**
322      * Registers an {@link DrmManagerClient.OnEventListener} callback, which is invoked when the
323      * DRM framework sends information about DRM processing.
324      *
325      * @param eventListener Interface definition for the callback.
326      */
setOnEventListener(OnEventListener eventListener)327     public synchronized void setOnEventListener(OnEventListener eventListener) {
328         mOnEventListener = eventListener;
329         if (null != eventListener) {
330             createListeners();
331         }
332     }
333 
334     /**
335      * Registers an {@link DrmManagerClient.OnErrorListener} callback, which is invoked when
336      * the DRM framework sends error information.
337      *
338      * @param errorListener Interface definition for the callback.
339      */
setOnErrorListener(OnErrorListener errorListener)340     public synchronized void setOnErrorListener(OnErrorListener errorListener) {
341         mOnErrorListener = errorListener;
342         if (null != errorListener) {
343             createListeners();
344         }
345     }
346 
347     /**
348      * Retrieves information about all the DRM plug-ins (agents) that are registered with
349      * the DRM framework.
350      *
351      * @return A <code>String</code> array of DRM plug-in descriptions.
352      */
getAvailableDrmEngines()353     public String[] getAvailableDrmEngines() {
354         DrmSupportInfo[] supportInfos = _getAllSupportInfo(mUniqueId);
355         ArrayList<String> descriptions = new ArrayList<String>();
356 
357         for (int i = 0; i < supportInfos.length; i++) {
358             descriptions.add(supportInfos[i].getDescriprition());
359         }
360 
361         String[] drmEngines = new String[descriptions.size()];
362         return descriptions.toArray(drmEngines);
363     }
364 
365     /**
366      * Retrieves constraint information for rights-protected content.
367      *
368      * @param path Path to the content from which you are retrieving DRM constraints.
369      * @param action Action defined in {@link DrmStore.Action}.
370      *
371      * @return A {@link android.content.ContentValues} instance that contains
372      * key-value pairs representing the constraints. Null in case of failure.
373      * The keys are defined in {@link DrmStore.ConstraintsColumns}.
374      */
getConstraints(String path, int action)375     public ContentValues getConstraints(String path, int action) {
376         if (null == path || path.equals("") || !DrmStore.Action.isValid(action)) {
377             throw new IllegalArgumentException("Given usage or path is invalid/null");
378         }
379         return _getConstraints(mUniqueId, path, action);
380     }
381 
382    /**
383     * Retrieves metadata information for rights-protected content.
384     *
385     * @param path Path to the content from which you are retrieving metadata information.
386     *
387     * @return A {@link android.content.ContentValues} instance that contains
388     * key-value pairs representing the metadata. Null in case of failure.
389     */
getMetadata(String path)390     public ContentValues getMetadata(String path) {
391         if (null == path || path.equals("")) {
392             throw new IllegalArgumentException("Given path is invalid/null");
393         }
394         return _getMetadata(mUniqueId, path);
395     }
396 
397     /**
398      * Retrieves constraint information for rights-protected content.
399      *
400      * @param uri URI for the content from which you are retrieving DRM constraints.
401      * @param action Action defined in {@link DrmStore.Action}.
402      *
403      * @return A {@link android.content.ContentValues} instance that contains
404      * key-value pairs representing the constraints. Null in case of failure.
405      */
getConstraints(Uri uri, int action)406     public ContentValues getConstraints(Uri uri, int action) {
407         if (null == uri || Uri.EMPTY == uri) {
408             throw new IllegalArgumentException("Uri should be non null");
409         }
410         return getConstraints(convertUriToPath(uri), action);
411     }
412 
413    /**
414     * Retrieves metadata information for rights-protected content.
415     *
416     * @param uri URI for the content from which you are retrieving metadata information.
417     *
418     * @return A {@link android.content.ContentValues} instance that contains
419     * key-value pairs representing the constraints. Null in case of failure.
420     */
getMetadata(Uri uri)421     public ContentValues getMetadata(Uri uri) {
422         if (null == uri || Uri.EMPTY == uri) {
423             throw new IllegalArgumentException("Uri should be non null");
424         }
425         return getMetadata(convertUriToPath(uri));
426     }
427 
428     /**
429      * Saves rights to a specified path and associates that path with the content path.
430      *
431      * <p class="note"><strong>Note:</strong> For OMA or WM-DRM, <code>rightsPath</code> and
432      * <code>contentPath</code> can be null.</p>
433      *
434      * @param drmRights The {@link DrmRights} to be saved.
435      * @param rightsPath File path where rights will be saved.
436      * @param contentPath File path where content is saved.
437      *
438      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
439      *
440      * @throws IOException If the call failed to save rights information at the given
441      * <code>rightsPath</code>.
442      */
saveRights( DrmRights drmRights, String rightsPath, String contentPath)443     public int saveRights(
444             DrmRights drmRights, String rightsPath, String contentPath) throws IOException {
445         if (null == drmRights || !drmRights.isValid()) {
446             throw new IllegalArgumentException("Given drmRights or contentPath is not valid");
447         }
448         if (null != rightsPath && !rightsPath.equals("")) {
449             DrmUtils.writeToFile(rightsPath, drmRights.getData());
450         }
451         return _saveRights(mUniqueId, drmRights, rightsPath, contentPath);
452     }
453 
454     /**
455      * Installs a new DRM plug-in (agent) at runtime.
456      *
457      * @param engineFilePath File path to the plug-in file to be installed.
458      *
459      * {@hide}
460      */
installDrmEngine(String engineFilePath)461     public void installDrmEngine(String engineFilePath) {
462         if (null == engineFilePath || engineFilePath.equals("")) {
463             throw new IllegalArgumentException(
464                 "Given engineFilePath: "+ engineFilePath + "is not valid");
465         }
466         _installDrmEngine(mUniqueId, engineFilePath);
467     }
468 
469     /**
470      * Checks whether the given MIME type or path can be handled.
471      *
472      * @param path Path of the content to be handled.
473      * @param mimeType MIME type of the object to be handled.
474      *
475      * @return True if the given MIME type or path can be handled; false if they cannot be handled.
476      */
canHandle(String path, String mimeType)477     public boolean canHandle(String path, String mimeType) {
478         if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) {
479             throw new IllegalArgumentException("Path or the mimetype should be non null");
480         }
481         return _canHandle(mUniqueId, path, mimeType);
482     }
483 
484     /**
485      * Checks whether the given MIME type or URI can be handled.
486      *
487      * @param uri URI for the content to be handled.
488      * @param mimeType MIME type of the object to be handled
489      *
490      * @return True if the given MIME type or URI can be handled; false if they cannot be handled.
491      */
canHandle(Uri uri, String mimeType)492     public boolean canHandle(Uri uri, String mimeType) {
493         if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) {
494             throw new IllegalArgumentException("Uri or the mimetype should be non null");
495         }
496         return canHandle(convertUriToPath(uri), mimeType);
497     }
498 
499     /**
500      * Processes the given DRM information based on the information type.
501      *
502      * @param drmInfo The {@link DrmInfo} to be processed.
503      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
504      */
processDrmInfo(DrmInfo drmInfo)505     public int processDrmInfo(DrmInfo drmInfo) {
506         if (null == drmInfo || !drmInfo.isValid()) {
507             throw new IllegalArgumentException("Given drmInfo is invalid/null");
508         }
509         int result = ERROR_UNKNOWN;
510         if (null != mEventHandler) {
511             Message msg = mEventHandler.obtainMessage(ACTION_PROCESS_DRM_INFO, drmInfo);
512             result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result;
513         }
514         return result;
515     }
516 
517     /**
518      * Retrieves information for registering, unregistering, or acquiring rights.
519      *
520      * @param drmInfoRequest The {@link DrmInfoRequest} that specifies the type of DRM
521      * information being retrieved.
522      *
523      * @return A {@link DrmInfo} instance.
524      */
acquireDrmInfo(DrmInfoRequest drmInfoRequest)525     public DrmInfo acquireDrmInfo(DrmInfoRequest drmInfoRequest) {
526         if (null == drmInfoRequest || !drmInfoRequest.isValid()) {
527             throw new IllegalArgumentException("Given drmInfoRequest is invalid/null");
528         }
529         return _acquireDrmInfo(mUniqueId, drmInfoRequest);
530     }
531 
532     /**
533      * Processes a given {@link DrmInfoRequest} and returns the rights information asynchronously.
534      *<p>
535      * This is a utility method that consists of an
536      * {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and a
537      * {@link #processDrmInfo(DrmInfo) processDrmInfo()} method call. This utility method can be
538      * used only if the selected DRM plug-in (agent) supports this sequence of calls. Some DRM
539      * agents, such as OMA, do not support this utility method, in which case an application must
540      * invoke {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and
541      * {@link #processDrmInfo(DrmInfo) processDrmInfo()} separately.
542      *
543      * @param drmInfoRequest The {@link DrmInfoRequest} used to acquire the rights.
544      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
545      */
acquireRights(DrmInfoRequest drmInfoRequest)546     public int acquireRights(DrmInfoRequest drmInfoRequest) {
547         DrmInfo drmInfo = acquireDrmInfo(drmInfoRequest);
548         if (null == drmInfo) {
549             return ERROR_UNKNOWN;
550         }
551         return processDrmInfo(drmInfo);
552     }
553 
554     /**
555      * Retrieves the type of rights-protected object (for example, content object, rights
556      * object, and so on) using the specified path or MIME type. At least one parameter must
557      * be specified to retrieve the DRM object type.
558      *
559      * @param path Path to the content or null.
560      * @param mimeType MIME type of the content or null.
561      *
562      * @return An <code>int</code> that corresponds to a {@link DrmStore.DrmObjectType}.
563      */
getDrmObjectType(String path, String mimeType)564     public int getDrmObjectType(String path, String mimeType) {
565         if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) {
566             throw new IllegalArgumentException("Path or the mimetype should be non null");
567         }
568         return _getDrmObjectType(mUniqueId, path, mimeType);
569     }
570 
571     /**
572      * Retrieves the type of rights-protected object (for example, content object, rights
573      * object, and so on) using the specified URI or MIME type. At least one parameter must
574      * be specified to retrieve the DRM object type.
575      *
576      * @param uri URI for the content or null.
577      * @param mimeType MIME type of the content or null.
578      *
579      * @return An <code>int</code> that corresponds to a {@link DrmStore.DrmObjectType}.
580      */
getDrmObjectType(Uri uri, String mimeType)581     public int getDrmObjectType(Uri uri, String mimeType) {
582         if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) {
583             throw new IllegalArgumentException("Uri or the mimetype should be non null");
584         }
585         String path = "";
586         try {
587             path = convertUriToPath(uri);
588         } catch (Exception e) {
589             // Even uri is invalid the mimetype shall be valid, so allow to proceed further.
590             Log.w(TAG, "Given Uri could not be found in media store");
591         }
592         return getDrmObjectType(path, mimeType);
593     }
594 
595     /**
596      * Retrieves the MIME type embedded in the original content.
597      *
598      * @param path Path to the rights-protected content.
599      *
600      * @return The MIME type of the original content, such as <code>video/mpeg</code>.
601      */
getOriginalMimeType(String path)602     public String getOriginalMimeType(String path) {
603         if (null == path || path.equals("")) {
604             throw new IllegalArgumentException("Given path should be non null");
605         }
606 
607         String mime = null;
608 
609         FileInputStream is = null;
610         try {
611             FileDescriptor fd = null;
612             File file = new File(path);
613             if (file.exists()) {
614                 is = new FileInputStream(file);
615                 fd = is.getFD();
616             }
617             mime = _getOriginalMimeType(mUniqueId, path, fd);
618         } catch (IOException ioe) {
619         } finally {
620             if (is != null) {
621                 try {
622                     is.close();
623                 } catch(IOException e) {}
624             }
625         }
626 
627         return mime;
628     }
629 
630     /**
631      * Retrieves the MIME type embedded in the original content.
632      *
633      * @param uri URI of the rights-protected content.
634      *
635      * @return MIME type of the original content, such as <code>video/mpeg</code>.
636      */
getOriginalMimeType(Uri uri)637     public String getOriginalMimeType(Uri uri) {
638         if (null == uri || Uri.EMPTY == uri) {
639             throw new IllegalArgumentException("Given uri is not valid");
640         }
641         return getOriginalMimeType(convertUriToPath(uri));
642     }
643 
644     /**
645      * Checks whether the given content has valid rights.
646      *
647      * @param path Path to the rights-protected content.
648      *
649      * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content.
650      */
checkRightsStatus(String path)651     public int checkRightsStatus(String path) {
652         return checkRightsStatus(path, DrmStore.Action.DEFAULT);
653     }
654 
655     /**
656      * Check whether the given content has valid rights.
657      *
658      * @param uri URI of the rights-protected content.
659      *
660      * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content.
661      */
checkRightsStatus(Uri uri)662     public int checkRightsStatus(Uri uri) {
663         if (null == uri || Uri.EMPTY == uri) {
664             throw new IllegalArgumentException("Given uri is not valid");
665         }
666         return checkRightsStatus(convertUriToPath(uri));
667     }
668 
669     /**
670      * Checks whether the given rights-protected content has valid rights for the specified
671      * {@link DrmStore.Action}.
672      *
673      * @param path Path to the rights-protected content.
674      * @param action The {@link DrmStore.Action} to perform.
675      *
676      * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content.
677      */
checkRightsStatus(String path, int action)678     public int checkRightsStatus(String path, int action) {
679         if (null == path || path.equals("") || !DrmStore.Action.isValid(action)) {
680             throw new IllegalArgumentException("Given path or action is not valid");
681         }
682         return _checkRightsStatus(mUniqueId, path, action);
683     }
684 
685     /**
686      * Checks whether the given rights-protected content has valid rights for the specified
687      * {@link DrmStore.Action}.
688      *
689      * @param uri URI for the rights-protected content.
690      * @param action The {@link DrmStore.Action} to perform.
691      *
692      * @return An <code>int</code> representing the {@link DrmStore.RightsStatus} of the content.
693      */
checkRightsStatus(Uri uri, int action)694     public int checkRightsStatus(Uri uri, int action) {
695         if (null == uri || Uri.EMPTY == uri) {
696             throw new IllegalArgumentException("Given uri is not valid");
697         }
698         return checkRightsStatus(convertUriToPath(uri), action);
699     }
700 
701     /**
702      * Removes the rights associated with the given rights-protected content.
703      *
704      * @param path Path to the rights-protected content.
705      *
706      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
707      */
removeRights(String path)708     public int removeRights(String path) {
709         if (null == path || path.equals("")) {
710             throw new IllegalArgumentException("Given path should be non null");
711         }
712         return _removeRights(mUniqueId, path);
713     }
714 
715     /**
716      * Removes the rights associated with the given rights-protected content.
717      *
718      * @param uri URI for the rights-protected content.
719      *
720      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
721      */
removeRights(Uri uri)722     public int removeRights(Uri uri) {
723         if (null == uri || Uri.EMPTY == uri) {
724             throw new IllegalArgumentException("Given uri is not valid");
725         }
726         return removeRights(convertUriToPath(uri));
727     }
728 
729     /**
730      * Removes all the rights information of every DRM plug-in (agent) associated with
731      * the DRM framework. Will be used during a master reset.
732      *
733      * @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
734      */
removeAllRights()735     public int removeAllRights() {
736         int result = ERROR_UNKNOWN;
737         if (null != mEventHandler) {
738             Message msg = mEventHandler.obtainMessage(ACTION_REMOVE_ALL_RIGHTS);
739             result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result;
740         }
741         return result;
742     }
743 
744     /**
745      * Initiates a new conversion session. An application must initiate a conversion session
746      * with this method each time it downloads a rights-protected file that needs to be converted.
747      *<p>
748      * This method applies only to forward-locking (copy protection) DRM schemes.
749      *
750      * @param mimeType MIME type of the input data packet.
751      *
752      * @return A convert ID that is used used to maintain the conversion session.
753      */
openConvertSession(String mimeType)754     public int openConvertSession(String mimeType) {
755         if (null == mimeType || mimeType.equals("")) {
756             throw new IllegalArgumentException("Path or the mimeType should be non null");
757         }
758         return _openConvertSession(mUniqueId, mimeType);
759     }
760 
761     /**
762      * Converts the input data (content) that is part of a rights-protected file. The converted
763      * data and status is returned in a {@link DrmConvertedStatus} object. This method should be
764      * called each time there is a new block of data received by the application.
765      *
766      * @param convertId Handle for the conversion session.
767      * @param inputData Input data that needs to be converted.
768      *
769      * @return A {@link DrmConvertedStatus} object that contains the status of the data conversion,
770      * the converted data, and offset for the header and body signature. An application can
771      * ignore the offset because it is only relevant to the
772      * {@link #closeConvertSession closeConvertSession()} method.
773      */
convertData(int convertId, byte[] inputData)774     public DrmConvertedStatus convertData(int convertId, byte[] inputData) {
775         if (null == inputData || 0 >= inputData.length) {
776             throw new IllegalArgumentException("Given inputData should be non null");
777         }
778         return _convertData(mUniqueId, convertId, inputData);
779     }
780 
781     /**
782      * Informs the DRM plug-in (agent) that there is no more data to convert or that an error
783      * has occurred. Upon successful conversion of the data, the DRM agent will provide an offset
784      * value indicating where the header and body signature should be added. Appending the
785      * signature is necessary to protect the integrity of the converted file.
786      *
787      * @param convertId Handle for the conversion session.
788      *
789      * @return A {@link DrmConvertedStatus} object that contains the status of the data conversion,
790      * the converted data, and the offset for the header and body signature.
791      */
closeConvertSession(int convertId)792     public DrmConvertedStatus closeConvertSession(int convertId) {
793         return _closeConvertSession(mUniqueId, convertId);
794     }
795 
getEventType(int infoType)796     private int getEventType(int infoType) {
797         int eventType = -1;
798 
799         switch (infoType) {
800         case DrmInfoRequest.TYPE_REGISTRATION_INFO:
801         case DrmInfoRequest.TYPE_UNREGISTRATION_INFO:
802         case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO:
803             eventType = DrmEvent.TYPE_DRM_INFO_PROCESSED;
804             break;
805         }
806         return eventType;
807     }
808 
getErrorType(int infoType)809     private int getErrorType(int infoType) {
810         int error = -1;
811 
812         switch (infoType) {
813         case DrmInfoRequest.TYPE_REGISTRATION_INFO:
814         case DrmInfoRequest.TYPE_UNREGISTRATION_INFO:
815         case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO:
816             error = DrmErrorEvent.TYPE_PROCESS_DRM_INFO_FAILED;
817             break;
818         }
819         return error;
820     }
821 
822     /**
823      * This method expects uri in the following format
824      *     content://media/<table_name>/<row_index> (or)
825      *     file://sdcard/test.mp4
826      *     http://test.com/test.mp4
827      *
828      * Here <table_name> shall be "video" or "audio" or "images"
829      * <row_index> the index of the content in given table
830      */
convertUriToPath(Uri uri)831     private String convertUriToPath(Uri uri) {
832         String path = null;
833         if (null != uri) {
834             String scheme = uri.getScheme();
835             if (null == scheme || scheme.equals("") ||
836                     scheme.equals(ContentResolver.SCHEME_FILE)) {
837                 path = uri.getPath();
838 
839             } else if (scheme.equals("http")) {
840                 path = uri.toString();
841 
842             } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
843                 String[] projection = new String[] {MediaStore.MediaColumns.DATA};
844                 Cursor cursor = null;
845                 try {
846                     cursor = mContext.getContentResolver().query(uri, projection, null,
847                             null, null);
848                     if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
849                         throw new IllegalArgumentException("Given Uri could not be found" +
850                                 " in media store");
851                     }
852                     int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
853                     path = cursor.getString(pathIndex);
854                 } catch (SQLiteException e) {
855                     throw new IllegalArgumentException("Given Uri is not formatted in a way " +
856                             "so that it can be found in media store.");
857                 } finally {
858                     if (null != cursor) {
859                         cursor.close();
860                     }
861                 }
862             } else {
863                 throw new IllegalArgumentException("Given Uri scheme is not supported");
864             }
865         }
866         return path;
867     }
868 
869     // private native interfaces
_initialize()870     private native int _initialize();
871 
_setListeners(int uniqueId, Object weak_this)872     private native void _setListeners(int uniqueId, Object weak_this);
873 
_release(int uniqueId)874     private native void _release(int uniqueId);
875 
_installDrmEngine(int uniqueId, String engineFilepath)876     private native void _installDrmEngine(int uniqueId, String engineFilepath);
877 
_getConstraints(int uniqueId, String path, int usage)878     private native ContentValues _getConstraints(int uniqueId, String path, int usage);
879 
_getMetadata(int uniqueId, String path)880     private native ContentValues _getMetadata(int uniqueId, String path);
881 
_canHandle(int uniqueId, String path, String mimeType)882     private native boolean _canHandle(int uniqueId, String path, String mimeType);
883 
_processDrmInfo(int uniqueId, DrmInfo drmInfo)884     private native DrmInfoStatus _processDrmInfo(int uniqueId, DrmInfo drmInfo);
885 
_acquireDrmInfo(int uniqueId, DrmInfoRequest drmInfoRequest)886     private native DrmInfo _acquireDrmInfo(int uniqueId, DrmInfoRequest drmInfoRequest);
887 
_saveRights( int uniqueId, DrmRights drmRights, String rightsPath, String contentPath)888     private native int _saveRights(
889             int uniqueId, DrmRights drmRights, String rightsPath, String contentPath);
890 
_getDrmObjectType(int uniqueId, String path, String mimeType)891     private native int _getDrmObjectType(int uniqueId, String path, String mimeType);
892 
_getOriginalMimeType(int uniqueId, String path, FileDescriptor fd)893     private native String _getOriginalMimeType(int uniqueId, String path, FileDescriptor fd);
894 
_checkRightsStatus(int uniqueId, String path, int action)895     private native int _checkRightsStatus(int uniqueId, String path, int action);
896 
_removeRights(int uniqueId, String path)897     private native int _removeRights(int uniqueId, String path);
898 
_removeAllRights(int uniqueId)899     private native int _removeAllRights(int uniqueId);
900 
_openConvertSession(int uniqueId, String mimeType)901     private native int _openConvertSession(int uniqueId, String mimeType);
902 
_convertData( int uniqueId, int convertId, byte[] inputData)903     private native DrmConvertedStatus _convertData(
904             int uniqueId, int convertId, byte[] inputData);
905 
_closeConvertSession(int uniqueId, int convertId)906     private native DrmConvertedStatus _closeConvertSession(int uniqueId, int convertId);
907 
_getAllSupportInfo(int uniqueId)908     private native DrmSupportInfo[] _getAllSupportInfo(int uniqueId);
909 
createEventThreads()910     private void createEventThreads() {
911         if (mEventHandler == null && mInfoHandler == null) {
912             mInfoThread = new HandlerThread("DrmManagerClient.InfoHandler");
913             mInfoThread.start();
914             mInfoHandler = new InfoHandler(mInfoThread.getLooper());
915 
916             mEventThread = new HandlerThread("DrmManagerClient.EventHandler");
917             mEventThread.start();
918             mEventHandler = new EventHandler(mEventThread.getLooper());
919         }
920     }
921 
createListeners()922     private void createListeners() {
923         _setListeners(mUniqueId, new WeakReference<DrmManagerClient>(this));
924     }
925 }
926 
927