Zip Autoflash; Massive refactor

This commit is contained in:
topjohnwu
2016-08-29 06:35:07 +08:00
parent 1f02d0f6d0
commit e21131d67e
20 changed files with 551 additions and 480 deletions
@@ -1,17 +0,0 @@
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);
}
}
@@ -1,52 +0,0 @@
package com.topjohnwu.magisk.receivers;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import com.topjohnwu.magisk.R;
import java.io.File;
/**
* Created by topjohnwu
*/
public abstract class DownloadReceiver extends BroadcastReceiver {
public Context mContext;
long downloadID;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadID);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
File file = new File(Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).getPath());
task(file);
break;
default:
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
break;
}
context.unregisterReceiver(this);
}
}
}
public void setDownloadID(long id) { downloadID = id;}
public abstract void task(File file);
}
@@ -1,23 +0,0 @@
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();
}
}