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

Commit aa1a278e authored by Tiger's avatar Tiger
Browse files

Describe requested visibilities in public types (6/n: clean up)

This CL
- removes unused files and imports,
- refines WindowInsets.Type#toString(int),
- reuses WindowInsets.Type#toString(int) in ControllerActivity, and
- refines RemoteInsetsControlTarget#isRequestedVisible.

Bug: 253420890
Bug: 234093736
Test: Open Window Insets Controller and see if ControllableInsetsTypes
      displays expected strings.
Test: Execute `adb shell dumpsys window all` and see if the strings of
      "Requested non-default-visibility types" are as expected.
Change-Id: I2aa041096db9880a32281fa684312c1f43d1c4a3
parent c9275cea
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ import android.view.IWindowSessionCallback;
import android.view.KeyEvent;
import android.view.InputEvent;
import android.view.InsetsState;
import android.view.InsetsVisibilities;
import android.view.MagnificationSpec;
import android.view.MotionEvent;
import android.view.InputChannel;
+0 −19
Original line number Diff line number Diff line
/**
 * Copyright (c) 2021, 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;

parcelable InsetsVisibilities;
+0 −134
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * A collection of visibilities of insets. This is used for carrying the requested visibilities.
 * @hide
 */
public class InsetsVisibilities implements Parcelable {

    private static final int UNSPECIFIED = 0;
    private static final int VISIBLE = 1;
    private static final int INVISIBLE = -1;

    private final int[] mVisibilities = new int[InsetsState.SIZE];

    public InsetsVisibilities() {
    }

    public InsetsVisibilities(InsetsVisibilities other) {
        set(other);
    }

    public InsetsVisibilities(Parcel in) {
        in.readIntArray(mVisibilities);
    }

    /**
     * Copies from another {@link InsetsVisibilities}.
     *
     * @param other an instance of {@link InsetsVisibilities}.
     */
    public void set(InsetsVisibilities other) {
        System.arraycopy(other.mVisibilities, InsetsState.FIRST_TYPE, mVisibilities,
                InsetsState.FIRST_TYPE, InsetsState.SIZE);
    }

    /**
     * Sets a visibility to a type.
     *
     * @param type The {@link @InsetsState.InternalInsetsType}.
     * @param visible {@code true} represents visible; {@code false} represents invisible.
     */
    public void setVisibility(@InsetsState.InternalInsetsType int type, boolean visible) {
        mVisibilities[type] = visible ? VISIBLE : INVISIBLE;
    }

