1/*
2 * Copyright (C) 2015 The Android Open Source Project
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 */
16package com.android.messaging.util;
17
18import android.content.Context;
19import android.support.annotation.NonNull;
20import android.view.View;
21import android.view.inputmethod.InputMethodManager;
22
23import com.google.common.annotations.VisibleForTesting;
24
25public class ImeUtil {
26    public interface ImeStateObserver {
27        void onImeStateChanged(boolean imeOpen);
28    }
29
30    public interface ImeStateHost {
31        void onDisplayHeightChanged(int heightMeasureSpec);
32        void registerImeStateObserver(ImeUtil.ImeStateObserver observer);
33        void unregisterImeStateObserver(ImeUtil.ImeStateObserver observer);
34        boolean isImeOpen();
35    }
36
37    private static volatile ImeUtil sInstance;
38
39    // Used to clear the static cached instance of ImeUtil during testing.  This is necessary
40    // because a previous test may have installed a mocked instance (or vice versa).
41    public static void clearInstance() {
42        sInstance = null;
43    }
44    public static ImeUtil get() {
45        if (sInstance == null) {
46            synchronized (ImeUtil.class) {
47                if (sInstance == null) {
48                    sInstance = new ImeUtil();
49                }
50            }
51        }
52        return sInstance;
53    }
54
55    @VisibleForTesting
56    public static void set(final ImeUtil imeUtil) {
57        sInstance = imeUtil;
58    }
59
60    public void hideImeKeyboard(@NonNull final Context context, @NonNull final View v) {
61        Assert.notNull(context);
62        Assert.notNull(v);
63
64        final InputMethodManager inputMethodManager =
65                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
66        if (inputMethodManager != null) {
67            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0 /* flags */);
68        }
69    }
70
71    public void showImeKeyboard(@NonNull final Context context, @NonNull final View v) {
72        Assert.notNull(context);
73        Assert.notNull(v);
74
75        final InputMethodManager inputMethodManager =
76                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
77        if (inputMethodManager != null) {
78            v.requestFocus();
79            inputMethodManager.showSoftInput(v, 0 /* flags */);
80        }
81    }
82
83    public static void hideSoftInput(@NonNull final Context context, @NonNull final View v) {
84        final InputMethodManager inputMethodManager =
85                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
86        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
87    }
88}
89