]> git.lizzy.rs Git - dragonfireclient.git/blob - android/app/src/main/java/net/minetest/minetest/CopyZipTask.java
Make /status message easier to read
[dragonfireclient.git] / android / app / src / main / java / net / minetest / minetest / CopyZipTask.java
1 /*
2 Minetest
3 Copyright (C) 2014-2020 MoNTE48, Maksim Gamarnik <MoNTE48@mail.ua>
4 Copyright (C) 2014-2020 ubulem,  Bektur Mambetov <berkut87@gmail.com>
5
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.
10
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.
15
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.
19 */
20
21 package net.minetest.minetest;
22
23 import android.content.Intent;
24 import android.os.AsyncTask;
25 import android.widget.Toast;
26
27 import androidx.appcompat.app.AppCompatActivity;
28
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;
34
35 public class CopyZipTask extends AsyncTask<String, Void, String> {
36
37         private final WeakReference<AppCompatActivity> activityRef;
38
39         CopyZipTask(AppCompatActivity activity) {
40                 activityRef = new WeakReference<>(activity);
41         }
42
43         protected String doInBackground(String... params) {
44                 copyAsset(params[0]);
45                 return params[0];
46         }
47
48         @Override
49         protected void onPostExecute(String result) {
50                 startUnzipService(result);
51         }
52
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)) {
57                         copyFile(in, out);
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());
62                         }
63                         cancel(true);
64                 }
65         }
66
67         private void copyFile(InputStream in, OutputStream out) throws IOException {
68                 byte[] buffer = new byte[1024];
69                 int read;
70                 while ((read = in.read(buffer)) != -1)
71                         out.write(buffer, 0, read);
72         }
73
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);
80                 }
81         }
82 }