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