1 /*
2  * Copyright (C) 2008 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 com.android.commands.svc;
18 
19 import android.content.Context;
20 import android.hardware.usb.IUsbManager;
21 import android.hardware.usb.UsbManager;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 
25 public class UsbCommand extends Svc.Command {
UsbCommand()26     public UsbCommand() {
27         super("usb");
28     }
29 
30     @Override
shortHelp()31     public String shortHelp() {
32         return "Control Usb state";
33     }
34 
35     @Override
longHelp()36     public String longHelp() {
37         return shortHelp() + "\n"
38                 + "\n"
39                 + "usage: svc usb setFunctions [function]\n"
40                 + "         Set the current usb function. If function is blank, sets to charging.\n"
41                 + "       svc usb setScreenUnlockedFunctions [function]\n"
42                 + "         Sets the functions which, if the device was charging, become current on"
43                     + "screen unlock. If function is blank, turn off this feature.\n"
44                 + "       svc usb getFunctions\n"
45                 + "          Gets the list of currently enabled functions\n"
46                 + "       svc usb resetUsbGadget\n"
47                 + "          Reset usb gadget\n\n"
48                 + "possible values of [function] are any of 'mtp', 'ptp', 'rndis', 'midi'\n";
49     }
50 
51     @Override
run(String[] args)52     public void run(String[] args) {
53         if (args.length >= 2) {
54             IUsbManager usbMgr = IUsbManager.Stub.asInterface(ServiceManager.getService(
55                     Context.USB_SERVICE));
56             if ("setFunctions".equals(args[1])) {
57                 try {
58                     usbMgr.setCurrentFunctions(UsbManager.usbFunctionsFromString(
59                             args.length >= 3 ? args[2] : ""));
60                 } catch (RemoteException e) {
61                     System.err.println("Error communicating with UsbManager: " + e);
62                 }
63                 return;
64             } else if ("getFunctions".equals(args[1])) {
65                 try {
66                     System.err.println(
67                             UsbManager.usbFunctionsToString(usbMgr.getCurrentFunctions()));
68                 } catch (RemoteException e) {
69                     System.err.println("Error communicating with UsbManager: " + e);
70                 }
71                 return;
72             } else if ("setScreenUnlockedFunctions".equals(args[1])) {
73                 try {
74                     usbMgr.setScreenUnlockedFunctions(UsbManager.usbFunctionsFromString(
75                             args.length >= 3 ? args[2] : ""));
76                 } catch (RemoteException e) {
77                     System.err.println("Error communicating with UsbManager: " + e);
78                 }
79                 return;
80             } else if ("resetUsbGadget".equals(args[1])) {
81                 try {
82                     usbMgr.resetUsbGadget();
83                 } catch (RemoteException e) {
84                     System.err.println("Error communicating with UsbManager: " + e);
85                 }
86                 return;
87             }
88         }
89         System.err.println(longHelp());
90     }
91 }
92