• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.messaging.util;
17 
18 import android.content.Context;
19 import android.support.annotation.NonNull;
20 import android.view.View;
21 import android.view.inputmethod.InputMethodManager;
22 
23 import com.google.common.annotations.VisibleForTesting;
24 
25 public class ImeUtil {
26     public interface ImeStateObserver {
onImeStateChanged(boolean imeOpen)27         void onImeStateChanged(boolean imeOpen);
28     }
29 
30     public interface ImeStateHost {
onDisplayHeightChanged(int heightMeasureSpec)31         void onDisplayHeightChanged(int heightMeasureSpec);
registerImeStateObserver(ImeUtil.ImeStateObserver observer)32         void registerImeStateObserver(ImeUtil.ImeStateObserver observer);
unregisterImeStateObserver(ImeUtil.ImeStateObserver observer)33         void unregisterImeStateObserver(ImeUtil.ImeStateObserver observer);
isImeOpen()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).
clearInstance()41     public static void clearInstance() {
42         sInstance = null;
43     }
get()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
set(final ImeUtil imeUtil)56     public static void set(final ImeUtil imeUtil) {
57         sInstance = imeUtil;
58     }
59 
hideImeKeyboard(@onNull final Context context, @NonNull final View v)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 
showImeKeyboard(@onNull final Context context, @NonNull final View v)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 
hideSoftInput(@onNull final Context context, @NonNull final View v)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