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

Commit 6faa9e50 authored by Chris Craik's avatar Chris Craik
Browse files

Add requireViewById

Bug:69461327
Test: atest android.view.cts.ViewTest android.view.cts.WindowTest android.app.cts.DialogTest
Test: make offline-sdk-docs

Didn't add require versions of #onFindViewById in
FragmentContainer/FragmentHostCallback since they're deprecated

Change-Id: I4231e318c92a39cfb82ab1ef8769ade2775d5e3e
parent 464eb00e
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -3758,6 +3758,7 @@ package android.app {
    method public final void requestShowKeyboardShortcuts();
    method public deprecated boolean requestVisibleBehind(boolean);
    method public final boolean requestWindowFeature(int);
    method public final <T extends android.view.View> T requireViewById(int);
    method public final void runOnUiThread(java.lang.Runnable);
    method public void setActionBar(android.widget.Toolbar);
    method public void setContentTransitionManager(android.transition.TransitionManager);
@@ -4452,6 +4453,7 @@ package android.app {
    method public void openOptionsMenu();
    method public void registerForContextMenu(android.view.View);
    method public final boolean requestWindowFeature(int);
    method public final <T extends android.view.View> T requireViewById(int);
    method public void setCancelMessage(android.os.Message);
    method public void setCancelable(boolean);
    method public void setCanceledOnTouchOutside(boolean);
@@ -38171,6 +38173,7 @@ package android.service.dreams {
    method public void onWindowFocusChanged(boolean);
    method public android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback);
    method public android.view.ActionMode onWindowStartingActionMode(android.view.ActionMode.Callback, int);
    method public final <T extends android.view.View> T requireViewById(int);
    method public void setContentView(int);
    method public void setContentView(android.view.View);
    method public void setContentView(android.view.View, android.view.ViewGroup.LayoutParams);
@@ -46487,6 +46490,7 @@ package android.view {
    method public boolean requestRectangleOnScreen(android.graphics.Rect);
    method public boolean requestRectangleOnScreen(android.graphics.Rect, boolean);
    method public final void requestUnbufferedDispatch(android.view.MotionEvent);
    method public final <T extends android.view.View> T requireViewById(int);
    method public static int resolveSize(int, int);
    method public static int resolveSizeAndState(int, int, int);
    method public boolean restoreDefaultFocus();
@@ -47520,6 +47524,7 @@ package android.view {
    method public abstract boolean performPanelShortcut(int, int, android.view.KeyEvent, int);
    method public final void removeOnFrameMetricsAvailableListener(android.view.Window.OnFrameMetricsAvailableListener);
    method public boolean requestFeature(int);
    method public final <T extends android.view.View> T requireViewById(int);
    method public abstract void restoreHierarchyState(android.os.Bundle);
    method public abstract android.os.Bundle saveHierarchyState();
    method public void setAllowEnterTransitionOverlap(boolean);
+25 −0
Original line number Diff line number Diff line
@@ -2587,12 +2587,37 @@ public class Activity extends ContextThemeWrapper
     * @param id the ID to search for
     * @return a view with given ID if found, or {@code null} otherwise
     * @see View#findViewById(int)
     * @see Activity#requireViewById(int)
     */
    @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
        return getWindow().findViewById(id);
    }

    /**
     * Finds a view that was  identified by the {@code android:id} XML attribute that was processed
     * in {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid, or there is
     * no matching view in the hierarchy.
     * <p>
     * <strong>Note:</strong> In most cases -- depending on compiler support --
     * the resulting view is automatically cast to the target class type. If
     * the target class type is unconstrained, an explicit cast may be
     * necessary.
     *
     * @param id the ID to search for
     * @return a view with given ID
     * @see View#requireViewById(int)
     * @see Activity#findViewById(int)
     */
    @NonNull
    public final <T extends View> T requireViewById(@IdRes int id) {
        T view = findViewById(id);
        if (view == null) {
            throw new IllegalArgumentException("ID does not reference a View inside this Activity");
        }
        return view;
    }

    /**
     * Retrieve a reference to this activity's ActionBar.
     *
+30 −5
Original line number Diff line number Diff line
@@ -16,10 +16,6 @@

package android.app;

import com.android.internal.R;
import com.android.internal.app.WindowDecorActionBar;
import com.android.internal.policy.PhoneWindow;

import android.annotation.CallSuper;
import android.annotation.DrawableRes;
import android.annotation.IdRes;
@@ -32,8 +28,8 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.content.res.ResourceId;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@@ -62,6 +58,10 @@ import android.view.Window;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;

import com.android.internal.R;
import com.android.internal.app.WindowDecorActionBar;
import com.android.internal.policy.PhoneWindow;

import java.lang.ref.WeakReference;

/**
@@ -512,12 +512,37 @@ public class Dialog implements DialogInterface, Window.Callback,
     * @param id the ID to search for
     * @return a view with given ID if found, or {@code null} otherwise
     * @see View#findViewById(int)
     * @see Dialog#requireViewById(int)
     */
    @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
        return mWindow.findViewById(id);
    }

    /**
     * Finds the first descendant view with the given ID or throws an IllegalArgumentException if
     * the ID is invalid (< 0), there is no matching view in the hierarchy, or the dialog has not
     * yet been fully created (for example, via {@link #show()} or {@link #create()}).
     * <p>
     * <strong>Note:</strong> In most cases -- depending on compiler support --
     * the resulting view is automatically cast to the target class type. If
     * the target class type is unconstrained, an explicit cast may be
     * necessary.
     *
     * @param id the ID to search for
     * @return a view with given ID
     * @see View#requireViewById(int)
     * @see Dialog#findViewById(int)
     */
    @NonNull
    public final <T extends View> T requireViewById(@IdRes int id) {
        T view = findViewById(id);
        if (view == null) {
            throw new IllegalArgumentException("ID does not reference a View inside this Dialog");
        }
        return view;
    }

    /**
     * Set the screen content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the screen.
+36 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package android.service.dreams;

import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
@@ -54,7 +55,6 @@ import com.android.internal.util.DumpUtils.Dump;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;

/**
 * Extend this class to implement a custom dream (available to the user as a "Daydream").
@@ -458,14 +458,49 @@ public class DreamService extends Service implements Window.Callback {
     * was processed in {@link #onCreate}.
     *
     * <p>Note: Requires a window, do not call before {@link #onAttachedToWindow()}</p>
     * <p>
     * <strong>Note:</strong> In most cases -- depending on compiler support --
     * the resulting view is automatically cast to the target class type. If
     * the target class type is unconstrained, an explicit cast may be
     * necessary.
     *
     * @param id the ID to search for
     * @return The view if found or null otherwise.
     * @see View#findViewById(int)
     * @see DreamService#requireViewById(int)
     */
    @Nullable
    public <T extends View> T findViewById(@IdRes int id) {
        return getWindow().findViewById(id);
    }

    /**
     * Finds a view that was identified by the id attribute from the XML that was processed in
     * {@link #onCreate}, or throws an IllegalArgumentException if the ID is invalid or there is no
     * matching view in the hierarchy.
     *
     * <p>Note: Requires a window, do not call before {@link #onAttachedToWindow()}</p>
     * <p>
     * <strong>Note:</strong> In most cases -- depending on compiler support --
     * the resulting view is automatically cast to the target class type. If
     * the target class type is unconstrained, an explicit cast may be
     * necessary.
     *
     * @param id the ID to search for
     * @return a view with given ID
     * @see View#requireViewById(int)
     * @see DreamService#findViewById(int)
     */
    @NonNull
    public final <T extends View> T requireViewById(@IdRes int id) {
        T view = findViewById(id);
        if (view == null) {
            throw new IllegalArgumentException(
                    "ID does not reference a View inside this DreamService");
        }
        return view;
    }

    /**
     * Marks this dream as interactive to receive input events.
     *
+24 −1
Original line number Diff line number Diff line
@@ -22106,7 +22106,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     *
     * @param id the ID to search for
     * @return a view with given ID if found, or {@code null} otherwise
     * @see View#findViewById(int)
     * @see View#requireViewById(int)
     */
    @Nullable
    public final <T extends View> T findViewById(@IdRes int id) {
@@ -22116,6 +22116,29 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        return findViewTraversal(id);
    }
    /**
     * Finds the first descendant view with the given ID, the view itself if the ID matches
     * {@link #getId()}, or throws an IllegalArgumentException if the ID is invalid or there is no
     * matching view in the hierarchy.
     * <p>
     * <strong>Note:</strong> In most cases -- depending on compiler support --
     * the resulting view is automatically cast to the target class type. If
     * the target class type is unconstrained, an explicit cast may be
     * necessary.
     *
     * @param id the ID to search for
     * @return a view with given ID
     * @see View#findViewById(int)
     */
    @NonNull
    public final <T extends View> T requireViewById(@IdRes int id) {
        T view = findViewById(id);
        if (view == null) {
            throw new IllegalArgumentException("ID does not reference a View inside this View");
        }
        return view;
    }
    /**
     * Finds a view by its unuque and stable accessibility id.
     *
Loading