Files
frameworks_base/graphics/java/android/graphics/Insets.java
Jorim Jaggi f96c90ac6c A brave new world for window insets (1/n)
This CL starts a journey to discover a brave new inset world. The
path to get us there may be rocky, but it's going to be rocky.

One of the main pledges of the new API is that an app can retrieve
what is causing a certain inset easily. For that, we need to
dispatch metadata who is causing what inset, such that we can query
it from the client side.

Furthermore, the client will be able to manipulate insets directly,
but also listen to animation changes. We don't want to go through
window manager for that, thus, there needs to be a local codepath
from (global window state -> WindowInsets).

Because we have these two requirements, we dispatch the relevant
global window state for insets, represented by InsetsState, and
dispatch it to the client. On the client side we take the frame
and the InsetsState and generate WindowInsets out of it.

Bug: 118118435
Test: InsetsSourceTest, InsetsStateTest, InsetsSourceProviderTest,
InsetsStateControllerTest
Change-Id: I2bfe9dda376512916261823fc2ee35cbedeb6731
2018-11-29 13:37:43 +01:00

137 lines
3.9 KiB
Java

/*
* Copyright (C) 2006 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.graphics;
import android.annotation.NonNull;
import android.annotation.Nullable;
/**
* An Insets instance holds four integer offsets which describe changes to the four
* edges of a Rectangle. By convention, positive values move edges towards the
* centre of the rectangle.
* <p>
* Insets are immutable so may be treated as values.
*
*/
public final class Insets {
public static final Insets NONE = new Insets(0, 0, 0, 0);
public final int left;
public final int top;
public final int right;
public final int bottom;
private Insets(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
// Factory methods
/**
* Return an Insets instance with the appropriate values.
*
* @param left the left inset
* @param top the top inset
* @param right the right inset
* @param bottom the bottom inset
*
* @return Insets instance with the appropriate values
*/
public static @NonNull Insets of(int left, int top, int right, int bottom) {
if (left == 0 && top == 0 && right == 0 && bottom == 0) {
return NONE;
}
return new Insets(left, top, right, bottom);
}
/**
* Return an Insets instance with the appropriate values.
*
* @param r the rectangle from which to take the values
*
* @return an Insets instance with the appropriate values
*/
public static @NonNull Insets of(@Nullable Rect r) {
return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
}
/**
* Returns a Rect intance with the appropriate values.
*
* @hide
*/
public @NonNull Rect toRect() {
return new Rect(left, top, right, bottom);
}
/**
* Add two Insets.
*
* @param a The first Insets to add.
* @param b The second Insets to add.
* @return a + b, i. e. all insets on every side are added together.
*/
public static @NonNull Insets add(@NonNull Insets a, @NonNull Insets b) {
return Insets.of(a.left + b.left, a.top + b.top, a.right + b.right, a.bottom + b.bottom);
}
/**
* Two Insets instances are equal iff they belong to the same class and their fields are
* pairwise equal.
*
* @param o the object to compare this instance with.
*
* @return true iff this object is equal {@code o}
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Insets insets = (Insets) o;
if (bottom != insets.bottom) return false;
if (left != insets.left) return false;
if (right != insets.right) return false;
if (top != insets.top) return false;
return true;
}
@Override
public int hashCode() {
int result = left;
result = 31 * result + top;
result = 31 * result + right;
result = 31 * result + bottom;
return result;
}
@Override
public String toString() {
return "Insets{" +
"left=" + left +
", top=" + top +
", right=" + right +
", bottom=" + bottom +
'}';
}
}