Merge "Set eventlistener during MediaCas initialization"
diff --git a/api/current.txt b/api/current.txt
index 524f9a0..4fdbc08 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -25278,6 +25278,7 @@
   public final class MediaCas implements java.lang.AutoCloseable {
     ctor public MediaCas(int) throws android.media.MediaCasException.UnsupportedCasException;
     ctor public MediaCas(@NonNull android.content.Context, int, @Nullable String, int) throws android.media.MediaCasException.UnsupportedCasException;
+    ctor public MediaCas(@NonNull android.content.Context, int, @Nullable String, int, @Nullable android.os.Handler, @Nullable android.media.MediaCas.EventListener) throws android.media.MediaCasException.UnsupportedCasException;
     method public void close();
     method public static android.media.MediaCas.PluginDescriptor[] enumeratePlugins();
     method protected void finalize();
diff --git a/core/api/current.txt b/core/api/current.txt
index 2cd01a6..0516f63 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -25260,6 +25260,7 @@
   public final class MediaCas implements java.lang.AutoCloseable {
     ctor public MediaCas(int) throws android.media.MediaCasException.UnsupportedCasException;
     ctor public MediaCas(@NonNull android.content.Context, int, @Nullable String, int) throws android.media.MediaCasException.UnsupportedCasException;
+    ctor public MediaCas(@NonNull android.content.Context, int, @Nullable String, int, @Nullable android.os.Handler, @Nullable android.media.MediaCas.EventListener) throws android.media.MediaCasException.UnsupportedCasException;
     method public void close();
     method public static android.media.MediaCas.PluginDescriptor[] enumeratePlugins();
     method protected void finalize();
diff --git a/media/java/android/media/MediaCas.java b/media/java/android/media/MediaCas.java
index 4b208ce..5c1f893 100644
--- a/media/java/android/media/MediaCas.java
+++ b/media/java/android/media/MediaCas.java
@@ -676,17 +676,9 @@
         return null;
     }
 
-    /**
-     * Instantiate a CA system of the specified system id.
-     *
-     * @param CA_system_id The system id of the CA system.
-     *
-     * @throws UnsupportedCasException if the device does not support the
-     * specified CA system.
-     */
-    public MediaCas(int CA_system_id) throws UnsupportedCasException {
+    private void createPlugin(int casSystemId) throws UnsupportedCasException {
         try {
-            mCasSystemId = CA_system_id;
+            mCasSystemId = casSystemId;
             mUserId = ActivityManager.getCurrentUser();
             IMediaCasService service = getService();
             android.hardware.cas.V1_2.IMediaCasService serviceV12 =
@@ -696,16 +688,16 @@
                     android.hardware.cas.V1_1.IMediaCasService.castFrom(service);
                 if (serviceV11 == null) {
                     Log.d(TAG, "Used cas@1_0 interface to create plugin");
-                    mICas = service.createPlugin(CA_system_id, mBinder);
+                    mICas = service.createPlugin(casSystemId, mBinder);
                 } else {
                     Log.d(TAG, "Used cas@1.1 interface to create plugin");
-                    mICas = mICasV11 = serviceV11.createPluginExt(CA_system_id, mBinder);
+                    mICas = mICasV11 = serviceV11.createPluginExt(casSystemId, mBinder);
                 }
             } else {
                 Log.d(TAG, "Used cas@1.2 interface to create plugin");
                 mICas = mICasV11 = mICasV12 =
                     android.hardware.cas.V1_2.ICas
-                    .castFrom(serviceV12.createPluginExt(CA_system_id, mBinder));
+                        .castFrom(serviceV12.createPluginExt(casSystemId, mBinder));
             }
         } catch(Exception e) {
             Log.e(TAG, "Failed to create plugin: " + e);
@@ -713,11 +705,37 @@
         } finally {
             if (mICas == null) {
                 throw new UnsupportedCasException(
-                        "Unsupported CA_system_id " + CA_system_id);
+                    "Unsupported casSystemId " + casSystemId);
             }
         }
     }
 
+    private void registerClient(@NonNull Context context,
+            @Nullable String tvInputServiceSessionId,  @PriorityHintUseCaseType int priorityHint)  {
+
+        mTunerResourceManager = (TunerResourceManager)
+            context.getSystemService(Context.TV_TUNER_RESOURCE_MGR_SERVICE);
+        if (mTunerResourceManager != null) {
+            int[] clientId = new int[1];
+            ResourceClientProfile profile =
+                    new ResourceClientProfile(tvInputServiceSessionId, priorityHint);
+            mTunerResourceManager.registerClientProfile(
+                    profile, context.getMainExecutor(), mResourceListener, clientId);
+            mClientId = clientId[0];
+        }
+    }
+    /**
+     * Instantiate a CA system of the specified system id.
+     *
+     * @param casSystemId The system id of the CA system.
+     *
+     * @throws UnsupportedCasException if the device does not support the
+     * specified CA system.
+     */
+    public MediaCas(int casSystemId) throws UnsupportedCasException {
+        createPlugin(casSystemId);
+    }
+
     /**
      * Instantiate a CA system of the specified system id.
      *
@@ -733,19 +751,35 @@
     public MediaCas(@NonNull Context context, int casSystemId,
             @Nullable String tvInputServiceSessionId,
             @PriorityHintUseCaseType int priorityHint) throws UnsupportedCasException {
-        this(casSystemId);
-
         Objects.requireNonNull(context, "context must not be null");
-        mTunerResourceManager = (TunerResourceManager)
-                context.getSystemService(Context.TV_TUNER_RESOURCE_MGR_SERVICE);
-        if (mTunerResourceManager != null) {
-            int[] clientId = new int[1];
-            ResourceClientProfile profile =
-                    new ResourceClientProfile(tvInputServiceSessionId, priorityHint);
-            mTunerResourceManager.registerClientProfile(
-                    profile, context.getMainExecutor(), mResourceListener, clientId);
-            mClientId = clientId[0];
-        }
+        createPlugin(casSystemId);
+        registerClient(context, tvInputServiceSessionId, priorityHint);
+    }
+    /**
+     * Instantiate a CA system of the specified system id with EvenListener.
+     *
+     * @param context the context of the caller.
+     * @param casSystemId The system id of the CA system.
+     * @param tvInputServiceSessionId The Id of the session opened in TV Input Service (TIS)
+     *        {@link android.media.tv.TvInputService#onCreateSession(String, String)}
+     * @param priorityHint priority hint from the use case type for new created CAS system.
+     * @param listener the event listener to be set.
+     * @param handler the handler whose looper the event listener will be called on.
+     * If handler is null, we'll try to use current thread's looper, or the main
+     * looper. If neither are available, an internal thread will be created instead.
+     *
+     * @throws UnsupportedCasException if the device does not support the
+     * specified CA system.
+     */
+    public MediaCas(@NonNull Context context, int casSystemId,
+            @Nullable String tvInputServiceSessionId,
+            @PriorityHintUseCaseType int priorityHint,
+            @Nullable Handler handler, @Nullable EventListener listener)
+            throws UnsupportedCasException {
+        Objects.requireNonNull(context, "context must not be null");
+        setEventListener(listener, handler);
+        createPlugin(casSystemId);
+        registerClient(context, tvInputServiceSessionId, priorityHint);
     }
 
     IHwBinder getBinder() {