You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Massive Zip flashing refactoring
This commit is contained in:
@@ -7,17 +7,19 @@ import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.MagiskManager;
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.asyncs.LoadRepos;
|
||||
import com.topjohnwu.magisk.components.AlertDialogBuilder;
|
||||
import com.topjohnwu.magisk.components.SnackbarMaker;
|
||||
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
|
||||
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
||||
|
||||
@@ -117,15 +119,6 @@ public class Utils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static AlertDialog.Builder getAlertDialogBuilder(Context context) {
|
||||
// if (((MagiskManager) context.getApplicationContext()).isDarkTheme) {
|
||||
// return new AlertDialog.Builder(context, R.style.AlertDialog_dh);
|
||||
// } else {
|
||||
// return new AlertDialog.Builder(context);
|
||||
// }
|
||||
return new AlertDialogBuilder(context);
|
||||
}
|
||||
|
||||
public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
|
||||
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
|
||||
}
|
||||
@@ -164,4 +157,29 @@ public class Utils {
|
||||
new RepoDatabaseHelper(activity).clearRepo();
|
||||
Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public static String getNameFromUri(Context context, Uri uri) {
|
||||
String name = null;
|
||||
try (Cursor c = context.getContentResolver().query(uri, null, null, null, null)) {
|
||||
if (c != null) {
|
||||
int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
c.moveToFirst();
|
||||
name = c.getString(nameIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (name == null) {
|
||||
int idx = uri.getPath().lastIndexOf('/');
|
||||
name = uri.getPath().substring(idx + 1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static void showUriSnack(Activity activity, Uri uri) {
|
||||
SnackbarMaker.make(activity, activity.getString(R.string.internal_storage,
|
||||
"/MagiskManager/" + Utils.getNameFromUri(activity, uri)),
|
||||
Snackbar.LENGTH_LONG)
|
||||
.setAction(R.string.ok, (v)->{}).show();
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.topjohnwu.magisk.components.AlertDialogBuilder;
|
||||
|
||||
public class WebWindow {
|
||||
|
||||
public WebWindow(String title, String url, Context context) {
|
||||
AlertDialog.Builder alert = Utils.getAlertDialogBuilder(context);
|
||||
AlertDialog.Builder alert = new AlertDialogBuilder(context);
|
||||
alert.setTitle(title);
|
||||
|
||||
Logger.dev("WebView: URL = " + url);
|
||||
|
||||
@@ -48,6 +48,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
@@ -88,7 +89,7 @@ public class ZipUtils {
|
||||
buffer.setBuffer(zipAdjust(buffer.toByteArray(), buffer.size()));
|
||||
}
|
||||
|
||||
public static void removeTopFolder(InputStream in, OutputStream out) {
|
||||
public static void removeTopFolder(InputStream in, OutputStream out) throws IOException {
|
||||
try {
|
||||
JarInputStream source = new JarInputStream(in);
|
||||
JarOutputStream dest = new JarOutputStream(out);
|
||||
@@ -113,16 +114,20 @@ public class ZipUtils {
|
||||
dest.close();
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Logger.dev("ZipUtils: removeTopFolder IO error!");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static void unzip(File file, File folder) {
|
||||
public static void unzip(File file, File folder) throws Exception {
|
||||
unzip(file, folder, "");
|
||||
}
|
||||
|
||||
public static void unzip(File file, File folder, String path) {
|
||||
public static void unzip(InputStream file, File folder) throws Exception {
|
||||
unzip(file, folder, "");
|
||||
}
|
||||
|
||||
public static void unzip(File file, File folder, String path) throws Exception {
|
||||
int count;
|
||||
FileOutputStream out;
|
||||
File dest;
|
||||
@@ -133,32 +138,56 @@ public class ZipUtils {
|
||||
Enumeration<JarEntry> e = zipfile.entries();
|
||||
while(e.hasMoreElements()) {
|
||||
entry = e.nextElement();
|
||||
if (!entry.getName().contains(path)
|
||||
|| entry.getName().charAt(entry.getName().length() - 1) == '/') {
|
||||
if (!entry.getName().contains(path) || entry.isDirectory())
|
||||
// Ignore directories, only create files
|
||||
continue;
|
||||
}
|
||||
Logger.dev("ZipUtils: Extracting: " + entry);
|
||||
is = zipfile.getInputStream(entry);
|
||||
dest = new File(folder, entry.getName());
|
||||
if (dest.getParentFile().mkdirs()) {
|
||||
if (dest.getParentFile().mkdirs())
|
||||
dest.createNewFile();
|
||||
}
|
||||
out = new FileOutputStream(dest);
|
||||
while ((count = is.read(data, 0, 4096)) != -1) {
|
||||
while ((count = is.read(data, 0, 4096)) != -1)
|
||||
out.write(data, 0, count);
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
is.close();
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static void unzip(InputStream file, File folder, String path) throws Exception {
|
||||
int count;
|
||||
FileOutputStream out;
|
||||
File dest;
|
||||
JarEntry entry;
|
||||
byte data[] = new byte[4096];
|
||||
try (JarInputStream zipfile = new JarInputStream(file)) {
|
||||
while((entry = zipfile.getNextJarEntry()) != null) {
|
||||
if (!entry.getName().contains(path) || entry.isDirectory())
|
||||
// Ignore directories, only create files
|
||||
continue;
|
||||
Logger.dev("ZipUtils: Extracting: " + entry);
|
||||
dest = new File(folder, entry.getName());
|
||||
if (dest.getParentFile().mkdirs())
|
||||
dest.createNewFile();
|
||||
out = new FileOutputStream(dest);
|
||||
while ((count = zipfile.read(data, 0, 4096)) != -1)
|
||||
out.write(data, 0, count);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static void signZip(Context context, InputStream inputStream,
|
||||
OutputStream outputStream, boolean signWholeFile) {
|
||||
OutputStream outputStream, boolean signWholeFile) throws Exception {
|
||||
JarMap inputJar;
|
||||
int hashes = 0;
|
||||
try {
|
||||
@@ -192,6 +221,7 @@ public class ZipUtils {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +257,13 @@ public class ZipUtils {
|
||||
public Manifest getManifest() {
|
||||
return manifest;
|
||||
}
|
||||
public Enumeration<JarEntry> entries() {
|
||||
Iterator<Map.Entry<String, Pair<JarEntry, ByteArrayOutputStream> >> i = entrySet().iterator();
|
||||
ArrayList<JarEntry> list = new ArrayList<>();
|
||||
while (i.hasNext())
|
||||
list.add(i.next().getValue().first);
|
||||
return Collections.enumeration(list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user