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

Commit 618e0efd authored by Ember Rose's avatar Ember Rose
Browse files

Refactor inspector API

- Remove unused IntFlagMapping.Builder#clear()
- Rename IntFlagMapping#namesOf(int) to #get(int)
- Change signature of IntFlagMapping#get(int) to return a Set
- Add doc comment explaing desing rationale to PropertyReader
- Remove IntEnumMapping in favor of SparseArray. Note that this removes
  the immutability gaurantees of IntEnumMapping.
- Miscelaneous doc fixes

Test: atest IntFlagMappingTest
Bug: 122518089
Change-Id: I94acf03431b238d84afcd74cdbdd347431381c40
parent fb89e1da
Loading
Loading
Loading
Loading
+2 −14
Original line number Diff line number Diff line
@@ -52961,19 +52961,8 @@ package android.view.inspector {
    ctor public InspectionCompanion.UninitializedPropertyMapException();
  }
  public final class IntEnumMapping {
    method public java.lang.String nameOf(int);
  }
  public static final class IntEnumMapping.Builder {
    ctor public IntEnumMapping.Builder();
    method public android.view.inspector.IntEnumMapping.Builder addValue(java.lang.String, int);
    method public android.view.inspector.IntEnumMapping build();
    method public void clear();
  }
  public final class IntFlagMapping {
    method public java.lang.String[] namesOf(int);
    method public java.util.Set<java.lang.String> get(int);
  }
  public static final class IntFlagMapping.Builder {
@@ -52981,7 +52970,6 @@ package android.view.inspector {
    method public android.view.inspector.IntFlagMapping.Builder addFlag(java.lang.String, int);
    method public android.view.inspector.IntFlagMapping.Builder addFlag(java.lang.String, int, int);
    method public android.view.inspector.IntFlagMapping build();
    method public void clear();
  }
  public abstract interface PropertyMapper {
@@ -52993,7 +52981,7 @@ package android.view.inspector {
    method public abstract int mapFloat(java.lang.String, int);
    method public abstract int mapGravity(java.lang.String, int);
    method public abstract int mapInt(java.lang.String, int);
    method public abstract int mapIntEnum(java.lang.String, int, android.view.inspector.IntEnumMapping);
    method public abstract int mapIntEnum(java.lang.String, int, android.util.SparseArray<java.lang.String>);
    method public abstract int mapIntFlag(java.lang.String, int, android.view.inspector.IntFlagMapping);
    method public abstract int mapLong(java.lang.String, int);
    method public abstract int mapObject(java.lang.String, int);
+0 −1
Original line number Diff line number Diff line
@@ -105,7 +105,6 @@ public @interface InspectableProperty {
    /**
     * One entry in an enumeration packed into a primitive {int}.
     *
     * @see IntEnumMapping
     * @hide
     */
    @Target({TYPE})
+0 −118
Original line number Diff line number Diff line
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view.inspector;

import android.annotation.NonNull;
import android.annotation.Nullable;

import java.util.ArrayList;

/**
 * Maps the values of an {int} property to string names for properties that encode enumerations.
 *
 * An {@link InspectionCompanion} may provide an instance of this class to a {@link PropertyMapper}
 * for enumerations packed into primitive {int} properties.
 *
 * This class is immutable, and must be constructed by a {@link Builder}.
 *
 * @see PropertyMapper#mapIntEnum(String, int, IntEnumMapping)
 */
public final class IntEnumMapping {
    private final Value[] mValues;

    /**
     * Map from a property value to a string name.
     *
     * @param value The value of a property
     * @return The name of the enumeration value, null if the value is not mapped
     */
    @Nullable
    public String nameOf(int value) {
        for (Value valueTuple : mValues) {
            if (valueTuple.mValue == value) {
                return valueTuple.mName;
            }
        }

        return null;
    }

    /**
     * Create a new instance from a builder.
     *
     * This constructor is private, use {@link Builder#build()} instead.
     *
     * @param builder A builder to create from
     */
    private IntEnumMapping(Builder builder) {
        mValues = builder.mValues.toArray(new Value[builder.mValues.size()]);
    }

    /**
     * A builder for {@link IntEnumMapping}
     */
    public static final class Builder {
        private final ArrayList<Value> mValues;

        public Builder() {
            mValues = new ArrayList<>();
        }

        /**
         * Add a new entry to this mapping.
         *
         * @param name Name of the enumeration value
         * @param value Int value of the enumeration value
         * @return This builder
         */
        @NonNull
        public Builder addValue(@NonNull String name, int value) {
            mValues.add(new Value(name, value));
            return this;
        }

        /**
         * Clear the builder, allowing for recycling.
         */
        public void clear() {
            mValues.clear();
        }

        /**
         * Build a new {@link IntEnumMapping} from this builder
         *
         * @return A new mapping
         */
        @NonNull
        public IntEnumMapping build() {
            return new IntEnumMapping(this);
        }
    }

    /**
     * Inner class that holds the name and value of an enumeration value.
     */
    private static final class Value {
        @NonNull private final String mName;
        private final int mValue;

        private Value(@NonNull String name, int value) {
            mName = name;
            mValue = value;
        }
    }
}
+15 −16
Original line number Diff line number Diff line
@@ -19,14 +19,20 @@ package android.view.inspector;
import android.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Maps the values of an {int} property to arrays of string for properties that encode flags.
 * Maps the values of an {@code int} property to arrays of string for properties that encode flags.
 *
 * An {@link InspectionCompanion} may provide an instance of this class to a {@link PropertyMapper}
 * for flag values packed into primitive {int} properties.
 * for flag values packed into primitive {@code int} properties.
 *
 * Each flag has a
 * Each flag has a mask and a target value, for non-exclusive flags, the target can also be used as
 * the mask. A given integer value is compared against each flag to find what flags are active for
 * it by bitwise anding it with the mask and comparing the result against the target, that is,
 * {@code (value & mask) == target}.
 *
 * This class is immutable, and must be constructed by a {@link Builder}.
 *
@@ -42,8 +48,8 @@ public final class IntFlagMapping {
     * @return The names of the enabled flags
     */
    @NonNull
    public String[] namesOf(int value) {
        ArrayList<String> enabledFlagNames = new ArrayList<>(mFlags.length);
    public Set<String> get(int value) {
        final Set<String> enabledFlagNames = new HashSet<>(mFlags.length);

        for (Flag flag : mFlags) {
            if (flag.isEnabledFor(value)) {
@@ -51,7 +57,7 @@ public final class IntFlagMapping {
            }
        }

        return enabledFlagNames.toArray(new String[enabledFlagNames.size()]);
        return Collections.unmodifiableSet(enabledFlagNames);
    }

    /**
@@ -81,7 +87,7 @@ public final class IntFlagMapping {
         * The target value will be used as a mask, to handle the common case where flag values
         * are not mutually exclusive. The flag will be considered enabled for a property value if
         * the result of bitwise anding the target and the value equals the target, that is:
         * {(value & target) == target}.
         * {@code (value & target) == target}.
         *
         * @param name The name of the flag
         * @param target The value to compare against
@@ -97,7 +103,7 @@ public final class IntFlagMapping {
         * Add a new flag with a mask.
         *
         * The flag will be considered enabled for a property value if the result of bitwise anding
         * the value and the mask equals the target, that is: {(value & mask) == target}.
         * the value and the mask equals the target, that is: {@code (value & mask) == target}.
         *
         * @param name The name of the flag
         * @param target The value to compare against
@@ -110,13 +116,6 @@ public final class IntFlagMapping {
            return this;
        }

        /**
         * Clear the builder, allowing for recycling.
         */
        public void clear() {
            mFlags.clear();
        }

        /**
         * Build a new {@link IntFlagMapping} from this builder.
         *
@@ -143,7 +142,7 @@ public final class IntFlagMapping {
        }

        /**
         * Compare the supplied property value against the mask and taget.
         * Compare the supplied property value against the mask and target.
         *
         * @param value The value to check
         * @return True if this flag is enabled
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.view.inspector;

import android.annotation.AttrRes;
import android.annotation.NonNull;
import android.util.SparseArray;

/**
 * An interface for mapping the string names of inspectable properties to integer identifiers.
@@ -154,7 +155,7 @@ public interface PropertyMapper {
    int mapIntEnum(
            @NonNull String name,
            @AttrRes int attributeId,
            @NonNull IntEnumMapping mapping);
            @NonNull SparseArray<String> mapping);

    /**
     * Map a string name to an integer ID for a flag set packed into an int property.
Loading