android adb 联系人,Android - Importing contacts through vCards via ADB

Selected answer didn't work for me. I needed that functionality for several specific tests. So what I did is added Override BeforeSuite to test class inside which I called methods from the below classes and called super class method after.

public class AdbUtils {

private static final String CH_DIR = "cd ";

private static final String LOCATE_ADB = "locate -r ~/\".*platform-tools/adb\"";

public static void createContacts(List contacts) throws Exception {

for (int i = 0; i < contacts.size(); i++) {

if (i < contacts.size() - 1) {

System.out.println(executeCommand(AdbCommands.CREATE_AND_SAVE_CONTACT

.getCommand(new String[]{contacts.get(i).getName(), contacts.get(i).getPhoneNumber()})));

} else {

System.out.println(executeCommand(AdbCommands.CREATE_AND_SAVE_CONTACT

.getCommand(new String[]{contacts.get(i).getName(), contacts.get(i).getPhoneNumber()})));

System.out.println(executeCommand(AdbCommands.START_CONTACTS_APP_LIST_STATE.getCommand(null)

+ " && " + AdbCommands.SLEEP.getCommand(new String[]{"3"})

+ " && " + AdbCommands.PRESS_BACK_BUTTON.getCommand(null)));

}

}

}

public static void removeAllContacts() {

System.out.println("DEBUG - Delete all contacts: " + executeCommand(new String[]{AdbCommands.DELETE_ALL_CONTACTS_CMD.getCommand(null)}));

}

private static String executeCommand(String command) {

return executeCommand(new String[]{command});

}

private static String executeCommand(String[] commandLines) {

String adbDir = locatePlatformToolsAdb();

adbDir = adbDir.substring(0, adbDir.length() - 4);

String[] command = new String[commandLines.length + 2];

command[0] = "bash";

command[1] = "-c";

commandLines[0] = CH_DIR + adbDir + " && " + commandLines[0];

System.arraycopy(commandLines, 0, command, 2, commandLines.length + 2 - 2);

String input;

String errorInput = null;

try {

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec(command);

proc.waitFor();

input = parseInputStream(proc.getInputStream());

errorInput = parseInputStream(proc.getErrorStream());

proc.destroy();

return input;

} catch (Exception ex) {

ex.printStackTrace();

}

return errorInput;

}

private static String locatePlatformToolsAdb() {

String path = null;

String error = null;

try {

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec(new String[]{"bash", "-c", LOCATE_ADB});

proc.waitFor();

path = parseInputStream(proc.getInputStream());

error = parseInputStream(proc.getErrorStream());

proc.destroy();

}

catch (Exception ex) {

ex.printStackTrace();

}

if(null != path){

System.out.println("DEBUG - Located platform tools: " + path);

return path;

}

else throw new IllegalStateException("DEBUG - error locating adb: " + error);

}

private static String parseInputStream(InputStream input) {

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(input));

String line;

StringBuilder resultBuilder = new StringBuilder();

while ((line = reader.readLine()) != null) {

resultBuilder.append(line);

}

return resultBuilder.toString();

}

catch (Exception ex) {

ex.printStackTrace();

}

return null;

}

}

and

public enum AdbCommands {

WAIT("./adb shell wait"),

SLEEP("./adb shell sleep %s"),

DELETE_ALL_CONTACTS_CMD("./adb shell pm clear com.android.providers.contacts"),

START_CONTACTS_APP_LIST_STATE("./adb shell am start com.android.contacts"),

PRESS_RECENT_BUTTON("./adb shell input keyevent 187"),

PRESS_HOME_BUTTON("./adb shell input keyevent 3"),

PRESS_BACK_BUTTON("./adb shell input keyevent 4"),

CREATE_CONTACT("./adb shell am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact " + "-e name '%s' -e phone %s"),

CREATE_AND_SAVE_CONTACT(CREATE_CONTACT.getCommand(null) + " && ./" + SLEEP.getCommand(new String[]{"3"})

+ " && ./" + PRESS_RECENT_BUTTON.getCommand(null) + " && ./" + SLEEP.getCommand(new String[]{"3"})

+ " && ./" + PRESS_RECENT_BUTTON.getCommand(null) + " && ./" + SLEEP.getCommand(new String[]{"3"})

+ " && ./" + PRESS_BACK_BUTTON.getCommand(null) + " && ./" + SLEEP.getCommand(new String[]{"3"}));

private String command;

private static final String arg = "%s";

AdbCommands(String command){ this.command = command; }

public String getCommand(@Nullable String[] args){

String command = this.command;

if(null == args) {

return this.command;

}

else{

if(countSubstring(this.command, arg) != args.length) throw new IllegalArgumentException("wrong args count!");

for (String arg : args) {

command = command.replaceFirst(AdbCommands.arg, arg);

}

return command;

}

}

private int countSubstring(String str, final String subStr){

int lastIndex = 0;

int count = 0;

while(lastIndex != -1){

lastIndex = str.indexOf(subStr,lastIndex);

if(lastIndex != -1){

count ++;

lastIndex += subStr.length();

}

}

return count;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值