|
@@ -20,11 +20,12 @@ |
|
|
|
|
|
package net.minetest.minetest; |
|
|
|
|
|
import android.content.Context; |
|
|
import android.content.Intent; |
|
|
import android.os.AsyncTask; |
|
|
import android.widget.Toast; |
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
@@ -33,10 +34,10 @@ |
|
|
|
|
|
public class CopyZipTask extends AsyncTask<String, Void, String> { |
|
|
|
|
|
private final WeakReference<Context> contextRef; |
|
|
private final WeakReference<AppCompatActivity> activityRef; |
|
|
|
|
|
CopyZipTask(Context context) { |
|
|
contextRef = new WeakReference<>(context); |
|
|
CopyZipTask(AppCompatActivity activity) { |
|
|
activityRef = new WeakReference<>(activity); |
|
|
} |
|
|
|
|
|
protected String doInBackground(String... params) { |
|
@@ -51,11 +52,14 @@ protected void onPostExecute(String result) { |
|
|
|
|
|
private void copyAsset(String zipName) { |
|
|
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)) { |
|
|
copyFile(in, out); |
|
|
} 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); |
|
|
} |
|
|
} |
|
@@ -68,8 +72,11 @@ private void copyFile(InputStream in, OutputStream out) throws IOException { |
|
|
} |
|
|
|
|
|
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); |
|
|
contextRef.get().startService(intent); |
|
|
AppCompatActivity activity = activityRef.get(); |
|
|
if (activity != null) { |
|
|
activity.startService(intent); |
|
|
} |
|
|
} |
|
|
} |