DrmManagerClient
instance.
* @param event The {@link DrmInfoEvent} instance that wraps the status information or
* warnings.
*/
public void onInfo(DrmManagerClient client, DrmInfoEvent event);
}
/**
* Interface definition for a callback that receives information
* about DRM processing events.
*/
public interface OnEventListener {
/**
* Called when the DRM framework sends information about a DRM processing request.
*
* @param client The DrmManagerClient
instance.
* @param event The {@link DrmEvent} instance that wraps the information being
* conveyed, such as the information type and message.
*/
public void onEvent(DrmManagerClient client, DrmEvent event);
}
/**
* Interface definition for a callback that receives information about DRM framework errors.
*/
public interface OnErrorListener {
/**
* Called when the DRM framework sends error information.
*
* @param client The DrmManagerClient
instance.
* @param event The {@link DrmErrorEvent} instance that wraps the error type and message.
*/
public void onError(DrmManagerClient client, DrmErrorEvent event);
}
private static final int ACTION_REMOVE_ALL_RIGHTS = 1001;
private static final int ACTION_PROCESS_DRM_INFO = 1002;
private int mUniqueId;
private long mNativeContext;
private Context mContext;
private InfoHandler mInfoHandler;
private EventHandler mEventHandler;
private OnInfoListener mOnInfoListener;
private OnEventListener mOnEventListener;
private OnErrorListener mOnErrorListener;
private class EventHandler extends Handler {
public EventHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
DrmEvent event = null;
DrmErrorEvent error = null;
HashMapDrmManagerClient
.
*
* @param context Context of the caller.
*/
public DrmManagerClient(Context context) {
mContext = context;
createEventThreads();
// save the unique id
mUniqueId = _initialize();
mCloseGuard.open("release");
}
@Override
protected void finalize() throws Throwable {
try {
mCloseGuard.warnIfOpen();
close();
} finally {
super.finalize();
}
}
/**
* Releases resources associated with the current session of
* DrmManagerClient. It is considered good practice to call this method when
* the {@link DrmManagerClient} object is no longer needed in your
* application. After this method is called, {@link DrmManagerClient} is no
* longer usable since it has lost all of its required resource.
*/
@Override
public void close() {
mCloseGuard.close();
if (mClosed.compareAndSet(false, true)) {
if (mEventHandler != null) {
mEventThread.quit();
mEventThread = null;
}
if (mInfoHandler != null) {
mInfoThread.quit();
mInfoThread = null;
}
mEventHandler = null;
mInfoHandler = null;
mOnEventListener = null;
mOnInfoListener = null;
mOnErrorListener = null;
_release(mUniqueId);
}
}
/**
* @deprecated replaced by {@link #close()}.
*/
@Deprecated
public void release() {
close();
}
/**
* Registers an {@link DrmManagerClient.OnInfoListener} callback, which is invoked when the
* DRM framework sends status or warning information during registration or rights acquisition.
*
* @param infoListener Interface definition for the callback.
*/
public synchronized void setOnInfoListener(OnInfoListener infoListener) {
mOnInfoListener = infoListener;
if (null != infoListener) {
createListeners();
}
}
/**
* Registers an {@link DrmManagerClient.OnEventListener} callback, which is invoked when the
* DRM framework sends information about DRM processing.
*
* @param eventListener Interface definition for the callback.
*/
public synchronized void setOnEventListener(OnEventListener eventListener) {
mOnEventListener = eventListener;
if (null != eventListener) {
createListeners();
}
}
/**
* Registers an {@link DrmManagerClient.OnErrorListener} callback, which is invoked when
* the DRM framework sends error information.
*
* @param errorListener Interface definition for the callback.
*/
public synchronized void setOnErrorListener(OnErrorListener errorListener) {
mOnErrorListener = errorListener;
if (null != errorListener) {
createListeners();
}
}
/**
* Retrieves information about all the DRM plug-ins (agents) that are registered with
* the DRM framework.
*
* @return A String
array of DRM plug-in descriptions.
*/
public String[] getAvailableDrmEngines() {
DrmSupportInfo[] supportInfos = _getAllSupportInfo(mUniqueId);
ArrayListNote: For OMA or WM-DRM, rightsPath
and
* contentPath
can be null.
rightsPath
.
*/
public int saveRights(
DrmRights drmRights, String rightsPath, String contentPath) throws IOException {
if (null == drmRights || !drmRights.isValid()) {
throw new IllegalArgumentException("Given drmRights or contentPath is not valid");
}
if (null != rightsPath && !rightsPath.equals("")) {
DrmUtils.writeToFile(rightsPath, drmRights.getData());
}
return _saveRights(mUniqueId, drmRights, rightsPath, contentPath);
}
/**
* Installs a new DRM plug-in (agent) at runtime.
*
* @param engineFilePath File path to the plug-in file to be installed.
*
* {@hide}
*/
public void installDrmEngine(String engineFilePath) {
if (null == engineFilePath || engineFilePath.equals("")) {
throw new IllegalArgumentException(
"Given engineFilePath: "+ engineFilePath + "is not valid");
}
_installDrmEngine(mUniqueId, engineFilePath);
}
/**
* Checks whether the given MIME type or path can be handled.
*
* @param path Path of the content to be handled.
* @param mimeType MIME type of the object to be handled.
*
* @return True if the given MIME type or path can be handled; false if they cannot be handled.
*/
public boolean canHandle(String path, String mimeType) {
if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) {
throw new IllegalArgumentException("Path or the mimetype should be non null");
}
return _canHandle(mUniqueId, path, mimeType);
}
/**
* Checks whether the given MIME type or URI can be handled.
*
* @param uri URI for the content to be handled.
* @param mimeType MIME type of the object to be handled
*
* @return True if the given MIME type or URI can be handled; false if they cannot be handled.
*/
public boolean canHandle(Uri uri, String mimeType) {
if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) {
throw new IllegalArgumentException("Uri or the mimetype should be non null");
}
return canHandle(convertUriToPath(uri), mimeType);
}
/**
* Processes the given DRM information based on the information type.
*
* @param drmInfo The {@link DrmInfo} to be processed.
* @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
*/
public int processDrmInfo(DrmInfo drmInfo) {
if (null == drmInfo || !drmInfo.isValid()) {
throw new IllegalArgumentException("Given drmInfo is invalid/null");
}
int result = ERROR_UNKNOWN;
if (null != mEventHandler) {
Message msg = mEventHandler.obtainMessage(ACTION_PROCESS_DRM_INFO, drmInfo);
result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result;
}
return result;
}
/**
* Retrieves information for registering, unregistering, or acquiring rights.
*
* @param drmInfoRequest The {@link DrmInfoRequest} that specifies the type of DRM
* information being retrieved.
*
* @return A {@link DrmInfo} instance.
*/
public DrmInfo acquireDrmInfo(DrmInfoRequest drmInfoRequest) {
if (null == drmInfoRequest || !drmInfoRequest.isValid()) {
throw new IllegalArgumentException("Given drmInfoRequest is invalid/null");
}
return _acquireDrmInfo(mUniqueId, drmInfoRequest);
}
/**
* Processes a given {@link DrmInfoRequest} and returns the rights information asynchronously.
*
* This is a utility method that consists of an
* {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and a
* {@link #processDrmInfo(DrmInfo) processDrmInfo()} method call. This utility method can be
* used only if the selected DRM plug-in (agent) supports this sequence of calls. Some DRM
* agents, such as OMA, do not support this utility method, in which case an application must
* invoke {@link #acquireDrmInfo(DrmInfoRequest) acquireDrmInfo()} and
* {@link #processDrmInfo(DrmInfo) processDrmInfo()} separately.
*
* @param drmInfoRequest The {@link DrmInfoRequest} used to acquire the rights.
* @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
*/
public int acquireRights(DrmInfoRequest drmInfoRequest) {
DrmInfo drmInfo = acquireDrmInfo(drmInfoRequest);
if (null == drmInfo) {
return ERROR_UNKNOWN;
}
return processDrmInfo(drmInfo);
}
/**
* Retrieves the type of rights-protected object (for example, content object, rights
* object, and so on) using the specified path or MIME type. At least one parameter must
* be specified to retrieve the DRM object type.
*
* @param path Path to the content or null.
* @param mimeType MIME type of the content or null.
*
* @return An int
that corresponds to a {@link DrmStore.DrmObjectType}.
*/
public int getDrmObjectType(String path, String mimeType) {
if ((null == path || path.equals("")) && (null == mimeType || mimeType.equals(""))) {
throw new IllegalArgumentException("Path or the mimetype should be non null");
}
return _getDrmObjectType(mUniqueId, path, mimeType);
}
/**
* Retrieves the type of rights-protected object (for example, content object, rights
* object, and so on) using the specified URI or MIME type. At least one parameter must
* be specified to retrieve the DRM object type.
*
* @param uri URI for the content or null.
* @param mimeType MIME type of the content or null.
*
* @return An int
that corresponds to a {@link DrmStore.DrmObjectType}.
*/
public int getDrmObjectType(Uri uri, String mimeType) {
if ((null == uri || Uri.EMPTY == uri) && (null == mimeType || mimeType.equals(""))) {
throw new IllegalArgumentException("Uri or the mimetype should be non null");
}
String path = "";
try {
path = convertUriToPath(uri);
} catch (Exception e) {
// Even uri is invalid the mimetype shall be valid, so allow to proceed further.
Log.w(TAG, "Given Uri could not be found in media store");
}
return getDrmObjectType(path, mimeType);
}
/**
* Retrieves the MIME type embedded in the original content.
*
* @param path Path to the rights-protected content.
*
* @return The MIME type of the original content, such as video/mpeg
.
*/
public String getOriginalMimeType(String path) {
if (null == path || path.equals("")) {
throw new IllegalArgumentException("Given path should be non null");
}
String mime = null;
FileInputStream is = null;
try {
FileDescriptor fd = null;
File file = new File(path);
if (file.exists()) {
is = new FileInputStream(file);
fd = is.getFD();
}
mime = _getOriginalMimeType(mUniqueId, path, fd);
} catch (IOException ioe) {
} finally {
if (is != null) {
try {
is.close();
} catch(IOException e) {}
}
}
return mime;
}
/**
* Retrieves the MIME type embedded in the original content.
*
* @param uri URI of the rights-protected content.
*
* @return MIME type of the original content, such as video/mpeg
.
*/
public String getOriginalMimeType(Uri uri) {
if (null == uri || Uri.EMPTY == uri) {
throw new IllegalArgumentException("Given uri is not valid");
}
return getOriginalMimeType(convertUriToPath(uri));
}
/**
* Checks whether the given content has valid rights.
*
* @param path Path to the rights-protected content.
*
* @return An int
representing the {@link DrmStore.RightsStatus} of the content.
*/
public int checkRightsStatus(String path) {
return checkRightsStatus(path, DrmStore.Action.DEFAULT);
}
/**
* Check whether the given content has valid rights.
*
* @param uri URI of the rights-protected content.
*
* @return An int
representing the {@link DrmStore.RightsStatus} of the content.
*/
public int checkRightsStatus(Uri uri) {
if (null == uri || Uri.EMPTY == uri) {
throw new IllegalArgumentException("Given uri is not valid");
}
return checkRightsStatus(convertUriToPath(uri));
}
/**
* Checks whether the given rights-protected content has valid rights for the specified
* {@link DrmStore.Action}.
*
* @param path Path to the rights-protected content.
* @param action The {@link DrmStore.Action} to perform.
*
* @return An int
representing the {@link DrmStore.RightsStatus} of the content.
*/
public int checkRightsStatus(String path, int action) {
if (null == path || path.equals("") || !DrmStore.Action.isValid(action)) {
throw new IllegalArgumentException("Given path or action is not valid");
}
return _checkRightsStatus(mUniqueId, path, action);
}
/**
* Checks whether the given rights-protected content has valid rights for the specified
* {@link DrmStore.Action}.
*
* @param uri URI for the rights-protected content.
* @param action The {@link DrmStore.Action} to perform.
*
* @return An int
representing the {@link DrmStore.RightsStatus} of the content.
*/
public int checkRightsStatus(Uri uri, int action) {
if (null == uri || Uri.EMPTY == uri) {
throw new IllegalArgumentException("Given uri is not valid");
}
return checkRightsStatus(convertUriToPath(uri), action);
}
/**
* Removes the rights associated with the given rights-protected content.
*
* @param path Path to the rights-protected content.
*
* @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
*/
public int removeRights(String path) {
if (null == path || path.equals("")) {
throw new IllegalArgumentException("Given path should be non null");
}
return _removeRights(mUniqueId, path);
}
/**
* Removes the rights associated with the given rights-protected content.
*
* @param uri URI for the rights-protected content.
*
* @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
*/
public int removeRights(Uri uri) {
if (null == uri || Uri.EMPTY == uri) {
throw new IllegalArgumentException("Given uri is not valid");
}
return removeRights(convertUriToPath(uri));
}
/**
* Removes all the rights information of every DRM plug-in (agent) associated with
* the DRM framework. Will be used during a master reset.
*
* @return ERROR_NONE for success; ERROR_UNKNOWN for failure.
*/
public int removeAllRights() {
int result = ERROR_UNKNOWN;
if (null != mEventHandler) {
Message msg = mEventHandler.obtainMessage(ACTION_REMOVE_ALL_RIGHTS);
result = (mEventHandler.sendMessage(msg)) ? ERROR_NONE : result;
}
return result;
}
/**
* Initiates a new conversion session. An application must initiate a conversion session
* with this method each time it downloads a rights-protected file that needs to be converted.
*
* This method applies only to forward-locking (copy protection) DRM schemes.
*
* @param mimeType MIME type of the input data packet.
*
* @return A convert ID that is used used to maintain the conversion session.
*/
public int openConvertSession(String mimeType) {
if (null == mimeType || mimeType.equals("")) {
throw new IllegalArgumentException("Path or the mimeType should be non null");
}
return _openConvertSession(mUniqueId, mimeType);
}
/**
* Converts the input data (content) that is part of a rights-protected file. The converted
* data and status is returned in a {@link DrmConvertedStatus} object. This method should be
* called each time there is a new block of data received by the application.
*
* @param convertId Handle for the conversion session.
* @param inputData Input data that needs to be converted.
*
* @return A {@link DrmConvertedStatus} object that contains the status of the data conversion,
* the converted data, and offset for the header and body signature. An application can
* ignore the offset because it is only relevant to the
* {@link #closeConvertSession closeConvertSession()} method.
*/
public DrmConvertedStatus convertData(int convertId, byte[] inputData) {
if (null == inputData || 0 >= inputData.length) {
throw new IllegalArgumentException("Given inputData should be non null");
}
return _convertData(mUniqueId, convertId, inputData);
}
/**
* Informs the DRM plug-in (agent) that there is no more data to convert or that an error
* has occurred. Upon successful conversion of the data, the DRM agent will provide an offset
* value indicating where the header and body signature should be added. Appending the
* signature is necessary to protect the integrity of the converted file.
*
* @param convertId Handle for the conversion session.
*
* @return A {@link DrmConvertedStatus} object that contains the status of the data conversion,
* the converted data, and the offset for the header and body signature.
*/
public DrmConvertedStatus closeConvertSession(int convertId) {
return _closeConvertSession(mUniqueId, convertId);
}
private int getEventType(int infoType) {
int eventType = -1;
switch (infoType) {
case DrmInfoRequest.TYPE_REGISTRATION_INFO:
case DrmInfoRequest.TYPE_UNREGISTRATION_INFO:
case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO:
eventType = DrmEvent.TYPE_DRM_INFO_PROCESSED;
break;
}
return eventType;
}
private int getErrorType(int infoType) {
int error = -1;
switch (infoType) {
case DrmInfoRequest.TYPE_REGISTRATION_INFO:
case DrmInfoRequest.TYPE_UNREGISTRATION_INFO:
case DrmInfoRequest.TYPE_RIGHTS_ACQUISITION_INFO:
error = DrmErrorEvent.TYPE_PROCESS_DRM_INFO_FAILED;
break;
}
return error;
}
/**
* This method expects uri in the following format
* content://media/