Android - Import contacts through vCards via ADB

I'm currently trying to automate some Android activities through ADB and get stuck with importing contacts through vCards. There are two ways to do this:

  • automate the exact "bindings" of the user, which suggests that you need to determine the position of each "button" in accordance with the screen resolution, etc. This is what I did right now, but it is really difficult to maintain, since there are too many parameters to take into account (custom roms, more complex resolutions, portrait / landscape mode, etc.).

  • find what happens when you click "import contact from vCards" and perform this action through ADB

Basically, I would like to apply the second option, but I don’t know what happens when you click “import contacts from vCard” and I will need to call the same action / intention from ADB. Any idea on an ADB command I have to execute?

+4
source share
2 answers

Try it. Update the parameter -dusing the correct path vcf.

adb shell am start -t "text/x-vcard" -d "file:///sdcard/contacts.vcf" -a android.intent.action.VIEW com.android.contacts
  • Mime Type: text/vcardortext/x-vcard
  • URI: path to vcard
  • Act: android.intent.action.VIEW
  • Package: com.android.contacts
+9
source

. . , , Override BeforeSuite , .

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<Contact> 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;
}

}

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;
}

}

+1

Source: https://habr.com/ru/post/1539392/


All Articles