mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
Show Toast in UI thread and fix unpacking on Android 10 (#9900)
This commit is contained in:
parent
850af80089
commit
87b25e583d
@ -20,11 +20,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
|
|
||||||
package net.minetest.minetest;
|
package net.minetest.minetest;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -33,10 +34,10 @@ import java.lang.ref.WeakReference;
|
|||||||
|
|
||||||
public class CopyZipTask extends AsyncTask<String, Void, String> {
|
public class CopyZipTask extends AsyncTask<String, Void, String> {
|
||||||
|
|
||||||
private final WeakReference<Context> contextRef;
|
private final WeakReference<AppCompatActivity> activityRef;
|
||||||
|
|
||||||
CopyZipTask(Context context) {
|
CopyZipTask(AppCompatActivity activity) {
|
||||||
contextRef = new WeakReference<>(context);
|
activityRef = new WeakReference<>(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String doInBackground(String... params) {
|
protected String doInBackground(String... params) {
|
||||||
@ -51,11 +52,14 @@ public class CopyZipTask extends AsyncTask<String, Void, String> {
|
|||||||
|
|
||||||
private void copyAsset(String zipName) {
|
private void copyAsset(String zipName) {
|
||||||
String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
|
String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
|
||||||
try (InputStream in = contextRef.get().getAssets().open(filename);
|
try (InputStream in = activityRef.get().getAssets().open(filename);
|
||||||
OutputStream out = new FileOutputStream(zipName)) {
|
OutputStream out = new FileOutputStream(zipName)) {
|
||||||
copyFile(in, out);
|
copyFile(in, out);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Toast.makeText(contextRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
|
AppCompatActivity activity = activityRef.get();
|
||||||
|
if (activity != null) {
|
||||||
|
activity.runOnUiThread(() -> Toast.makeText(activityRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show());
|
||||||
|
}
|
||||||
cancel(true);
|
cancel(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,8 +72,11 @@ public class CopyZipTask extends AsyncTask<String, Void, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startUnzipService(String file) {
|
private void startUnzipService(String file) {
|
||||||
Intent intent = new Intent(contextRef.get(), UnzipService.class);
|
Intent intent = new Intent(activityRef.get(), UnzipService.class);
|
||||||
intent.putExtra(UnzipService.EXTRA_KEY_IN_FILE, file);
|
intent.putExtra(UnzipService.EXTRA_KEY_IN_FILE, file);
|
||||||
contextRef.get().startService(intent);
|
AppCompatActivity activity = activityRef.get();
|
||||||
|
if (activity != null) {
|
||||||
|
activity.startService(intent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.minetest.minetest.UnzipService.ACTION_FAILURE;
|
||||||
import static net.minetest.minetest.UnzipService.ACTION_PROGRESS;
|
import static net.minetest.minetest.UnzipService.ACTION_PROGRESS;
|
||||||
import static net.minetest.minetest.UnzipService.ACTION_UPDATE;
|
import static net.minetest.minetest.UnzipService.ACTION_UPDATE;
|
||||||
import static net.minetest.minetest.UnzipService.FAILURE;
|
import static net.minetest.minetest.UnzipService.FAILURE;
|
||||||
@ -71,6 +72,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
mTextView.setVisibility(View.VISIBLE);
|
mTextView.setVisibility(View.VISIBLE);
|
||||||
} else if (progress == FAILURE) {
|
} else if (progress == FAILURE) {
|
||||||
|
Toast.makeText(MainActivity.this, intent.getStringExtra(ACTION_FAILURE), Toast.LENGTH_LONG).show();
|
||||||
finish();
|
finish();
|
||||||
} else if (progress == SUCCESS)
|
} else if (progress == SUCCESS)
|
||||||
startNative();
|
startNative();
|
||||||
@ -138,6 +140,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBackPressed() {
|
||||||
|
// Prevent abrupt interruption when copy game files from assets
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
@ -42,20 +42,21 @@ import java.util.zip.ZipInputStream;
|
|||||||
public class UnzipService extends IntentService {
|
public class UnzipService extends IntentService {
|
||||||
public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
|
public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
|
||||||
public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
|
public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
|
||||||
|
public static final String ACTION_FAILURE = "net.minetest.minetest.FAILURE";
|
||||||
public static final String EXTRA_KEY_IN_FILE = "file";
|
public static final String EXTRA_KEY_IN_FILE = "file";
|
||||||
public static final int SUCCESS = -1;
|
public static final int SUCCESS = -1;
|
||||||
public static final int FAILURE = -2;
|
public static final int FAILURE = -2;
|
||||||
private static final String TAG = "UnzipService";
|
|
||||||
private final int id = 1;
|
private final int id = 1;
|
||||||
private NotificationManager mNotifyManager;
|
private NotificationManager mNotifyManager;
|
||||||
private boolean isSuccess = true;
|
private boolean isSuccess = true;
|
||||||
|
private String failureMessage;
|
||||||
|
|
||||||
public UnzipService() {
|
public UnzipService() {
|
||||||
super("net.minetest.minetest.UnzipService");
|
super("net.minetest.minetest.UnzipService");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void isDir(String dir, String location) {
|
private void isDir(String dir, String location) {
|
||||||
File f = new File(location + dir);
|
File f = new File(location, dir);
|
||||||
if (!f.isDirectory())
|
if (!f.isDirectory())
|
||||||
f.mkdirs();
|
f.mkdirs();
|
||||||
}
|
}
|
||||||
@ -99,7 +100,8 @@ public class UnzipService extends IntentService {
|
|||||||
|
|
||||||
private void unzip(Intent intent) {
|
private void unzip(Intent intent) {
|
||||||
String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
|
String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
|
||||||
String location = Environment.getExternalStorageDirectory() + "/Minetest/";
|
isDir("Minetest", Environment.getExternalStorageDirectory().toString());
|
||||||
|
String location = Environment.getExternalStorageDirectory() + File.separator + "Minetest" + File.separator;
|
||||||
int per = 0;
|
int per = 0;
|
||||||
int size = getSummarySize(zip);
|
int size = getSummarySize(zip);
|
||||||
File zipFile = new File(zip);
|
File zipFile = new File(zip);
|
||||||
@ -124,13 +126,14 @@ public class UnzipService extends IntentService {
|
|||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
isSuccess = false;
|
isSuccess = false;
|
||||||
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
|
failureMessage = e.getLocalizedMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void publishProgress(int progress) {
|
private void publishProgress(int progress) {
|
||||||
Intent intentUpdate = new Intent(ACTION_UPDATE);
|
Intent intentUpdate = new Intent(ACTION_UPDATE);
|
||||||
intentUpdate.putExtra(ACTION_PROGRESS, progress);
|
intentUpdate.putExtra(ACTION_PROGRESS, progress);
|
||||||
|
if (!isSuccess) intentUpdate.putExtra(ACTION_FAILURE, failureMessage);
|
||||||
sendBroadcast(intentUpdate);
|
sendBroadcast(intentUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user