Speed up startup time

This commit is contained in:
topjohnwu 2018-06-26 00:29:01 +08:00
parent 23f697d62b
commit 4601989d4a
11 changed files with 82 additions and 87 deletions

View File

@ -113,13 +113,15 @@ public class FlashActivity extends Activity {
case Const.Value.FLASH_ZIP: case Const.Value.FLASH_ZIP:
new FlashZip(this, uri, console, logs).exec(); new FlashZip(this, uri, console, logs).exec();
break; break;
case Const.Value.PATCH_BOOT:
new InstallMagisk(this, console, logs, uri, (Uri) intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT))
.exec();
break;
case Const.Value.FLASH_MAGISK: case Const.Value.FLASH_MAGISK:
new InstallMagisk(this, console, logs, uri, intent.getStringExtra(Const.Key.FLASH_SET_BOOT)) new InstallMagisk(this, console, logs, uri, InstallMagisk.DIRECT_MODE).exec();
.exec(); break;
case Const.Value.FLASH_SECOND_SLOT:
new InstallMagisk(this, console, logs, uri, InstallMagisk.SECOND_SLOT_MODE).exec();
break;
case Const.Value.PATCH_BOOT:
new InstallMagisk(this, console, logs, uri,
intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec();
break; break;
} }
} }

View File

@ -59,7 +59,6 @@ public class MagiskManager extends Application implements Shell.Container {
public String remoteManagerVersionString; public String remoteManagerVersionString;
public int remoteManagerVersionCode = -1; public int remoteManagerVersionCode = -1;
public String managerLink; public String managerLink;
public String bootBlock = null;
public boolean keepVerity = false; public boolean keepVerity = false;
public boolean keepEnc = false; public boolean keepEnc = false;
@ -193,8 +192,6 @@ public class MagiskManager extends Application implements Shell.Container {
"resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP); "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP);
magiskHide = s == null || Integer.parseInt(s) != 0; magiskHide = s == null || Integer.parseInt(s) != 0;
} catch (Exception ignored) {} } catch (Exception ignored) {}
bootBlock = ShellUtils.fastCmd("echo \"$BOOTIMAGE\"");
} }
public void getDefaultInstallFlags() { public void getDefaultInstallFlags() {

View File

@ -60,8 +60,6 @@ public class SplashActivity extends Activity {
mm.setupUpdateCheck(); mm.setupUpdateCheck();
// Fire asynctasks // Fire asynctasks
loadModuleTask.exec(); loadModuleTask.exec();
// Check dtbo status
RootUtils.patchDTBO();
} }
// Write back default values // Write back default values

View File

@ -39,8 +39,9 @@ import java.util.List;
public class InstallMagisk extends ParallelTask<Void, Void, Boolean> { public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
private static final int PATCH_MODE = 0; private static final int PATCH_MODE = 0;
private static final int DIRECT_MODE = 1; public static final int DIRECT_MODE = 1;
private static final int FIX_ENV_MODE = 2; private static final int FIX_ENV_MODE = 2;
public static final int SECOND_SLOT_MODE = 3;
private Uri bootUri, mZip; private Uri bootUri, mZip;
private List<String> console, logs; private List<String> console, logs;
@ -57,22 +58,16 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
mode = FIX_ENV_MODE; mode = FIX_ENV_MODE;
} }
private InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip) { public InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip, int mode) {
this(context, zip); this(context, zip);
this.console = console; this.console = console;
this.logs = logs; this.logs = logs;
this.mode = mode;
} }
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, Uri boot) { public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, Uri boot) {
this(context, console, logs, zip); this(context, console, logs, zip, PATCH_MODE);
bootUri = boot; bootUri = boot;
mode = PATCH_MODE;
}
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, String boot) {
this(context, console, logs, zip);
mBoot = boot;
mode = DIRECT_MODE;
} }
@Override @Override
@ -209,6 +204,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
console.add(" " + dest + " "); console.add(" " + dest + " ");
console.add("*********************************"); console.add("*********************************");
break; break;
case SECOND_SLOT_MODE:
case DIRECT_MODE: case DIRECT_MODE:
String binPath = mm.remoteMagiskVersionCode >= Const.MAGISK_VER.HIDDEN_PATH ? String binPath = mm.remoteMagiskVersionCode >= Const.MAGISK_VER.HIDDEN_PATH ?
"/data/adb/magisk" : "/data/magisk"; "/data/adb/magisk" : "/data/magisk";
@ -237,11 +233,32 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
installDir.mkdirs(); installDir.mkdirs();
} }
if (mode == PATCH_MODE) { switch (mode) {
mBoot = new File(installDir, "boot.img").getAbsolutePath(); case PATCH_MODE:
if (!dumpBoot()) mBoot = new File(installDir, "boot.img").getAbsolutePath();
return false; if (!dumpBoot())
return false;
break;
case DIRECT_MODE:
console.add("- Detecting ramdisk/boot image");
mBoot = ShellUtils.fastCmd("find_boot_image", "echo \"$BOOTIMAGE\"");
break;
case SECOND_SLOT_MODE:
console.add("- Detecting ramdisk/boot image");
char slot[] = ShellUtils.fastCmd("echo $SLOT").toCharArray();
if (slot[1] == 'a') slot[1] = 'b';
else slot[1] = 'a';
mBoot = ShellUtils.fastCmd("SLOT=" + String.valueOf(slot),
"find_boot_image", "echo \"$BOOTIMAGE\"");
Shell.Async.su("mount_partitions");
break;
} }
if (mBoot == null) {
console.add("- Unable to detect ramdisk/boot image");
return false;
}
console.add("- Use ramdisk/boot image: " + mBoot);
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS); List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);
String arch; String arch;

