package com.viaccessorca.voplayerdemo.drm;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.viaccessorca.drm.VOCommonDrmAgent.VODrmRightsInfo;
import com.viaccessorca.drm.VOPlayreadyAgent;
import com.viaccessorca.drm.VOViaccessAgent;
import com.viaccessorca.drm.VOWidevineAgent;
import com.viaccessorca.exceptions.VOException;
import com.viaccessorca.exceptions.VOStatusCode;
import com.viaccessorca.voplayer.VOPlayer;
import com.viaccessorca.voplayerdemo.Constants;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class DrmManager
{
    private static final String TAG = "DrmManager";

    //DRM action types
    public enum ActionType {
        NONE,
        ACQUIRE_RIGHTS,
        DELETE_RIGHTS,
        PERSO,
        DELETE_PERSO,
        PERSO_AND_ACQUIRE_RIGHT_IF_NOT_ALREADY_DONE,
        PERSO_IF_NOT_ALREADY_DONE,
        ACQUIRE_RIGHTS_IF_NOT_ALREADY_DONE,
        RENEW_RIGHTS,
        GET_RIGHTS_INFO,
        DELETE_STORE,
    }

    //parameters for the DRM action
    public static class ActionParams{
        public ActionType actionType = ActionType.NONE;
        public String contentUrl = null;
        public String drmAgentType = null;
        public String persoUrl = "debug://localpersonalization";
        public String persoAppVersion = "1.0";
        public String persoCustomData = null;
        public String licenseAcquisitionCustomData = null;
        public String licenseAcquisitionUrl = null;
        public String licenseAcquisitionUrlParameters = null;
        public String licenseAcquisitionCookie = null;
        public String licenseContentId = null;
        public boolean useVOSecurityGateWay = false;

        public ActionParams(){
            super();
        }

        public ActionParams(ActionParams params) {
            this.actionType = params.actionType;
            this.contentUrl = params.contentUrl;
            this.drmAgentType = params.drmAgentType;
            this.persoUrl = params.persoUrl;
            this.persoAppVersion = params.persoAppVersion;
            this.persoCustomData = params.persoCustomData;
            this.licenseAcquisitionCustomData = params.licenseAcquisitionCustomData;
            this.licenseAcquisitionUrl = params.licenseAcquisitionUrl;
            this.licenseAcquisitionUrlParameters = params.licenseAcquisitionUrlParameters;
            this.licenseAcquisitionCookie = params.licenseAcquisitionCookie;
            this.licenseContentId = params.licenseContentId;
            this.useVOSecurityGateWay = params.useVOSecurityGateWay;
        }
    }

    //notifications type
    public enum NotificationType {
        DISPLAY_UNKNOWN,
        DISPLAY_ERROR,
        ACQUIRE_RIGHTS_START,
        ACQUIRE_RIGHTS_DONE,
        DELETE_RIGHTS_START,
        DELETE_RIGHTS_DONE,
        PERSO_START,
        PERSO_DONE,
        DELETE_PERSO_START,
        DELETE_PERSO_DONE,
        DELETE_STORE_START,
        DELETE_STORE_END,
        GET_RIGHTS_INFO_START,
        GET_RIGHTS_INFO_DONE,
    }

    //notifications parameters received during DRM action
    public static class NotificationParams{
        public NotificationType notificationType = NotificationType.DISPLAY_UNKNOWN;
        public String text = null;
    }

    private static void sendNotification(Handler handler,NotificationType type, String notificationText){
        if (handler == null){return;}

        NotificationParams notificationParams = new NotificationParams();
        notificationParams.notificationType = type;
        if(notificationText != null ){
            notificationParams.text = notificationText;}
        Message msg = Message.obtain(null, 0, (Object)notificationParams);
        handler.sendMessage(msg);
    }

    private static void sendNotification(Handler handler,NotificationType type){
        sendNotification(handler,type,null);
    }

    //class used to perform DRM action on a thread pool
    public static class DRMActionPooler extends ThreadPoolExecutor {
        private static final int MAX_THREADS = 1;
        private static final int MAX_TASKS_QUEUED = 10;

        private VOPlayer player = null;
        private Context appContext = null;
        private Handler handler = null;
        private ActionType actionAfterPerso = ActionType.NONE;

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
            boolean success = super.awaitTermination(timeout, unit);
            if (player != null) {
                player.release();
            }
            return success;
        }

        public DRMActionPooler(Context appContext, Handler handler) {
            super(MAX_THREADS, MAX_THREADS, 1,
                    TimeUnit.MINUTES,
                    new ArrayBlockingQueue<Runnable>(MAX_TASKS_QUEUED, true),
                    new ThreadPoolExecutor.CallerRunsPolicy());

            this.appContext = appContext;
            this.handler = handler;
            try {
                player = new VOPlayer(this.appContext);
            } catch (Exception e) {
                e.printStackTrace();
                Log.v(TAG, "actionThread constructor fails");
            }
        }

        public void submitDrmTask(ActionParams params) {
            Runnable task;
            if (params.drmAgentType != null) {
                if (params.drmAgentType.equals(Constants.DRM_AGENT_TYPE_PLAYREADY)) {
                    task = new PlayReadyTask(params);
                } else if(params.drmAgentType.equals(Constants.DRM_AGENT_TYPE_WIDEVINE)) {
                    task = new WidevineTask(params);
                }
                else if(params.drmAgentType.equals(Constants.DRM_AGENT_TYPE_VODRM)) {
                    task = new VoDrmTask(params);
                }
                else
                {
                    Log.v(TAG, "actionThread DRM type not recognized fails");
                    return;
                }
                this.submit(task);
            }
        }

        private class PlayReadyTask implements Runnable {
            private ActionParams params = null;
            private VOPlayreadyAgent playreadyAgent = null;

            PlayReadyTask(ActionParams params) {
                //Log.v(TAG, "PlayReadyTask contentUrl="+ params.contentUrl);
                this.params = params;
                try {
                    player.setDataSource(params.contentUrl);
                    playreadyAgent = player.getAgentPlayready();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void run() {
                try {
                    playreadyAgent.setPersonalizationURL(params.persoUrl);
                    playreadyAgent.setPersonalizationAppVersion(params.persoAppVersion);
                    playreadyAgent.setPersonalizationCustomData(params.persoCustomData);
                    playreadyAgent.setLicenseAcquisitionURL(params.licenseAcquisitionUrl);
                    playreadyAgent.setLicenseAcquisitionURLParameters(params.licenseAcquisitionUrlParameters);
                    playreadyAgent.setLicenseAcquisitionCustomData(params.licenseAcquisitionCustomData);
                    //playreadyAgent.setContentIdentifier(params.licenseContentId);
                    //playreadyAgent.useVOSecurityGateway(params.useVOSecurityGateWay);
                    if (null != params.licenseAcquisitionCookie){
                        String[] cookies = new String[1];
                        cookies[0] = params.licenseAcquisitionCookie;
                        playreadyAgent.setLicenseAcquisitionCookies(cookies);
                    } else {
                        playreadyAgent.setLicenseAcquisitionCookies(null);
                    }
                } catch (VOException e) {
                    sendNotification(handler,NotificationType.DISPLAY_ERROR,new String("set parameter error:"+e.getStatus().toString()));
                    return;
                }

                Log.v(TAG, params.actionType+ " started");
                switch(params.actionType){

                    //perform perso + right acquisition only if not done yet
                    case PERSO_AND_ACQUIRE_RIGHT_IF_NOT_ALREADY_DONE: {
                            actionAfterPerso = ActionType.ACQUIRE_RIGHTS_IF_NOT_ALREADY_DONE;
                            ActionParams nextTaskParams = new ActionParams(params);
                            nextTaskParams.actionType = ActionType.PERSO_IF_NOT_ALREADY_DONE;
                            DRMActionPooler.this.submitDrmTask(nextTaskParams);
                        }
                        break;

                    //perform a perso only if not done yet
                    case PERSO_IF_NOT_ALREADY_DONE:
                        try {
                            if (!playreadyAgent.verifyPersonalization()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.PERSO;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            } else if (ActionType.NONE != actionAfterPerso){
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = actionAfterPerso;
                                actionAfterPerso = ActionType.NONE;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }
                        } catch (VOException e) {
                            if (e.getStatus() != VOStatusCode.ERROR_DRM_CONTENT_NOT_RECOGNIZED) {
                                sendNotification(handler,NotificationType.DISPLAY_ERROR,new String("perso if not done error:" + e.getStatus().toString()));
                            }
                        }
                        break;

                    //perform a right acquisition only if there is no rights
                    case ACQUIRE_RIGHTS_IF_NOT_ALREADY_DONE:
                        try {
                            //warning: verifyRights() can be called only if perso is done
                            if (!playreadyAgent.verifyRights()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.ACQUIRE_RIGHTS;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }
                        } catch (VOException e) {
                            if (e.getStatus() != VOStatusCode.ERROR_DRM_CONTENT_NOT_RECOGNIZED) {
                                sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("acquire rights if not done error:" + e.getStatus().toString()));
                            }
                        }
                        break;

                    //force right acquisition (do perso if needed)
                    case RENEW_RIGHTS:
                        try {
                            if (!playreadyAgent.verifyPersonalization()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.PERSO;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }

                            ActionParams nextTaskParams = new ActionParams(params);
                            nextTaskParams.actionType = ActionType.ACQUIRE_RIGHTS;
                            DRMActionPooler.this.submitDrmTask(nextTaskParams);

                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("renew rights rights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call acquireRights method of drm agent
                    case ACQUIRE_RIGHTS:
                        sendNotification(handler,NotificationType.ACQUIRE_RIGHTS_START);
                        try {
                            playreadyAgent.acquireRights();
                            sendNotification(handler,NotificationType.ACQUIRE_RIGHTS_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("acquireRights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call deleteRights method of drm agent
                    case DELETE_RIGHTS:
                        sendNotification(handler,NotificationType.DELETE_RIGHTS_START);
                        try {
                            playreadyAgent.deleteRights();
                            sendNotification(handler,NotificationType.DELETE_RIGHTS_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("deleteRights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call performPersonalization method of drm agent
                    case PERSO:
                        sendNotification(handler,NotificationType.PERSO_START);
                        try {
                            playreadyAgent.performPersonalization();
                            sendNotification(handler,NotificationType.PERSO_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("performPersonalization error:"+e.getStatus().toString()));
                        }
                        if (ActionType.NONE != actionAfterPerso){
                            ActionParams nextTaskParams = new ActionParams(params);
                            nextTaskParams.actionType = actionAfterPerso;
                            actionAfterPerso = ActionType.NONE;
                            DRMActionPooler.this.submitDrmTask(nextTaskParams);
                        }
                        break;

                    //call deletePersonalization method of drm agent
                    case DELETE_PERSO:
                        sendNotification(handler,NotificationType.DELETE_PERSO_START);
                        try {
                            playreadyAgent.deletePersonalization();
                            sendNotification(handler,NotificationType.DELETE_PERSO_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("deletePersonalization error:"+e.getStatus().toString()));
                        }
                        break;

                    //get rights info of drm agent
                    case GET_RIGHTS_INFO:
                        try {
                            sendNotification(handler,NotificationType.GET_RIGHTS_INFO_START);
                            String rightsInfoString = "Rights info:";
                            boolean bVerifyPerso = playreadyAgent.verifyPersonalization();
                            rightsInfoString = rightsInfoString + "\nverifyPerso="+ bVerifyPerso;
                            if (bVerifyPerso){
                                rightsInfoString = new String(rightsInfoString + "\nverifyRights="+ playreadyAgent.verifyRights());
                            }

                            if (bVerifyPerso){
                                VODrmRightsInfo[] rights = playreadyAgent.getRightsInfo();
                                if (null != rights) {
                                    for (int i=0; i<rights.length; i++){
                                        Log.d(TAG, "rights[" + i+"]:\nhasTimeConstraint="+ rights[i].hasTimeConstraint()
                                                +"\nhasCountConstraint="+ rights[i].hasCountConstraint()
                                                +"\nhasIntervalConstraint="+ rights[i].hasIntervalConstraint());
                                        if (0 < i){
                                            //add separator line
                                            rightsInfoString = rightsInfoString + "\n------";
                                        }
                                        if (rights[i].hasTimeConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\nstartTime="+rights[i].getStartTime()
                                                    + ", endtime="+rights[i].getEndTime();
                                        }
                                        if (rights[i].hasCountConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\ninitialCount="+rights[i].getInitialCount()
                                                    + ", countLeft="+rights[i].getCountLeft();
                                        }
                                        if (rights[i].hasIntervalConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\nIntervalPeriodInSeconds="+rights[i].getIntervalPeriodInSeconds();
                                        }
                                    }
                                }
                            }
                            sendNotification(handler,NotificationType.GET_RIGHTS_INFO_DONE, rightsInfoString);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("getRightsInfo error:"+e.getStatus().toString()));
                        }
                        break;
                    default:
                        break;
                }
                Log.v(TAG, params.actionType + " finished!");
            }
        }

        private class VoDrmTask implements Runnable {
            private ActionParams params = null;
            private VOViaccessAgent drmAgent = null;

            VoDrmTask(ActionParams params) {
                //Log.v(TAG, "PlayReadyTask contentUrl="+ params.contentUrl);
                this.params = params;
                try {
                    player.setDataSource(params.contentUrl);
                    drmAgent = player.getAgentVodrm();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void run() {
                try {
                    drmAgent.setPersonalizationURL(params.persoUrl);
                    //drmAgent.setPersonalizationAppVersion(params.persoAppVersion);
                    //drmAgent.setPersonalizationCustomData(params.persoCustomData);
                    drmAgent.setLicenseAcquisitionURL(params.licenseAcquisitionUrl);
                    drmAgent.setLicenseAcquisitionURLParameters(params.licenseAcquisitionUrlParameters);
                    drmAgent.setLicenseAcquisitionCustomData(params.licenseAcquisitionCustomData);
                    //drmAgent.setContentIdentifier(params.licenseContentId);
                    //drmAgent.useVOSecurityGateway(params.useVOSecurityGateWay);
                    if (null != params.licenseAcquisitionCookie){
                        String[] cookies = new String[1];
                        cookies[0] = params.licenseAcquisitionCookie;
                        drmAgent.setLicenseAcquisitionCookies(cookies);
                    } else {
                        drmAgent.setLicenseAcquisitionCookies(null);
                    }
                } catch (VOException e) {
                    sendNotification(handler,NotificationType.DISPLAY_ERROR,new String("set parameter error:"+e.getStatus().toString()));
                    return;
                }

                Log.v(TAG, params.actionType+ " started");
                switch(params.actionType){

                    //perform perso + right acquisition only if not done yet
                    case PERSO_AND_ACQUIRE_RIGHT_IF_NOT_ALREADY_DONE: {
                        actionAfterPerso = ActionType.ACQUIRE_RIGHTS_IF_NOT_ALREADY_DONE;
                        ActionParams nextTaskParams = new ActionParams(params);
                        nextTaskParams.actionType = ActionType.PERSO_IF_NOT_ALREADY_DONE;
                        DRMActionPooler.this.submitDrmTask(nextTaskParams);
                    }
                    break;

                    //perform a perso only if not done yet
                    case PERSO_IF_NOT_ALREADY_DONE:
                        try {
                            if (!drmAgent.verifyPersonalization()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.PERSO;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            } else if (ActionType.NONE != actionAfterPerso){
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = actionAfterPerso;
                                actionAfterPerso = ActionType.NONE;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }
                        } catch (VOException e) {
                            if (e.getStatus() != VOStatusCode.ERROR_DRM_CONTENT_NOT_RECOGNIZED) {
                                sendNotification(handler,NotificationType.DISPLAY_ERROR,new String("perso if not done error:" + e.getStatus().toString()));
                            }
                        }
                        break;

                    //perform a right acquisition only if there is no rights
                    case ACQUIRE_RIGHTS_IF_NOT_ALREADY_DONE:
                        try {
                            //warning: verifyRights() can be called only if perso is done
                            if (!drmAgent.verifyRights()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.ACQUIRE_RIGHTS;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }
                        } catch (VOException e) {
                            if (e.getStatus() != VOStatusCode.ERROR_DRM_CONTENT_NOT_RECOGNIZED) {
                                sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("acquire rights if not done error:" + e.getStatus().toString()));
                            }
                        }
                        break;

                    //force right acquisition (do perso if needed)
                    case RENEW_RIGHTS:
                        try {
                            if (!drmAgent.verifyPersonalization()) {
                                ActionParams nextTaskParams = new ActionParams(params);
                                nextTaskParams.actionType = ActionType.PERSO;
                                DRMActionPooler.this.submitDrmTask(nextTaskParams);
                            }

                            ActionParams nextTaskParams = new ActionParams(params);
                            nextTaskParams.actionType = ActionType.ACQUIRE_RIGHTS;
                            DRMActionPooler.this.submitDrmTask(nextTaskParams);

                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("renew rights rights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call acquireRights method of drm agent
                    case ACQUIRE_RIGHTS:
                        sendNotification(handler,NotificationType.ACQUIRE_RIGHTS_START);
                        try {
                            drmAgent.acquireRights();
                            sendNotification(handler,NotificationType.ACQUIRE_RIGHTS_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("acquireRights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call deleteRights method of drm agent
                    case DELETE_RIGHTS:
                        sendNotification(handler,NotificationType.DELETE_RIGHTS_START);
                        try {
                            drmAgent.deleteRights();
                            sendNotification(handler,NotificationType.DELETE_RIGHTS_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("deleteRights error:"+e.getStatus().toString()));
                        }
                        break;

                    //call performPersonalization method of drm agent
                    case PERSO:
                        sendNotification(handler,NotificationType.PERSO_START);
                        try {
                            drmAgent.performPersonalization();
                            sendNotification(handler,NotificationType.PERSO_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("performPersonalization error:"+e.getStatus().toString()));
                        }
                        if (ActionType.NONE != actionAfterPerso){
                            ActionParams nextTaskParams = new ActionParams(params);
                            nextTaskParams.actionType = actionAfterPerso;
                            actionAfterPerso = ActionType.NONE;
                            DRMActionPooler.this.submitDrmTask(nextTaskParams);
                        }
                        break;

                    //call deletePersonalization method of drm agent
                    case DELETE_PERSO:
                        sendNotification(handler,NotificationType.DELETE_PERSO_START);
                        try {
                            drmAgent.deletePersonalization();
                            sendNotification(handler,NotificationType.DELETE_PERSO_DONE);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("deletePersonalization error:"+e.getStatus().toString()));
                        }
                        break;

                    //get rights info of drm agent
                    case GET_RIGHTS_INFO:
                        try {
                            sendNotification(handler,NotificationType.GET_RIGHTS_INFO_START);
                            String rightsInfoString = "Rights info:";
                            boolean bVerifyPerso = drmAgent.verifyPersonalization();
                            rightsInfoString = rightsInfoString + "\nverifyPerso="+ bVerifyPerso;
                            if (bVerifyPerso){
                                rightsInfoString = new String(rightsInfoString + "\nverifyRights="+ drmAgent.verifyRights());
                            }

                            if (bVerifyPerso){
                                VODrmRightsInfo[] rights = drmAgent.getRightsInfo();
                                if (null != rights) {
                                    int contentMaturityRating = drmAgent.getContentMaturityRatingThreshold();
                                    int maturityRating = drmAgent.getMaturityRatingThreshold();
                                    int maturityMaxAuthorizeRating = drmAgent.getMaturityRatingMaxAuthorizedThreshold();
                                    String maturityRatingtxt ="\nMaturity Rating: " + maturityRating+"\nMax Authorized Maturity Rating: "+ maturityMaxAuthorizeRating
                                            +"\nContent's Maturity Rating:"+ contentMaturityRating;
                                    Log.d(TAG,maturityRatingtxt);
                                    rightsInfoString+=maturityRatingtxt;

                                    for (int i=0; i<rights.length; i++){
                                        Log.d(TAG, "rights[" + i+"]:\nhasTimeConstraint="+ rights[i].hasTimeConstraint()
                                                +"\nhasCountConstraint="+ rights[i].hasCountConstraint()
                                                +"\nhasIntervalConstraint="+ rights[i].hasIntervalConstraint());
                                        if (0 < i){
                                            //add separator line
                                            rightsInfoString = rightsInfoString + "\n------";
                                        }
                                        if (rights[i].hasTimeConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\nstartTime="+rights[i].getStartTime()
                                                    + ", endtime="+rights[i].getEndTime();
                                        }
                                        if (rights[i].hasCountConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\ninitialCount="+rights[i].getInitialCount()
                                                    + ", countLeft="+rights[i].getCountLeft();
                                        }
                                        if (rights[i].hasIntervalConstraint()){
                                            rightsInfoString = rightsInfoString
                                                    + "\nIntervalPeriodInSeconds="+rights[i].getIntervalPeriodInSeconds();
                                        }
                                    }
                                }
                            }
                            sendNotification(handler,NotificationType.GET_RIGHTS_INFO_DONE, rightsInfoString);
                        } catch (VOException e) {
                            sendNotification(handler,NotificationType.DISPLAY_ERROR, new String("getRightsInfo error:"+e.getStatus().toString()));
                        }
                        break;
                    default:
                        break;
                }
                Log.v(TAG, params.actionType + " finished!");
            }
        }

        private class WidevineTask implements Runnable {
            private ActionParams params = null;
            private VOWidevineAgent widevineAgent = null;

            WidevineTask(ActionParams params) {
                //Log.v(TAG, "PlayReadyTask contentUrl="+ params.contentUrl);
                this.params = params;
                try {
                    player.setDataSource(params.contentUrl);
                    widevineAgent = player.getAgentWidevine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void run() {
                //set parameters for widevine agent
                widevineAgent.setLicenseAcquisitionURL(params.licenseAcquisitionUrl);
                widevineAgent.setLicenseAcquisitionURLParameters(params.licenseAcquisitionUrlParameters);
                try {
                    widevineAgent.setContentIdentifier(params.licenseContentId);
                    widevineAgent.useVOSecurityGateway(params.useVOSecurityGateWay);
                } catch (VOException e) {
                    sendNotification(handler,NotificationType.DISPLAY_ERROR,new String("set parameter error:"+e.getStatus().toString()));
                    return;
                }

                if (null != params.licenseAcquisitionCookie){
                    String[] cookies = new String[1];
                    cookies[0] = params.licenseAcquisitionCookie;
                    widevineAgent.setLicenseAcquisitionCookies(cookies);
                } else {
                    widevineAgent.setLicenseAcquisitionCookies(null);
                }
                Log.v(TAG, params.actionType+ " started");
                switch (params.actionType) {
                    case PERSO_AND_ACQUIRE_RIGHT_IF_NOT_ALREADY_DONE:
                        // Widevine has not personalization, so we just have to acquireRights
                    case ACQUIRE_RIGHTS:
                        try {
                            sendNotification(handler, NotificationType.ACQUIRE_RIGHTS_START);
                            widevineAgent.acquireRights();
                            sendNotification(handler, NotificationType.ACQUIRE_RIGHTS_DONE);
                        } catch (VOException e) {
                            sendNotification(handler, NotificationType.DISPLAY_ERROR,
                                    new String("acquireRights error:" + e.getStatus().toString()));
                        }
                        break;

                    case DELETE_STORE:
                        try {
                            sendNotification(handler, NotificationType.DELETE_STORE_START);
                            widevineAgent.deleteStore();
                            sendNotification(handler, NotificationType.DELETE_STORE_END);
                        } catch (VOException e) {
                            sendNotification(handler, NotificationType.DISPLAY_ERROR,
                                    new String("deleteStore error:" + e.getStatus().toString()));
                        }
                        break;

                    default:
                        sendNotification(handler, NotificationType.DISPLAY_ERROR,
                                new String(params.actionType.toString() + ": Not applicable"));
                }

            }
        }
    }

}
