1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 #include <stdlib.h> 17 18 #include <StringUtil.h> 19 #include "Adb.h" 20 21 Adb::Adb(const android::String8& device) 22 : mDevice(device) 23 { 24 25 } 26 27 Adb::~Adb() 28 { 29 30 } 31 32 bool Adb::setPortForwarding(int hostPort, int devicePort) 33 { 34 android::String8 command; 35 if (command.appendFormat("forward tcp:%d tcp:%d", hostPort, devicePort) != 0) { 36 return false; 37 } 38 if (executeCommand(command) != 0) { 39 return false; 40 } 41 return true; 42 } 43 44 bool Adb::launchClient(const android::String8& clientBinary, const android::String8& component) 45 { 46 android::String8 command; 47 if (command.appendFormat("install -r %s", clientBinary.string()) != 0) { 48 return false; 49 } 50 if (executeCommand(command) != 0) { 51 return false; 52 } 53 command.clear(); 54 if (command.appendFormat("shell am start -W -n %s", component.string()) != 0) { 55 return false; 56 } 57 if (executeCommand(command) != 0) { 58 return false; 59 } 60 return true; 61 } 62 63 /** @param command ADB command except adb -s XYZW */ 64 int Adb::executeCommand(const android::String8& command) 65 { 66 android::String8 adbCommand; 67 if (mDevice.empty()) { 68 if (adbCommand.appendFormat("adb %s", command.string()) != 0) { 69 return -1; 70 } 71 } else { 72 if (adbCommand.appendFormat("adb -s %s %s", mDevice.string(), 73 command.string()) != 0) { 74 return -1; 75 } 76 } 77 return system(adbCommand.string()); 78 } 79 80