View File

@ -9,7 +9,8 @@ import com.topjohnwu.magisk.services.OnBootIntentService;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
private void startIntentService(Context context) { @Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, OnBootIntentService.class)); context.startForegroundService(new Intent(context, OnBootIntentService.class));
} else { } else {
@ -17,9 +18,4 @@ public class BootReceiver extends BroadcastReceiver {
} }
} }
@Override
public void onReceive(Context context, Intent intent) {
startIntentService(context);
}
} }

View File

@ -9,8 +9,6 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
public class OnBootIntentService extends IntentService { public class OnBootIntentService extends IntentService {
@ -22,12 +20,12 @@ public class OnBootIntentService extends IntentService {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder builder = startForeground(Const.ID.ONBOOT_NOTIFICATION_ID,
new NotificationCompat.Builder(this, Const.ID.NOTIFICATION_CHANNEL); new NotificationCompat.Builder(this, Const.ID.NOTIFICATION_CHANNEL)
builder.setSmallIcon(R.drawable.ic_magisk_outline) .setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle("onBoot") .setContentTitle("onBoot")
.setContentText("Running onBoot operations..."); .setContentText("Running onBoot operations...")
startForeground(Const.ID.ONBOOT_NOTIFICATION_ID, builder.build()); .build());
} }
} }
@ -40,11 +38,7 @@ public class OnBootIntentService extends IntentService {
* Check for dtbo status every boot time, and prompt user * Check for dtbo status every boot time, and prompt user
* to reboot if dtbo wasn't patched and patched by Magisk Manager. * to reboot if dtbo wasn't patched and patched by Magisk Manager.
* */ * */
MagiskManager mm = Utils.getMagiskManager(this); MagiskManager.get().loadMagiskInfo();
mm.loadMagiskInfo(); RootUtils.patchDTBO();
mm.getDefaultInstallFlags();
if (Shell.rootAccess()) {
RootUtils.patchDTBO();
}
} }
} }

View File

