yy1717
2021-01-12 683b1595260e638d1d3c6cc0d6543a72f6d6f925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.anyun.exam.lib;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Handler;
import android.util.Log;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
 
public class Upgrade {
    public static final String TAG = Upgrade.class.getCanonicalName();
    private static final String newProgramIndUrl = "http://upgrade.safeluck.com/ftp/aykj4/aks3.json";
    private static Upgrade instance = null;
    private Context context;
    private DownloadManagerUtil downloadManagerUtil = null;
    private InstallUtil installUtil = null;
 
    public static Upgrade getInstance(Context context) {
        if (instance == null) {
            synchronized (Upgrade.class) {
                if (instance == null) {
                    instance = new Upgrade(context);
                    instance.context = context;
                }
            }
        }
        return instance;
    }
 
    private Upgrade(Context context) {
        this.context = context;
 
        installUtil = InstallUtil.getInstance(context);
        downloadManagerUtil = DownloadManagerUtil.getInstance(context);
        downloadManagerUtil.SetDMCallback(new DMCB());
    }
 
    private String CheckNewVersion(String urlStr) {
        HttpURLConnection uRLConnection = null;
        InputStream is = null;
        BufferedReader buffer = null;
        String result = null;
        try {
            URL url = new URL(urlStr);
            uRLConnection = (HttpURLConnection) url.openConnection();
            uRLConnection.setConnectTimeout(8000);
            uRLConnection.setReadTimeout(8000);
            uRLConnection.setRequestMethod("GET");
            is = uRLConnection.getInputStream();
            buffer = new BufferedReader(new InputStreamReader(is));
            StringBuilder strBuilder = new StringBuilder();
            String line;
            while ((line = buffer.readLine()) != null) {
                strBuilder.append(line);
            }
            result = strBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "http post error");
        } finally {
            if (buffer != null) {
                try {
                    buffer.close();
                } catch (IOException ignored) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignored) {
 
                }
            }
            if (uRLConnection != null) {
                uRLConnection.disconnect();
            }
        }
        return result;
    }
 
    class DfuFileInfo {
        boolean valid = false;
        int appVerCode = 0;
        String appUrl = null;
        int systemVerCode = 0;
        String systemUrl = null;
 
        public void setValid(boolean en) {
            this.valid = en;
        }
 
        public void setApp(int code, String url) {
            this.appVerCode = code;
            this.appUrl = url;
        }
        public void setSystem(int code, String url) {
            this.systemVerCode = code;
            this.systemUrl = url;
        }
 
        public boolean getValid() {
            return valid;
        }
        public int getAppVerCode() {
            return appVerCode;
        }
        public String getAppUrl() {
            return appUrl;
        }
        public int getSystemVerCode() {
            return systemVerCode;
        }
        public String getSystemUrl() {
            return systemUrl;
        }
    }
 
    private DfuFileInfo parseJson(String result, int province, int city) {
        Log.d(TAG, "parseJson province " + province + " city " + city);
 
        DfuFileInfo dfuFileInfo = new DfuFileInfo();
 
        try {
            JSONObject obj = new JSONObject(result);
 
            JSONArray jsonarray = obj.getJSONArray("province");
 
            boolean marchProvince = false;
 
            for (int i = 0; !marchProvince && i < jsonarray.length(); i++) {
                JSONObject obj2 = jsonarray.getJSONObject(i);
 
                int pid = obj2.getInt("id");
                Log.d(TAG,  "pid = " + pid);
 
                if (pid == province) {
                    JSONArray jsonarray2 = obj2.getJSONArray("city");
 
                    boolean marchCity = false;
 
                    // 匹配省id成功
                    marchProvince = true;
 
                    for (int j = 0; !marchCity && j < jsonarray2.length(); j++) {
                        // 遍历市id组
                        JSONObject obj3 = jsonarray2.getJSONObject(j);
 
                        JSONArray jsonarray3 = obj3.getJSONArray("id");
 
                        marchCity = false;
 
                        for (int k = 0; k < jsonarray3.length(); k++) {
                            // 遍历市id
                            int cid = jsonarray3.getInt(k);
                            Log.d(TAG, "cid = " + cid);
 
                            if (cid == 0) {
                                Log.d(TAG, "March GeneralCity");
                                dfuFileInfo.setValid(true);
                                if (obj3.has("appVersionCode") && obj3.has("appUpgradeUrl")) {
                                    dfuFileInfo.setApp(obj3.getInt("appVersionCode"), obj3.getString("appUpgradeUrl"));
                                }
                                if (obj3.has("systemVersionCode") && obj3.has("systemUpgradeUrl")) {
                                    dfuFileInfo.setSystem(obj3.getInt("systemVersionCode"), obj3.getString("systemUpgradeUrl"));
                                }
                            }
 
                            if (cid == city) {
                                marchCity = true;
                                Log.d(TAG, "March City");
                                dfuFileInfo.setValid(true);
                                if (obj3.has("appVersionCode") && obj3.has("appUpgradeUrl")) {
                                    dfuFileInfo.setApp(obj3.getInt("appVersionCode"), obj3.getString("appUpgradeUrl"));
                                }
                                if (obj3.has("systemVersionCode") && obj3.has("systemUpgradeUrl")) {
                                    dfuFileInfo.setSystem(obj3.getInt("systemVersionCode"), obj3.getString("systemUpgradeUrl"));
                                }
                                break;
                            }
                        }
                    }
                }
            }
            Log.d(TAG, "Parse End");
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage());
            dfuFileInfo.setValid(false);
        }
        return dfuFileInfo;
    }
 
    class CheckNewVersionThread implements Runnable {
        @Override
        public void run() {
            Log.d(TAG, "检查升级站点");
 
            String result = CheckNewVersion(newProgramIndUrl);
 
            if (result != null) {
                DfuFileInfo dfuFileInfo = parseJson(result, 53, 100);
                if (dfuFileInfo != null && dfuFileInfo.getValid()) {
                    if (dfuFileInfo.getAppVerCode() > 0 && dfuFileInfo.getAppUrl() != null) {
                        if (installUtil.getVerCode() < dfuFileInfo.getAppVerCode()) {
                            Log.d(TAG, "下载地址 " + dfuFileInfo.getAppUrl());
 
                            downloadManagerUtil.DownloadFile(dfuFileInfo.getAppUrl(), "aks3.apk", "aks3", "驾考app升级");
//                            downloadManagerUtil.DownloadFile("https://ftp-idc.pconline.com.cn/pub/download/201010/AutoCAD2007.rar?md5=KeZ0iEmKKGEWJ5c7OVxk_w&expires=1592964150", "aks3.apk", "aks3", "驾考app升级");
                        }
                    }
                    if (dfuFileInfo.getSystemVerCode() > 0 && dfuFileInfo.getSystemUrl() != null) {
 
                    }
                }
            }
 
            handlerCheckNewVersion.removeCallbacks(runnableCheckNewVersion);
            handlerCheckNewVersion.postDelayed(runnableCheckNewVersion, 30*60*1000);
        }
    }
 
    Handler handlerCheckNewVersion = new Handler();
    Runnable runnableCheckNewVersion = new Runnable() {
        @Override
        public void run() {
            new Thread(new CheckNewVersionThread()).start();
        }
    };
 
    public void CheckUpgrade() {
        handlerCheckNewVersion.removeCallbacks(runnableCheckNewVersion);
        handlerCheckNewVersion.postDelayed(runnableCheckNewVersion, 100);
    }
 
    public void ManualUpgrade(String path) {
        installUtil.InstallAppNormal(context, path);
    }
 
    class DMCB implements DownloadManagerCallback {
        @Override
        public void DownloadComplete(String title, String path) {
            if (title != null && path != null) {
                if (title.equals("aks3")) {
                    Signature[] sig = installUtil.getSignature();
                    Signature[] sig2 = installUtil.getSignature(path);
 
                    if (sig != null && sig2 != null && installUtil.getFingerprint(sig[0], "SHA-1").equals(installUtil.getFingerprint(sig2[0], "SHA-1"))) {
                        if (RemoteService.mAyDevice) {
                            installUtil.InstallApp(path);
                        } else {
                            installUtil.InstallAppNormal(context, path);
                        }
                    }
                }
            }
        }
    }
}