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