1 package edu.jiangxin.apktoolbox.swing.extend.plugin;
2
3 import edu.jiangxin.apktoolbox.swing.extend.plugin.runnable.DownloadRunnable;
4 import edu.jiangxin.apktoolbox.swing.extend.plugin.runnable.UnzipRunnable;
5 import edu.jiangxin.apktoolbox.utils.Utils;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8
9 import java.io.File;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14
15 public class PluginUtils {
16 private static final Logger logger = LogManager.getLogger(PluginUtils.class.getSimpleName());
17
18 private static final String URL_PREFIX = "https://gitee.com/jiangxinnju/apk-tool-box-gui-plugins/releases/download/";
19
20 private static final String DOWNLOAD_VERSION = "v1.0.5";
21
22 private PluginUtils() {
23
24 }
25
26 public static void checkPlugin(String pluginFilename, IPreparePluginCallback callBack) {
27 File pluginFile = new File(Utils.getPluginDirPath(), pluginFilename);
28 File pluginDir = new File(Utils.getPluginDirPath(), pluginFilename.substring(0, pluginFilename.lastIndexOf(".")));
29 if (pluginFile.exists() && pluginDir.exists()) {
30 callBack.onCheckFinished(ChangeMenuPreparePluginController.RESULT_CHECK_SUCCESS);
31 } else if (pluginFile.exists() && !pluginDir.exists()) {
32 callBack.onCheckFinished(ChangeMenuPreparePluginController.RESULT_CHECK_ZIP_EXIST);
33 } else {
34 callBack.onCheckFinished(ChangeMenuPreparePluginController.RESULT_CHECK_ZIP_NOT_EXIST);
35 }
36 }
37
38 public static void downloadPlugin(String pluginFilename, IPreparePluginCallback callBack) {
39 String downloadUrlStr = URL_PREFIX + DOWNLOAD_VERSION + "/" + pluginFilename;
40 URL url;
41 try {
42 url = new URL(downloadUrlStr);
43 } catch (MalformedURLException e) {
44 logger.error("MalformedURLException: {}", e.getMessage());
45 return;
46 }
47 File pluginDir = new File(Utils.getPluginDirPath());
48
49 ExecutorService executorService = Executors.newSingleThreadExecutor();
50 DownloadRunnable downloadRunnable = new DownloadRunnable(url, pluginDir, callBack);
51 executorService.submit(downloadRunnable);
52 }
53
54
55 public static void unzipPlugin(String pluginFilename, IPreparePluginCallback callback) {
56 File pluginFile = new File(Utils.getPluginDirPath(), pluginFilename);
57
58 ExecutorService executorService = Executors.newSingleThreadExecutor();
59 UnzipRunnable unzipRunnable = new UnzipRunnable(pluginFile, callback);
60 executorService.submit(unzipRunnable);
61 }
62 }