SoftInputWindow.java revision fe9f8ab03a63b1037f07dd85799fbea80ec6adaa
1/* 2 * Copyright (C) 2007-2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package android.inputmethodservice; 18 19import android.app.Dialog; 20import android.content.Context; 21import android.content.pm.ActivityInfo; 22import android.graphics.Rect; 23import android.os.IBinder; 24import android.view.Gravity; 25import android.view.KeyEvent; 26import android.view.MotionEvent; 27import android.view.View; 28import android.view.WindowManager; 29 30import java.lang.Math; 31 32/** 33 * A SoftInputWindow is a Dialog that is intended to be used for a top-level input 34 * method window. It will be displayed along the edge of the screen, moving 35 * the application user interface away from it so that the focused item is 36 * always visible. 37 */ 38class SoftInputWindow extends Dialog { 39 final KeyEvent.DispatcherState mDispatcherState; 40 private final Rect mBounds = new Rect(); 41 42 public void setToken(IBinder token) { 43 WindowManager.LayoutParams lp = getWindow().getAttributes(); 44 lp.token = token; 45 getWindow().setAttributes(lp); 46 } 47 48 /** 49 * Create a DockWindow that uses a custom style. 50 * 51 * @param context The Context in which the DockWindow should run. In 52 * particular, it uses the window manager and theme from this context 53 * to present its UI. 54 * @param theme A style resource describing the theme to use for the window. 55 * See <a href="{@docRoot}reference/available-resources.html#stylesandthemes">Style 56 * and Theme Resources</a> for more information about defining and 57 * using styles. This theme is applied on top of the current theme in 58 * <var>context</var>. If 0, the default dialog theme will be used. 59 */ 60 public SoftInputWindow(Context context, int theme, 61 KeyEvent.DispatcherState dispatcherState) { 62 super(context, theme); 63 mDispatcherState = dispatcherState; 64 initDockWindow(); 65 } 66 67 @Override 68 public void onWindowFocusChanged(boolean hasFocus) { 69 super.onWindowFocusChanged(hasFocus); 70 mDispatcherState.reset(); 71 } 72 73 @Override 74 public boolean dispatchTouchEvent(MotionEvent ev) { 75 getWindow().getDecorView().getHitRect(mBounds); 76 77 if (ev.isWithinBoundsNoHistory(mBounds.left, mBounds.top, 78 mBounds.right - 1, mBounds.bottom - 1)) { 79 return super.dispatchTouchEvent(ev); 80 } else { 81 MotionEvent temp = ev.clampNoHistory(mBounds.left, mBounds.top, 82 mBounds.right - 1, mBounds.bottom - 1); 83 boolean handled = super.dispatchTouchEvent(temp); 84 temp.recycle(); 85 return handled; 86 } 87 } 88 89 /** 90 * Get the size of the DockWindow. 91 * 92 * @return If the DockWindow sticks to the top or bottom of the screen, the 93 * return value is the height of the DockWindow, and its width is 94 * equal to the width of the screen; If the DockWindow sticks to the 95 * left or right of the screen, the return value is the width of the 96 * DockWindow, and its height is equal to the height of the screen. 97 */ 98 public int getSize() { 99 WindowManager.LayoutParams lp = getWindow().getAttributes(); 100 101 if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) { 102 return lp.height; 103 } else { 104 return lp.width; 105 } 106 } 107 108 /** 109 * Set the size of the DockWindow. 110 * 111 * @param size If the DockWindow sticks to the top or bottom of the screen, 112 * <var>size</var> is the height of the DockWindow, and its width is 113 * equal to the width of the screen; If the DockWindow sticks to the 114 * left or right of the screen, <var>size</var> is the width of the 115 * DockWindow, and its height is equal to the height of the screen. 116 */ 117 public void setSize(int size) { 118 WindowManager.LayoutParams lp = getWindow().getAttributes(); 119 120 if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) { 121 lp.width = -1; 122 lp.height = size; 123 } else { 124 lp.width = size; 125 lp.height = -1; 126 } 127 getWindow().setAttributes(lp); 128 } 129 130 /** 131 * Set which boundary of the screen the DockWindow sticks to. 132 * 133 * @param gravity The boundary of the screen to stick. See {#link 134 * android.view.Gravity.LEFT}, {#link android.view.Gravity.TOP}, 135 * {#link android.view.Gravity.BOTTOM}, {#link 136 * android.view.Gravity.RIGHT}. 137 */ 138 public void setGravity(int gravity) { 139 WindowManager.LayoutParams lp = getWindow().getAttributes(); 140 141 boolean oldIsVertical = (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM); 142 143 lp.gravity = gravity; 144 145 boolean newIsVertical = (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM); 146 147 if (oldIsVertical != newIsVertical) { 148 int tmp = lp.width; 149 lp.width = lp.height; 150 lp.height = tmp; 151 getWindow().setAttributes(lp); 152 } 153 } 154 155 private void initDockWindow() { 156 WindowManager.LayoutParams lp = getWindow().getAttributes(); 157 158 lp.type = WindowManager.LayoutParams.TYPE_INPUT_METHOD; 159 lp.setTitle("InputMethod"); 160 161 lp.gravity = Gravity.BOTTOM; 162 lp.width = -1; 163 // Let the input method window's orientation follow sensor based rotation 164 // Turn this off for now, it is very problematic. 165 //lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER; 166 167 getWindow().setAttributes(lp); 168 getWindow().setFlags( 169 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 170 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 171 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 172 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 173 WindowManager.LayoutParams.FLAG_DIM_BEHIND); 174 } 175} 176