1 /* 2 * Copyright (C) 2017 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 android.inputmethodservice.cts.common.test; 18 19 import java.util.Arrays; 20 21 /** 22 * Utility class for preparing "adb shell" command. 23 */ 24 public final class ShellCommandUtils { 25 26 // This is utility class, can't instantiate. ShellCommandUtils()27 private ShellCommandUtils() {} 28 29 // Copied from android.content.pm.PackageManager#FEATURE_INPUT_METHODS. 30 public static final String FEATURE_INPUT_METHODS = "android.software.input_methods"; 31 32 private static final String SETTING_DEFAULT_IME = "secure default_input_method"; 33 34 /** Command to get ID of current IME. */ getCurrentIme()35 public static String getCurrentIme() { 36 return "settings get " + SETTING_DEFAULT_IME; 37 } 38 39 /** Command to get ID of current IME. */ getCurrentIme(int userId)40 public static String getCurrentIme(int userId) { 41 return String.format("settings --user %d get %s", userId, SETTING_DEFAULT_IME); 42 } 43 44 /** Command to set current IME to {@code imeId} synchronously */ setCurrentImeSync(String imeId)45 public static String setCurrentImeSync(String imeId) { 46 return "ime set " + imeId; 47 } 48 49 /** Command to set current IME to {@code imeId} synchronously for the specified {@code user}*/ setCurrentImeSync(String imeId, int userId)50 public static String setCurrentImeSync(String imeId, int userId) { 51 return String.format("ime set --user %d %s", userId, imeId); 52 } 53 getEnabledImes()54 public static String getEnabledImes() { 55 return "ime list -s"; 56 } 57 getEnabledImes(int userId)58 public static String getEnabledImes(int userId) { 59 return String.format("ime list -s --user %d", userId); 60 } 61 getAvailableImes()62 public static String getAvailableImes() { 63 return "ime list -s -a"; 64 } 65 getAvailableImes(int userId)66 public static String getAvailableImes(int userId) { 67 return String.format("ime list -s -a --user %d", userId); 68 } 69 listPackage(String packageName)70 public static String listPackage(String packageName) { 71 return "pm list package " + packageName; 72 } 73 74 /** Command to enable IME of {@code imeId}. */ enableIme(String imeId)75 public static String enableIme(String imeId) { 76 return "ime enable " + imeId; 77 } 78 79 /** Command to enable IME of {@code imeId} for the specified {@code userId}. */ enableIme(String imeId, int userId)80 public static String enableIme(String imeId, int userId) { 81 return String.format("ime enable --user %d %s", userId, imeId); 82 } 83 84 /** Command to disable IME of {@code imeId}. */ disableIme(String imeId)85 public static String disableIme(String imeId) { 86 return "ime disable " + imeId; 87 } 88 89 /** Command to disable IME of {@code imeId} for the specified {@code userId}. */ disableIme(String imeId, int userId)90 public static String disableIme(String imeId, int userId) { 91 return String.format("ime disable --user %d %s", userId, imeId); 92 } 93 94 /** Command to reset currently selected/enabled IMEs to the default ones. */ resetImes()95 public static String resetImes() { 96 return "ime reset"; 97 } 98 99 /** Command to reset currently selected/enabled IMEs to the default ones for the specified 100 * {@code userId} */ resetImes(int userId)101 public static String resetImes(int userId) { 102 return String.format("ime reset --user %d", userId); 103 } 104 105 /** Command to reset currently selected/enabled IMEs to the default ones for all the users. */ resetImesForAllUsers()106 public static String resetImesForAllUsers() { 107 return "ime reset --user all"; 108 } 109 110 /** Command to delete all records of IME event provider. */ deleteContent(String contentUri)111 public static String deleteContent(String contentUri) { 112 return "content delete --uri " + contentUri; 113 } 114 uninstallPackage(String packageName)115 public static String uninstallPackage(String packageName) { 116 return "pm uninstall " + packageName; 117 } 118 119 /** 120 * Command to uninstall {@code packageName} only for {@code userId}. 121 * 122 * @param packageName package name of the package to be uninstalled. 123 * @param userId user ID to specify the user. 124 */ uninstallPackage(String packageName, int userId)125 public static String uninstallPackage(String packageName, int userId) { 126 return "pm uninstall --user " + userId + " " + packageName; 127 } 128 129 /** 130 * Command to get the last user ID that is specified to 131 * InputMethodManagerService.Lifecycle#onUserSwitching(). 132 * 133 * @return the command to be passed to shell command. 134 */ getLastSwitchUserId()135 public static String getLastSwitchUserId() { 136 return "cmd input_method get-last-switch-user-id"; 137 } 138 139 /** 140 * Command to create a new profile user. 141 * 142 * @param parentUserId parent user to whom the new profile user should belong 143 * @param userName name of the new profile user. 144 * @return the command to be passed to shell command. 145 */ createManagedProfileUser(int parentUserId, String userName)146 public static String createManagedProfileUser(int parentUserId, String userName) { 147 return "pm create-user --profileOf " + parentUserId + " --managed " + userName; 148 } 149 150 /** Command to turn on the display (if it's sleeping). */ wakeUp()151 public static String wakeUp() { 152 return "input keyevent KEYCODE_WAKEUP"; 153 } 154 155 /** Command to turn off the display */ sleepDevice()156 public static String sleepDevice() { 157 return "input keyevent KEYCODE_SLEEP"; 158 } 159 160 /** Command to dismiss Keyguard (if it's shown) */ dismissKeyguard()161 public static String dismissKeyguard() { 162 return "wm dismiss-keyguard"; 163 } 164 165 /** Command to close system dialogs (if shown) */ closeSystemDialog()166 public static String closeSystemDialog() { 167 return "am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS"; 168 } 169 170 /** 171 * Command to unlock screen. 172 * 173 * Note that this command is originated from 174 * {@code android.server.wm.UiDeviceUtils#pressUnlockButton()}, which is only valid for 175 * unlocking insecure keyguard for test automation. 176 */ unlockScreen()177 public static String unlockScreen() { 178 return "input keyevent KEYCODE_MENU"; 179 } 180 181 /** 182 * Command to show IME picker popup window. 183 * 184 * Note that {@code android.view.inputmethod.InputMethodManager#dispatchInputEvent} will handle 185 * KEYCODE_SYM to show IME picker when any input method enabled. 186 */ showImePicker()187 public static String showImePicker() { 188 return "input keyevent KEYCODE_SYM"; 189 } 190 191 /** 192 * Command to enable app-compat change for a package . 193 * 194 * @param compatChange name of the app-compat change. 195 * @param packageName name of the package to enable the change for. 196 * @return the command to be passed to shell command. 197 */ enableCompatChange(String compatChange, String packageName)198 public static String enableCompatChange(String compatChange, String packageName) { 199 return "am compat enable " + compatChange + " " + packageName; 200 } 201 202 /** 203 * Command to send broadcast {@code Intent}. 204 * 205 * @param action action of intent. 206 * @param targetComponent target of intent. 207 * @param extras extra of intent, must be specified as triplet of option flag, key, and value. 208 * @return shell command to send broadcast intent. 209 */ broadcastIntent(String action, String targetComponent, String... extras)210 public static String broadcastIntent(String action, String targetComponent, String... extras) { 211 if (extras.length % 3 != 0) { 212 throw new IllegalArgumentException( 213 "extras must be triplets: " + Arrays.toString(extras)); 214 } 215 final StringBuilder sb = new StringBuilder("am broadcast -a ") 216 .append(action); 217 for (int index = 0; index < extras.length; index += 3) { 218 final String optionFlag = extras[index]; 219 final String extraKey = extras[index + 1]; 220 final String extraValue = extras[index + 2]; 221 sb.append(" ").append(optionFlag) 222 .append(" ").append(extraKey) 223 .append(" ").append(extraValue); 224 } 225 return sb.append(" ").append(targetComponent).toString(); 226 } 227 } 228