Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit c3586b1

Browse files
committed
Allow to cancel if module install script not yet started. (Fix #45)
1 parent f665030 commit c3586b1

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ public class InstallerActivity extends CompatActivity {
4343
private File moduleCache;
4444
private File toDelete;
4545
private boolean textWrap;
46+
private boolean canceled;
4647

4748
@Override
4849
protected void onCreate(Bundle savedInstanceState) {
4950
this.moduleCache = new File(this.getCacheDir(), "installer");
5051
if (!this.moduleCache.exists() && !this.moduleCache.mkdirs())
5152
Log.e(TAG, "Failed to mkdir module cache dir!");
5253
super.onCreate(savedInstanceState);
53-
this.setDisplayHomeAsUpEnabled(false);
54-
this.setOnBackPressedCallback(DISABLE_BACK_BUTTON);
54+
this.setDisplayHomeAsUpEnabled(true);
55+
this.setOnBackPressedCallback(a -> {
56+
this.canceled = true; return false;
57+
});
5558
final Intent intent = this.getIntent();
5659
final String target;
5760
final String name;
@@ -125,6 +128,7 @@ protected void onCreate(Bundle savedInstanceState) {
125128
this.progressIndicator.setProgressCompat(progress, true);
126129
});
127130
});
131+
if (this.canceled) return;
128132
if (checksum != null && !checksum.isEmpty()) {
129133
Log.d(TAG, "Checking for checksum: " + checksum);
130134
this.installerTerminal.addLine("- Checking file integrity");
@@ -134,6 +138,7 @@ protected void onCreate(Bundle savedInstanceState) {
134138
return;
135139
}
136140
}
141+
if (this.canceled) return;
137142
if (noPatch) {
138143
try (OutputStream outputStream = new FileOutputStream(moduleCache)) {
139144
outputStream.write(rawModule);
@@ -151,8 +156,9 @@ protected void onCreate(Bundle savedInstanceState) {
151156
outputStream.flush();
152157
}
153158
}
159+
if (this.canceled) return;
154160
//noinspection UnusedAssignment (Important to avoid OutOfMemoryError)
155-
rawModule = null;
161+
rawModule = null; // Because reference is kept when calling doInstall
156162
this.runOnUiThread(() -> {
157163
this.installerTerminal.addLine("- Installing " + name);
158164
});
@@ -180,6 +186,7 @@ protected void onCreate(Bundle savedInstanceState) {
180186
"! File integrity check failed", "");
181187
return;
182188
}
189+
if (this.canceled) return;
183190
}
184191
this.installerTerminal.addLine("- Installing " + name);
185192
new Thread(() -> this.doInstall(
@@ -190,6 +197,9 @@ protected void onCreate(Bundle savedInstanceState) {
190197

191198

192199
private void doInstall(File file,boolean noExtensions,boolean rootless) {
200+
this.setOnBackPressedCallback(DISABLE_BACK_BUTTON);
201+
this.setDisplayHomeAsUpEnabled(false);
202+
if (this.canceled) return;
193203
Log.i(TAG, "Installing: " + moduleCache.getName());
194204
InstallerController installerController = new InstallerController(
195205
this.progressIndicator, this.installerTerminal,
@@ -420,15 +430,12 @@ private String doCleanUp() {
420430
}
421431
}
422432

423-
private static boolean didExtract = false;
424-
425433
private File extractInstallScript(String script) {
426434
File compatInstallScript = new File(this.moduleCache, script);
427-
if (!compatInstallScript.exists() || compatInstallScript.length() == 0 || !didExtract) {
435+
if (!compatInstallScript.exists() || compatInstallScript.length() == 0) {
428436
try {
429437
Files.write(compatInstallScript, Files.readAllBytes(
430438
this.getAssets().open(script)));
431-
didExtract = true;
432439
} catch (IOException e) {
433440
compatInstallScript.delete();
434441
Log.e(TAG, "Failed to extract " + script, e);

0 commit comments

Comments
 (0)