Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
    private static List<HashMap<String, Object>> contactHashMaps;

    public static HashMap<String, Object> getNumberToNameHashMap() {
        if (contactHashMaps == null)
            contactHashMaps = getContactsHashMaps();

        //0 - number to name
        return getContactsHashMaps().get(0);
    }

    private static List<HashMap<String, Object>> getContactsHashMaps() {

        String[] arr = execSQL("/data/data/com.whatsapp/databases/wa.db",
                "Select display_name, jid FROM wa_contacts WHERE is_whatsapp_user=1 and jid like " + '"'
                        + "%@s.whatsapp.net" + '"');

        List<HashMap<String, Object>> hashMaps = new ArrayList<>(2);

        hashMaps.add(new HashMap<String, Object>());
        hashMaps.add(new HashMap<String, Object>());

        if (arr == null)
            return null;

        for (String contact : arr) {
            String potential[] = contact.split("\\|");

            if (potential.length < 2)
                continue;

            // swap = true -> number to name hashmap
            hashMaps.get(0).put(potential[1].split("@")[0], potential[0]);

            // swap = false -> name to number hashmap
            Object value = hashMaps.get(1).get(potential[0]);

            if (value != null) {
                List<String> numbers = new ArrayList<>();

                if (value instanceof String) {
                    numbers.add(value.toString());
                    numbers.add(potential[1].split("@")[0]);
                } else if (value instanceof List) {
                    numbers = (List<String>) value;
                    numbers.add(potential[1].split("@")[0]);
                }
                hashMaps.get(1).put(potential[0], numbers);
            } else {
                hashMaps.get(1).put(potential[0], potential[1].split("@")[0]);
            }

        }
        return hashMaps;
    }

    public static String[] execSQL(String dbName, String query) {
        Process process = null;
        Runtime runtime = Runtime.getRuntime();
        OutputStreamWriter outputStreamWriter;

        try {
            String command = dbName + " " + "'" + query + "'" + ";";
            process = runtime.exec("su");

            outputStreamWriter = new OutputStreamWriter(process.getOutputStream());

            outputStreamWriter.write("sqlite3 " + command);

            outputStreamWriter.flush();
            outputStreamWriter.close();
            outputStreamWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        final InputStreamReader errorStreamReader = new InputStreamReader(process.getErrorStream());

        (new Thread() {
            @Override
            public void run() {
                try {

                    BufferedReader bufferedReader = new BufferedReader(errorStreamReader);
                    String s;
                    while ((s = bufferedReader.readLine()) != null) {
                        Log.d("com.suraj.waext", "WhatsAppDBHelper:" + s);
                    }

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }).start();

        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String s;
            StringBuilder op = new StringBuilder();

            while ((s = bufferedReader.readLine()) != null) {
                op.append(s).append("\n");
            }

            return op.toString().split("\n");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;

    }
}