1 package edu.jiangxin.apktoolbox.help;
2
3 import java.awt.Component;
4 import java.awt.Toolkit;
5 import java.io.IOException;
6
7 import javax.swing.JOptionPane;
8
9 import edu.jiangxin.apktoolbox.swing.extend.listener.ChangeMenuListener;
10 import org.apache.commons.lang3.StringUtils;
11 import org.apache.http.HttpEntity;
12 import org.apache.http.HttpStatus;
13 import org.apache.http.ParseException;
14 import org.apache.http.StatusLine;
15 import org.apache.http.client.config.RequestConfig;
16 import org.apache.http.client.methods.CloseableHttpResponse;
17 import org.apache.http.client.methods.HttpGet;
18 import org.apache.http.impl.client.CloseableHttpClient;
19 import org.apache.http.impl.client.HttpClients;
20 import org.apache.http.util.EntityUtils;
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25
26 import edu.jiangxin.apktoolbox.Version;
27
28
29
30
31
32
33 public class CheckUpdateActionListener implements ChangeMenuListener {
34 private static final int SOCKET_TIMEOUT_TIME = 4000;
35
36 private static final int CONNECT_TIMEOUT_TIME = 4000;
37
38 private static final Logger logger = LogManager.getLogger(CheckUpdateActionListener.class.getSimpleName());
39
40 private Component parent;
41
42 private CloseableHttpClient closeableHttpClient;
43
44 private CloseableHttpResponse closeableHttpResponse;
45
46 public CheckUpdateActionListener(Component component) {
47 super();
48 parent = component;
49 }
50
51 @Override
52 public void onChangeMenu() {
53 String responseString = null;
54
55 closeableHttpClient = HttpClients.createDefault();
56
57 try {
58 HttpGet httpGet = new HttpGet(Constant.URL_CHECK_UPDATE);
59
60 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT_TIME)
61 .setConnectTimeout(CONNECT_TIMEOUT_TIME).build();
62 httpGet.setConfig(requestConfig);
63 closeableHttpResponse = closeableHttpClient.execute(httpGet);
64 logger.info("execute request finished");
65 } catch (IOException ex) {
66 processException(ex);
67 return;
68 }
69
70 StatusLine statusLine = closeableHttpResponse.getStatusLine();
71 if (statusLine == null) {
72 processException(new Exception("statusLine is null"));
73 return;
74 }
75
76 if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
77 HttpEntity entity = closeableHttpResponse.getEntity();
78 try {
79 responseString = EntityUtils.toString(entity);
80 logger.info(responseString);
81 } catch (ParseException | IOException ex) {
82 processException(ex);
83 return;
84 }
85 } else {
86 processException(new Exception("invalid statusCode"));
87 return;
88 }
89
90 JSONObject release;
91 try {
92 release = new JSONObject(responseString);
93 } catch (JSONException ex) {
94 processException(ex);
95 return;
96 }
97 String latestVersion = release.getString("tag_name");
98 if (StringUtils.isEmpty(latestVersion)) {
99 processException(new Exception("latestVersion is empyt"));
100 return;
101 }
102 processResult(latestVersion);
103 }
104
105 private void processException(Exception ex) {
106 logger.error("checking for updates failed: ", ex);
107 Toolkit.getDefaultToolkit().beep();
108 JOptionPane.showMessageDialog(parent, "checking for updates failed", "ERROR", JOptionPane.ERROR_MESSAGE);
109 releaseResource();
110 }
111
112 private void processResult(String latestVersion) {
113 logger.info("checking for updates successed");
114 Toolkit.getDefaultToolkit().beep();
115 JOptionPane.showMessageDialog(parent,
116 "Latest version: " + latestVersion + "\nLocal version: " + Version.VERSION, "Update",
117 JOptionPane.INFORMATION_MESSAGE);
118 releaseResource();
119 }
120
121 private void releaseResource() {
122 if (closeableHttpResponse != null) {
123 try {
124 closeableHttpResponse.close();
125 } catch (IOException e) {
126 logger.error("closeableHttpResponse close failed", e);
127 }
128 }
129 if (closeableHttpClient != null) {
130 try {
131 closeableHttpClient.close();
132 } catch (IOException e) {
133 logger.error("closeableHttpClient close failed", e);
134 }
135 }
136 }
137
138 }