add zip handler to start install from a diff app
Signed-off-by: androidacy-user <opensource@androidacy.com>pull/267/head
parent
cb20fb4a8f
commit
efd5391ef8
@ -0,0 +1,75 @@
|
||||
package com.fox2code.mmm.utils;
|
||||
|
||||
import static androidx.fragment.app.FragmentManager.TAG;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.fox2code.foxcompat.FoxActivity;
|
||||
import com.fox2code.mmm.BuildConfig;
|
||||
import com.fox2code.mmm.R;
|
||||
import com.fox2code.mmm.installer.InstallerInitializer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ZipFileOpener extends FoxActivity {
|
||||
// Adds us as a handler for zip files, so we can pass them to the installer
|
||||
// We should have a content uri provided to us.
|
||||
@SuppressLint("RestrictedApi")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d("ZipFileOpener", "onCreate: " + getIntent());
|
||||
}
|
||||
File zipFile;
|
||||
Uri uri = getIntent().getData();
|
||||
if (uri == null) {
|
||||
Toast.makeText(this, R.string.zip_load_failed, Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
// Try to copy the file to our cache
|
||||
try {
|
||||
zipFile = File.createTempFile("module", ".zip", getCacheDir());
|
||||
try (InputStream inputStream = getContentResolver().openInputStream(uri); FileOutputStream outputStream = new FileOutputStream(zipFile)) {
|
||||
if (inputStream == null) {
|
||||
Log.e(TAG, "onCreate: Failed to open input stream");
|
||||
Toast.makeText(this, R.string.zip_load_failed, Toast.LENGTH_LONG).show();
|
||||
finishAndRemoveTask();
|
||||
return;
|
||||
}
|
||||
byte[] buffer = new byte[4096];
|
||||
int read;
|
||||
while ((read = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
} catch (
|
||||
Exception e) {
|
||||
Log.e(TAG, "onCreate: Failed to copy zip file", e);
|
||||
Toast.makeText(this, R.string.zip_load_failed, Toast.LENGTH_LONG).show();
|
||||
finishAndRemoveTask();
|
||||
return;
|
||||
}
|
||||
// Ensure zip is not empty
|
||||
if (zipFile.length() == 0) {
|
||||
Log.e(TAG, "onCreate: Zip file is empty");
|
||||
Toast.makeText(this, R.string.zip_load_failed, Toast.LENGTH_LONG).show();
|
||||
finishAndRemoveTask();
|
||||
return;
|
||||
}
|
||||
// Pass the file to the installer
|
||||
FoxActivity compatActivity = FoxActivity.getFoxActivity(this);
|
||||
IntentHelper.openInstaller(compatActivity, zipFile.getAbsolutePath(),
|
||||
compatActivity.getString(
|
||||
R.string.local_install_title), null, null, false,
|
||||
BuildConfig.DEBUG && // Use debug mode if no root
|
||||
InstallerInitializer.peekMagiskPath() == null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue