Download下载DRM

4. DRM文件下载解析流程
finalizeDestinationFile(mInfoDelta);  DownloadThread.java
private void finalizeDestinationFile(DownloadInfoDelta state) {  // DownloadInfoDelta是啥?
    if (state.mFileName != null) {
        // make sure the file is readable
        FileUtils.setPermissions(state.mFileName, 0644, -1, -1);

        File file = new File(state.mFileName);

        /// M: support MTK DRM @{
        if (file.length() == state.mCurrentBytes) {
            ContentValues values = new ContentValues();
            // If written bytes is not equal to file.length(), don't install DRM file
            if ((Constants.MTK_DRM_ENABLED)
                    && Helpers.isMtkDRMFile(state.mMimeType)) {

                OmaDrmClient drmClient = new OmaDrmClient(this.mContext);
                if (Helpers.isMtkDRMFLOrCDFile(state.mMimeType)) {
                    int result = drmClient.installDrmMsg(state.mFileName);  // 这个API的作用

                    String[] paths = {state.mFileName};
                    String[] mimeTypes = {state.mMimeType};
                    MediaScannerConnection.scanFile(mContext, paths, mimeTypes, null); // 这句话什么意思?
                } else if (Helpers.isMtkDRMRightFile(state.mMimeType)) {
                    try {
                        DrmRights rights = new DrmRights(state.mFileName, state.mMimeType);
                        int result = drmClient.saveRights(rights, null, null); //层层调用,最终调用的是DrmManagerService中的该方法
                        if (result == OmaDrmClient.ERROR_NONE) {

                            drmClient.rescanDrmMediaFiles(mContext, rights, null);
                        }

                        // Mark for delete for DRM right file 将权限文件删除
                        values = new ContentValues();
                        values.put(Downloads.Impl.COLUMN_DELETED, 1);
                        mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);


                    } catch (IOException e) {
                        Xlog.e(Constants.DL_DRM, "save rights " + state.mFileName + " exception");
                    }
                }
                /// M : when file length change after install drm msg, update state.mCurrentBytes. @{ 为啥?
                if (new File(state.mFileName).length() != state.mTotalWriteBytes) {
                    state.mCurrentBytes = new File(state.mFileName).length();
                    state.mTotalWriteBytes = state.mCurrentBytes;
                }
                /// @}
            }
            values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, state.mCurrentBytes);
            //cheng.hu.hz merge from PR900576
            values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, state.mCurrentBytes);
            mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
            Xlog.d(Constants.DL_ENHANCE, "finalizeDestinationFile: " +
                    " Update Total Bytes:"  + state.mCurrentBytes);
        }
        /// @}
    }
}

OmaDrmClient.java
public int installDrmMsg(String path, boolean useFd) {
    Log.v(TAG, "installDrmMsg FD path : " + path);

    if (null == path || path.equals("")) {
        Log.e(TAG, "installDrmMsg : Given path is not valid");
        return DrmManagerClient.ERROR_UNKNOWN;
    }
    DrmInfo info = null;
    RandomAccessFile dmStream = null;
    FileOutputStream dcfStream = null;
    FileDescriptor dmFd = null;
    FileDescriptor dcfFd = null;
    try {
        File dmFile = new File(path);
        if (dmFile.exists()) {
            dmStream = new RandomAccessFile(dmFile, "rw");
            dmFd = dmStream.getFD();
        }
        String dcfPath = OmaDrmUtils.generateDcfFilePath(path);
        Log.v(TAG, "installDrmMsg :dcfPathL: " + dcfPath);
        if (dcfPath == null) {
            Log.e(TAG, "installDrmMsg : dcfPath is " + dcfPath);
            if (dmStream != null) {
                try {
                    dmStream.close();
                } catch (IOException e) {
                    Log.w(TAG, "close dm stream: I/O Exception: " + e.getMessage());
                }
            }
            return DrmManagerClient.ERROR_UNKNOWN;
        }
        File dcfFile = new File(dcfPath);
        if (!dcfFile.exists()) {
            dcfFile.createNewFile();
        }
        if (dmFile.exists()) {
            dcfStream = new FileOutputStream(dcfFile);
            dcfFd = dcfStream.getFD();
        }

        // constructs the request and process it by acquireDrmInfo
        DrmInfoRequest request = new DrmInfoRequest(OmaDrmStore.DrmRequestType.TYPE_SET_DRM_INFO,
                OmaDrmStore.DrmObjectMime.MIME_DRM_MESSAGE);
        request.put(OmaDrmStore.DrmRequestKey.KEY_ACTION, OmaDrmStore.DrmRequestAction.ACTION_INSTALL_DRM_MSG_BY_FD);
        request.put(OmaDrmStore.DrmRequestKey.KEY_DM_FD, dmFd); // dm
                                                                                   // file
                                                                                   // descriptor
        request.put(OmaDrmStore.DrmRequestKey.KEY_DCF_FD, dcfFd);
        Log.d(TAG, "installDrmMsg FD:" + dmFd + "," + dcfFd);
        info = mDrmManagerClient.acquireDrmInfo(request);
        // delete file 为什么要这么做,新建一个临时文件然后又删掉
        if (dmFile.exists()) {
            dmFile.delete();
        }
        if (dcfFile.exists()) {
            dcfFile.renameTo(new File(path));
        }

    } catch (IOException ioe) {
        // / M: Added for debug.
        Log.d(TAG, "getOriginalMimeType: File I/O exception: " + ioe.getMessage());
    } finally {
        if (dmStream != null) {
            try {
                dmStream.close();
            } catch (IOException e) {
                Log.w(TAG, "close dm stream: I/O Exception: " + e.getMessage());
            }
        }
        if (dcfStream != null) {
            try {
                dcfStream.close();
            } catch (IOException e) {
                Log.w(TAG, "close dcf stream: I/O Exception: " + e.getMessage());
            }
        }
    }

    // get message from returned DrmInfo
    byte[] data = null;
    if (info != null) {
        data = info.getData();
    }
    String message = "";
    if (null != data) {
        try {
            // the information shall be in format of ASCII string
            message = new String(data, "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Unsupported encoding type of the returned DrmInfo data");
            message = "";
        }
    }
    Log.v(TAG, "installDrmMsg FD path: >" + message);

    return OmaDrmStore.DrmRequestResult.RESULT_SUCCESS.equals(message) ?
            DrmManagerClient.ERROR_NONE : DrmManagerClient.ERROR_UNKNOWN;
}

private class DownloadInfoDelta { //其成员
    public String mUri;
    public String mFileName;
    public String mMimeType;
    public int mStatus;
    public int mNumFailed;
    public int mRetryAfter;
    public long mTotalBytes;
    public long mCurrentBytes;
    public String mETag;

    public String mErrorMsg;

    // M: Add to support OMA download
    public int mOmaDownload;
    public int mOmaDownloadStatus;
    public String mOmaDownloadInsNotifyUrl;

    public String mNotifyPkg;//add by hucheng for D345010 20150616
}

你可能感兴趣的:(Download下载DRM)