1 /* 2 * Copyright (C) 2018 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.tv.settings.inputmethod; 17 18 import android.app.ActivityManager; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.RemoteException; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 import android.text.TextUtils; 25 import android.util.Log; 26 import android.view.inputmethod.InputMethodInfo; 27 import android.view.inputmethod.InputMethodManager; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * Helper class for InputMethod 34 */ 35 public class InputMethodHelper { 36 37 public static final String TAG = "InputMethodHelper"; 38 39 /** 40 * Get list of enabled InputMethod 41 */ getEnabledSystemInputMethodList(Context context)42 public static List<InputMethodInfo> getEnabledSystemInputMethodList(Context context) { 43 InputMethodManager inputMethodManager = 44 (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 45 List<InputMethodInfo> enabledInputMethodInfos = 46 new ArrayList<>(inputMethodManager.getEnabledInputMethodList()); 47 // Filter auxiliary keyboards out 48 enabledInputMethodInfos.removeIf(InputMethodInfo::isAuxiliaryIme); 49 return enabledInputMethodInfos; 50 } 51 52 /** 53 * Get id of default InputMethod 54 */ getDefaultInputMethodId(Context context)55 public static String getDefaultInputMethodId(Context context) { 56 return Settings.Secure.getString(context.getContentResolver(), 57 Settings.Secure.DEFAULT_INPUT_METHOD); 58 } 59 60 /** 61 * Set default InputMethod by id 62 */ setDefaultInputMethodId(Context context, String imid)63 public static void setDefaultInputMethodId(Context context, String imid) { 64 if (imid == null) { 65 throw new IllegalArgumentException("Null ID"); 66 } 67 68 try { 69 int userId = ActivityManager.getService().getCurrentUser().id; 70 Settings.Secure.putStringForUser(context.getContentResolver(), 71 Settings.Secure.DEFAULT_INPUT_METHOD, imid, userId); 72 73 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); 74 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 75 intent.putExtra("input_method_id", imid); 76 context.sendBroadcastAsUser(intent, UserHandle.CURRENT); 77 } catch (RemoteException e) { 78 Log.d(TAG, "set default input method remote exception", e); 79 } 80 } 81 82 /** 83 * Find InputMethod from a List by id. 84 */ findInputMethod(String imid, List<InputMethodInfo> enabledInputMethodInfos)85 public static InputMethodInfo findInputMethod(String imid, 86 List<InputMethodInfo> enabledInputMethodInfos) { 87 for (int i = 0, size = enabledInputMethodInfos.size(); i < size; i++) { 88 final InputMethodInfo info = enabledInputMethodInfos.get(i); 89 final String id = info.getId(); 90 if (TextUtils.equals(id, imid)) { 91 return info; 92 } 93 } 94 return null; 95 } 96 97 /** 98 * Get settings Intent of an InputMethod. 99 */ getInputMethodSettingsIntent(InputMethodInfo imi)100 public static Intent getInputMethodSettingsIntent(InputMethodInfo imi) { 101 final Intent intent; 102 final String settingsActivity = imi.getSettingsActivity(); 103 if (!TextUtils.isEmpty(settingsActivity)) { 104 intent = new Intent(Intent.ACTION_MAIN); 105 intent.setClassName(imi.getPackageName(), settingsActivity); 106 } else { 107 intent = null; 108 } 109 return intent; 110 } 111 } 112