@ -8,16 +8,18 @@ import com.topjohnwu.superuser.io.SuFile;
public class RootUtils { public class RootUtils {
public static void init() { public static void init() {
Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); if (Shell.rootAccess()) {
SuFile file = new SuFile("/sbin/.core/img"); Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk");
if (file.exists()) { SuFile file = new SuFile("/sbin/.core/img");
Const.MAGISK_PATH = file; if (file.exists()) {
} else if ((file = new SuFile("/dev/magisk/img")).exists()) { Const.MAGISK_PATH = file;
Const.MAGISK_PATH = file; } else if ((file = new SuFile("/dev/magisk/img")).exists()) {
} else { Const.MAGISK_PATH = file;
Const.MAGISK_PATH = new SuFile("/magisk"); } else {
Const.MAGISK_PATH = new SuFile("/magisk");
}
Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts");
} }
Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts");
} }
public static void uninstallPkg(String pkg) { public static void uninstallPkg(String pkg) {
@ -25,10 +27,11 @@ public class RootUtils {
} }
public static void patchDTBO() { public static void patchDTBO() {
MagiskManager mm = MagiskManager.get(); if (Shell.rootAccess()) {
if (mm.magiskVersionCode >= Const.MAGISK_VER.DTBO_SUPPORT && !mm.keepVerity) { MagiskManager mm = MagiskManager.get();
if (ShellUtils.fastCmdResult(Shell.getShell(), "patch_dtbo_image")) { if (mm.magiskVersionCode >= Const.MAGISK_VER.DTBO_SUPPORT) {
ShowUI.dtboPatchedNotification(); if (Boolean.parseBoolean(ShellUtils.fastCmd("mm_patch_dtbo")))
ShowUI.dtboPatchedNotification();
} }
} }
} }

View File

@ -23,8 +23,6 @@ public class ShellInitializer extends Shell.Initializer {
} }
shell.run(null, null, shell.run(null, null,
"mount_partitions", "mount_partitions",
"find_boot_image",
"find_dtbo_image",
"get_flags", "get_flags",
"run_migrations"); "run_migrations");
return true; return true;

View File

@ -137,17 +137,15 @@ public class ShowUI {
if (Shell.rootAccess()) { if (Shell.rootAccess()) {
options.add(mm.getString(R.string.direct_install)); options.add(mm.getString(R.string.direct_install));
} }
String s = ShellUtils.fastCmd("echo $SLOT"); String s = ShellUtils.fastCmd("grep_prop ro.build.ab_update");
if (s != null) { if (s != null && Boolean.parseBoolean(s)) {
options.add(mm.getString(R.string.install_second_slot)); options.add(mm.getString(R.string.install_second_slot));
} }
char[] slot = s == null ? null : s.toCharArray();
new AlertDialog.Builder(activity) new AlertDialog.Builder(activity)
.setTitle(R.string.select_method) .setTitle(R.string.select_method)
.setItems( .setItems(
options.toArray(new String [0]), options.toArray(new String [0]),
(dialog, idx) -> { (dialog, idx) -> {
String boot;
DownloadReceiver receiver = null; DownloadReceiver receiver = null;
switch (idx) { switch (idx) {
case 1: case 1:
@ -191,41 +189,23 @@ public class ShowUI {
}; };
break; break;
case 2: case 2:
boot = mm.bootBlock;
if (boot == null)
return;
receiver = new DownloadReceiver() { receiver = new DownloadReceiver() {
@Override @Override
public void onDownloadDone(Context context, Uri uri) { public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class); Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri) intent.setData(uri).putExtra(Const.Key.FLASH_ACTION,
.putExtra(Const.Key.FLASH_SET_BOOT, boot) Const.Value.FLASH_MAGISK);
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK);
activity.startActivity(intent); activity.startActivity(intent);
} }
}; };
break; break;
case 3: case 3:
assert (slot != null);
// Choose the other slot
if (slot[1] == 'a') slot[1] = 'b';
else slot[1] = 'a';
// Then find the boot image again
boot = ShellUtils.fastCmd(
"SLOT=" + String.valueOf(slot),
"find_boot_image",
"echo \"$BOOTIMAGE\""
);
Shell.Async.su("mount_partitions");
if (boot == null)
return;
receiver = new DownloadReceiver() { receiver = new DownloadReceiver() {
@Override @Override
public void onDownloadDone(Context context, Uri uri) { public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class); Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri) intent.setData(uri).putExtra(Const.Key.FLASH_ACTION,
.putExtra(Const.Key.FLASH_SET_BOOT, boot) Const.Value.FLASH_SECOND_SLOT);
.putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK);
activity.startActivity(intent); activity.startActivity(intent);
} }
}; };

View File

@ -39,7 +39,16 @@ db_setup() {
env_check() { env_check() {
for file in busybox magisk magiskboot magiskinit util_functions.sh boot_patch.sh; do for file in busybox magisk magiskboot magiskinit util_functions.sh boot_patch.sh; do
[ -e /data/adb/magisk/$file ] || return 1 [ -f /data/adb/magisk/$file ] || return 1
done done
return 0 return 0
} }
mm_patch_dtbo() {
if $KEEPVERITY; then
echo false
else
find_dtbo_image
patch_dtbo_image >/dev/null 2>&1 && echo true || echo false
fi
}

View File

@ -153,6 +153,7 @@ public class Const {
public static final String FLASH_ZIP = "flash"; public static final String FLASH_ZIP = "flash";
public static final String PATCH_BOOT = "patch"; public static final String PATCH_BOOT = "patch";
public static final String FLASH_MAGISK = "magisk"; public static final String FLASH_MAGISK = "magisk";
public static final String FLASH_SECOND_SLOT = "slot";
public static final int[] timeoutList = {0, -1, 10, 20, 30, 60}; public static final int[] timeoutList = {0, -1, 10, 20, 30, 60};
public static final int ORDER_NAME = 0; public static final int ORDER_NAME = 0;
public static final int ORDER_DATE = 1; public static final int ORDER_DATE = 1;