Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee38ec4e authored by Venkatarama Avadhani's avatar Venkatarama Avadhani
Browse files

MediaCas: Reinit the CAS service if killed

When the MediaCasService is killed or dies, allow for it to be
re-initialized with the new instance of the service. The service is
restarted by the system, this code just re-initializes a pointer to this
new service.

Test: atest android.media.misc.cts.MediaCasTest
Bug: 327332440
Change-Id: I9d98c4e200adc4ee987dc8d1e472dfe5ac8f8da2
parent fa33da55
Loading
Loading
Loading
Loading
+34 −21
Original line number Original line Diff line number Diff line
@@ -35,6 +35,7 @@ import android.media.tv.tunerresourcemanager.TunerResourceManager;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.IHwBinder;
import android.os.IHwBinder;
import android.os.Looper;
import android.os.Looper;
import android.os.Message;
import android.os.Message;
@@ -264,25 +265,41 @@ public final class MediaCas implements AutoCloseable {
    public static final int PLUGIN_STATUS_SESSION_NUMBER_CHANGED =
    public static final int PLUGIN_STATUS_SESSION_NUMBER_CHANGED =
            android.hardware.cas.StatusEvent.PLUGIN_SESSION_NUMBER_CHANGED;
            android.hardware.cas.StatusEvent.PLUGIN_SESSION_NUMBER_CHANGED;


    private static final Singleton<IMediaCasService> sService =
    private static IMediaCasService sService = null;
            new Singleton<IMediaCasService>() {
    private static Object sAidlLock = new Object();

    /** DeathListener for AIDL service */
    private static IBinder.DeathRecipient sDeathListener =
            new IBinder.DeathRecipient() {
                @Override
                @Override
                protected IMediaCasService create() {
                public void binderDied() {
                    synchronized (sAidlLock) {
                        Log.d(TAG, "The service is dead");
                        sService.asBinder().unlinkToDeath(sDeathListener, 0);
                        sService = null;
                    }
                }
            };

    static IMediaCasService getService() {
        synchronized (sAidlLock) {
            if (sService == null || !sService.asBinder().isBinderAlive()) {
                try {
                try {
                    Log.d(TAG, "Trying to get AIDL service");
                    Log.d(TAG, "Trying to get AIDL service");
                        IMediaCasService serviceAidl =
                    sService =
                            IMediaCasService.Stub.asInterface(
                            IMediaCasService.Stub.asInterface(
                                    ServiceManager.waitForDeclaredService(
                                    ServiceManager.waitForDeclaredService(
                                            IMediaCasService.DESCRIPTOR + "/default"));
                                            IMediaCasService.DESCRIPTOR + "/default"));
                        if (serviceAidl != null) {
                    if (sService != null) {
                            return serviceAidl;
                        sService.asBinder().linkToDeath(sDeathListener, 0);
                    }
                    }
                } catch (Exception eAidl) {
                } catch (Exception eAidl) {
                    Log.d(TAG, "Failed to get cas AIDL service");
                    Log.d(TAG, "Failed to get cas AIDL service");
                }
                }
                    return null;
            }
            }
            };
            return sService;
        }
    }


    private static final Singleton<android.hardware.cas.V1_0.IMediaCasService> sServiceHidl =
    private static final Singleton<android.hardware.cas.V1_0.IMediaCasService> sServiceHidl =
            new Singleton<android.hardware.cas.V1_0.IMediaCasService>() {
            new Singleton<android.hardware.cas.V1_0.IMediaCasService>() {
@@ -323,10 +340,6 @@ public final class MediaCas implements AutoCloseable {
                }
                }
            };
            };


    static IMediaCasService getService() {
        return sService.get();
    }

    static android.hardware.cas.V1_0.IMediaCasService getServiceHidl() {
    static android.hardware.cas.V1_0.IMediaCasService getServiceHidl() {
        return sServiceHidl.get();
        return sServiceHidl.get();
    }
    }
@@ -756,7 +769,7 @@ public final class MediaCas implements AutoCloseable {
     * @return Whether the specified CA system is supported on this device.
     * @return Whether the specified CA system is supported on this device.
     */
     */
    public static boolean isSystemIdSupported(int CA_system_id) {
    public static boolean isSystemIdSupported(int CA_system_id) {
        IMediaCasService service = sService.get();
        IMediaCasService service = getService();
        if (service != null) {
        if (service != null) {
            try {
            try {
                return service.isSystemIdSupported(CA_system_id);
                return service.isSystemIdSupported(CA_system_id);
@@ -781,7 +794,7 @@ public final class MediaCas implements AutoCloseable {
     * @return an array of descriptors for the available CA plugins.
     * @return an array of descriptors for the available CA plugins.
     */
     */
    public static PluginDescriptor[] enumeratePlugins() {
    public static PluginDescriptor[] enumeratePlugins() {
        IMediaCasService service = sService.get();
        IMediaCasService service = getService();
        if (service != null) {
        if (service != null) {
            try {
            try {
                AidlCasPluginDescriptor[] descriptors = service.enumeratePlugins();
                AidlCasPluginDescriptor[] descriptors = service.enumeratePlugins();