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 set current IME to {@code imeId}. */
setCurrentIme(final String imeId)40     public static String setCurrentIme(final String imeId) {
41         return "settings put " + SETTING_DEFAULT_IME + " " + imeId;
42     }
43 
44     /** Command to enable IME of {@code imeId}. */
enableIme(final String imeId)45     public static String enableIme(final String imeId) {
46         return "ime enable " + imeId;
47     }
48 
49     /** Command to disable IME of {@code imeId}. */
disableIme(final String imeId)50     public static String disableIme(final String imeId) {
51         return "ime disable " + imeId;
52     }
53 
54     /** Command to reset currently selected/enabled IMEs to the default ones. */
resetImes()55     public static String resetImes() {
56         return "ime reset";
57     }
58 
59     /** Command to delete all records of IME event provider. */
deleteContent(final String contentUri)60     public static String deleteContent(final String contentUri) {
61         return "content delete --uri " + contentUri;
62     }
63 
uninstallPackage(String packageName)64     public static String uninstallPackage(String packageName) {
65         return "pm uninstall " + packageName;
66     }
67 
68     /**
69      * Command to send broadcast {@code Intent}.
70      *
71      * @param action action of intent.
72      * @param targetComponent target of intent.
73      * @param extras extra of intent, must be specified as triplet of option flag, key, and value.
74      * @return shell command to send broadcast intent.
75      */
broadcastIntent(final String action, final String targetComponent, final String... extras)76     public static String broadcastIntent(final String action, final String targetComponent,
77             final String... extras) {
78         if (extras.length % 3 != 0) {
79             throw new IllegalArgumentException(
80                     "extras must be triplets: " + Arrays.toString(extras));
81         }
82         final StringBuilder sb = new StringBuilder("am broadcast -a ")
83                 .append(action);
84         for (int index = 0; index < extras.length; index += 3) {
85             final String optionFlag = extras[index];
86             final String extraKey = extras[index + 1];
87             final String extraValue = extras[index + 2];
88             sb.append(" ").append(optionFlag)
89                     .append(" ").append(extraKey)
90                     .append(" ").append(extraValue);
91         }
92         return sb.append(" ").append(targetComponent).toString();
93     }
94 }
95