• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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  * A special purpose layout for the editor extract view for tiny (sub 250dp) screens.
33  * The layout is based on sizes proportional to screen pixel size to provide for the
34  * best layout fidelity on varying pixel sizes and densities.
35  *
36  * @hide
37  */
38 public class CompactExtractEditLayout extends LinearLayout {
39     private View mInputExtractEditText;
40     private View mInputExtractAccessories;
41     private View mInputExtractAction;
42     private boolean mPerformLayoutChanges;
43 
CompactExtractEditLayout(Context context)44     public CompactExtractEditLayout(Context context) {
45         super(context);
46     }
47 
CompactExtractEditLayout(Context context, AttributeSet attrs)48     public CompactExtractEditLayout(Context context, AttributeSet attrs) {
49         super(context, attrs);
50     }
51 
CompactExtractEditLayout(Context context, AttributeSet attrs, int defStyleAttr)52     public CompactExtractEditLayout(Context context, AttributeSet attrs, int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54     }
55 
56     @Override
onFinishInflate()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 
applyFractionInt(@ractionRes int fraction, int whole)69     private int applyFractionInt(@FractionRes int fraction, int whole) {
70         return Math.round(getResources().getFraction(fraction, whole, whole));
71     }
72 
setLayoutHeight(View v, int px)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 
setLayoutMarginBottom(View v, int px)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 
applyProportionalLayout(int screenWidthPx, int screenHeightPx)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
onAttachedToWindow()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             // Percentages must be based on the pixel height of the full (apparent) display height
120             // which is sometimes different from display metrics.
121             //
122             // On a round device, a display height smaller than width indicates a chin (cropped
123             // edge of the display) for which there is no screen buffer allocated. This is
124             // typically 25-35px in height.
125             //
126             // getRootWindowInsets() does not function for InputMethod windows (always null).
127             // Instead just set height to match width if less. This is safe because round wear
128             // devices are by definition 1:1 aspect ratio.
129 
130             if (cfg.isScreenRound() && heightPixels < widthPixels) {
131                 heightPixels = widthPixels;
132             }
133             applyProportionalLayout(widthPixels, heightPixels);
134         }
135     }
136 }
137