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

Commit f0d9929c authored by Neil Fuller's avatar Neil Fuller
Browse files

Simplify RealLocationTimeZoneProviderProxy

At the time of writing RealLocationTimeZoneProviderProxy, ServiceWatcher
could not accept a Handler. Now it does. Small updates to comments /
annotations made here too.

Test: build / boot / treehugger
Bug: 152746105
Change-Id: I1602dab7c31f07b0f8eb8319f84f56f7ad0f72e3
parent 86e3bd8a
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.content.res.Resources;
import android.os.Binder;
import android.os.Handler;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.os.SystemProperties;
@@ -129,14 +130,17 @@ public class LocationTimeZoneManagerService extends Binder {
    @NonNull private final Context mContext;

    /**
     * The {@link ThreadingDomain} used to supply the {@link android.os.Handler} and shared lock
     * object used by the controller and related components.
     * The {@link ThreadingDomain} used to supply the shared lock object used by the controller and
     * related components.
     *
     * <p>Most operations are executed on the associated handler thread <em>but not all</em>, hence
     * the requirement for additional synchronization using a shared lock.
     */
    @NonNull private final ThreadingDomain mThreadingDomain;

    /** A handler associated with the {@link #mThreadingDomain}. */
    @NonNull private final Handler mHandler;

    /** The shared lock from {@link #mThreadingDomain}. */
    @NonNull private final Object mSharedLock;

@@ -146,7 +150,8 @@ public class LocationTimeZoneManagerService extends Binder {

    LocationTimeZoneManagerService(Context context) {
        mContext = context.createAttributionContext(ATTRIBUTION_TAG);
        mThreadingDomain = new HandlerThreadingDomain(FgThread.getHandler());
        mHandler = FgThread.getHandler();
        mThreadingDomain = new HandlerThreadingDomain(mHandler);
        mSharedLock = mThreadingDomain.getLockObject();
    }

@@ -193,6 +198,7 @@ public class LocationTimeZoneManagerService extends Binder {
        } else {
            proxy = new RealLocationTimeZoneProviderProxy(
                    mContext,
                    mHandler,
                    mThreadingDomain,
                    PRIMARY_LOCATION_TIME_ZONE_SERVICE_ACTION,
                    R.bool.config_enablePrimaryLocationTimeZoneOverlay,
@@ -214,6 +220,7 @@ public class LocationTimeZoneManagerService extends Binder {
        } else {
            proxy = new RealLocationTimeZoneProviderProxy(
                    mContext,
                    mHandler,
                    mThreadingDomain,
                    SECONDARY_LOCATION_TIME_ZONE_SERVICE_ACTION,
                    R.bool.config_enableSecondaryLocationTimeZoneOverlay,
+9 −23
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.service.timezone.ITimeZoneProvider;
@@ -43,20 +44,20 @@ class RealLocationTimeZoneProviderProxy extends LocationTimeZoneProviderProxy {

    @NonNull private final ServiceWatcher mServiceWatcher;

    @GuardedBy("mProxyLock")
    @GuardedBy("mSharedLock")
    @Nullable private ManagerProxy mManagerProxy;

    @GuardedBy("mProxyLock")
    @GuardedBy("mSharedLock")
    @NonNull private TimeZoneProviderRequest mRequest;

    RealLocationTimeZoneProviderProxy(
            @NonNull Context context, @NonNull ThreadingDomain threadingDomain,
            @NonNull String action, int enableOverlayResId,
            int nonOverlayPackageResId) {
            @NonNull Context context, @NonNull Handler handler,
            @NonNull ThreadingDomain threadingDomain, @NonNull String action,
            int enableOverlayResId, int nonOverlayPackageResId) {
        super(context, threadingDomain);
        mManagerProxy = null;
        mRequest = TimeZoneProviderRequest.createStopUpdatesRequest();
        mServiceWatcher = new ServiceWatcher(context, action, this::onBind, this::onUnbind,
        mServiceWatcher = new ServiceWatcher(context, handler, action, this::onBind, this::onUnbind,
                enableOverlayResId, nonOverlayPackageResId);
    }

@@ -76,21 +77,6 @@ class RealLocationTimeZoneProviderProxy extends LocationTimeZoneProviderProxy {
    }

    private void onBind(IBinder binder, ComponentName componentName) {
        processServiceWatcherCallbackOnThreadingDomainThread(() -> onBindOnHandlerThread(binder));
    }

    private void onUnbind() {
        processServiceWatcherCallbackOnThreadingDomainThread(this::onUnbindOnHandlerThread);
    }

    private void processServiceWatcherCallbackOnThreadingDomainThread(@NonNull Runnable runnable) {
        // For simplicity, this code just post()s the runnable to the mThreadingDomain Thread in all
        // cases. This adds a delay if ServiceWatcher and ThreadingDomain happen to be using the
        // same thread, but nothing here should be performance critical.
        mThreadingDomain.post(runnable);
    }

    private void onBindOnHandlerThread(@NonNull IBinder binder) {
        mThreadingDomain.assertCurrentThread();

        ITimeZoneProvider provider = ITimeZoneProvider.Stub.asInterface(binder);
@@ -108,7 +94,7 @@ class RealLocationTimeZoneProviderProxy extends LocationTimeZoneProviderProxy {
        }
    }

    private void onUnbindOnHandlerThread() {
    private void onUnbind() {
        mThreadingDomain.assertCurrentThread();

        synchronized (mSharedLock) {
@@ -129,7 +115,7 @@ class RealLocationTimeZoneProviderProxy extends LocationTimeZoneProviderProxy {
        }
    }

    @GuardedBy("mProxyLock")
    @GuardedBy("mSharedLock")
    private void trySendCurrentRequest() {
        TimeZoneProviderRequest request = mRequest;
        mServiceWatcher.runOnBinder(binder -> {