Added comments and summaries to BackgroundTask.java

This commit is contained in:
Chris 2019-11-12 18:45:32 -05:00
parent ff66dd4a1a
commit 361eef5b9a
1 changed files with 56 additions and 2 deletions

View File

@ -7,9 +7,15 @@ import android.util.Log;
import java.io.File; import java.io.File;
/**
* Handles the background task of creating the new SMAPI APK
*/
public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> { public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
//TAG used for debug purposes
private static final String TAG = "BackgroundTask"; private static final String TAG = "BackgroundTask";
//String constants used for file paths
private static final String ASSET_APK_FILES = "SMAPI"; private static final String ASSET_APK_FILES = "SMAPI";
private static final String ASSET_STARDEW_FILES = "Stardew"; private static final String ASSET_STARDEW_FILES = "Stardew";
private static final String MOD_FILES_VK = "VirtualKeyboard"; private static final String MOD_FILES_VK = "VirtualKeyboard";
@ -34,12 +40,24 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
private static final String MOD_DIR = Environment.getExternalStorageDirectory() + "/StardewValley/Mods/"; private static final String MOD_DIR = Environment.getExternalStorageDirectory() + "/StardewValley/Mods/";
private static final String STARDEW_VALLEY_DIR = Environment.getExternalStorageDirectory() + "/StardewValley/"; private static final String STARDEW_VALLEY_DIR = Environment.getExternalStorageDirectory() + "/StardewValley/";
//MainActivity context
private final Context contextActivity; private final Context contextActivity;
//Instance of ApkInstall
private ApkInstall apkInstall; private ApkInstall apkInstall;
//Instance of ApkExtractor
private ApkExtractor extractor; private ApkExtractor extractor;
//Whether the app is updating
private boolean updating; private boolean updating;
/**
* Constructor that sets the context, apk extractor and whether it's updating
* @param context = The activities context
* @param apkExtractor = The apk extractor
* @param update = Whether the app is updating
*/
public BackgroundTask(Context context, ApkExtractor apkExtractor, boolean update) public BackgroundTask(Context context, ApkExtractor apkExtractor, boolean update)
{ {
this.contextActivity = context; this.contextActivity = context;
@ -47,22 +65,34 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
this.updating = update; this.updating = update;
} }
/**
* Override that gets called before the task gets ran
*/
@Override @Override
protected void onPreExecute() protected void onPreExecute()
{ {
super.onPreExecute(); super.onPreExecute();
//The string to show in the dialog
int dialogString; int dialogString;
if (updating) if (updating)
dialogString = R.string.update_apk; dialogString = R.string.update_apk;
else else
dialogString = R.string.modify_apk; dialogString = R.string.modify_apk;
//Show the dialog
DialogFrag.showDialog(contextActivity, dialogString, 0); DialogFrag.showDialog(contextActivity, dialogString, 0);
} }
/**
* Override that does the bulk work
* @param voids = The void methods from AsyncTasks
* @return true if it completed successfully
*/
@Override @Override
protected Boolean doInBackground(Void... voids) protected Boolean doInBackground(Void... voids)
{ {
//Create instances of the needed classes
CopyAssets copy = new CopyAssets(contextActivity); CopyAssets copy = new CopyAssets(contextActivity);
WriteApk writeApk = new WriteApk(); WriteApk writeApk = new WriteApk();
SignApk signApk = new SignApk(); SignApk signApk = new SignApk();
@ -70,6 +100,7 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
try try
{ {
//Extract the apk
publishProgress(9); publishProgress(9);
extractor.extractAPK(); extractor.extractAPK();
@ -88,6 +119,7 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
if (!noMedia.exists()) if (!noMedia.exists())
noMedia.createNewFile(); noMedia.createNewFile();
//Copies the already provided assets to the needed directory from within the app
copy.copyAssets(ASSET_APK_FILES, DIR_APK_FILES); copy.copyAssets(ASSET_APK_FILES, DIR_APK_FILES);
publishProgress(18); publishProgress(18);
@ -108,6 +140,7 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
copy.copyAssets(MIPMAP_MDPI_ASSET, DIR_APK_FILES_MDPI); copy.copyAssets(MIPMAP_MDPI_ASSET, DIR_APK_FILES_MDPI);
copy.copyAssets(MIPMAP_HDPI_ASSET, DIR_APK_FILES_HDPI); copy.copyAssets(MIPMAP_HDPI_ASSET, DIR_APK_FILES_HDPI);
//File array that holds all the files that need to go inside the APK
File[] moddingAPI = File[] moddingAPI =
{ {
new File(Environment.getExternalStorageDirectory() + DIR_APK_FILES + "AndroidManifest.xml"), new File(Environment.getExternalStorageDirectory() + DIR_APK_FILES + "AndroidManifest.xml"),
@ -140,6 +173,7 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
new File( Environment.getExternalStorageDirectory() + DIR_APK_FILES_HDPI + "ic_launcher_round.png") new File( Environment.getExternalStorageDirectory() + DIR_APK_FILES_HDPI + "ic_launcher_round.png")
}; };
//Boolean array whether the correlating file from moddingAPI array needs to be compressed
boolean[] compressed = boolean[] compressed =
{ {
true, true,
@ -172,6 +206,7 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
true true
}; };
//String array for the path where the files from moddingAPI need to go in the APK
String[] paths = String[] paths =
{ {
"", "",
@ -205,19 +240,24 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
}; };
publishProgress(54); publishProgress(54);
//Add the files to the APK
writeApk.addFilesToApk(new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/" + ApkExtractor.sourceApkFilename), moddingAPI, paths, compressed); writeApk.addFilesToApk(new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/" + ApkExtractor.sourceApkFilename), moddingAPI, paths, compressed);
publishProgress(63); publishProgress(63);
//Sign the APK using jar signing
signApk.commitSignApk(); signApk.commitSignApk();
publishProgress(81); publishProgress(81);
//Delete the old files/APK
File deleteOldApk = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/" + ApkExtractor.sourceApkFilename + "_patched.apk"); File deleteOldApk = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/" + ApkExtractor.sourceApkFilename + "_patched.apk");
deleteOldApk.delete(); deleteOldApk.delete();
publishProgress(90); publishProgress(90);
//Cleanup other misc files
deleteApkFiles(); deleteApkFiles();
publishProgress(100); publishProgress(100);
//Return true since it completed
return true; return true;
} }
catch(Exception e) catch(Exception e)
@ -228,9 +268,15 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
} }
} }
/**
* Override that provides progress updates to get a progress bar in the dialog
* @param values = Integer values that rate to the current progress
*/
@Override @Override
protected void onProgressUpdate(Integer... values) { protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values); super.onProgressUpdate(values);
//Show a fun little message when the progress reaches a certain point
int message; int message;
switch(values[0]) switch(values[0])
{ {
@ -262,17 +308,25 @@ public class BackgroundTask extends AsyncTask<Void, Integer, Boolean> {
DialogFrag.updateProgressBar(values[0], message); DialogFrag.updateProgressBar(values[0], message);
} }
/**
* Override that gets called after the task finishes
* @param aBoolean = Whether the task was successful
*/
@Override @Override
protected void onPostExecute(Boolean aBoolean) { protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean); super.onPostExecute(aBoolean);
if (aBoolean) if (aBoolean)
{ {
//Dismiss the dialog and install the new APK
DialogFrag.dismissDialog(); DialogFrag.dismissDialog();
apkInstall.installNewStardew(); apkInstall.installNewStardew();
} }
} }
//This is super wack but I'm on a time crunch and can probably do this in one nested FOR loop... /**
* Delete all the files associated with changing the app icon
* This is super wack, this can be done better
*/
private void deleteApkFiles() private void deleteApkFiles()
{ {
File filesToDelete = new File(Environment.getExternalStorageDirectory() + DIR_APK_FILES); File filesToDelete = new File(Environment.getExternalStorageDirectory() + DIR_APK_FILES);