Commit 1f02d0f6 authored by topjohnwu's avatar topjohnwu

Add auto zip flashing support

parent 830fde80
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
//apply plugin: 'android-apt'
android { android {
compileSdkVersion 24 compileSdkVersion 24
......
...@@ -9,6 +9,8 @@ import android.support.v4.app.FragmentManager; ...@@ -9,6 +9,8 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -50,10 +52,21 @@ public class ModulesFragment extends Fragment { ...@@ -50,10 +52,21 @@ public class ModulesFragment extends Fragment {
return view; return view;
} }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_module, menu);
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.force_reload: case R.id.force_reload:
listModules.clear();
listModulesCache.clear();
progressBar.setVisibility(View.VISIBLE);
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
loadMod = new loadModules(); loadMod = new loadModules();
loadMod.execute(); loadMod.execute();
new updateUI().execute(); new updateUI().execute();
......
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.IdRes; import android.support.annotation.IdRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
...@@ -17,6 +22,8 @@ import android.support.v7.widget.Toolbar; ...@@ -17,6 +22,8 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import com.topjohnwu.magisk.utils.Shell;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
...@@ -43,9 +50,10 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView ...@@ -43,9 +50,10 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} }
// Load mods in the background // Load mods in the background, as it is time consuming
ModulesFragment.loadMod = new ModulesFragment.loadModules(); ModulesFragment.loadMod = new ModulesFragment.loadModules();
ModulesFragment.loadMod.execute(); ModulesFragment.loadMod.execute();
new Popups().execute();
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
...@@ -76,7 +84,11 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView ...@@ -76,7 +84,11 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
} if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
...@@ -145,4 +157,19 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView ...@@ -145,4 +157,19 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
} }
} }
private class Popups extends AsyncTask <Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
return Shell.rootAccess();
}
@Override
protected void onPostExecute(Boolean rootAccess) {
super.onPostExecute(rootAccess);
if (!rootAccess) {
Snackbar.make(findViewById(android.R.id.content), R.string.no_root_access, Snackbar.LENGTH_LONG).show();
}
}
}
} }
package com.topjohnwu.magisk.receivers;
import android.content.Intent;
import android.support.v4.content.FileProvider;
import java.io.File;
public class ApkReceiver extends DownloadReceiver {
@Override
public void task(File file) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setData(FileProvider.getUriForFile(mContext, "com.topjohnwu.magisk.provider", file));
mContext.startActivity(install);
}
}
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.receivers;
import android.app.DownloadManager; import android.app.DownloadManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
...@@ -6,7 +6,7 @@ import android.content.Context; ...@@ -6,7 +6,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.support.v4.content.FileProvider;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
...@@ -14,20 +14,16 @@ import com.topjohnwu.magisk.R; ...@@ -14,20 +14,16 @@ import com.topjohnwu.magisk.R;
import java.io.File; import java.io.File;
/** /**
* Created by topjohnwu on 2016/8/27. * Created by topjohnwu
*/ */
public abstract class DownloadReceiver extends BroadcastReceiver{ public abstract class DownloadReceiver extends BroadcastReceiver {
public Context context; public Context mContext;
DownloadManager downloadManager;
long downloadID; long downloadID;
public DownloadReceiver(long downloadID) {
this.downloadID = downloadID;
}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
this.context = context; mContext = context;
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String action = intent.getAction(); String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){ if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
DownloadManager.Query query = new DownloadManager.Query(); DownloadManager.Query query = new DownloadManager.Query();
...@@ -50,5 +46,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver{ ...@@ -50,5 +46,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver{
} }
} }
public void setDownloadID(long id) { downloadID = id;}
public abstract void task(File file); public abstract void task(File file);
} }
\ No newline at end of file
package com.topjohnwu.magisk.receivers;
import android.support.v7.app.AlertDialog;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
import java.io.File;
public class ZipReceiver extends DownloadReceiver {
@Override
public void task(File file) {
new AlertDialog.Builder(mContext)
.setTitle("Installation")
.setMessage("Do you want to install now?")
.setCancelable(false)
.setPositiveButton("Yes, flash now", (dialogInterface, i) -> {
new Utils.flashZIP(file.getPath(), mContext).execute();
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
...@@ -16,6 +17,7 @@ public class Shell { ...@@ -16,6 +17,7 @@ public class Shell {
// -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted; 2 = improperly rooted; // -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted; 2 = improperly rooted;
public static int rootStatus; public static int rootStatus;
public static int magiskVersion;
private static Process rootShell; private static Process rootShell;
private static DataOutputStream rootSTDIN; private static DataOutputStream rootSTDIN;
...@@ -28,6 +30,13 @@ public class Shell { ...@@ -28,6 +30,13 @@ public class Shell {
private static void init() { private static void init() {
List<String> ret = sh("getprop magisk.version");
if (ret.get(0).replaceAll("\\s", "").isEmpty()) {
magiskVersion = -1;
} else {
magiskVersion = Integer.parseInt(ret.get(0));
}
try { try {
rootShell = Runtime.getRuntime().exec(sh("getprop magisk.supath").get(0) + "/su"); rootShell = Runtime.getRuntime().exec(sh("getprop magisk.supath").get(0) + "/su");
rootStatus = 1; rootStatus = 1;
...@@ -47,7 +56,7 @@ public class Shell { ...@@ -47,7 +56,7 @@ public class Shell {
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList); rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList);
rootSTDOUT.start(); rootSTDOUT.start();
List<String> ret = su("echo -BOC-", "id"); ret = su("echo -BOC-", "id");
if (ret == null) { if (ret == null) {
// Something wrong with root, not allowed? // Something wrong with root, not allowed?
rootStatus = -1; rootStatus = -1;
...@@ -66,19 +75,6 @@ public class Shell { ...@@ -66,19 +75,6 @@ public class Shell {
} }
} }
@Override
protected void finalize() throws Throwable {
super.finalize();
if (rootAccess()) {
rootSTDIN.write("exit\n".getBytes("UTF-8"));
rootSTDIN.flush();
rootShell.waitFor();
rootSTDIN.close();
rootSTDOUT.join();
rootShell.destroy();
}
}
public static boolean rootAccess() { public static boolean rootAccess() {
return rootStatus > 0; return rootStatus > 0;
} }
......
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
......
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
import com.topjohnwu.magisk.R;
import java.util.List; import java.util.List;
public class Utils { public class Utils {
...@@ -41,6 +49,54 @@ public class Utils { ...@@ -41,6 +49,54 @@ public class Utils {
return ret; return ret;
} }
public static class flashZIP extends AsyncTask<Void, Void, Boolean> {
private String mPath;
private ProgressDialog progress;
Context mContext;
public flashZIP(String path, Context context) {
mPath = path;
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(mContext, "Installing", "Patching boot image for Magisk....");
}
@Override
protected Boolean doInBackground(Void... voids) {
if (!Shell.rootAccess()) {
return false;
} else {
Shell.su(
"rm -rf /data/tmp",
"mkdir -p /data/tmp",
"cp -af " + mPath + " /data/tmp/install.zip",
"unzip -o /data/tmp/install.zip META-INF/com/google/android/* -d /data/tmp",
"BOOTMODE=true sh /data/tmp/META-INF/com/google/android/update-binary dummy 1 /data/tmp/install.zip"
);
return Boolean.parseBoolean(Shell.su("if [ $? -eq 0 ]; then echo true; else echo false; fi").get(0));
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Shell.su("rm -rf /data/tmp");
progress.dismiss();
if (!result) {
Toast.makeText(mContext, "Installation failed...", Toast.LENGTH_LONG).show();
return;
}
new AlertDialog.Builder(mContext)
.setTitle("Reboot now?")
.setPositiveButton("Yes", (dialogInterface1, i1) -> Toast.makeText(mContext, "Reboot...", Toast.LENGTH_LONG).show())
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
} }
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
<android.support.v7.widget.CardView <android.support.v7.widget.CardView
android:id="@+id/app_updateView" android:id="@+id/magisk_updateView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
...@@ -78,21 +78,22 @@ ...@@ -78,21 +78,22 @@
android:orientation="vertical"> android:orientation="vertical">
<FrameLayout <FrameLayout
android:id="@+id/app_check_updates_container" android:id="@+id/magisk_check_updates_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="100dp" android:layout_height="100dp"
android:background="?android:attr/selectableItemBackground"
android:foregroundGravity="center" android:foregroundGravity="center"
android:orientation="vertical"> android:orientation="vertical">
<ImageView <ImageView
android:id="@+id/app_check_updates_icon" android:id="@+id/magisk_check_updates_icon"
android:layout_width="84dp" android:layout_width="84dp"
android:layout_height="84dp" android:layout_height="84dp"
android:layout_gravity="center" android:layout_gravity="center"
android:src="@drawable/ic_check_circle"/> android:src="@drawable/ic_check_circle"/>
<ProgressBar <ProgressBar
android:id="@+id/app_check_updates_progress" android:id="@+id/magisk_check_updates_progress"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/> android:layout_gravity="center"/>
...@@ -100,7 +101,7 @@ ...@@ -100,7 +101,7 @@
</FrameLayout> </FrameLayout>
<TextView <TextView
android:id="@+id/app_check_updates_status" android:id="@+id/magisk_check_updates_status"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
...@@ -113,7 +114,7 @@ ...@@ -113,7 +114,7 @@
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
<android.support.v7.widget.CardView <android.support.v7.widget.CardView
android:id="@+id/magisk_updateView" android:id="@+id/app_updateView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
...@@ -128,22 +129,21 @@ ...@@ -128,22 +129,21 @@
android:orientation="vertical"> android:orientation="vertical">
<FrameLayout <FrameLayout
android:id="@+id/magisk_check_updates_container" android:id="@+id/app_check_updates_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="100dp" android:layout_height="100dp"
android:background="?android:attr/selectableItemBackground"
android:foregroundGravity="center" android:foregroundGravity="center"
android:orientation="vertical"> android:orientation="vertical">
<ImageView <ImageView
android:id="@+id/magisk_check_updates_icon" android:id="@+id/app_check_updates_icon"
android:layout_width="84dp" android:layout_width="84dp"
android:layout_height="84dp" android:layout_height="84dp"
android:layout_gravity="center" android:layout_gravity="center"
android:src="@drawable/ic_check_circle"/> android:src="@drawable/ic_check_circle"/>
<ProgressBar <ProgressBar
android:id="@+id/magisk_check_updates_progress" android:id="@+id/app_check_updates_progress"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/> android:layout_gravity="center"/>
...@@ -151,7 +151,7 @@ ...@@ -151,7 +151,7 @@
</FrameLayout> </FrameLayout>
<TextView <TextView
android:id="@+id/magisk_check_updates_status" android:id="@+id/app_check_updates_status"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
...@@ -162,6 +162,7 @@ ...@@ -162,6 +162,7 @@
</LinearLayout> </LinearLayout>
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>
\ No newline at end of file
...@@ -58,16 +58,16 @@ ...@@ -58,16 +58,16 @@
<string name="no_modules_found">No modules found</string> <string name="no_modules_found">No modules found</string>
<string name="loading">Loading…</string> <string name="loading">Loading…</string>
<string name="update_available">Update available!</string> <string name="update_available">Update available!</string>
<string name="update">Update</string> <string name="download">Download</string>
<string name="update_available_message">A new update of %1$s is available.\nChangelog version %2$s: \n%3$s</string> <string name="update_available_message"><![CDATA[A new version of %1$s is available!<br>Version: v%2$s<br>Changelog:<br>%3$s]]></string>
<string name="no_thanks">No, thanks</string> <string name="no_thanks">No, thanks</string>
<string name="never_show_again">Don\'t show again</string> <string name="never_show_again">Don\'t show again</string>
<string name="error_download_file">Error downloading file</string> <string name="error_download_file">Error downloading file</string>
<string name="flash_recovery">File downloaded in Magisk directory, you can flash it in recovery</string> <string name="flash_recovery">File downloaded in Magisk directory, you can flash it in recovery</string>
<string name="root">Root</string> <string name="root">Root</string>
<string name="app_update_available">An update for Magisk Manager is available!</string> <string name="app_update_available">Magisk Manager App v%1$s update!</string>
<string name="magisk_update_available">An update for Magisk is available!</string> <string name="magisk_update_available">Magisk v%1$s update!</string>
<string name="cannot_check_updates">Cannot check for updates</string> <string name="cannot_check_updates">Cannot check for updates</string>
<string name="up_to_date">You\'ve got the latest version available of %1$s</string> <string name="up_to_date">Latest version of %1$s installed</string>
</resources> </resources>
...@@ -6,7 +6,7 @@ buildscript { ...@@ -6,7 +6,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-beta1' classpath 'com.android.tools.build:gradle:2.2.0-beta3'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment