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