2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.commands.ime;
19 import com.android.internal.view.IInputMethodManager;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.util.PrintStreamPrinter;
24 import android.util.Printer;
25 import android.view.inputmethod.InputMethodInfo;
27 import java.util.List;
29 public final class Ime {
30 IInputMethodManager mImm;
32 private String[] mArgs;
34 private String mCurArgData;
36 private static final String IMM_NOT_RUNNING_ERR =
37 "Error: Could not access the Input Method Manager. Is the system running?";
39 public static void main(String[] args) {
43 public void run(String[] args) {
44 if (args.length < 1) {
49 mImm = IInputMethodManager.Stub.asInterface(ServiceManager.getService("input_method"));
51 System.err.println(IMM_NOT_RUNNING_ERR);
59 if ("list".equals(op)) {
64 if ("enable".equals(op)) {
69 if ("disable".equals(op)) {
74 if ("set".equals(op)) {
80 System.err.println("Error: unknown command '" + op + "'");
86 * Execute the list sub-command.
88 private void runList() {
91 boolean brief = false;
92 while ((opt=nextOption()) != null) {
93 if (opt.equals("-a")) {
95 } else if (opt.equals("-s")) {
98 System.err.println("Error: Unknown option: " + opt);
105 List<InputMethodInfo> methods;
108 methods = mImm.getEnabledInputMethodList();
109 } catch (RemoteException e) {
110 System.err.println(e.toString());
111 System.err.println(IMM_NOT_RUNNING_ERR);
116 methods = mImm.getInputMethodList();
117 } catch (RemoteException e) {
118 System.err.println(e.toString());
119 System.err.println(IMM_NOT_RUNNING_ERR);
124 if (methods != null) {
125 Printer pr = new PrintStreamPrinter(System.out);
126 for (int i=0; i<methods.size(); i++) {
127 InputMethodInfo imi = methods.get(i);
129 System.out.println(imi.getId());
131 System.out.println(imi.getId() + ":");
138 private void runSetEnabled(boolean state) {
139 String id = nextArg();
141 System.err.println("Error: no input method ID specified");
147 boolean res = mImm.setInputMethodEnabled(id, state);
149 System.out.println("Input method " + id + ": "
150 + (res ? "already enabled" : "now enabled"));
152 System.out.println("Input method " + id + ": "
153 + (res ? "now disabled" : "already disabled"));
155 } catch (IllegalArgumentException e) {
156 System.err.println("Error: " + e.getMessage());
158 } catch (RemoteException e) {
159 System.err.println(e.toString());
160 System.err.println(IMM_NOT_RUNNING_ERR);
165 private void runSet() {
166 String id = nextArg();
168 System.err.println("Error: no input method ID specified");
174 mImm.setInputMethod(null, id);
175 System.out.println("Input method " + id + " selected");
176 } catch (IllegalArgumentException e) {
177 System.err.println("Error: " + e.getMessage());
179 } catch (RemoteException e) {
180 System.err.println(e.toString());
181 System.err.println(IMM_NOT_RUNNING_ERR);
186 private String nextOption() {
187 if (mNextArg >= mArgs.length) {
190 String arg = mArgs[mNextArg];
191 if (!arg.startsWith("-")) {
195 if (arg.equals("--")) {
198 if (arg.length() > 1 && arg.charAt(1) != '-') {
199 if (arg.length() > 2) {
200 mCurArgData = arg.substring(2);
201 return arg.substring(0, 2);
211 private String nextOptionData() {
212 if (mCurArgData != null) {
215 if (mNextArg >= mArgs.length) {
218 String data = mArgs[mNextArg];
223 private String nextArg() {
224 if (mNextArg >= mArgs.length) {
227 String arg = mArgs[mNextArg];
232 private static void showUsage() {
233 System.err.println("usage: ime list [-a] [-s]");
234 System.err.println(" ime enable ID");
235 System.err.println(" ime disable ID");
236 System.err.println(" ime set ID");
237 System.err.println("");
238 System.err.println("The list command prints all enabled input methods. Use");
239 System.err.println("the -a option to see all input methods. Use");
240 System.err.println("the -s option to see only a single summary line of each.");
241 System.err.println("");
242 System.err.println("The enable command allows the given input method ID to be used.");
243 System.err.println("");
244 System.err.println("The disable command disallows the given input method ID from use.");
245 System.err.println("");
246 System.err.println("The set command switches to the given input method ID.");