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