1/*
2 * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package jp.co.omronsoft.openwnn;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.util.AttributeSet;
23import android.view.Window;
24import android.view.WindowManager;
25import android.widget.LinearLayout;
26
27/**
28 * The view of the base input.
29 */
30public class BaseInputView extends LinearLayout {
31    /** The dialog that opens with long tap */
32    public AlertDialog mOptionsDialog = null;
33
34    /**
35     * Constructor
36     */
37    public BaseInputView(Context context, AttributeSet attrs) {
38        super(context, attrs);
39    }
40
41    /**
42     * Constructor
43     */
44    BaseInputView(Context context) {
45        super(context);
46    }
47
48    /**
49     * Called when the window containing has change its visibility.
50     *
51     * @see android.view.View#onWindowVisibilityChanged(int)
52     */
53    @Override protected void onWindowVisibilityChanged(int visibility) {
54       super.onWindowVisibilityChanged(visibility);
55       if ((visibility != VISIBLE) && (mOptionsDialog != null)) {
56           mOptionsDialog.dismiss();
57       }
58    }
59
60    /**
61     * Show dialog.
62     *
63     * @param builder   the builder of dialog
64     */
65    public void showDialog(AlertDialog.Builder builder) {
66        if (mOptionsDialog != null) {
67            mOptionsDialog.dismiss();
68        }
69
70        mOptionsDialog = builder.create();
71        Window window = mOptionsDialog.getWindow();
72        WindowManager.LayoutParams dialogLayoutParams = window.getAttributes();
73        dialogLayoutParams.token = getWindowToken();
74        dialogLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
75        window.setAttributes(dialogLayoutParams);
76        window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
77
78        mOptionsDialog.show();
79    }
80
81    /**
82     * Close dialog.
83     */
84    public void closeDialog() {
85       if (mOptionsDialog != null) {
86           mOptionsDialog.dismiss();
87           mOptionsDialog = null;
88       }
89    }
90}
91