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#onSwitchUser().
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 dismiss Keyguard (if it's shown) */
dismissKeyguard()156     public static String dismissKeyguard() {
157         return "wm dismiss-keyguard";
158     }
159 
160     /** Command to close system dialogs (if shown) */
closeSystemDialog()161     public static String closeSystemDialog() {
162         return "am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS";
163     }
164 
165     /**
166      * Command to send broadcast {@code Intent}.
167      *
168      * @param action action of intent.
169      * @param targetComponent target of intent.
170      * @param extras extra of intent, must be specified as triplet of option flag, key, and value.
171      * @return shell command to send broadcast intent.
172      */
broadcastIntent(String action, String targetComponent, String... extras)173     public static String broadcastIntent(String action, String targetComponent, String... extras) {
174         if (extras.length % 3 != 0) {
175             throw new IllegalArgumentException(
176                     "extras must be triplets: " + Arrays.toString(extras));
177         }
178         final StringBuilder sb = new StringBuilder("am broadcast -a ")
179                 .append(action);
180         for (int index = 0; index < extras.length; index += 3) {
181             final String optionFlag = extras[index];
182             final String extraKey = extras[index + 1];
183             final String extraValue = extras[index + 2];
184             sb.append(" ").append(optionFlag)
185                     .append(" ").append(extraKey)
186                     .append(" ").append(extraValue);
187         }
188         return sb.append(" ").append(targetComponent).toString();
189     }
190 }
191