Mark Renouf | 91eb265 | 2016-04-11 16:03:26 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.annotation.FractionRes; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.util.DisplayMetrics; |
| 24 | import android.view.Gravity; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.LinearLayout; |
| 28 | |
| 29 | /** |
| 30 | * A special purpose layout for the editor extract view for tiny (sub 250dp) screens. |
| 31 | * The layout is based on sizes proportional to screen pixel size to provide for the |
| 32 | * best layout fidelity on varying pixel sizes and densities. |
| 33 | * |
| 34 | * @hide |
| 35 | */ |
| 36 | public class CompactExtractEditLayout extends LinearLayout { |
| 37 | private View mInputExtractEditText; |
| 38 | private View mInputExtractAccessories; |
| 39 | private View mInputExtractAction; |
| 40 | private boolean mPerformLayoutChanges; |
| 41 | |
| 42 | public CompactExtractEditLayout(Context context) { |
| 43 | super(context); |
| 44 | } |
| 45 | |
| 46 | public CompactExtractEditLayout(Context context, AttributeSet attrs) { |
| 47 | super(context, attrs); |
| 48 | } |
| 49 | |
| 50 | public CompactExtractEditLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 51 | super(context, attrs, defStyleAttr); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected void onFinishInflate() { |
| 56 | super.onFinishInflate(); |
| 57 | mInputExtractEditText = findViewById(com.android.internal.R.id.inputExtractEditText); |
| 58 | mInputExtractAccessories = findViewById(com.android.internal.R.id.inputExtractAccessories); |
| 59 | mInputExtractAction = findViewById(com.android.internal.R.id.inputExtractAction); |
| 60 | |
| 61 | if (mInputExtractEditText != null && mInputExtractAccessories != null |
| 62 | && mInputExtractAction != null) { |
| 63 | mPerformLayoutChanges = true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private int applyFractionInt(@FractionRes int fraction, int whole) { |
| 68 | return Math.round(getResources().getFraction(fraction, whole, whole)); |
| 69 | } |
| 70 | |
| 71 | private static void setLayoutHeight(View v, int px) { |
| 72 | ViewGroup.LayoutParams lp = v.getLayoutParams(); |
| 73 | lp.height = px; |
| 74 | v.setLayoutParams(lp); |
| 75 | } |
| 76 | |
| 77 | private static void setLayoutMarginBottom(View v, int px) { |
| 78 | ViewGroup.MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); |
| 79 | lp.bottomMargin = px; |
| 80 | v.setLayoutParams(lp); |
| 81 | } |
| 82 | |
| 83 | private void applyProportionalLayout(int screenWidthPx, int screenHeightPx) { |
| 84 | if (getResources().getConfiguration().isScreenRound()) { |
| 85 | setGravity(Gravity.BOTTOM); |
| 86 | } |
| 87 | setLayoutHeight(this, applyFractionInt( |
| 88 | com.android.internal.R.fraction.input_extract_layout_height, screenHeightPx)); |
| 89 | |
| 90 | setPadding( |
| 91 | applyFractionInt(com.android.internal.R.fraction.input_extract_layout_padding_left, |
| 92 | screenWidthPx), |
| 93 | 0, |
| 94 | applyFractionInt(com.android.internal.R.fraction.input_extract_layout_padding_right, |
| 95 | screenWidthPx), |
| 96 | 0); |
| 97 | |
| 98 | setLayoutMarginBottom(mInputExtractEditText, |
| 99 | applyFractionInt(com.android.internal.R.fraction.input_extract_text_margin_bottom, |
| 100 | screenHeightPx)); |
| 101 | |
| 102 | setLayoutMarginBottom(mInputExtractAccessories, |
| 103 | applyFractionInt(com.android.internal.R.fraction.input_extract_action_margin_bottom, |
| 104 | screenHeightPx)); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected void onAttachedToWindow() { |
| 109 | super.onAttachedToWindow(); |
| 110 | if (mPerformLayoutChanges) { |
| 111 | Resources res = getResources(); |
| 112 | DisplayMetrics dm = res.getDisplayMetrics(); |
| 113 | int heightPixels = dm.heightPixels; |
| 114 | int widthPixels = dm.widthPixels; |
| 115 | applyProportionalLayout(widthPixels, heightPixels); |
| 116 | } |
| 117 | } |
| 118 | } |