Android APK Information Get restoreToUserApp(Context context)

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

Description

Function restore app from system to user app.

Parameter

Parameter Description
context a parameter

Declaration

public static void restoreToUserApp(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 tuisolutions.tuisecurity.models.Parameters;
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 w w  w  . j  a  va 2 s  . c  o m*/
     * Function restore app from system to user app.
     * 
     * @param context
     */
    public static void restoreToUserApp(Context context) {
        try {
            String packageName = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0).packageName;

            String oldApkFile = getNameApkInstalledFile(packageName,
                    USER_APP);

            if (oldApkFile != null && oldApkFile != "") {
                deleteApkInSystem(oldApkFile, USER_APP);
            }

            String apkFile = getNameApkInstalledFile(packageName,
                    SYSTEM_APP);

            if (apkFile != null && apkFile != "") {
                copyToDataApp(apkFile);
                deleteApkInSystem(apkFile, SYSTEM_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);

        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 copyToDataApp(String apkFile) throws IOException,
            InterruptedException {
        String[] commands = { "sysrw",
                "mount -o remount rw /system/", // Change pemission
                "/system/bin/cat " + "/system/app/" + apkFile
                        + " > /data/app/" + apkFile, // Copy file
                "sysro" };
        runAsRoot(commands);
    }
    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. getAppPkg(Context c, int uid)
  2. getNameApkInstalledFile(String packageName, int type)
  3. isAppPlatformSignature(Context ctx)
  4. isApplicationBroughtToBackground(Context context)
  5. isApplicationSentToBackground( final Context context)
  6. getApplicationName(Context context)
  7. getAppName(Context context)
  8. getAppPackage(Context context)
  9. getAppVersionCode(Context context)