1 /* 2 * Copyright (C) 2019 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 17 package com.android.inputmethod.leanback; 18 19 import android.os.Handler; 20 import android.view.View; 21 import android.view.accessibility.AccessibilityEvent; 22 import android.view.inputmethod.EditorInfo; 23 24 /** 25 * This class contains common methods used by LeanbackImeService classes 26 */ 27 public class LeanbackUtils { 28 29 private static final int ACCESSIBILITY_DELAY_MS = 250; 30 private static final Handler sAccessibilityHandler = new Handler(); 31 32 /** 33 * checks if the keyCode represents an alphabet char 34 * 35 * @return true if the keyCode represents an alphabet char 36 */ isAlphabet(int keyCode)37 public static boolean isAlphabet(int keyCode) { 38 if (Character.isLetter(keyCode)) { 39 return true; 40 } else { 41 return false; 42 } 43 } 44 45 /** 46 * get the IME action of the current {@link EditText} 47 */ getImeAction(EditorInfo attribute)48 public static int getImeAction(EditorInfo attribute) { 49 return attribute.imeOptions 50 & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION); 51 } 52 53 /** 54 * get the input type class of the current {@link EditText} 55 */ getInputTypeClass(EditorInfo attribute)56 public static int getInputTypeClass(EditorInfo attribute) { 57 return attribute.inputType & EditorInfo.TYPE_MASK_CLASS; 58 } 59 60 /** 61 * get the input type variation of the current {@link EditText} 62 */ getInputTypeVariation(EditorInfo attribute)63 public static int getInputTypeVariation(EditorInfo attribute) { 64 return attribute.inputType & EditorInfo.TYPE_MASK_VARIATION; 65 } 66 sendAccessibilityEvent(final View view, boolean focusGained)67 public static void sendAccessibilityEvent(final View view, boolean focusGained) { 68 if (view != null && focusGained) { 69 sAccessibilityHandler.removeCallbacksAndMessages(null); 70 sAccessibilityHandler.postDelayed(new Runnable() { 71 public void run() { 72 view.sendAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT); 73 } 74 }, ACCESSIBILITY_DELAY_MS); 75 } 76 } 77 } 78