package android.inputmethodservice;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.IBinder;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager;
public class SoftInputWindow extends Dialog {
final String mName;
final Callback mCallback;
final KeyEvent.Callback mKeyEventCallback;
final KeyEvent.DispatcherState mDispatcherState;
final int mWindowType;
final int mGravity;
final boolean mTakesFocus;
private final Rect mBounds = new Rect();
public interface Callback {
public void onBackPressed();
}
public void setToken(IBinder token) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.token = token;
getWindow().setAttributes(lp);
}
public SoftInputWindow(Context context, String name, int theme, Callback callback,
KeyEvent.Callback keyEventCallback, KeyEvent.DispatcherState dispatcherState,
int windowType, int gravity, boolean takesFocus) {
super(context, theme);
mName = name;
mCallback = callback;
mKeyEventCallback = keyEventCallback;
mDispatcherState = dispatcherState;
mWindowType = windowType;
mGravity = gravity;
mTakesFocus = takesFocus;
initDockWindow();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mDispatcherState.reset();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
getWindow().getDecorView().getHitRect(mBounds);
if (ev.isWithinBoundsNoHistory(mBounds.left, mBounds.top,
mBounds.right - 1, mBounds.bottom - 1)) {
return super.dispatchTouchEvent(ev);
} else {
MotionEvent temp = ev.clampNoHistory(mBounds.left, mBounds.top,
mBounds.right - 1, mBounds.bottom - 1);
boolean handled = super.dispatchTouchEvent(temp);
temp.recycle();
return handled;
}
}
public void setGravity(int gravity) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.gravity = gravity;
updateWidthHeight(lp);
getWindow().setAttributes(lp);
}
public int getGravity() {
return getWindow().getAttributes().gravity;
}
private void updateWidthHeight(WindowManager.LayoutParams lp) {
if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) {
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
} else {
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mKeyEventCallback != null && mKeyEventCallback.onKeyDown(keyCode, event)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (mKeyEventCallback != null && mKeyEventCallback.onKeyLongPress(keyCode, event)) {
return true;
}
return super.onKeyLongPress(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (mKeyEventCallback != null && mKeyEventCallback.onKeyUp(keyCode, event)) {
return true;
}
return super.onKeyUp(keyCode, event);
}
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
if (mKeyEventCallback != null && mKeyEventCallback.onKeyMultiple(keyCode, count, event)) {
return true;
}
return super.onKeyMultiple(keyCode, count, event);
}
public void onBackPressed() {
if (mCallback != null) {
mCallback.onBackPressed();
} else {
super.onBackPressed();
}
}
private void initDockWindow() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.type = mWindowType;
lp.setTitle(mName);
lp.gravity = mGravity;
updateWidthHeight(lp);
getWindow().setAttributes(lp);
int windowSetFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
int windowModFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_DIM_BEHIND;
if (!mTakesFocus) {
windowSetFlags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
} else {
windowSetFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
windowModFlags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
}
getWindow().setFlags(windowSetFlags, windowModFlags);
}
}