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

Commit b0a009e1 authored by Cintia Martins's avatar Cintia Martins
Browse files

Conditionally start and stop SupervisionAppServiceFinder

Bug: 422165304
Test: atest CtsAppBindingHostTestCases
Flag: android.app.supervision.flags.enable_supervision_app_service
Change-Id: I9dffb691d9890c2c7e3c6ac3043e8832a4dba102
parent 154bf5eb
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ public class SupervisionManager {
         * @param userId Int ID of the user for whom supervision was enabled.
         * @hide
         */
        public abstract void onSupervisionEnabled(@UserIdInt int userId);
        public void onSupervisionEnabled(@UserIdInt int userId) {}

        /**
         * Called after supervision has been enabled for a given user.
@@ -77,7 +77,7 @@ public class SupervisionManager {
         * @param userId Int ID of the user for whom supervision was enabled.
         * @hide
         */
        public abstract void onSupervisionDisabled(@UserIdInt int userId);
        public void onSupervisionDisabled(@UserIdInt int userId) {}
    }

    private final Context mContext;
+23 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.appbinding;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AppGlobals;
import android.app.supervision.SupervisionManager;
import android.app.supervision.flags.Flags;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
@@ -214,6 +215,28 @@ public class AppBindingService extends Binder {
            case SystemService.PHASE_THIRD_PARTY_APPS_CAN_START:
                onPhaseThirdPartyAppsCanStart();
                break;
            case SystemService.PHASE_SYSTEM_SERVICES_READY:
                if (Flags.enableSupervisionAppService()) {
                    registerSupervisionListener();
                }
                break;
        }
    }

    private void registerSupervisionListener() {
        SupervisionManager supervisionManager =
                mContext.getSystemService(SupervisionManager.class);
        if (supervisionManager != null) {
            SupervisionManager.SupervisionListener listener =
                    new SupervisionManager.SupervisionListener() {
                        @Override
                        public void onSupervisionDisabled(int userId) {
                            synchronized (mLock) {
                                unbindServicesLocked(userId, null, "supervision disabled");
                            }
                        }
                    };
            supervisionManager.registerSupervisionListener(listener);
        }
    }

+49 −1
Original line number Diff line number Diff line
@@ -25,6 +25,10 @@ import android.os.IInterface;

import com.android.server.am.PersistentConnection;
import com.android.server.appbinding.finders.AppServiceFinder;
import com.android.server.utils.Slogf;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Establishes a persistent connection to a given service component for a given user
@@ -36,6 +40,19 @@ public class AppServiceConnection extends PersistentConnection<IInterface> {
    private final AppBindingConstants mConstants;
    private final AppServiceFinder mFinder;

    /**
     * Listener for connection status updates
     * TODO: Refactor this (b/423644620)
     */
    public interface ConnectionStatusListener {
        void onConnected(@NonNull AppServiceConnection connection, @NonNull IInterface service);
        void onDisconnected(@NonNull AppServiceConnection connection);
        void onBinderDied(@NonNull AppServiceConnection connection);
    }

    private final List<ConnectionStatusListener> mConnectionListeners =
            new CopyOnWriteArrayList<>();

    AppServiceConnection(Context context, int userId, AppBindingConstants constants,
            Handler handler, AppServiceFinder finder,
            @NonNull ComponentName componentName) {
@@ -55,10 +72,41 @@ public class AppServiceConnection extends PersistentConnection<IInterface> {

    @Override
    protected IInterface asInterface(IBinder obj) {
        return mFinder.asInterface(obj);
        final IInterface service = mFinder.asInterface(obj);

        if (service != null) {
            // Notify all listeners.
            Slogf.d(TAG, "Service for %s is connected. Notifying listeners.",
                    getComponentName());
            for (ConnectionStatusListener listener : mConnectionListeners) {
                listener.onConnected(this, service);
            }
        } else {
            Slogf.w(TAG, "Service for %s is null.", getComponentName());
        }
        return service;
    }

    public AppServiceFinder getFinder() {
        return mFinder;
    }

    /**
     * Adds a listener to be notified of connection changes.
     * If service is already connected, notify immediately.
     */
    public void addConnectionStatusListener(@NonNull ConnectionStatusListener listener) {
        mConnectionListeners.add(listener);
        final IInterface service = getServiceBinder();
        if (isConnected() && service != null) {
            listener.onConnected(this, service);
        }
    }

    /**
     * Removes an existing listener.
     */
    public void removeConnectionStatusListener(@NonNull ConnectionStatusListener listener) {
        mConnectionListeners.remove(listener);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public abstract class AppServiceFinder<TServiceType, TServiceInterfaceType exten
    }

    /** Whether this service should really be enabled. */
    protected boolean isEnabled(AppBindingConstants constants) {
    protected boolean isEnabled(AppBindingConstants constants, int userId) {
        return true;
    }

@@ -102,7 +102,7 @@ public abstract class AppServiceFinder<TServiceType, TServiceInterfaceType exten
            mTargetServices.put(userId, null);
            mLastMessages.put(userId, null);

            if (!isEnabled(constants)) {
            if (!isEnabled(constants, userId)) {
                final String message = "feature disabled";
                mLastMessages.put(userId, message);
                Slog.i(TAG, getAppDescription() + " " + message);
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ public class CarrierMessagingClientServiceFinder
    }

    @Override
    protected boolean isEnabled(AppBindingConstants constants) {
    protected boolean isEnabled(AppBindingConstants constants, int userId) {
        return constants.SMS_SERVICE_ENABLED
                && mContext.getResources().getBoolean(R.bool.config_useSmsAppService);
    }
Loading