3 Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
4 Copyright (C) 2014-2020 ubulem, Bektur Mambetov <berkut87@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 package net.minetest.minetest;
23 import android.content.Intent;
24 import android.os.AsyncTask;
25 import android.widget.Toast;
27 import androidx.appcompat.app.AppCompatActivity;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.lang.ref.WeakReference;
35 public class CopyZipTask extends AsyncTask<String, Void, String> {
37 private final WeakReference<AppCompatActivity> activityRef;
39 CopyZipTask(AppCompatActivity activity) {
40 activityRef = new WeakReference<>(activity);
43 protected String doInBackground(String... params) {
49 protected void onPostExecute(String result) {
50 startUnzipService(result);
53 private void copyAsset(String zipName) {
54 String filename = zipName.substring(zipName.lastIndexOf("/") + 1);
55 try (InputStream in = activityRef.get().getAssets().open(filename);
56 OutputStream out = new FileOutputStream(zipName)) {
58 } catch (IOException e) {
59 AppCompatActivity activity = activityRef.get();
60 if (activity != null) {
61 activity.runOnUiThread(() -> Toast.makeText(activityRef.get(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show());
67 private void copyFile(InputStream in, OutputStream out) throws IOException {
68 byte[] buffer = new byte[1024];
70 while ((read = in.read(buffer)) != -1)
71 out.write(buffer, 0, read);
74 private void startUnzipService(String file) {
75 Intent intent = new Intent(activityRef.get(), UnzipService.class);
76 intent.putExtra(UnzipService.EXTRA_KEY_IN_FILE, file);
77 AppCompatActivity activity = activityRef.get();
78 if (activity != null) {
79 activity.startService(intent);