View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend.plugin;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.listener.IPreChangeMenuCallBack;
4   import org.apache.logging.log4j.LogManager;
5   import org.apache.logging.log4j.Logger;
6   
7   import javax.swing.*;
8   
9   /**
10   * This class is used to prepare plugin for change menu.
11   * <p>
12   * All operations in this class will be executed in main thread.
13   */
14  public class ChangeMenuPreparePluginController implements IPreparePluginCallback {
15      private static final Logger logger = LogManager.getLogger(ChangeMenuPreparePluginController.class.getSimpleName());
16      private final String pluginFilename;
17      private final boolean isPluginNeedUnzip;
18      private final IPreChangeMenuCallBack callBack;
19  
20      public static final int RESULT_CHECK_SUCCESS = 0;
21  
22      public static final int RESULT_CHECK_ZIP_EXIST = 1;
23  
24      public static final int RESULT_CHECK_ZIP_NOT_EXIST = 2;
25  
26      public static final int RESULT_DOWNLOAD_SUCCESS = 0;
27  
28      public static final int RESULT_DOWNLOAD_FAILED = -1;
29  
30      public static final int RESULT_DOWNLOAD_CANCELLED = 1;
31  
32      public static final int RESULT_UNZIP_SUCCESS = 0;
33  
34      public static final int RESULT_UNZIP_FAILED = -1;
35  
36      public static final int RESULT_UNZIP_CANCELLED = 1;
37  
38      public ChangeMenuPreparePluginController(String pluginFilename, boolean isPluginNeedUnzip, IPreChangeMenuCallBack callBack) {
39          this.pluginFilename = pluginFilename;
40          this.isPluginNeedUnzip = isPluginNeedUnzip;
41          this.callBack = callBack;
42      }
43  
44      @Override
45      public void onPrepareStarted() {
46          PluginUtils.checkPlugin(pluginFilename, this);
47      }
48  
49      @Override
50      public void onCheckFinished(int result) {
51          switch (result) {
52              case RESULT_CHECK_SUCCESS -> onPrepareFinished();
53              case RESULT_CHECK_ZIP_EXIST -> PluginUtils.unzipPlugin(pluginFilename, this);
54              case RESULT_CHECK_ZIP_NOT_EXIST -> {
55                  int userChoose = JOptionPane.showConfirmDialog(null, "未找到对应插件,是否下载", "提示", JOptionPane.YES_NO_OPTION);
56                  if (userChoose == JOptionPane.YES_OPTION) {
57                      PluginUtils.downloadPlugin(pluginFilename, this);
58                  }
59              }
60              default -> logger.info("onCheckFinished: {}", result);
61          }
62      }
63  
64      @Override
65      public void onDownloadFinished(int result) {
66          switch (result) {
67              case RESULT_DOWNLOAD_SUCCESS:
68                  if (isPluginNeedUnzip) {
69                      PluginUtils.unzipPlugin(pluginFilename, this);
70                  } else {
71                      onPrepareFinished();
72                  }
73                  break;
74              case RESULT_DOWNLOAD_FAILED:
75                  JOptionPane.showMessageDialog(null, "下载失败,请检查网络", "错误", JOptionPane.ERROR_MESSAGE);
76                  break;
77              case RESULT_DOWNLOAD_CANCELLED:
78                  JOptionPane.showMessageDialog(null, "下载取消", "提示", JOptionPane.INFORMATION_MESSAGE);
79                  break;
80              default:
81                  logger.info("onDownloadFinished: {}", result);
82                  break;
83          }
84      }
85  
86      @Override
87      public void onUnzipFinished(int result) {
88          switch (result) {
89              case RESULT_UNZIP_SUCCESS -> onPrepareFinished();
90              case RESULT_UNZIP_FAILED -> JOptionPane.showMessageDialog(null, "解压失败", "错误", JOptionPane.ERROR_MESSAGE);
91              case RESULT_UNZIP_CANCELLED -> JOptionPane.showMessageDialog(null, "解压取消", "提示", JOptionPane.INFORMATION_MESSAGE);
92              default -> logger.info("onUnzipFinished: {}", result);
93          }
94      }
95  
96      @Override
97      public void onPrepareFinished() {
98          if (callBack != null) {
99              callBack.onPreChangeMenuFinished();
100         }
101     }
102 }