blob: 795117e493b606e06370b4f768fa530fec08fb7e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Tadashi G. Takaoka7bd6c202011-01-25 18:02:55 +090021import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.os.IBinder;
23import android.view.Gravity;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070024import android.view.KeyEvent;
Tadashi G. Takaoka7bd6c202011-01-25 18:02:55 +090025import android.view.MotionEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.WindowManager;
27
28/**
29 * A SoftInputWindow is a Dialog that is intended to be used for a top-level input
30 * method window. It will be displayed along the edge of the screen, moving
31 * the application user interface away from it so that the focused item is
32 * always visible.
Dianne Hackbornc03c9162014-05-02 10:45:59 -070033 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 */
Dianne Hackbornc03c9162014-05-02 10:45:59 -070035public class SoftInputWindow extends Dialog {
36 final String mName;
37 final Callback mCallback;
38 final KeyEvent.Callback mKeyEventCallback;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070039 final KeyEvent.DispatcherState mDispatcherState;
Dianne Hackborne30e02f2014-05-27 18:24:45 -070040 final int mWindowType;
41 final int mGravity;
Dianne Hackbornc03c9162014-05-02 10:45:59 -070042 final boolean mTakesFocus;
Tadashi G. Takaoka7bd6c202011-01-25 18:02:55 +090043 private final Rect mBounds = new Rect();
Dianne Hackbornc03c9162014-05-02 10:45:59 -070044
45 public interface Callback {
46 public void onBackPressed();
47 }
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 public void setToken(IBinder token) {
50 WindowManager.LayoutParams lp = getWindow().getAttributes();
51 lp.token = token;
52 getWindow().setAttributes(lp);
53 }
54
55 /**
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070056 * Create a SoftInputWindow that uses a custom style.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 *
58 * @param context The Context in which the DockWindow should run. In
59 * particular, it uses the window manager and theme from this context
60 * to present its UI.
61 * @param theme A style resource describing the theme to use for the window.
62 * See <a href="{@docRoot}reference/available-resources.html#stylesandthemes">Style
63 * and Theme Resources</a> for more information about defining and
64 * using styles. This theme is applied on top of the current theme in
65 * <var>context</var>. If 0, the default dialog theme will be used.
66 */
Dianne Hackbornc03c9162014-05-02 10:45:59 -070067 public SoftInputWindow(Context context, String name, int theme, Callback callback,
68 KeyEvent.Callback keyEventCallback, KeyEvent.DispatcherState dispatcherState,
Dianne Hackborne30e02f2014-05-27 18:24:45 -070069 int windowType, int gravity, boolean takesFocus) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 super(context, theme);
Dianne Hackbornc03c9162014-05-02 10:45:59 -070071 mName = name;
72 mCallback = callback;
73 mKeyEventCallback = keyEventCallback;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070074 mDispatcherState = dispatcherState;
Dianne Hackborne30e02f2014-05-27 18:24:45 -070075 mWindowType = windowType;
76 mGravity = gravity;
Dianne Hackbornc03c9162014-05-02 10:45:59 -070077 mTakesFocus = takesFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 initDockWindow();
79 }
80
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070081 @Override
82 public void onWindowFocusChanged(boolean hasFocus) {
83 super.onWindowFocusChanged(hasFocus);
84 mDispatcherState.reset();
85 }
86
Tadashi G. Takaoka7bd6c202011-01-25 18:02:55 +090087 @Override
88 public boolean dispatchTouchEvent(MotionEvent ev) {
89 getWindow().getDecorView().getHitRect(mBounds);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070090
91 if (ev.isWithinBoundsNoHistory(mBounds.left, mBounds.top,
92 mBounds.right - 1, mBounds.bottom - 1)) {
93 return super.dispatchTouchEvent(ev);
94 } else {
95 MotionEvent temp = ev.clampNoHistory(mBounds.left, mBounds.top,
96 mBounds.right - 1, mBounds.bottom - 1);
97 boolean handled = super.dispatchTouchEvent(temp);
98 temp.recycle();
99 return handled;
100 }
Tadashi G. Takaoka7bd6c202011-01-25 18:02:55 +0900101 }
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * Set which boundary of the screen the DockWindow sticks to.
105 *
106 * @param gravity The boundary of the screen to stick. See {#link
107 * android.view.Gravity.LEFT}, {#link android.view.Gravity.TOP},
108 * {#link android.view.Gravity.BOTTOM}, {#link
109 * android.view.Gravity.RIGHT}.
110 */
111 public void setGravity(int gravity) {
112 WindowManager.LayoutParams lp = getWindow().getAttributes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 lp.gravity = gravity;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700114 updateWidthHeight(lp);
115 getWindow().setAttributes(lp);
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Dianne Hackborn20d94742014-05-29 18:35:45 -0700118 public int getGravity() {
119 return getWindow().getAttributes().gravity;
120 }
121
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700122 private void updateWidthHeight(WindowManager.LayoutParams lp) {
123 if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) {
124 lp.width = WindowManager.LayoutParams.MATCH_PARENT;
125 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
126 } else {
127 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
128 lp.height = WindowManager.LayoutParams.MATCH_PARENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
130 }
131
Dianne Hackbornc03c9162014-05-02 10:45:59 -0700132 public boolean onKeyDown(int keyCode, KeyEvent event) {
133 if (mKeyEventCallback != null && mKeyEventCallback.onKeyDown(keyCode, event)) {
134 return true;
135 }
136 return super.onKeyDown(keyCode, event);
137 }
138
139 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
140 if (mKeyEventCallback != null && mKeyEventCallback.onKeyLongPress(keyCode, event)) {
141 return true;
142 }
143 return super.onKeyLongPress(keyCode, event);
144 }
145
146 public boolean onKeyUp(int keyCode, KeyEvent event) {
147 if (mKeyEventCallback != null && mKeyEventCallback.onKeyUp(keyCode, event)) {
148 return true;
149 }
150 return super.onKeyUp(keyCode, event);
151 }
152
153 public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
154 if (mKeyEventCallback != null && mKeyEventCallback.onKeyMultiple(keyCode, count, event)) {
155 return true;
156 }
157 return super.onKeyMultiple(keyCode, count, event);
158 }
159
160 public void onBackPressed() {
161 if (mCallback != null) {
162 mCallback.onBackPressed();
163 } else {
164 super.onBackPressed();
165 }
166 }
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private void initDockWindow() {
169 WindowManager.LayoutParams lp = getWindow().getAttributes();
170
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700171 lp.type = mWindowType;
Dianne Hackbornc03c9162014-05-02 10:45:59 -0700172 lp.setTitle(mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700174 lp.gravity = mGravity;
175 updateWidthHeight(lp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 getWindow().setAttributes(lp);
Dianne Hackbornc03c9162014-05-02 10:45:59 -0700178
179 int windowSetFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
180 int windowModFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
Dianne Hackbornc03c9162014-05-02 10:45:59 -0700182 WindowManager.LayoutParams.FLAG_DIM_BEHIND;
183
184 if (!mTakesFocus) {
185 windowSetFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
186 } else {
187 windowSetFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
188 windowModFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
189 }
190
191 getWindow().setFlags(windowSetFlags, windowModFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193}