From 8fe4cfecb6d4bb89ae6c28318131ed35d970eeaf Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 25 Dec 2016 17:17:20 +0800 Subject: [PATCH] Add Install UI --- .../com/topjohnwu/magisk/InstallFragment.java | 118 +++++++++++----- .../com/topjohnwu/magisk/utils/Utils.java | 21 ++- app/src/main/res/layout/install_fragment.xml | 127 +++++++++++++++++- 3 files changed, 231 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java index 3b2da17d5..0ed0429a6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java @@ -8,6 +8,10 @@ import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; +import android.widget.TextView; import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.utils.Async; @@ -16,56 +20,104 @@ import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ZipUtils; import java.io.File; +import java.util.List; +import butterknife.BindView; import butterknife.ButterKnife; -// Currently empty, placing some code that we be used in the future public class InstallFragment extends Fragment { + private List blockList; + + @BindView(R.id.block_spinner) Spinner spinner; + @BindView(R.id.detect_bootimage) Button detectButton; + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.install_fragment, container, false); ButterKnife.bind(this, v); + detectButton.setOnClickListener(v1 -> detectBootImage()); + + getBlockList(); + return v; } + private void getBlockList() { + new Async.RootTask() { + @Override + protected Void doInBackground(Void... params) { + blockList = Utils.getBlockList(); + return null; + } + @Override + protected void onPostExecute(Void aVoid) { + ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, blockList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + detectBootImage(); + } + }.exec(); + } + + private void detectBootImage() { + new Async.RootTask() { + String boot; + @Override + protected Void doInBackground(Void... params) { + boot = Utils.detectBootImage(); + return null; + } + @Override + protected void onPostExecute(Void aVoid) { + if (boot != null) { + int idx = blockList.indexOf(boot); + if (idx != -1) { + spinner.setSelection(idx); + } + } + } + }.exec(); + } + @Override public void onResume() { super.onResume(); getActivity().setTitle(R.string.install); } - private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.dlAndReceive( - getActivity(), - new DownloadReceiver() { - @Override - public void task(Uri uri) { - new Async.FlashZIP(mContext, uri, mFilename) { - @Override - protected boolean unzipAndCheck() { - publishProgress(mContext.getString(R.string.zip_install_unzip_zip_msg)); - if (Shell.rootAccess()) { - // We might not have busybox yet, unzip with Java - // We will have complete busybox after Magisk installation - ZipUtils.unzip(mCachedFile, new File(mCachedFile.getParent(), "magisk")); - Shell.su( - "mkdir -p " + Async.TMP_FOLDER_PATH + "/magisk", - "cp -af " + mCachedFile.getParent() + "/magisk/. " + Async.TMP_FOLDER_PATH + "/magisk" - ); - } - super.unzipAndCheck(); - return true; - } - - @Override - protected void done() { - Shell.su("setprop magisk.version " + String.valueOf(StatusFragment.remoteMagiskVersion)); - super.done(); - } - }.exec(); - } - }, - StatusFragment.magiskLink, - "Magisk-v" + String.valueOf(StatusFragment.remoteMagiskVersion) + ".zip"); } + +// private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.dlAndReceive( +// getActivity(), +// new DownloadReceiver() { +// @Override +// public void task(Uri uri) { +// new Async.FlashZIP(mContext, uri, mFilename) { +// @Override +// protected boolean unzipAndCheck() { +// publishProgress(mContext.getString(R.string.zip_install_unzip_zip_msg)); +// if (Shell.rootAccess()) { +// // We might not have busybox yet, unzip with Java +// // We will have complete busybox after Magisk installation +// ZipUtils.unzip(mCachedFile, new File(mCachedFile.getParent(), "magisk")); +// Shell.su( +// "mkdir -p " + Async.TMP_FOLDER_PATH + "/magisk", +// "cp -af " + mCachedFile.getParent() + "/magisk/. " + Async.TMP_FOLDER_PATH + "/magisk" +// ); +// } +// super.unzipAndCheck(); +// return true; +// } +// +// @Override +// protected void done() { +// Shell.su("setprop magisk.version " + String.valueOf(StatusFragment.remoteMagiskVersion)); +// super.done(); +// } +// }.exec(); +// } +// }, +// StatusFragment.magiskLink, +// "Magisk-v" + String.valueOf(StatusFragment.remoteMagiskVersion) + ".zip"); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 8e9763ff0..7c527e0a2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -67,7 +67,7 @@ public class Utils { return Shell.rootAccess() && Boolean.parseBoolean(Shell.su(command).get(0)); } - static List getModList(String path) { + public static List getModList(String path) { List ret; String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\""; if (Shell.rootAccess()) { @@ -138,6 +138,25 @@ public class Utils { .replace("#", "").replace("@", "").replace("*", ""); } + public static List getBlockList() { + return Shell.su("ls /dev/block | grep mmc"); + } + + public static String detectBootImage() { + String[] commands = { + "for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do", + "BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION`", + "if [ ! -z \"$BOOTIMAGE\" ]; then break; fi", + "done", + "echo \"${BOOTIMAGE##*/}\"" + }; + List ret = Shell.su(commands); + if (!ret.isEmpty()) { + return ret.get(0); + } + return null; + } + public static class ByteArrayInOutStream extends ByteArrayOutputStream { public ByteArrayInputStream getInputStream() { ByteArrayInputStream in = new ByteArrayInputStream(buf, 0, count); diff --git a/app/src/main/res/layout/install_fragment.xml b/app/src/main/res/layout/install_fragment.xml index f9504c9a4..2b6b72ff7 100644 --- a/app/src/main/res/layout/install_fragment.xml +++ b/app/src/main/res/layout/install_fragment.xml @@ -1,6 +1,131 @@ + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_marginTop="?attr/actionBarSize" + android:orientation="vertical"> + + + + + + + + + + + + + + + + + +