| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.res.Configuration; |
| 21 |
import android.content.res.Resources; |
| 22 |
import android.annotation.FractionRes; |
| 23 |
import android.util.AttributeSet; |
| 24 |
import android.util.DisplayMetrics; |
| 25 |
import android.view.Gravity; |
| 26 |
import android.view.View; |
| 27 |
import android.view.ViewGroup; |
| 28 |
import android.view.WindowInsets; |
| 29 |
import android.widget.LinearLayout; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class CompactExtractEditLayout extends LinearLayout { |
| 39 |
private View mInputExtractEditText; |
| 40 |
private View mInputExtractAccessories; |
| 41 |
private View mInputExtractAction; |
| 42 |
private boolean mPerformLayoutChanges; |
| 43 |
|
| 44 |
public CompactExtractEditLayout(Context context) { |
| 45 |
super(context); |
| 46 |
} |
| 47 |
|
| 48 |
public CompactExtractEditLayout(Context context, AttributeSet attrs) { |
| 49 |
super(context, attrs); |
| 50 |
} |
| 51 |
|
| 52 |
public CompactExtractEditLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 53 |
super(context, attrs, defStyleAttr); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected void onFinishInflate() { |
| 58 |
super.onFinishInflate(); |
| 59 |
mInputExtractEditText = findViewById(com.android.internal.R.id.inputExtractEditText); |
| 60 |
mInputExtractAccessories = findViewById(com.android.internal.R.id.inputExtractAccessories); |
| 61 |
mInputExtractAction = findViewById(com.android.internal.R.id.inputExtractAction); |
| 62 |
|
| 63 |
if (mInputExtractEditText != null && mInputExtractAccessories != null |
| 64 |
&& mInputExtractAction != null) { |
| 65 |
mPerformLayoutChanges = true; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
private int applyFractionInt(@FractionRes int fraction, int whole) { |
| 70 |
return Math.round(getResources().getFraction(fraction, whole, whole)); |
| 71 |
} |
| 72 |
|
| 73 |
private static void setLayoutHeight(View v, int px) { |
| 74 |
ViewGroup.LayoutParams lp = v.getLayoutParams(); |
| 75 |
lp.height = px; |
| 76 |
v.setLayoutParams(lp); |
| 77 |
} |
| 78 |
|
| 79 |
private static void setLayoutMarginBottom(View v, int px) { |
| 80 |
ViewGroup.MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); |
| 81 |
lp.bottomMargin = px; |
| 82 |
v.setLayoutParams(lp); |
| 83 |
} |
| 84 |
|
| 85 |
private void applyProportionalLayout(int screenWidthPx, int screenHeightPx) { |
| 86 |
if (getResources().getConfiguration().isScreenRound()) { |
| 87 |
setGravity(Gravity.BOTTOM); |
| 88 |
} |
| 89 |
setLayoutHeight(this, applyFractionInt( |
| 90 |
com.android.internal.R.fraction.input_extract_layout_height, screenHeightPx)); |
| 91 |
|
| 92 |
setPadding( |
| 93 |
applyFractionInt(com.android.internal.R.fraction.input_extract_layout_padding_left, |
| 94 |
screenWidthPx), |
| 95 |
0, |
| 96 |
applyFractionInt(com.android.internal.R.fraction.input_extract_layout_padding_right, |
| 97 |
screenWidthPx), |
| 98 |
0); |
| 99 |
|
| 100 |
setLayoutMarginBottom(mInputExtractEditText, |
| 101 |
applyFractionInt(com.android.internal.R.fraction.input_extract_text_margin_bottom, |
| 102 |
screenHeightPx)); |
| 103 |
|
| 104 |
setLayoutMarginBottom(mInputExtractAccessories, |
| 105 |
applyFractionInt(com.android.internal.R.fraction.input_extract_action_margin_bottom, |
| 106 |
screenHeightPx)); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
protected void onAttachedToWindow() { |
| 111 |
super.onAttachedToWindow(); |
| 112 |
if (mPerformLayoutChanges) { |
| 113 |
Resources res = getResources(); |
| 114 |
Configuration cfg = res.getConfiguration(); |
| 115 |
DisplayMetrics dm = res.getDisplayMetrics(); |
| 116 |
int widthPixels = dm.widthPixels; |
| 117 |
int heightPixels = dm.heightPixels; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if (cfg.isScreenRound() && heightPixels < widthPixels) { |
| 131 |
heightPixels = widthPixels; |
| 132 |
} |
| 133 |
applyProportionalLayout(widthPixels, heightPixels); |
| 134 |
} |
| 135 |
} |
| 136 |
} |