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

Commit 8b0cff71 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Give hidden API behavior to legacy apps.

Starting in R, there is a new public API overload that delivers a
flags argument.  Some apps may be relying on a previous hidden API
that delivered a userId argument, and this change is used to control
delivery of the new flags argument in its place.

There are dozens of these hidden API users are in the system UID,
either in the system process or the Settings app, so we hard-code
giving them the legacy behavior, since refactoring would be messy
between internal and AOSP branches.

Also adjust incoming and outgoing method signatures to use slightly
more flexible Collection<Uri>, which has handy methods like
contains() and isEmpty().

Bug: 150939131
Test: atest --test-mapping packages/providers/MediaProvider
Test: atest FrameworksServicesTests:com.android.server.devicepolicy.DevicePolicyManagerTest
Exempt-From-Owner-Approval: trivial refactoring
Change-Id: If6a77449e19215cf1c60d4217e62fc04b0959bfc
parent f46b9c96
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -9882,7 +9882,7 @@ package android.content {
    method public void notifyChange(@NonNull android.net.Uri, @Nullable android.database.ContentObserver);
    method @Deprecated public void notifyChange(@NonNull android.net.Uri, @Nullable android.database.ContentObserver, boolean);
    method public void notifyChange(@NonNull android.net.Uri, @Nullable android.database.ContentObserver, int);
    method public void notifyChange(@NonNull Iterable<android.net.Uri>, @Nullable android.database.ContentObserver, int);
    method public void notifyChange(@NonNull java.util.Collection<android.net.Uri>, @Nullable android.database.ContentObserver, int);
    method @Nullable public final android.content.res.AssetFileDescriptor openAssetFile(@NonNull android.net.Uri, @NonNull String, @Nullable android.os.CancellationSignal) throws java.io.FileNotFoundException;
    method @Nullable public final android.content.res.AssetFileDescriptor openAssetFileDescriptor(@NonNull android.net.Uri, @NonNull String) throws java.io.FileNotFoundException;
    method @Nullable public final android.content.res.AssetFileDescriptor openAssetFileDescriptor(@NonNull android.net.Uri, @NonNull String, @Nullable android.os.CancellationSignal) throws java.io.FileNotFoundException;
@@ -12972,11 +12972,11 @@ package android.database {
    method @Deprecated public final void dispatchChange(boolean);
    method public final void dispatchChange(boolean, @Nullable android.net.Uri);
    method public final void dispatchChange(boolean, @Nullable android.net.Uri, int);
    method public final void dispatchChange(boolean, @NonNull Iterable<android.net.Uri>, int);
    method public final void dispatchChange(boolean, @NonNull java.util.Collection<android.net.Uri>, int);
    method public void onChange(boolean);
    method public void onChange(boolean, @Nullable android.net.Uri);
    method public void onChange(boolean, @Nullable android.net.Uri, int);
    method public void onChange(boolean, @NonNull Iterable<android.net.Uri>, int);
    method public void onChange(boolean, @NonNull java.util.Collection<android.net.Uri>, int);
  }
  public interface CrossProcessCursor extends android.database.Cursor {
+4 −0
Original line number Diff line number Diff line
@@ -69,6 +69,10 @@ package android.app.usage {

package android.content {

  public abstract class ContentResolver {
    method @Deprecated public void notifyChange(@NonNull Iterable<android.net.Uri>, @Nullable android.database.ContentObserver, int);
  }

  public abstract class Context {
    method public abstract android.content.SharedPreferences getSharedPreferences(java.io.File, int);
    method public abstract java.io.File getSharedPreferencesPath(String);
+11 −1
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import java.io.OutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Random;
@@ -2670,6 +2671,15 @@ public abstract class ContentResolver implements ContentInterface {
                ContentProvider.getUserIdFromUri(uri, mContext.getUserId()));
    }

    /** @removed */
    @Deprecated
    public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer,
            @NotifyFlags int flags) {
        final Collection<Uri> asCollection = new ArrayList<>();
        uris.forEach(asCollection::add);
        notifyChange(asCollection, observer, flags);
    }

    /**
     * Notify registered observers that several rows have been updated.
     * <p>
@@ -2694,7 +2704,7 @@ public abstract class ContentResolver implements ContentInterface {
     * @param flags Flags such as {@link #NOTIFY_SYNC_TO_NETWORK} or
     *            {@link #NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS}.
     */
    public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer,
    public void notifyChange(@NonNull Collection<Uri> uris, @Nullable ContentObserver observer,
            @NotifyFlags int flags) {
        Objects.requireNonNull(uris, "uris");

+36 −9
Original line number Diff line number Diff line
@@ -19,6 +19,9 @@ package android.database;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ContentResolver.NotifyFlags;
import android.net.Uri;
@@ -26,12 +29,26 @@ import android.os.Handler;
import android.os.UserHandle;

import java.util.Arrays;
import java.util.Collection;

/**
 * Receives call backs for changes to content.
 * Must be implemented by objects which are added to a {@link ContentObservable}.
 */
public abstract class ContentObserver {
    /**
     * Starting in {@link android.os.Build.VERSION_CODES#R}, there is a new
     * public API overload {@link #onChange(boolean, Uri, int)} that delivers a
     * {@code int flags} argument.
     * <p>
     * Some apps may be relying on a previous hidden API that delivered a
     * {@code int userId} argument, and this change is used to control delivery
     * of the new {@code int flags} argument in its place.
     */
    @ChangeId
    @EnabledAfter(targetSdkVersion=android.os.Build.VERSION_CODES.Q)
    private static final long ADD_CONTENT_OBSERVER_FLAGS = 150939131L;

    private final Object mLock = new Object();
    private Transport mTransport; // guarded by mLock

@@ -164,17 +181,27 @@ public abstract class ContentObserver {
     * @param uris The Uris of the changed content.
     * @param flags Flags indicating details about this change.
     */
    public void onChange(boolean selfChange, @NonNull Iterable<Uri> uris, @NotifyFlags int flags) {
    public void onChange(boolean selfChange, @NonNull Collection<Uri> uris,
            @NotifyFlags int flags) {
        for (Uri uri : uris) {
            onChange(selfChange, uri, flags);
        }
    }

    /** @hide */
    public void onChange(boolean selfChange, @NonNull Iterable<Uri> uris, @NotifyFlags int flags,
            @UserIdInt int userId) {
    public void onChange(boolean selfChange, @NonNull Collection<Uri> uris,
            @NotifyFlags int flags, @UserIdInt int userId) {
        // There are dozens of people relying on the hidden API inside the
        // system UID, so hard-code the old behavior for all of them; for
        // everyone else we gate based on a specific change
        if (!CompatChanges.isChangeEnabled(ADD_CONTENT_OBSERVER_FLAGS)
                || android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
            // Deliver userId through argument to preserve hidden API behavior
            onChange(selfChange, uris, userId);
        } else {
            onChange(selfChange, uris, flags);
        }
    }

    /**
     * Dispatches a change notification to the observer.
@@ -186,7 +213,7 @@ public abstract class ContentObserver {
     *
     * @deprecated Callers should migrate towards using a richer overload that
     *             provides more details about the change, such as
     *             {@link #dispatchChange(boolean, Iterable, int)}.
     *             {@link #dispatchChange(boolean, Collection, int)}.
     */
    @Deprecated
    public final void dispatchChange(boolean selfChange) {
@@ -206,7 +233,7 @@ public abstract class ContentObserver {
     * @param uri The Uri of the changed content.
     */
    public final void dispatchChange(boolean selfChange, @Nullable Uri uri) {
        dispatchChange(selfChange, Arrays.asList(uri), 0, UserHandle.getCallingUserId());
        dispatchChange(selfChange, uri, 0);
    }

    /**
@@ -224,7 +251,7 @@ public abstract class ContentObserver {
     */
    public final void dispatchChange(boolean selfChange, @Nullable Uri uri,
            @NotifyFlags int flags) {
        dispatchChange(selfChange, Arrays.asList(uri), flags, UserHandle.getCallingUserId());
        dispatchChange(selfChange, Arrays.asList(uri), flags);
    }

    /**
@@ -240,13 +267,13 @@ public abstract class ContentObserver {
     * @param uris The Uri of the changed content.
     * @param flags Flags indicating details about this change.
     */
    public final void dispatchChange(boolean selfChange, @NonNull Iterable<Uri> uris,
    public final void dispatchChange(boolean selfChange, @NonNull Collection<Uri> uris,
            @NotifyFlags int flags) {
        dispatchChange(selfChange, uris, flags, UserHandle.getCallingUserId());
    }

    /** @hide */
    public final void dispatchChange(boolean selfChange, @NonNull Iterable<Uri> uris,
    public final void dispatchChange(boolean selfChange, @NonNull Collection<Uri> uris,
            @NotifyFlags int flags, @UserIdInt int userId) {
        if (mHandler == null) {
            onChange(selfChange, uris, flags, userId);
+5 −3
Original line number Diff line number Diff line
@@ -20,10 +20,12 @@ import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.content.ContentResolver.NotifyFlags;
import android.net.Uri;
import android.os.*;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;

import java.util.ArrayList;

import java.util.Collection;

/**
 * Wraps a BulkCursor around an existing Cursor making it remotable.
@@ -81,7 +83,7 @@ public final class CursorToBulkCursorAdaptor extends BulkCursorNative
        }

        @Override
        public void onChange(boolean selfChange, @NonNull Iterable<Uri> uris,
        public void onChange(boolean selfChange, @NonNull Collection<Uri> uris,
                @NotifyFlags int flags, @UserIdInt int userId) {
            // Since we deliver changes from the most-specific to least-specific
            // overloads, we only need to redirect from the most-specific local
Loading