6356dff1931e4738e82e08216e338f3551633935
[oweals/minetest.git] / build / android / app / src / main / java / net / minetest / minetest / UnzipService.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.app.IntentService;
24 import android.app.Notification;
25 import android.app.NotificationChannel;
26 import android.app.NotificationManager;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.os.Build;
30 import android.os.Environment;
31 import android.widget.Toast;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStream;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipFile;
40 import java.util.zip.ZipInputStream;
41
42 public class UnzipService extends IntentService {
43         public static final String ACTION_UPDATE = "net.minetest.minetest.UPDATE";
44         public static final String ACTION_PROGRESS = "net.minetest.minetest.PROGRESS";
45         public static final String EXTRA_KEY_IN_FILE = "file";
46         public static final int SUCCESS = -1;
47         public static final int FAILURE = -2;
48         private static final String TAG = "UnzipService";
49         private final int id = 1;
50         private NotificationManager mNotifyManager;
51         private boolean isSuccess = true;
52
53         public UnzipService() {
54                 super("net.minetest.minetest.UnzipService");
55         }
56
57         private void isDir(String dir, String location) {
58                 File f = new File(location + dir);
59                 if (!f.isDirectory())
60                         f.mkdirs();
61         }
62
63         @Override
64         protected void onHandleIntent(Intent intent) {
65                 createNotification();
66                 unzip(intent);
67         }
68
69         private void createNotification() {
70                 String name = "net.minetest.minetest";
71                 String channelId = "Minetest channel";
72                 String description = "notifications from Minetest";
73                 Notification.Builder builder;
74                 if (mNotifyManager == null)
75                         mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
76                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
77                         int importance = NotificationManager.IMPORTANCE_LOW;
78                         NotificationChannel mChannel = null;
79                         if (mNotifyManager != null)
80                                 mChannel = mNotifyManager.getNotificationChannel(channelId);
81                         if (mChannel == null) {
82                                 mChannel = new NotificationChannel(channelId, name, importance);
83                                 mChannel.setDescription(description);
84                                 // Configure the notification channel, NO SOUND
85                                 mChannel.setSound(null, null);
86                                 mChannel.enableLights(false);
87                                 mChannel.enableVibration(false);
88                                 mNotifyManager.createNotificationChannel(mChannel);
89                         }
90                         builder = new Notification.Builder(this, channelId);
91                 } else {
92                         builder = new Notification.Builder(this);
93                 }
94                 builder.setContentTitle(getString(R.string.notification_title))
95                                 .setSmallIcon(R.mipmap.ic_launcher)
96                                 .setContentText(getString(R.string.notification_description));
97                 mNotifyManager.notify(id, builder.build());
98         }
99
100         private void unzip(Intent intent) {
101                 String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
102                 String location = Environment.getExternalStorageDirectory() + "/Minetest/";
103                 int per = 0;
104                 int size = getSummarySize(zip);
105                 File zipFile = new File(zip);
106                 int readLen;
107                 byte[] readBuffer = new byte[8192];
108                 try (FileInputStream fileInputStream = new FileInputStream(zipFile);
109                      ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
110                         ZipEntry ze;
111                         while ((ze = zipInputStream.getNextEntry()) != null) {
112                                 if (ze.isDirectory()) {
113                                         ++per;
114                                         isDir(ze.getName(), location);
115                                 } else {
116                                         publishProgress(100 * ++per / size);
117                                         try (OutputStream outputStream = new FileOutputStream(location + ze.getName())) {
118                                                 while ((readLen = zipInputStream.read(readBuffer)) != -1) {
119                                                         outputStream.write(readBuffer, 0, readLen);
120                                                 }
121                                         }
122                                 }
123                                 zipFile.delete();
124                         }
125                 } catch (IOException e) {
126                         isSuccess = false;
127                         Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
128                 }
129         }
130
131         private void publishProgress(int progress) {
132                 Intent intentUpdate = new Intent(ACTION_UPDATE);
133                 intentUpdate.putExtra(ACTION_PROGRESS, progress);
134                 sendBroadcast(intentUpdate);
135         }
136
137         private int getSummarySize(String zip) {
138                 int size = 0;
139                 try {
140                         ZipFile zipSize = new ZipFile(zip);
141                         size += zipSize.size();
142                 } catch (IOException e) {
143                         Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
144                 }
145                 return size;
146         }
147
148         @Override
149         public void onDestroy() {
150                 super.onDestroy();
151                 mNotifyManager.cancel(id);
152                 publishProgress(isSuccess ? SUCCESS : FAILURE);
153         }
154 }