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.os.IBinder;
23import android.view.Gravity;
24import android.view.KeyEvent;
25import android.view.WindowManager;
26
27/**
28 * A SoftInputWindow is a Dialog that is intended to be used for a top-level input
29 * method window.  It will be displayed along the edge of the screen, moving
30 * the application user interface away from it so that the focused item is
31 * always visible.
32 */
33class SoftInputWindow extends Dialog {
34    final KeyEvent.DispatcherState mDispatcherState;
35
36    public void setToken(IBinder token) {
37        WindowManager.LayoutParams lp = getWindow().getAttributes();
38        lp.token = token;
39        getWindow().setAttributes(lp);
40    }
41
42    /**
43     * Create a DockWindow that uses a custom style.
44     *
45     * @param context The Context in which the DockWindow should run. In
46     *        particular, it uses the window manager and theme from this context
47     *        to present its UI.
48     * @param theme A style resource describing the theme to use for the window.
49     *        See <a href="{@docRoot}reference/available-resources.html#stylesandthemes">Style
50     *        and Theme Resources</a> for more information about defining and
51     *        using styles. This theme is applied on top of the current theme in
52     *        <var>context</var>. If 0, the default dialog theme will be used.
53     */
54    public SoftInputWindow(Context context, int theme,
55            KeyEvent.DispatcherState dispatcherState) {
56        super(context, theme);
57        mDispatcherState = dispatcherState;
58        initDockWindow();
59    }
60
61    @Override
62    public void onWindowFocusChanged(boolean hasFocus) {
63        super.onWindowFocusChanged(hasFocus);
64        mDispatcherState.reset();
65    }
66
67    /**
68     * Get the size of the DockWindow.
69     *
70     * @return If the DockWindow sticks to the top or bottom of the screen, the
71     *         return value is the height of the DockWindow, and its width is
72     *         equal to the width of the screen; If the DockWindow sticks to the
73     *         left or right of the screen, the return value is the width of the
74     *         DockWindow, and its height is equal to the height of the screen.
75     */
76    public int getSize() {
77        WindowManager.LayoutParams lp = getWindow().getAttributes();
78
79        if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) {
80            return lp.height;
81        } else {
82            return lp.width;
83        }
84    }
85
86    /**
87     * Set the size of the DockWindow.
88     *
89     * @param size If the DockWindow sticks to the top or bottom of the screen,
90     *        <var>size</var> is the height of the DockWindow, and its width is
91     *        equal to the width of the screen; If the DockWindow sticks to the
92     *        left or right of the screen, <var>size</var> is the width of the
93     *        DockWindow, and its height is equal to the height of the screen.
94     */
95    public void setSize(int size) {
96        WindowManager.LayoutParams lp = getWindow().getAttributes();
97
98        if (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM) {
99            lp.width = -1;
100            lp.height = size;
101        } else {
102            lp.width = size;
103            lp.height = -1;
104        }
105        getWindow().setAttributes(lp);
106    }
107
108    /**
109     * Set which boundary of the screen the DockWindow sticks to.
110     *
111     * @param gravity The boundary of the screen to stick. See {#link
112     *        android.view.Gravity.LEFT}, {#link android.view.Gravity.TOP},
113     *        {#link android.view.Gravity.BOTTOM}, {#link
114     *        android.view.Gravity.RIGHT}.
115     */
116    public void setGravity(int gravity) {
117        WindowManager.LayoutParams lp = getWindow().getAttributes();
118
119        boolean oldIsVertical = (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM);
120
121        lp.gravity = gravity;
122
123        boolean newIsVertical = (lp.gravity == Gravity.TOP || lp.gravity == Gravity.BOTTOM);
124
125        if (oldIsVertical != newIsVertical) {
126            int tmp = lp.width;
127            lp.width = lp.height;
128            lp.height = tmp;
129            getWindow().setAttributes(lp);
130        }
131    }
132
133    private void initDockWindow() {
134        WindowManager.LayoutParams lp = getWindow().getAttributes();
135
136        lp.type = WindowManager.LayoutParams.TYPE_INPUT_METHOD;
137        lp.setTitle("InputMethod");
138
139        lp.gravity = Gravity.BOTTOM;
140        lp.width = -1;
141        // Let the input method window's orientation follow sensor based rotation
142        // Turn this off for now, it is very problematic.
143        //lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
144
145        getWindow().setAttributes(lp);
146        getWindow().setFlags(
147                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
148                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
149                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
150                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
151                WindowManager.LayoutParams.FLAG_DIM_BEHIND);
152    }
153}
154