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