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

Commit 41162b58 authored by Patrick Baumann's avatar Patrick Baumann
Browse files

Throw when automatic watching expected but missing

This change adds verifies @Watched fields that are not
automatically watchable and requires a new manual argument
be explicitly set to true for those fields.

Bug: 180418767
Test: Confirm expected warning logs on eng build
Change-Id: I5d44b5989ac854751dcc7cc9e1f37dafc92aeb28
parent 1bec64d1
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -1449,21 +1449,21 @@ public class PackageManagerService extends IPackageManager.Stub
    /** Token for keys in mPendingEnableRollback. */
    private int mPendingEnableRollbackToken = 0;
    @Watched
    @Watched(manual = true)
    volatile boolean mSystemReady;
    @Watched
    @Watched(manual = true)
    private volatile boolean mSafeMode;
    volatile boolean mHasSystemUidErrors;
    @Watched
    private final WatchedSparseBooleanArray mWebInstantAppsDisabled =
            new WatchedSparseBooleanArray();
    @Watched
    @Watched(manual = true)
    private ApplicationInfo mAndroidApplication;
    @Watched
    @Watched(manual = true)
    final ActivityInfo mResolveActivity = new ActivityInfo();
    final ResolveInfo mResolveInfo = new ResolveInfo();
    @Watched
    @Watched(manual = true)
    private ComponentName mResolveComponentName;
    AndroidPackage mPlatformPackage;
    ComponentName mCustomResolverComponentName;
@@ -1479,9 +1479,9 @@ public class PackageManagerService extends IPackageManager.Stub
    final ComponentName mInstantAppResolverSettingsComponent;
    /** Activity used to install instant applications */
    @Watched
    @Watched(manual = true)
    private ActivityInfo mInstantAppInstallerActivity;
    @Watched
    @Watched(manual = true)
    private final ResolveInfo mInstantAppInstallerInfo = new ResolveInfo();
    private final Map<String, Pair<PackageInstalledInfo, IPackageInstallObserver2>>
+15 −12
Original line number Diff line number Diff line
@@ -73,7 +73,8 @@ public interface Watchable {
            return;
        }
        for (Field f : base.getClass().getDeclaredFields()) {
            if (f.getAnnotation(Watched.class) != null) {
            final Watched annotation = f.getAnnotation(Watched.class);
            if (annotation != null) {
                final String fn = base.getClass().getName() + "." + f.getName();
                try {
                    f.setAccessible(true);
@@ -81,25 +82,27 @@ public interface Watchable {
                    if (o instanceof Watchable) {
                        Watchable attr = (Watchable) (o);
                        if (attr != null && !attr.isRegisteredObserver(observer)) {
                            if (logOnly) {
                                Log.e("Watchable", fn + " missing an observer");
                            } else {
                                throw new RuntimeException("Watchable " + fn
                                                           + " missing an observer");
                            }
                            handleVerifyError("Watchable " + fn + " missing an observer", logOnly);
                        }
                    } else if (!annotation.manual()) {
                        handleVerifyError("@Watched annotated field " + fn + " is not a watchable"
                                + " type and is not flagged for manual watching.", logOnly);
                    }
                } catch (IllegalAccessException e) {
                    // The field is protected; ignore it.  Other exceptions that may be thrown by
                    // Field.get() are allowed to roll up.
                    if (logOnly) {
                        Log.e("Watchable", fn + " not visible");
                    } else {
                        throw new RuntimeException("Watchable " + fn + " not visible");
                    handleVerifyError("Watchable " + fn + " not visible", logOnly);
                }
            }
        }
    }

    static void handleVerifyError(String errorMessage, boolean logOnly) {
        if (logOnly) {
            Log.e("Watchable", errorMessage);
        } else {
            throw new RuntimeException(errorMessage);
        }
    }

    /**
+2 −0
Original line number Diff line number Diff line
@@ -29,4 +29,6 @@ import java.lang.annotation.Target;
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Watched {
    /** true if the annotated field is manually tracked */
    boolean manual() default false;
}