Skip to content

Commit 87b25e5

Browse files
authoredJun 5, 2020
Show Toast in UI thread and fix unpacking on Android 10 (#9900)
1 parent 850af80 commit 87b25e5

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed
 

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/CopyZipTask.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
package net.minetest.minetest;
2222

23-
import android.content.Context;
2423
import android.content.Intent;
2524
import android.os.AsyncTask;
2625
import android.widget.Toast;
2726

27+
import androidx.appcompat.app.AppCompatActivity;
28+
2829
import java.io.FileOutputStream;
2930
import java.io.IOException;
3031
import java.io.InputStream;
@@ -33,10 +34,10 @@
3334

3435
public class CopyZipTask extends AsyncTask<String, Void, String> {
3536

36-
private final WeakReference<Context> contextRef;
37+
private final WeakReference<AppCompatActivity> activityRef;
3738

38-
CopyZipTask(Context context) {
39-
contextRef = new WeakReference<>(context);
39+
CopyZipTask(AppCompatActivity activity) {
40+
activityRef = new WeakReference<>(activity);
4041
}
4142

4243
protected String doInBackground(String... params) {
@@ -51,11 +52,14 @@ protected void onPostExecute(String result) {
5152

5253
private void copyAsset(String zipName) {
5354
String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
54-
try (InputStream in = contextRef.get().getAssets().open(filename);
55+
try (InputStream in = activityRef.get().getAssets().open(filename);
5556
OutputStream out = new FileOutputStream(zipName)) {
5657
copyFile(in, out);
5758
} catch (IOException e) {
58-
Toast.makeText(contextRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
59+
AppCompatActivity activity = activityRef.get();
60+
if (activity != null) {
61+
activity.runOnUiThread(() -> Toast.makeText(activityRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show());
62+
}
5963
cancel(true);
6064
}
6165
}
@@ -68,8 +72,11 @@ private void copyFile(InputStream in, OutputStream out) throws IOException {
6872
}
6973

7074
private void startUnzipService(String file) {
71-
Intent intent = new Intent(contextRef.get(), UnzipService.class);
75+
Intent intent = new Intent(activityRef.get(), UnzipService.class);
7276
intent.putExtra(UnzipService.EXTRA_KEY_IN_FILE, file);
73-
contextRef.get().startService(intent);
77+
AppCompatActivity activity = activityRef.get();
78+
if (activity != null) {
79+
activity.startService(intent);
80+
}
7481
}
7582
}

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/MainActivity.java

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Arrays;
4444
import java.util.List;
4545

46+
import static net.minetest.minetest.UnzipService.ACTION_FAILURE;
4647
import static net.minetest.minetest.UnzipService.ACTION_PROGRESS;
4748
import static net.minetest.minetest.UnzipService.ACTION_UPDATE;
4849
import static net.minetest.minetest.UnzipService.FAILURE;
@@ -71,6 +72,7 @@ public void onReceive(Context context, Intent intent) {
7172
}
7273
mTextView.setVisibility(View.VISIBLE);
7374
} else if (progress == FAILURE) {
75+
Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show();
7476
finish();
7577
} else if (progress == SUCCESS)
7678
startNative();
@@ -138,6 +140,11 @@ private void startNative() {
138140
startActivity(intent);
139141
}
140142

143+
@Override
144+
public void onBackPressed() {
145+
// Prevent abrupt interruption when copy game files from assets
146+
}
147+
141148
@Override
142149
protected void onDestroy() {
143150
super.onDestroy();

Diff for: ‎build/android/app/src/main/java/net/minetest/minetest/UnzipService.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,21 @@
4242
public class UnzipService extends IntentService {
4343
public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
4444
public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
45+
public static final String ACTION_FAILURE = "net.minetest.minetest.FAILURE";
4546
public static final String EXTRA_KEY_IN_FILE = "file";
4647
public static final int SUCCESS = -1;
4748
public static final int FAILURE = -2;
48-
private static final String TAG = "UnzipService";
4949
private final int id = 1;
5050
private NotificationManager mNotifyManager;
5151
private boolean isSuccess = true;
52+
private String failureMessage;
5253

5354
public UnzipService() {
5455
super("net.minetest.minetest.UnzipService");
5556
}
5657

5758
private void isDir(String dir, String location) {
58-
File f = new File(location + dir);
59+
File f = new File(location, dir);
5960
if (!f.isDirectory())
6061
f.mkdirs();
6162
}
@@ -99,7 +100,8 @@ private void createNotification() {
99100

100101
private void unzip(Intent intent) {
101102
String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
102-
String location = Environment.getExternalStorageDirectory() + "/Minetest/";
103+
isDir("Minetest", Environment.getExternalStorageDirectory().toString());
104+
String location = Environment.getExternalStorageDirectory() + File.separator + "Minetest" + File.separator;
103105
int per = 0;
104106
int size = getSummarySize(zip);
105107
File zipFile = new File(zip);
@@ -124,13 +126,14 @@ private void unzip(Intent intent) {
124126
}
125127
} catch (IOException e) {
126128
isSuccess = false;
127-
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
129+
failureMessage = e.getLocalizedMessage();
128130
}
129131
}
130132

131133
private void publishProgress(int progress) {
132134
Intent intentUpdate = new Intent(ACTION_UPDATE);
133135
intentUpdate.putExtra(ACTION_PROGRESS, progress);
136+
if (!isSuccess) intentUpdate.putExtra(ACTION_FAILURE, failureMessage);
134137
sendBroadcast(intentUpdate);
135138
}
136139

0 commit comments

Comments
 (0)
Please sign in to comment.