Search in sources :

Example 6 with RootFile

use of com.moro.mtweaks.utils.root.RootFile in project MTweaks-KernelAdiutorMOD by morogoku.

the class Utils method writeFile.

public static void writeFile(String path, String text, boolean append, boolean asRoot) {
    if (asRoot) {
        new RootFile(path).write(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();
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) RootFile(com.moro.mtweaks.utils.root.RootFile)

Example 7 with RootFile

use of com.moro.mtweaks.utils.root.RootFile in project MTweaks-KernelAdiutorMOD by morogoku.

the class ApplyOnBoot method apply.

public static boolean apply(final Context context, final ApplyOnBootListener listener) {
    // Initialize AppUpdate check
    AppUpdaterTask.appCheckNotification(context);
    // Initialize Boeffla Wakelock Blocker Files
    if (BoefflaWakelock.supported()) {
        BoefflaWakelock.CopyWakelockBlockerDefault();
    }
    // Check if kernel is changed
    String kernel_old = Prefs.getString("kernel_version_old", "", context);
    String kernel_new = Device.getKernelVersion(true);
    // If is changed save voltage files
    if (!kernel_old.equals(kernel_new)) {
        // Save backup of Cluster0 stock voltages
        if (VoltageCl0.supported()) {
            RootUtils.runCommand("cp " + VoltageCl0.CL0_VOLTAGE + " " + VoltageCl0.BACKUP);
            Prefs.saveBoolean("cl0_voltage_saved", true, context);
        }
        // Save backup of Cluster1 stock voltages
        if (VoltageCl1.supported()) {
            RootUtils.runCommand("cp " + VoltageCl1.CL1_VOLTAGE + " " + VoltageCl1.BACKUP);
            Prefs.saveBoolean("cl1_voltage_saved", true, context);
        }
        // Save backup of GPU stock voltages
        if (GPUFreq.supported() && GPUFreq.hasVoltage()) {
            RootUtils.runCommand("cp " + GPUFreq.AVAILABLE_VOLTS + " " + GPUFreq.BACKUP);
            Prefs.saveBoolean("gpu_voltage_saved", true, context);
        }
        RootUtils.runCommand("setprop mtweaks.voltage_saved 1");
    }
    if (!Prefs.getBoolean(ApplyOnBootFragment.getAssignment(CPUHotplugFragment.class), false, context)) {
        Prefs.remove("core_ctl_min_cpus_big", context);
    }
    boolean enabled = false;
    final Settings settings = new Settings(context);
    Controls controls = new Controls(context);
    final HashMap<String, Boolean> mCategoryEnabled = new HashMap<>();
    final HashMap<String, String> mCustomControls = new HashMap<>();
    final List<String> mProfiles = new ArrayList<>();
    List<Profiles.ProfileItem> profiles = new Profiles(context).getAllProfiles();
    Tile.publishProfileTile(profiles, context);
    for (Settings.SettingsItem item : settings.getAllSettings()) {
        if (!mCategoryEnabled.containsKey(item.getCategory())) {
            boolean categoryEnabled = Prefs.getBoolean(item.getCategory(), false, context);
            mCategoryEnabled.put(item.getCategory(), categoryEnabled);
            if (!enabled && categoryEnabled) {
                enabled = true;
            }
        }
    }
    for (Controls.ControlItem item : controls.getAllControls()) {
        if (item.isOnBootEnabled() && item.getArguments() != null) {
            mCustomControls.put(item.getApply(), item.getArguments());
        }
    }
    for (Profiles.ProfileItem profileItem : profiles) {
        if (profileItem.isOnBootEnabled()) {
            for (Profiles.ProfileItem.CommandItem commandItem : profileItem.getCommands()) {
                mProfiles.add(commandItem.getCommand());
            }
        }
    }
    final boolean initdEnabled = Prefs.getBoolean("initd_onboot", false, context);
    enabled = enabled || mCustomControls.size() > 0 || mProfiles.size() > 0 || initdEnabled;
    if (!enabled) {
        return false;
    }
    final int seconds = Utils.strToInt(Prefs.getString("applyonbootdelay", "10", context));
    final boolean hideNotification = Prefs.getBoolean("applyonboothide", false, context);
    final boolean confirmationNotification = Prefs.getBoolean("applyonbootconfirmationnotification", true, context);
    final boolean toast = Prefs.getBoolean("applyonboottoast", false, context);
    final boolean script = Prefs.getBoolean("applyonbootscript", false, context);
    PendingIntent cancelIntent = PendingIntent.getBroadcast(context, 1, new Intent(context, CancelReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
    Intent launchIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, 0);
    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ApplyOnBootService.CHANNEL_ID);
    if (!hideNotification) {
        builder.setContentTitle(context.getString(R.string.app_name)).setContentText(context.getString(R.string.apply_on_boot_text, seconds)).setSmallIcon(R.mipmap.ic_launcher).addAction(0, context.getString(R.string.cancel), cancelIntent).setOngoing(true).setWhen(0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder.setPriority(Notification.PRIORITY_MAX);
        }
    }
    final NotificationCompat.Builder builderComplete = new NotificationCompat.Builder(context, ApplyOnBootService.CHANNEL_ID);
    if (!hideNotification) {
        builderComplete.setContentTitle(context.getString(R.string.app_name)).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(contentIntent).setAutoCancel(true);
    }
    final Handler handler = new Handler();
    new Thread(new Runnable() {

        @Override
        public void run() {
            sCancel = false;
            for (int i = 0; i < seconds; i++) {
                if (!hideNotification) {
                    if (sCancel) {
                        break;
                    }
                    builder.setContentText(context.getString(R.string.apply_on_boot_text, seconds - i));
                    builder.setProgress(seconds, i, false);
                    notificationManager.notify(NotificationId.APPLY_ON_BOOT, builder.build());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (!hideNotification) {
                notificationManager.cancel(NotificationId.APPLY_ON_BOOT);
                if (confirmationNotification) {
                    builderComplete.setContentText(context.getString(sCancel ? R.string.apply_on_boot_canceled : R.string.apply_on_boot_complete));
                    notificationManager.notify(NotificationId.APPLY_ON_BOOT_CONFIRMATION, builderComplete.build());
                }
                if (sCancel) {
                    sCancel = false;
                    listener.onFinish();
                    return;
                }
            }
            RootUtils.SU su = new RootUtils.SU(true, TAG);
            if (initdEnabled) {
                RootUtils.mount(true, "/system", su);
                su.runCommand("for i in `ls /system/etc/init.d`;do chmod 755 $i;done");
                su.runCommand("[ -d /system/etc/init.d ] && run-parts /system/etc/init.d");
                RootUtils.mount(false, "/system", su);
            }
            List<String> commands = new ArrayList<>();
            for (Settings.SettingsItem item : settings.getAllSettings()) {
                String category = item.getCategory();
                String setting = item.getSetting();
                String id = item.getId();
                CPUFreq.ApplyCpu applyCpu;
                if (mCategoryEnabled.get(category)) {
                    if (category.equals(ApplyOnBootFragment.CPU) && id.contains("%d") && setting.startsWith("#") && ((applyCpu = new CPUFreq.ApplyCpu(setting.substring(1))).toString() != null)) {
                        synchronized (this) {
                            commands.addAll(getApplyCpu(applyCpu, su, context));
                        }
                    } else {
                        commands.add(setting);
                    }
                }
            }
            if (script) {
                StringBuilder s = new StringBuilder("#!/system/bin/sh\n\n");
                for (String command : commands) {
                    s.append(command).append("\n");
                }
                RootFile file = new RootFile("/data/local/tmp/kerneladiutortmp.sh", su);
                file.mkdir();
                file.write(s.toString(), false);
                file.execute();
            } else {
                for (String command : commands) {
                    synchronized (this) {
                        su.runCommand(command);
                    }
                }
            }
            for (String script : mCustomControls.keySet()) {
                RootFile file = new RootFile("/data/local/tmp/kerneladiutortmp.sh", su);
                file.mkdir();
                file.write(script, false);
                file.execute(mCustomControls.get(script));
            }
            List<String> profileCommands = new ArrayList<>();
            for (String command : mProfiles) {
                CPUFreq.ApplyCpu applyCpu;
                if (command.startsWith("#") && ((applyCpu = new CPUFreq.ApplyCpu(command.substring(1))).toString() != null)) {
                    synchronized (this) {
                        profileCommands.addAll(getApplyCpu(applyCpu, su, context));
                    }
                }
                profileCommands.add(command);
            }
            if (script) {
                StringBuilder s = new StringBuilder("#!/system/bin/sh\n\n");
                for (String command : profileCommands) {
                    s.append(command).append("\n");
                }
                RootFile file = new RootFile("/data/local/tmp/kerneladiutortmp.sh", su);
                file.mkdir();
                file.write(s.toString(), false);
                file.execute();
            } else {
                for (String command : profileCommands) {
                    synchronized (this) {
                        su.runCommand(command);
                    }
                }
            }
            su.close();
            if (toast) {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        Utils.toast(R.string.apply_on_boot_complete, context);
                    }
                });
            }
            listener.onFinish();
        }
    }).start();
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Profiles(com.moro.mtweaks.database.tools.profiles.Profiles) Controls(com.moro.mtweaks.database.tools.customcontrols.Controls) NotificationCompat(android.support.v4.app.NotificationCompat) Settings(com.moro.mtweaks.database.Settings) NotificationManager(android.app.NotificationManager) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) CPUFreq(com.moro.mtweaks.utils.kernel.cpu.CPUFreq) RootUtils(com.moro.mtweaks.utils.root.RootUtils) PendingIntent(android.app.PendingIntent) RootFile(com.moro.mtweaks.utils.root.RootFile)

Example 8 with RootFile

use of com.moro.mtweaks.utils.root.RootFile in project MTweaks-KernelAdiutorMOD by morogoku.

the class Initd method write.

public static void write(String file, String text) {
    RootUtils.mount(true, "/system");
    RootFile f = new RootFile(INITD + "/" + file);
    f.write(text, false);
    RootUtils.chmod(INITD + "/" + file, "755");
    RootUtils.mount(false, "/system");
}
Also used : RootFile(com.moro.mtweaks.utils.root.RootFile)

Example 9 with RootFile

use of com.moro.mtweaks.utils.root.RootFile in project MTweaks-KernelAdiutorMOD by morogoku.

the class Initd method list.

public static List<String> list() {
    RootFile file = new RootFile(INITD);
    if (!file.exists()) {
        RootUtils.mount(true, "/system");
        file.mkdir();
        RootUtils.mount(false, "/system");
    }
    return file.list();
}
Also used : RootFile(com.moro.mtweaks.utils.root.RootFile)

Example 10 with RootFile

use of com.moro.mtweaks.utils.root.RootFile in project MTweaks-KernelAdiutorMOD by morogoku.

the class DownloadKernelView method postMD5Check.

private void postMD5Check(boolean match, final String path, final String installMethod) {
    if (match) {
        mInstallButton.setVisibility(View.VISIBLE);
        mInstallButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {
                if (!Utils.existFile(path)) {
                    Utils.toast(view.getContext().getString(R.string.went_wrong), view.getContext());
                    return;
                }
                if (installMethod != null) {
                    ViewUtils.dialogBuilder(view.getContext().getString(R.string.sure_question), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    }, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            RootUtils.runCommand(installMethod.replace("$FILE", path));
                            RootUtils.runCommand("rm -f " + path);
                            RootUtils.runCommand("reboot");
                        }
                    }, new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                        }
                    }, view.getContext()).setTitle(view.getContext().getString(R.string.install)).show();
                } else {
                    mRecoverySelection = 0;
                    String[] items = view.getResources().getStringArray(R.array.downloads_recovery);
                    new Dialog(view.getContext()).setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            mRecoverySelection = i;
                        }
                    }).setPositiveButton(view.getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (mRecoverySelection == 2) {
                                Utils.toast(view.getContext().getString(R.string.file_location, path), view.getContext());
                                return;
                            }
                            Recovery recovery = new Recovery(Recovery.RECOVERY_COMMAND.FLASH_ZIP, new File(path));
                            Recovery.RECOVERY type = mRecoverySelection == 1 ? Recovery.RECOVERY.TWRP : Recovery.RECOVERY.CWM;
                            RootFile recoveryFile = new RootFile("/cache/recovery/" + recovery.getFile(type));
                            for (String command : recovery.getCommands(type)) {
                                recoveryFile.write(command, true);
                            }
                            RootUtils.runCommand("reboot recovery");
                        }
                    }).show();
                }
            }
        });
    } else {
        mMismatchMD5.setVisibility(View.VISIBLE);
    }
    mCheckMD5.setVisibility(View.GONE);
    mDownloadSection.setVisibility(View.VISIBLE);
    mCheckMD5Task = null;
}
Also used : DialogInterface(android.content.DialogInterface) TextView(android.widget.TextView) View(android.view.View) Recovery(com.moro.mtweaks.utils.tools.Recovery) Dialog(com.moro.mtweaks.views.dialog.Dialog) RootFile(com.moro.mtweaks.utils.root.RootFile) File(java.io.File) RootFile(com.moro.mtweaks.utils.root.RootFile)

Aggregations

RootFile (com.moro.mtweaks.utils.root.RootFile)13 File (java.io.File)3 IOException (java.io.IOException)3 DialogInterface (android.content.DialogInterface)2 Recovery (com.moro.mtweaks.utils.tools.Recovery)2 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 Handler (android.os.Handler)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 View (android.view.View)1 TextView (android.widget.TextView)1 Settings (com.moro.mtweaks.database.Settings)1 Controls (com.moro.mtweaks.database.tools.customcontrols.Controls)1 Profiles (com.moro.mtweaks.database.tools.profiles.Profiles)1 CPUFreq (com.moro.mtweaks.utils.kernel.cpu.CPUFreq)1 RootUtils (com.moro.mtweaks.utils.root.RootUtils)1