Merge "Add public API to use new interfaces from cas@1.1"
diff --git a/Android.bp b/Android.bp
index e19ca84..a3d4b61 100644
--- a/Android.bp
+++ b/Android.bp
@@ -739,6 +739,7 @@
         "game-driver-protos",
         "mediaplayer2-protos",
         "android.hidl.base-V1.0-java",
+        "android.hardware.cas-V1.1-java",
         "android.hardware.cas-V1.0-java",
         "android.hardware.contexthub-V1.0-java",
         "android.hardware.health-V1.0-java-constants",
diff --git a/api/current.txt b/api/current.txt
index 6b88547..2ee5e1e 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -24032,7 +24032,8 @@
   }
 
   public static interface MediaCas.EventListener {
-    method public void onEvent(android.media.MediaCas, int, int, @Nullable byte[]);
+    method public void onEvent(@NonNull android.media.MediaCas, int, int, @Nullable byte[]);
+    method public void onSessionEvent(@NonNull android.media.MediaCas, @NonNull android.media.MediaCas.Session, int, int, @Nullable byte[]);
   }
 
   public static class MediaCas.PluginDescriptor {
@@ -24044,6 +24045,7 @@
     method public void close();
     method public void processEcm(@NonNull byte[], int, int) throws android.media.MediaCasException;
     method public void processEcm(@NonNull byte[]) throws android.media.MediaCasException;
+    method public void sendSessionEvent(int, int, @Nullable byte[]) throws android.media.MediaCasException;
     method public void setPrivateData(@NonNull byte[]) throws android.media.MediaCasException;
   }
 
diff --git a/media/java/android/media/MediaCas.java b/media/java/android/media/MediaCas.java
index ce631a433..d432933 100644
--- a/media/java/android/media/MediaCas.java
+++ b/media/java/android/media/MediaCas.java
@@ -18,8 +18,12 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.hardware.cas.V1_0.*;
+import android.hardware.cas.V1_0.HidlCasPluginDescriptor;
+import android.hardware.cas.V1_1.ICas;
+import android.hardware.cas.V1_1.ICasListener;
+import android.hardware.cas.V1_1.IMediaCasService;
 import android.media.MediaCasException.*;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IHwBinder;
@@ -128,6 +132,9 @@
     private class EventHandler extends Handler
     {
         private static final int MSG_CAS_EVENT = 0;
+        private static final int MSG_CAS_SESSION_EVENT = 1;
+        private static final String SESSION_KEY = "sessionId";
+        private static final String DATA_KEY = "data";
 
         public EventHandler(Looper looper) {
             super(looper);
@@ -138,6 +145,12 @@
             if (msg.what == MSG_CAS_EVENT) {
                 mListener.onEvent(MediaCas.this, msg.arg1, msg.arg2,
                         toBytes((ArrayList<Byte>) msg.obj));
+            } else if (msg.what == MSG_CAS_SESSION_EVENT) {
+                Bundle bundle = msg.getData();
+                ArrayList<Byte> sessionId = toByteArray(bundle.getByteArray(SESSION_KEY));
+                mListener.onSessionEvent(MediaCas.this,
+                        createFromSessionId(sessionId), msg.arg1, msg.arg2,
+                        bundle.getByteArray(DATA_KEY));
             }
         }
     }
@@ -149,6 +162,20 @@
             mEventHandler.sendMessage(mEventHandler.obtainMessage(
                     EventHandler.MSG_CAS_EVENT, event, arg, data));
         }
+        @Override
+        public void onSessionEvent(@NonNull ArrayList<Byte> sessionId,
+                        int event, int arg, @Nullable ArrayList<Byte> data)
+                throws RemoteException {
+            Message msg = mEventHandler.obtainMessage();
+            msg.what = EventHandler.MSG_CAS_SESSION_EVENT;
+            msg.arg1 = event;
+            msg.arg2 = arg;
+            Bundle bundle = new Bundle();
+            bundle.putByteArray(EventHandler.SESSION_KEY, toBytes(sessionId));
+            bundle.putByteArray(EventHandler.DATA_KEY, toBytes(data));
+            msg.setData(bundle);
+            mEventHandler.sendMessage(msg);
+        }
     };
 
     /**
@@ -222,6 +249,20 @@
         }
 
         /**
+         * Query if an object equal current Session object.
+         *
+         * @param obj an object to compare to current Session object.
+         *
+         * @return Whether input object equal current Session object.
+         */
+        public boolean equals(Object obj) {
+            if (obj instanceof Session) {
+                return mSessionId.equals(((Session) obj).mSessionId);
+            }
+            return false;
+        }
+
+        /**
          * Set the private data for a session.
          *
          * @param data byte array of the private data.
@@ -282,6 +323,30 @@
         }
 
         /**
+         * Send a session event to a CA system. The format of the event is
+         * scheme-specific and is opaque to the framework.
+         *
+         * @param event an integer denoting a scheme-specific event to be sent.
+         * @param arg a scheme-specific integer argument for the event.
+         * @param data a byte array containing scheme-specific data for the event.
+         *
+         * @throws IllegalStateException if the MediaCas instance is not valid.
+         * @throws MediaCasException for CAS-specific errors.
+         * @throws MediaCasStateException for CAS-specific state exceptions.
+         */
+        public void sendSessionEvent(int event, int arg, @Nullable byte[] data)
+                throws MediaCasException {
+            validateInternalStates();
+
+            try {
+                MediaCasException.throwExceptionIfNeeded(
+                        mICas.sendSessionEvent(mSessionId, event, arg, toByteArray(data)));
+            } catch (RemoteException e) {
+                cleanupAndRethrowIllegalState();
+            }
+        }
+
+        /**
          * Close the session.
          *
          * @throws IllegalStateException if the MediaCas instance is not valid.
@@ -362,7 +427,7 @@
      */
     public MediaCas(int CA_system_id) throws UnsupportedCasException {
         try {
-            mICas = getService().createPlugin(CA_system_id, mBinder);
+            mICas = getService().createPluginExt(CA_system_id, mBinder);
         } catch(Exception e) {
             Log.e(TAG, "Failed to create plugin: " + e);
             mICas = null;
@@ -388,13 +453,26 @@
         /**
          * Notify the listener of a scheme-specific event from the CA system.
          *
-         * @param MediaCas the MediaCas object to receive this event.
+         * @param mediaCas the MediaCas object to receive this event.
          * @param event an integer whose meaning is scheme-specific.
          * @param arg an integer whose meaning is scheme-specific.
          * @param data a byte array of data whose format and meaning are
          * scheme-specific.
          */
-        void onEvent(MediaCas MediaCas, int event, int arg, @Nullable byte[] data);
+        void onEvent(@NonNull MediaCas mediaCas, int event, int arg, @Nullable byte[] data);
+
+        /**
+         * Notify the listener of a scheme-specific session event from CA system.
+         *
+         * @param mediaCas the MediaCas object to receive this event.
+         * @param session session object which the event is for.
+         * @param event an integer whose meaning is scheme-specific.
+         * @param arg an integer whose meaning is scheme-specific.
+         * @param data a byte array of data whose format and meaning are
+         * scheme-specific.
+         */
+        void onSessionEvent(@NonNull MediaCas mediaCas, @NonNull Session session,
+                int event, int arg, @Nullable byte[] data);
     }
 
     /**
@@ -543,7 +621,7 @@
         }
     }
 
-    /**
+   /**
      * Initiate a provisioning operation for a CA system.
      *
      * @param provisionString string containing information needed for the
@@ -603,4 +681,4 @@
     protected void finalize() {
         close();
     }
-}
\ No newline at end of file
+}