1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (C) 2022 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.inputmethodservice.navigationbar;
import android.annotation.Nullable;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
/**
* Automatically reverses the order of children as they are added.
* Also reverse the width and height values of layout params
*/
class ReverseLinearLayout extends LinearLayout {
/** If true, the layout is reversed vs. a regular linear layout */
private boolean mIsLayoutReverse;
/** If true, the layout is opposite to it's natural reversity from the layout direction */
private boolean mIsAlternativeOrder;
ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
updateOrder();
}
@Override
public void addView(View child) {
reverseParams(child.getLayoutParams(), child, mIsLayoutReverse);
if (mIsLayoutReverse) {
super.addView(child, 0);
} else {
super.addView(child);
}
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
reverseParams(params, child, mIsLayoutReverse);
if (mIsLayoutReverse) {
super.addView(child, 0, params);
} else {
super.addView(child, params);
}
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
updateOrder();
}
public void setAlternativeOrder(boolean alternative) {
mIsAlternativeOrder = alternative;
updateOrder();
}
/**
* In landscape, the LinearLayout is not auto mirrored since it is vertical. Therefore we
* have to do it manually
*/
private void updateOrder() {
boolean isLayoutRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
boolean isLayoutReverse = isLayoutRtl ^ mIsAlternativeOrder;
if (mIsLayoutReverse != isLayoutReverse) {
// reversity changed, swap the order of all views.
int childCount = getChildCount();
ArrayList<View> childList = new ArrayList<>(childCount);
for (int i = 0; i < childCount; i++) {
childList.add(getChildAt(i));
}
removeAllViews();
for (int i = childCount - 1; i >= 0; i--) {
final View child = childList.get(i);
super.addView(child);
}
mIsLayoutReverse = isLayoutReverse;
}
}
private static void reverseParams(ViewGroup.LayoutParams params, View child,
boolean isLayoutReverse) {
if (child instanceof Reversible) {
((Reversible) child).reverse(isLayoutReverse);
}
if (child.getPaddingLeft() == child.getPaddingRight()
&& child.getPaddingTop() == child.getPaddingBottom()) {
child.setPadding(child.getPaddingTop(), child.getPaddingLeft(),
child.getPaddingTop(), child.getPaddingLeft());
}
if (params == null) {
return;
}
int width = params.width;
params.width = params.height;
params.height = width;
}
interface Reversible {
void reverse(boolean isLayoutReverse);
}
public static class ReverseRelativeLayout extends RelativeLayout implements Reversible {
ReverseRelativeLayout(Context context) {
super(context);
}
@Override
public void reverse(boolean isLayoutReverse) {
updateGravity(isLayoutReverse);
reverseGroup(this, isLayoutReverse);
}
private int mDefaultGravity = Gravity.NO_GRAVITY;
public void setDefaultGravity(int gravity) {
mDefaultGravity = gravity;
}
public void updateGravity(boolean isLayoutReverse) {
// Flip gravity if top of bottom is used
if (mDefaultGravity != Gravity.TOP && mDefaultGravity != Gravity.BOTTOM) return;
// Use the default (intended for 270 LTR and 90 RTL) unless layout is otherwise
int gravityToApply = mDefaultGravity;
if (isLayoutReverse) {
gravityToApply = mDefaultGravity == Gravity.TOP ? Gravity.BOTTOM : Gravity.TOP;
}
if (getGravity() != gravityToApply) setGravity(gravityToApply);
}
}
private static void reverseGroup(ViewGroup group, boolean isLayoutReverse) {
for (int i = 0; i < group.getChildCount(); i++) {
final View child = group.getChildAt(i);
reverseParams(child.getLayoutParams(), child, isLayoutReverse);
// Recursively reverse all children
if (child instanceof ViewGroup) {
reverseGroup((ViewGroup) child, isLayoutReverse);
}
}
}
}
|