blob: fce10862999987a82c089221fd3c1d66690a99da [file] [log] [blame]
Brad Ebinger4dc095a2018-04-03 15:17:52 -07001/*
2 * Copyright (C) 2018 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
17package com.android.phone;
18
19import android.os.RemoteException;
20import android.os.ShellCommand;
21import android.telephony.SubscriptionManager;
22import android.util.Log;
23
24import com.android.internal.telephony.ITelephony;
25
26import java.io.PrintWriter;
27
28/**
29 * Takes actions based on the adb commands given by "adb shell cmd phone ...". Be careful, no
30 * permission checks have been done before onCommand was called. Make sure any commands processed
31 * here also contain the appropriate permissions checks.
32 */
33
34public class TelephonyShellCommand extends ShellCommand {
35
36 private static final String LOG_TAG = "TelephonyShellCommand";
37 // Don't commit with this true.
38 private static final boolean VDBG = true;
39
40 private static final String IMS_SUBCOMMAND = "ims";
41 private static final String IMS_SET_CARRIER_SERVICE = "set-ims-service";
42 private static final String IMS_GET_CARRIER_SERVICE = "get-ims-service";
43 private static final String IMS_ENABLE = "enable";
44 private static final String IMS_DISABLE = "disable";
45
46 // Take advantage of existing methods that already contain permissions checks when possible.
47 private final ITelephony mInterface;
48
49 public TelephonyShellCommand(ITelephony binder) {
50 mInterface = binder;
51 }
52
53 @Override
54 public int onCommand(String cmd) {
55 if (cmd == null) {
56 return handleDefaultCommands(null);
57 }
58
59 switch (cmd) {
60 case IMS_SUBCOMMAND: {
61 return handleImsCommand();
62 }
63 default: {
64 return handleDefaultCommands(cmd);
65 }
66 }
67 }
68
69 @Override
70 public void onHelp() {
71 PrintWriter pw = getOutPrintWriter();
72 pw.println("Telephony Commands:");
73 pw.println(" help");
74 pw.println(" Print this help text.");
75 pw.println(" ims");
76 pw.println(" IMS Commands.");
77 onHelpIms();
78 }
79
80 private void onHelpIms() {
81 PrintWriter pw = getOutPrintWriter();
82 pw.println("IMS Commands:");
83 pw.println(" ims set-ims-service [-s SLOT_ID] (-c | -d) PACKAGE_NAME");
84 pw.println(" Sets the ImsService defined in PACKAGE_NAME to to be the bound");
85 pw.println(" ImsService. Options are:");
86 pw.println(" -s: the slot ID that the ImsService should be bound for. If no option");
87 pw.println(" is specified, it will choose the default voice SIM slot.");
88 pw.println(" -c: Override the ImsService defined in the carrier configuration.");
89 pw.println(" -d: Override the ImsService defined in the device overlay.");
90 pw.println(" ims get-ims-service [-s SLOT_ID] [-c | -d]");
91 pw.println(" Gets the package name of the currently defined ImsService.");
92 pw.println(" Options are:");
93 pw.println(" -s: The SIM slot ID for the registered ImsService. If no option");
94 pw.println(" is specified, it will choose the default voice SIM slot.");
95 pw.println(" -c: The ImsService defined as the carrier configured ImsService.");
96 pw.println(" -c: The ImsService defined as the device default ImsService.");
97 pw.println(" ims enable [-s SLOT_ID]");
98 pw.println(" enables IMS for the SIM slot specified, or for the default voice SIM slot");
99 pw.println(" if none is specified.");
100 pw.println(" ims disable [-s SLOT_ID]");
101 pw.println(" disables IMS for the SIM slot specified, or for the default voice SIM");
102 pw.println(" slot if none is specified.");
103 }
104
105 private int handleImsCommand() {
106 String arg = getNextArg();
107 if (arg == null) {
108 onHelpIms();
109 return 0;
110 }
111
112 switch (arg) {
113 case IMS_SET_CARRIER_SERVICE: {
114 return handleImsSetServiceCommand();
115 }
116 case IMS_GET_CARRIER_SERVICE: {
117 return handleImsGetServiceCommand();
118 }
119 case IMS_ENABLE: {
120 return handleEnableIms();
121 }
122 case IMS_DISABLE: {
123 return handleDisableIms();
124 }
125 }
126
127 return -1;
128 }
129
130 // ims set-ims-service
131 private int handleImsSetServiceCommand() {
132 PrintWriter errPw = getErrPrintWriter();
133 int slotId = SubscriptionManager.getDefaultVoicePhoneId();
134 Boolean isCarrierService = null;
135
136 String opt;
137 while ((opt = getNextOption()) != null) {
138 switch (opt) {
139 case "-s": {
140 try {
141 slotId = Integer.parseInt(getNextArgRequired());
142 } catch (NumberFormatException e) {
143 errPw.println("ims set-ims-service requires an integer as a SLOT_ID.");
144 return -1;
145 }
146 break;
147 }
148 case "-c": {
149 isCarrierService = true;
150 break;
151 }
152 case "-d": {
153 isCarrierService = false;
154 break;
155 }
156 }
157 }
158 // Mandatory param, either -c or -d
159 if (isCarrierService == null) {
160 errPw.println("ims set-ims-service requires either \"-c\" or \"-d\" to be set.");
161 return -1;
162 }
163
164 String packageName = getNextArg();
165
166 try {
167 if (packageName == null) {
168 packageName = "";
169 }
170 boolean result = mInterface.setImsService(slotId, isCarrierService, packageName);
171 if (VDBG) {
172 Log.v(LOG_TAG, "ims set-ims-service -s " + slotId + " "
173 + (isCarrierService ? "-c " : "-d ") + packageName + ", result=" + result);
174 }
175 getOutPrintWriter().println(result);
176 } catch (RemoteException e) {
177 Log.w(LOG_TAG, "ims set-ims-service -s " + slotId + " "
178 + (isCarrierService ? "-c " : "-d ") + packageName + ", error"
179 + e.getMessage());
180 errPw.println("Exception: " + e.getMessage());
181 return -1;
182 }
183 return 0;
184 }
185
186 // ims get-ims-service
187 private int handleImsGetServiceCommand() {
188 PrintWriter errPw = getErrPrintWriter();
189 int slotId = SubscriptionManager.getDefaultVoicePhoneId();
190 Boolean isCarrierService = null;
191
192 String opt;
193 while ((opt = getNextOption()) != null) {
194 switch (opt) {
195 case "-s": {
196 try {
197 slotId = Integer.parseInt(getNextArgRequired());
198 } catch (NumberFormatException e) {
199 errPw.println("ims set-ims-service requires an integer as a SLOT_ID.");
200 return -1;
201 }
202 break;
203 }
204 case "-c": {
205 isCarrierService = true;
206 break;
207 }
208 case "-d": {
209 isCarrierService = false;
210 break;
211 }
212 }
213 }
214 // Mandatory param, either -c or -d
215 if (isCarrierService == null) {
216 errPw.println("ims set-ims-service requires either \"-c\" or \"-d\" to be set.");
217 return -1;
218 }
219
220 String result;
221 try {
222 result = mInterface.getImsService(slotId, isCarrierService);
223 } catch (RemoteException e) {
224 return -1;
225 }
226 if (VDBG) {
227 Log.v(LOG_TAG, "ims get-ims-service -s " + slotId + " "
228 + (isCarrierService ? "-c " : "-d ") + ", returned: " + result);
229 }
230 getOutPrintWriter().println(result);
231 return 0;
232 }
233
234 private int handleEnableIms() {
235 int slotId = SubscriptionManager.getDefaultVoicePhoneId();
236 String opt;
237 while ((opt = getNextOption()) != null) {
238 switch (opt) {
239 case "-s": {
240 try {
241 slotId = Integer.parseInt(getNextArgRequired());
242 } catch (NumberFormatException e) {
243 getErrPrintWriter().println("ims enable requires an integer as a SLOT_ID.");
244 return -1;
245 }
246 break;
247 }
248 }
249 }
250 try {
251 mInterface.enableIms(slotId);
252 } catch (RemoteException e) {
253 return -1;
254 }
255 if (VDBG) {
256 Log.v(LOG_TAG, "ims enable -s " + slotId);
257 }
258 return 0;
259 }
260
261 private int handleDisableIms() {
262 int slotId = SubscriptionManager.getDefaultVoicePhoneId();
263 String opt;
264 while ((opt = getNextOption()) != null) {
265 switch (opt) {
266 case "-s": {
267 try {
268 slotId = Integer.parseInt(getNextArgRequired());
269 } catch (NumberFormatException e) {
270 getErrPrintWriter().println(
271 "ims disable requires an integer as a SLOT_ID.");
272 return -1;
273 }
274 break;
275 }
276 }
277 }
278 try {
279 mInterface.disableIms(slotId);
280 } catch (RemoteException e) {
281 return -1;
282 }
283 if (VDBG) {
284 Log.v(LOG_TAG, "ims disable -s " + slotId);
285 }
286 return 0;
287 }
288}