Example usage for android.media MediaDrm openSession

List of usage examples for android.media MediaDrm openSession

Introduction

In this page you can find the example usage for android.media MediaDrm openSession.

Prototype

@NonNull
public byte[] openSession() throws NotProvisionedException, ResourceBusyException 

Source Link

Document

Open a new session with the MediaDrm object.

Usage

From source file:mtmo.test.mediadrm.MainActivity.java

private void setupDrmProcessButton(final int appMode) {
    final Button btnRegistration = (Button) findViewById(R.id.btn_registration);
    final Button btnSaveLicense = (Button) findViewById(R.id.btn_license);
    final Button btnDeregistration = (Button) findViewById(R.id.btn_deregistration);
    final Button btnCheckRights = (Button) findViewById(R.id.btn_check_rights);
    final Button btnRemoveRights = (Button) findViewById(R.id.btn_remove_rights);
    final Button btnStatus = (Button) findViewById(R.id.btn_check_regist);

    if (btnRegistration != null) {
        btnRegistration.setOnClickListener(new OnClickListener() {
            @Override//w w w. j  a va  2  s  .  com
            public void onClick(View v) {
                mLogger.enter("requeseted registration...");

                final TaskInfo taskInfo = new TaskInfo(TaskType.REGISTRATION, mAccountId, mServiceId,
                        mCurrentATKNFilePath);
                mHandler.post(new TaskStarter(taskInfo));
            }
        });
    }
    if (btnSaveLicense != null) {
        btnSaveLicense.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLogger.d("requeseted getting License...");
                final TaskInfo taskInfo = new TaskInfo(TaskType.LICENSE, mAccountId, mServiceId,
                        mCurrentATKNFilePath);
                mHandler.post(new TaskStarter(taskInfo));
            }
        });
    }
    if (btnDeregistration != null) {
        btnDeregistration.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLogger.d("requeseted deregistration...");
                final TaskInfo taskInfo = new TaskInfo(TaskType.DEREGISTRATION, mAccountId, mServiceId,
                        mCurrentATKNFilePath);
                mHandler.post(new TaskStarter(taskInfo));
            }
        });
    }
    if (btnCheckRights != null) {
        btnCheckRights.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLogger.d("requeseted check License...");
                TextView log = (TextView) findViewById(R.id.log);
                byte[] sessionId = null;
                MediaDrm mediaDrm = null;
                byte[] contentData = null;
                log.setText("");

                try {
                    mediaDrm = new MediaDrm(Constants.MBB_UUID);
                    sessionId = mediaDrm.openSession();

                    switch (appMode) {
                    case Constants.APP_MODE_ABS:
                        ABSContentInfo absContentInfo = getABSContentInfo();
                        contentData = Utils.readPsshDataFromFile(true);
                        mediaDrm.restoreKeys(sessionId,
                                InitData.getPSSHTableForAndroid(contentData, absContentInfo.getVideoKid()));
                        break;
                    case Constants.APP_MODE_OFFLINE:
                        contentData = Utils.readIPMPDataFromFile(true);
                        mediaDrm.restoreKeys(sessionId, InitData.getIPMPTableForAndroid(contentData));
                        break;
                    default:
                        Toast.makeText(mContext, "Unknown App Mode", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    HashMap<String, String> infoMap = mediaDrm.queryKeyStatus(sessionId);

                    if (infoMap != null && infoMap.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        Iterator<String> iterator = infoMap.keySet().iterator();
                        log.setText("");
                        Time time = new Time();
                        while (iterator.hasNext()) {
                            String name = iterator.next();
                            time.set(Long.valueOf(infoMap.get(name)));
                            mLogger.d("\t" + name + " = " + infoMap.get(name) + " [" + time.format2445() + "]");
                            sb.append(
                                    "\n\t" + name + " = " + infoMap.get(name) + " [" + time.format2445() + "]");
                        }
                        log.append(sb);
                    }
                    mediaDrm.closeSession(sessionId);
                    sessionId = null;
                    Toast.makeText(MainActivity.this, "queryKeyStatus finished", Toast.LENGTH_LONG).show();
                    return;
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (UnsupportedSchemeException e) {
                    e.printStackTrace();
                } catch (NotProvisionedException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mediaDrm.closeSession(sessionId);
                sessionId = null;
                Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
    if (btnRemoveRights != null) {
        btnRemoveRights.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLogger.d("requeseted removing License...");
                Toast.makeText(MainActivity.this, "Not implementation", Toast.LENGTH_LONG).show();
            }
        });
    }
    if (btnStatus != null) {
        btnStatus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mLogger.enter("Check registration Status...");

                RequestParser parser = null;
                MediaDrm mediaDrm = null;
                byte[] sessionid = null;
                HashMap<String, String> optionalParameters = null;
                try {
                    mediaDrm = new MediaDrm(Constants.MBB_UUID);
                    sessionid = mediaDrm.openSession();
                    KeyRequest keyRequest = mediaDrm.getKeyRequest(sessionid,
                            InitData.getPropertyTableForAndroid(Constants.QUERY_NAME_REGISTERED_STATE,
                                    Utils.accountIdToMarlinFormat(mAccountId), mServiceId),
                            Constants.REQUEST_MIMETYPE_QUERY_PROPERTY, MediaDrm.KEY_TYPE_OFFLINE,
                            optionalParameters);
                    parser = new RequestParser(keyRequest.getData());

                    if (parser.parse()) {
                        Toast.makeText(MainActivity.this, parser.getProperty(), Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_LONG).show();
                    }
                    return;
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (UnsupportedSchemeException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (NotProvisionedException e) {
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_LONG).show();
            }
        });
    }
}