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

Commit a9108a21 authored by Adam Powell's avatar Adam Powell
Browse files

Add View#generateViewId; make RadioGroup use it

Bug 6448164

generateViewId provides a way for applications to generate opaque ID
values suitable for use with View#setId that will not collide with
values generated by aapt for R.id.

Fix a bug where RadioGroup assumes object hash codes will always be
positive.

Change-Id: I3e2870cd672d6061bb465128f428c81aeef0c44b
parent accf721e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -24103,6 +24103,7 @@ package android.view {
    method protected boolean fitSystemWindows(android.graphics.Rect);
    method public android.view.View focusSearch(int);
    method public void forceLayout();
    method public static int generateViewId();
    method public android.view.accessibility.AccessibilityNodeProvider getAccessibilityNodeProvider();
    method public float getAlpha();
    method public android.view.animation.Animation getAnimation();
+21 −0
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * <p>
@@ -3078,6 +3079,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     */
    private boolean mSendingHoverAccessibilityEvents;
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    /**
     * Simple constructor to use when creating a view from code.
     *
@@ -16426,6 +16429,24 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
    public void onResolvedTextAlignmentReset() {
    }
    /**
     * Generate a value suitable for use in {@link #setId(int)}.
     * This value will not collide with ID values generated at build time by aapt for R.id.
     *
     * @return a generated ID value
     */
    public static int generateViewId() {
        for (;;) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
    //
    // Properties
    //
+1 −1
Original line number Diff line number Diff line
@@ -349,7 +349,7 @@ public class RadioGroup extends LinearLayout {
                int id = child.getId();
                // generates an id if it's missing
                if (id == View.NO_ID) {
                    id = child.hashCode();
                    id = View.generateViewId();
                    child.setId(id);
                }
                ((RadioButton) child).setOnCheckedChangeWidgetListener(