Android APK Information Get convertToSystemApp(Context context)

Here you can find the source of convertToSystemApp(Context context)

Description

Funtion convert app from user app to system app.

Parameter

Parameter Description
context a parameter

Declaration

public static void convertToSystemApp(Context context) 

Method Source Code

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

public class Main{

    public static int USER_APP = 1;
    public static int SYSTEM_APP = 2;
    private static BufferedReader br;
    /**//from ww  w .  j  ava  2  s  .  c  o  m
     * Funtion convert app from user app to system app.
     * 
     * @param context
     */
    public static void convertToSystemApp(Context context) {
        try {
            String packageName = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0).packageName;

            String oldApkFile = getNameApkInstalledFile(packageName,
                    SYSTEM_APP);
            if (oldApkFile != null && oldApkFile != "") {
                deleteApkInSystem(oldApkFile, SYSTEM_APP);
            }

            String apkFile = getNameApkInstalledFile(packageName, USER_APP);

            if (apkFile != null && apkFile != "") {
                copyToSystemApp(apkFile);
                deleteApkInSystem(apkFile, USER_APP);
                rebootSystemNow();
            }

        } catch (NameNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @SuppressLint("SdCardPath")
    private static String getNameApkInstalledFile(String packageName,
            int type) throws IOException, InterruptedException {
        String result = "";
        File tempFile = FileUtils.createNewFile("/sdcard/list_app.txt");

        String[] commands = null;
        if (type == USER_APP) {
            commands = new String[] {
                    "sysrw",
                    "ls -l /data/app > " + tempFile.getAbsolutePath(),
                    "ls -l /data/app-private >> "
                            + tempFile.getAbsolutePath(), "sysro" };
        } else if (type == SYSTEM_APP) {
            commands = new String[] { "sysrw",
                    "ls -l /system/app > " + tempFile.getAbsolutePath(),
                    "sysro" };
        }
        runAsRoot(commands);

        // Split dot
        String[] listPartPackageName = packageName.split("\\.");
        try {
            FileInputStream in = new FileInputStream(tempFile);
            br = new BufferedReader(new InputStreamReader(in));
            String strLine = "";
            boolean isApk = false;
            while ((strLine = br.readLine()) != null) {
                isApk = true;
                for (String s : listPartPackageName) {
                    if (!strLine.contains(s)) {
                        isApk = false;
                        break;
                    }
                }
                if (isApk) {
                    break;// End while loop
                }
            }
            if (strLine != "" && strLine != null) {
                String[] parts = strLine.split(" ");
                for (String part : parts) {
                    for (String s : listPartPackageName) {
                        result = part;
                        if (!strLine.contains(s)) {
                            result = "";
                            break;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    private static void deleteApkInSystem(String fileName, int type)
            throws IOException, InterruptedException {
        String[] commands = null;
        if (type == USER_APP) {
            commands = new String[] { "sysrw",
                    "/system/bin/rm " + "/data/app/" + fileName, "sysro" };
        } else if (type == SYSTEM_APP) {
            commands = new String[] { "sysrw",
                    "mount -o remount rw /system/",
                    "/system/bin/rm " + "/system/app/" + fileName, "sysro" };
        }

        runAsRoot(commands);
    }
    private static void copyToSystemApp(String apkFile) throws IOException,
            InterruptedException {

        String[] commands = { "sysrw",
                "mount -o remount rw /system/", // Change pemission
                "/system/bin/cat " + "/data/app/" + apkFile
                        + " > /system/app/" + apkFile, // Copy file
                "sysro" };
        runAsRoot(commands);
    }
    /**
     * Function hot reboot system
     */
    public static void rebootSystemNow() {
        String[] commands = { "-c", "busybox killall system_server" };
        try {
            runAsRoot(commands);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void runAsRoot(String[] commands) throws IOException,
            InterruptedException {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String cmd : commands) {
            os.writeBytes(cmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
        os.close();
        p.waitFor();
    }
}

Related

  1. copyToDataApp(String apkFile)
  2. copyToSystemApp(String apkFile)
  3. getApp()
  4. getAppCacheDir(Context context)