com.bhb27.isu.tools.Tools.java Source code

Java tutorial

Introduction

Here is the source code for com.bhb27.isu.tools.Tools.java

Source

/*
 * Copyright (C) 2015-2016 Willi Ye <williye97@gmail.com>
 *
 * This file is part of Kernel Adiutor.
 *
 * Kernel Adiutor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Kernel Adiutor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Kernel Adiutor.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.bhb27.isu.tools;

import android.content.Context;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;

import com.bhb27.isu.R;
import com.bhb27.isu.tools.RootFile;
import com.bhb27.isu.tools.RootUtils;

public class Tools implements Constants {

    public static boolean KernelSupport() {
        String kernel_support_rc, kernel_support_sh;
        if (existFile(xbin_su, true)) {
            kernel_support_rc = RootUtils.runCommand("grep -r -i isu_daemon *.rc ") + "";
            kernel_support_sh = RootUtils.runCommand("grep -r -i isu_daemon *.sh ") + ""
                    + RootUtils.runCommand("grep -r -i /system/xbin/isu /sbin/*.sh ");
            if (kernel_support_rc.contains("isu_daemon") && kernel_support_sh.contains("/system/xbin/isu")) {
                return true;
            } else
                return false;
        } else {
            kernel_support_rc = RootUtils.runICommand("grep -r -i isu_daemon *.rc ") + "";
            kernel_support_sh = RootUtils.runICommand("grep -r -i isu_daemon *.sh ") + ""
                    + RootUtils.runICommand("grep -r -i /system/xbin/isu /sbin/*.sh ");
            if (kernel_support_rc.contains("isu_daemon") && kernel_support_sh.contains("/system/xbin/isu")) {
                return true;
            } else
                return false;
        }
    }

    public static boolean ReadSystemPatch() {
        String reboot_support_rc, reboot_support_sh;
        if (SuBinary(xbin_su)) {
            reboot_support_rc = RootUtils.runCommand("grep -i isu_daemon system/etc/init/superuser.rc") + "";
            reboot_support_sh = RootUtils.runCommand("grep -i /system/xbin/isu system/xbin/isush") + "";
            if (reboot_support_rc.contains("isu_daemon") && reboot_support_sh.contains("/system/xbin/isu"))
                return true;
        } else if (SuBinary(xbin_isu)) {
            reboot_support_rc = RootUtils.runICommand("grep -i isu_daemon system/etc/init/superuser.rc") + "";
            reboot_support_sh = RootUtils.runICommand("grep -i /system/xbin/isu system/xbin/isush") + "";
            if (reboot_support_rc.contains("isu_daemon") && reboot_support_sh.contains("/system/xbin/isu"))
                return true;
        }
        return false;
    }

    public static void SystemPatch(String executableFilePath) {
        if (SuBinary(xbin_su)) {
            RootUtils.runCommand("mount -o rw,remount /system");
            RootUtils.runCommand("cp -f " + executableFilePath + "isush" + " /system/xbin/");
            RootUtils.runCommand("chmod 0755" + " /system/xbin/isush");
            RootUtils.runCommand("cp -f " + executableFilePath + "superuser.rc" + " /system/etc/init/");
            RootUtils.runCommand("chmod 0644" + " /system/etc/init/superuser.rc");
            RootUtils.runCommand("mount -o ro,remount /system");
        } else if (SuBinary(xbin_isu)) {
            RootUtils.runICommand("mount -o rw,remount /system");
            RootUtils.runICommand("cp -f " + executableFilePath + "isush" + " /system/xbin/");
            RootUtils.runICommand("chmod 0755" + " /system/xbin/isush");
            RootUtils.runICommand("cp -f " + executableFilePath + "superuser.rc" + " /system/etc/init/");
            RootUtils.runICommand("chmod 0644" + " /system/etc/init/superuser.rc");
            RootUtils.runICommand("mount -o ro,remount /system");
        }
    }

    public static void PatchSepolicy(String executableFilePath) {
        if (SuBinary(xbin_su))
            RootUtils.runCommand("LD_LIBRARY_PATH=" + executableFilePath + " " + executableFilePath + sepolicy);
        else if (SuBinary(xbin_isu))
            RootUtils.runICommand("LD_LIBRARY_PATH=" + executableFilePath + " " + executableFilePath + sepolicy);
    }

    public static void SwitchSu(boolean isChecked, Context context) {
        if (isChecked) {
            // Mount rw to change mount ro after
            RootUtils.runICommand("mount -o rw,remount /system");
            RootUtils.runICommand("mv " + xbin_isu + " " + xbin_su);
            RootUtils.runCommand("mv " + bin_temp_su + " " + bin_su);
            RootUtils.runCommand("mount -o ro,remount /system");
            ClearAllNotification(context);
        } else {
            // Make a link to isu so all root tool work
            RootUtils.runCommand("mount -o rw,remount /system");
            RootUtils.runCommand("ln -s -f " + xbin_isu + " " + bin_isu);
            RootUtils.runCommand("mv " + xbin_su + " " + xbin_isu);
            RootUtils.runICommand("mv " + bin_su + " " + bin_temp_su);
            RootUtils.runICommand("mount -o ro,remount /system");
        }
    }

    public static void SwitchSelinux(boolean isChecked) {
        if (SuBinary(xbin_su))
            RootUtils.runCommand(Constants.SETENFORCE + (isChecked ? " 1" : " 0"));
        else if (SuBinary(xbin_isu))
            RootUtils.runCommand(Constants.SETENFORCE + (isChecked ? " 1" : " 0"));
    }

    public static String SuVersion(Context context) {
        String su_bin_version = "";
        // Check if is CM-SU
        if (SuBinary(xbin_su)) {
            su_bin_version = RootUtils.runCommand("su --version") + "";
        } else if (SuBinary(xbin_isu))
            su_bin_version = RootUtils.runICommand("isu --version") + "";
        else
            su_bin_version = RootUtils.runCommand("su --version") + "";

        if (su_bin_version.contains("null"))
            su_bin_version = context.getString(R.string.device_not_root);

        return su_bin_version;
    }

    public static void DoNotification(Context context) {

        NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
        notification.setSmallIcon(R.drawable.ic_notification);
        notification.setContentTitle(context.getString(R.string.notification_title));
        notification.setOngoing(true);
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);

        Intent yesReceiver = new Intent();
        yesReceiver.setAction(Constants.YES_ACTION);
        PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceiver,
                PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action actiony = new NotificationCompat.Action.Builder(R.drawable.ic_notification,
                context.getString(R.string.yes), pendingIntentYes).build();
        notification.addAction(actiony);

        Intent dismissReceiver = new Intent();
        dismissReceiver.setAction(Constants.DISSMISS_ACTION);
        PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(context, 12345, dismissReceiver,
                PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action actionn = new NotificationCompat.Action.Builder(R.drawable.ic_notification,
                context.getString(R.string.dismiss), pendingIntentYes2).build();
        notification.addAction(actionn);

        notificationManager.notify(10, notification.build());
    }

    public static void ClearAllNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
    }

    // simple toast function to center the message Main.this
    public static void DoAToast(String message, Context context) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        TextView view = (TextView) toast.getView().findViewById(android.R.id.message);
        if (view != null)
            view.setGravity(Gravity.CENTER);
        toast.show();
    }

    // Random int generated base on a String[] length to get a Random String
    public static String RandomString(Context context) {
        String[] pokemonstrings = new String[] { context.getString(R.string.pokemongo_1),
                context.getString(R.string.pokemongo_2), context.getString(R.string.pokemongo_3),
                context.getString(R.string.pokemongo_4), context.getString(R.string.pokemongo_5),
                context.getString(R.string.pokemongo_6), context.getString(R.string.pokemongo_7),
                context.getString(R.string.pokemongo_8), context.getString(R.string.pokemongo_9),
                context.getString(R.string.isu_by) };
        Random rand = new Random();
        int generate = 0;
        generate = rand.nextInt(pokemonstrings.length);
        return pokemonstrings[generate];
    }

    public static boolean isSELinuxActive() {
        if (getSELinuxStatus().equals("Enforcing"))
            return true;
        return false;
    }

    public static void RestartApp(String packageName) {
        RootUtils.runCommand("am force-stop " + packageName);
        RootUtils.runCommand("am start " + packageName);
    }

    public static String getSELinuxStatus() {
        String result = "";
        if (existFile(xbin_su, true))
            result = RootUtils.runCommand(GETENFORCE);
        else
            result = RootUtils.runICommand(GETENFORCE);
        if (result != null) {
            if (result.equals("Enforcing"))
                return "Enforcing";
            else if (result.equals("Permissive"))
                return "Permissive";
        }
        return "Unknown Status";
    }

    public static boolean getBoolean(String name, boolean defaults, Context context) {
        try {
            return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).getBoolean(name, defaults);
        } catch (Exception ignored) {
            return false;
        }
    }

    public static void saveBoolean(String name, boolean value, Context context) {
        context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit().putBoolean(name, value).apply();
    }

    public static String StringreadFileN(String file) {
        return readFileN(file, true);
    }

    public static String readFileN(String file, boolean asRoot) {

        StringBuilder s = null;
        FileReader fileReader = null;
        BufferedReader buf = null;
        try {
            fileReader = new FileReader(file);
            buf = new BufferedReader(fileReader);

            String line;
            s = new StringBuilder();
            while ((line = buf.readLine()) != null)
                s.append(line).append("\n");
        } catch (FileNotFoundException ignored) {
            Log.e(TAG, "File does not exist " + file);
        } catch (IOException e) {
            Log.e(TAG, "Failed to read " + file);
        } finally {
            try {
                if (fileReader != null)
                    fileReader.close();
                if (buf != null)
                    buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s == null ? null : s.toString().trim();
    }

    /**
     * as Root
     */
    public static String StringreadFile(String file) {
        return readFile(file, true);
    }

    public static boolean NewexistFile(String file, boolean asRoot) {
        if (existFile(file, asRoot))
            return true;
        else if (IexistFile(file, asRoot))
            return true;
        return false;
    }

    public static boolean existFile(String file, boolean asRoot) {
        if (asRoot)
            return new RootFile(file).exists();
        return new File(file).exists();
    }

    public static boolean IexistFile(String file, boolean asRoot) {
        if (asRoot)
            return new RootFile(file).Iexists();
        return new File(file).exists();
    }

    public static String readFile(String file, boolean asRoot) {
        if (asRoot) {
            if (SuBinary(xbin_su))
                return new RootFile(file).readFile();
            else if (SuBinary(xbin_isu))
                return new RootFile(file).IreadFile();
        }

        StringBuilder s = null;
        FileReader fileReader = null;
        BufferedReader buf = null;
        try {
            fileReader = new FileReader(file);
            buf = new BufferedReader(fileReader);

            String line;
            s = new StringBuilder();
            while ((line = buf.readLine()) != null)
                s.append(line).append("\n");
        } catch (FileNotFoundException ignored) {
            Log.e(TAG, "File does not exist " + file);
        } catch (IOException e) {
            Log.e(TAG, "Failed to read " + file);
        } finally {
            try {
                if (fileReader != null)
                    fileReader.close();
                if (buf != null)
                    buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s == null ? null : s.toString().trim();
    }

    public static boolean SuBinary(String binary) {
        if (binary.equals(xbin_su)) {
            if (existFile(binary, true))
                return true;
        }
        if (binary.equals(xbin_isu)) {
            if (IexistFile(binary, true))
                return true;
        }
        return false;
    }

    /**
     * Write a string to any file
     *
     * @param path   path to file
     * @param text   your text
     * @param append append your text to file
     * @param asRoot write as root
     */
    public static void writeFile(String path, String text, boolean append, boolean asRoot) {
        if (asRoot) {
            if (SuBinary(xbin_su))
                new RootFile(path).write(text, append);
            else if (SuBinary(xbin_isu))
                new RootFile(path).Iwrite(text, append);
            return;
        }

        FileWriter writer = null;
        try {
            writer = new FileWriter(path, append);
            writer.write(text);
            writer.flush();
        } catch (IOException e) {
            Log.e(TAG, "Failed to write " + path);
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}