    /**
     * Returns the specified insets visibility of the type. If it has never been specified,
     * this returns the default visibility.
     *
     * @param type The {@link @InsetsState.InternalInsetsType}.
     * @return The specified visibility or the default one if it is not specified.
     */
    public boolean getVisibility(@InsetsState.InternalInsetsType int type) {
        final int visibility = mVisibilities[type];
        return visibility == UNSPECIFIED
                ? InsetsState.getDefaultVisibility(type)
                : visibility == VISIBLE;
    }

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ");
        for (int type = InsetsState.FIRST_TYPE; type <= InsetsState.LAST_TYPE; type++) {
            final int visibility = mVisibilities[type];
            if (visibility != UNSPECIFIED) {
                joiner.add(InsetsState.typeToString(type) + ": "
                        + (visibility == VISIBLE ? "visible" : "invisible"));
            }
        }
        return joiner.toString();
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(mVisibilities);
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof InsetsVisibilities)) {
            return false;
        }
        return Arrays.equals(mVisibilities, ((InsetsVisibilities) other).mVisibilities);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeIntArray(mVisibilities);
    }

    public void readFromParcel(@NonNull Parcel in) {
        in.readIntArray(mVisibilities);
    }

    public static final @NonNull Creator<InsetsVisibilities> CREATOR =
            new Creator<InsetsVisibilities>() {

        public InsetsVisibilities createFromParcel(Parcel in) {
            return new InsetsVisibilities(in);
        }

        public InsetsVisibilities[] newArray(int size) {
            return new InsetsVisibilities[size];
        }
    };
}
+11 −11
Original line number Diff line number Diff line
@@ -1463,37 +1463,37 @@ public final class WindowInsets {
        public static String toString(@InsetsType int types) {
            StringBuilder result = new StringBuilder();
            if ((types & STATUS_BARS) != 0) {
                result.append("statusBars |");
                result.append("statusBars ");
            }
            if ((types & NAVIGATION_BARS) != 0) {
                result.append("navigationBars |");
                result.append("navigationBars ");
            }
            if ((types & CAPTION_BAR) != 0) {
                result.append("captionBar |");
                result.append("captionBar ");
            }
            if ((types & IME) != 0) {
                result.append("ime |");
                result.append("ime ");
            }
            if ((types & SYSTEM_GESTURES) != 0) {
                result.append("systemGestures |");
                result.append("systemGestures ");
            }
            if ((types & MANDATORY_SYSTEM_GESTURES) != 0) {
                result.append("mandatorySystemGestures |");
                result.append("mandatorySystemGestures ");
            }
            if ((types & TAPPABLE_ELEMENT) != 0) {
                result.append("tappableElement |");
                result.append("tappableElement ");
            }
            if ((types & DISPLAY_CUTOUT) != 0) {
                result.append("displayCutout |");
                result.append("displayCutout ");
            }
            if ((types & WINDOW_DECOR) != 0) {
                result.append("windowDecor |");
                result.append("windowDecor ");
            }
            if ((types & SYSTEM_OVERLAYS) != 0) {
                result.append("systemOverlays |");
                result.append("systemOverlays ");
            }
            if (result.length() > 0) {
                result.delete(result.length() - 2, result.length());
                result.delete(result.length() - 1, result.length());
            }
            return result.toString();
        }
+0 −121
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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;

import static android.view.InsetsState.FIRST_TYPE;
import static android.view.InsetsState.LAST_TYPE;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import android.platform.test.annotations.Presubmit;
import android.view.InsetsState.InternalInsetsType;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests for {@link InsetsVisibilities}.
 *
 * <p>Build/Install/Run:
 *  atest FrameworksCoreTests:InsetsVisibilities
 *
 * <p>This test class is a part of Window Manager Service tests and specified in
 * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}.
 */
@Presubmit
@RunWith(AndroidJUnit4.class)
public class InsetsVisibilitiesTest {

    @Test
    public void testEquals() {
        final InsetsVisibilities v1 = new InsetsVisibilities();
        final InsetsVisibilities v2 = new InsetsVisibilities();
        final InsetsVisibilities v3 = new InsetsVisibilities();
        assertEquals(v1, v2);
        assertEquals(v1, v3);

        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            v1.setVisibility(type, false);
            v2.setVisibility(type, false);
        }
        assertEquals(v1, v2);
        assertNotEquals(v1, v3);

        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            v1.setVisibility(type, true);
            v2.setVisibility(type, true);
        }
        assertEquals(v1, v2);
        assertNotEquals(v1, v3);
    }

    @Test
    public void testSet() {
        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            final InsetsVisibilities v1 = new InsetsVisibilities();
            final InsetsVisibilities v2 = new InsetsVisibilities();

            v1.setVisibility(type, true);
            assertNotEquals(v1, v2);

            v2.set(v1);
            assertEquals(v1, v2);

            v2.setVisibility(type, false);
            assertNotEquals(v1, v2);

            v1.set(v2);
            assertEquals(v1, v2);
        }
    }

    @Test
    public void testCopyConstructor() {
        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            final InsetsVisibilities v1 = new InsetsVisibilities();
            v1.setVisibility(type, true);
            final InsetsVisibilities v2 = new InsetsVisibilities(v1);
            assertEquals(v1, v2);

            v2.setVisibility(type, false);
            assertNotEquals(v1, v2);
        }
    }

    @Test
    public void testGetterAndSetter() {
        final InsetsVisibilities v1 = new InsetsVisibilities();
        final InsetsVisibilities v2 = new InsetsVisibilities();

        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            assertEquals(InsetsState.getDefaultVisibility(type), v1.getVisibility(type));
        }

        for (@InternalInsetsType int type = FIRST_TYPE; type <= LAST_TYPE; type++) {
            v1.setVisibility(type, true);
            assertTrue(v1.getVisibility(type));

            v2.setVisibility(type, false);
            assertFalse(v2.getVisibility(type));
        }
    }
}
Loading