1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.view;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Arrays;
24 import java.util.StringJoiner;
25 
26 /**
27  * A collection of visibilities of insets. This is used for carrying the requested visibilities.
28  * @hide
29  */
30 public class InsetsVisibilities implements Parcelable {
31 
32     private static final int UNSPECIFIED = 0;
33     private static final int VISIBLE = 1;
34     private static final int INVISIBLE = -1;
35 
36     private final int[] mVisibilities = new int[InsetsState.SIZE];
37 
InsetsVisibilities()38     public InsetsVisibilities() {
39     }
40 
InsetsVisibilities(InsetsVisibilities other)41     public InsetsVisibilities(InsetsVisibilities other) {
42         set(other);
43     }
44 
InsetsVisibilities(Parcel in)45     public InsetsVisibilities(Parcel in) {
46         in.readIntArray(mVisibilities);
47     }
48 
49     /**
50      * Copies from another {@link InsetsVisibilities}.
51      *
52      * @param other an instance of {@link InsetsVisibilities}.
53      */
set(InsetsVisibilities other)54     public void set(InsetsVisibilities other) {
55         System.arraycopy(other.mVisibilities, InsetsState.FIRST_TYPE, mVisibilities,
56                 InsetsState.FIRST_TYPE, InsetsState.SIZE);
57     }
58 
59     /**
60      * Sets a visibility to a type.
61      *
62      * @param type The {@link @InsetsState.InternalInsetsType}.
63      * @param visible {@code true} represents visible; {@code false} represents invisible.
64      */
setVisibility(@nsetsState.InternalInsetsType int type, boolean visible)65     public void setVisibility(@InsetsState.InternalInsetsType int type, boolean visible) {
66         mVisibilities[type] = visible ? VISIBLE : INVISIBLE;
67     }
68 
69     /**
70      * Returns the specified insets visibility of the type. If it has never been specified,
71      * this returns the default visibility.
72      *
73      * @param type The {@link @InsetsState.InternalInsetsType}.
74      * @return The specified visibility or the default one if it is not specified.
75      */
getVisibility(@nsetsState.InternalInsetsType int type)76     public boolean getVisibility(@InsetsState.InternalInsetsType int type) {
77         final int visibility = mVisibilities[type];
78         return visibility == UNSPECIFIED
79                 ? InsetsState.getDefaultVisibility(type)
80                 : visibility == VISIBLE;
81     }
82 
83     @Override
toString()84     public String toString() {
85         StringJoiner joiner = new StringJoiner(", ");
86         for (int type = InsetsState.FIRST_TYPE; type <= InsetsState.LAST_TYPE; type++) {
87             final int visibility = mVisibilities[type];
88             if (visibility != UNSPECIFIED) {
89                 joiner.add(InsetsState.typeToString(type) + ": "
90                         + (visibility == VISIBLE ? "visible" : "invisible"));
91             }
92         }
93         return joiner.toString();
94     }
95 
96     @Override
hashCode()97     public int hashCode() {
98         return Arrays.hashCode(mVisibilities);
99     }
100 
101     @Override
equals(Object other)102     public boolean equals(Object other) {
103         if (!(other instanceof InsetsVisibilities)) {
104             return false;
105         }
106         return Arrays.equals(mVisibilities, ((InsetsVisibilities) other).mVisibilities);
107     }
108 
109     @Override
describeContents()110     public int describeContents() {
111         return 0;
112     }
113 
114     @Override
writeToParcel(Parcel dest, int flags)115     public void writeToParcel(Parcel dest, int flags) {
116         dest.writeIntArray(mVisibilities);
117     }
118 
readFromParcel(@onNull Parcel in)119     public void readFromParcel(@NonNull Parcel in) {
120         in.readIntArray(mVisibilities);
121     }
122 
123     public static final @NonNull Creator<InsetsVisibilities> CREATOR =
124             new Creator<InsetsVisibilities>() {
125 
126         public InsetsVisibilities createFromParcel(Parcel in) {
127             return new InsetsVisibilities(in);
128         }
129 
130         public InsetsVisibilities[] newArray(int size) {
131             return new InsetsVisibilities[size];
132         }
133     };
134 }
135