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.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;
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;
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;
53 public UnzipService() {
54 super("net.minetest.minetest.UnzipService");
57 private void isDir(String dir, String location) {
58 File f = new File(location + dir);
64 protected void onHandleIntent(Intent intent) {
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);
90 builder = new Notification.Builder(this, channelId);
92 builder = new Notification.Builder(this);
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());
100 private void unzip(Intent intent) {
101 String zip = intent.getStringExtra(EXTRA_KEY_IN_FILE);
102 String location = Environment.getExternalStorageDirectory() + "/Minetest/";
104 int size = getSummarySize(zip);
105 File zipFile = new File(zip);
107 byte[] readBuffer = new byte[8192];
108 try (FileInputStream fileInputStream = new FileInputStream(zipFile);
109 ZipInputStream zipInputStream = new ZipInputStream(fileInputStream)) {
111 while ((ze = zipInputStream.getNextEntry()) != null) {
112 if (ze.isDirectory()) {
114 isDir(ze.getName(), location);
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);
125 } catch (IOException e) {
127 Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
131 private void publishProgress(int progress) {
132 Intent intentUpdate = new Intent(ACTION_UPDATE);
133 intentUpdate.putExtra(ACTION_PROGRESS, progress);
134 sendBroadcast(intentUpdate);
137 private int getSummarySize(String zip) {
140 ZipFile zipSize = new ZipFile(zip);
141 size += zipSize.size();
142 } catch (IOException e) {
143 Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
149 public void onDestroy() {
151 mNotifyManager.cancel(id);
152 publishProgress(isSuccess ? SUCCESS : FAILURE);