zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
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
package com.basic.security.utils;
 
import android.os.Environment;
 
import com.alfeye.a1io.DeviceControl;
import com.basic.security.base.BaseApplication;
import com.google.gson.Gson;
 
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class UpdateApk {
 
    static ExecutorService singleThreadExecutorService = Executors.newSingleThreadExecutor();
 
//    public static boolean urlExists(String URLName) {
//        try {
//            HttpURLConnection.setFollowRedirects(false);
//            HttpURLConnection con =
//                    (HttpURLConnection) new URL(URLName).openConnection();
//            con.setRequestMethod("HEAD");
//            return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
 
//    public static boolean downloadApk(String urlStr, String apkName) {
//        try {
//            URL url = new URL(urlStr);
//            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//            urlConnection.setRequestMethod("GET");
//            urlConnection.setDoOutput(true);
//            urlConnection.connect();
//
//            File file = new File(apkName);
//
//            FileOutputStream fileOutput = new FileOutputStream(file);
//            InputStream inputStream = urlConnection.getInputStream();
//
//            byte[] buffer = new byte[1024];
//            int bufferLength = 0;
//
//            while ((bufferLength = inputStream.read(buffer)) > 0) {
//                fileOutput.write(buffer, 0, bufferLength);
//            }
//            fileOutput.close();
//            return true;
//        } catch (Exception e) {
//            System.out.println("UpdateApk.downloadapk");
//            return false;
//        }
//    }
 
//    public static void checkUpgrade() {
//        try {
//            String dirName = Environment.getExternalStorageDirectory() + "/download/";
//            File dir = new File(dirName);
//            dir.mkdirs();
//            File apkFile = new File(dir, Constants.updateApkFileName);
//
//            String apkUrl = Constants.updateApkUrl + Constants.updateApkFileName;
//            if (urlExists(apkUrl)) {
//                if (downloadApk(apkUrl, apkFile.getAbsolutePath())) {
//                    installApk(apkFile.getAbsolutePath());
//                }
//            }
//        } catch (Exception e1) {
//            System.out.println("UpdateApk.run ");
//        }
//    }
 
//    public static void checkUpdateInLoop() {
//        singleThreadExecutorService.execute(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    try {
//                        try {
//                            String dirName = Environment.getExternalStorageDirectory() + "/download/";
//                            File dir = new File(dirName);
//                            dir.mkdirs();
//                            File apkFile = new File(dir, Constants.updateApkFileName);
//
//                            String apkUrl = Constants.updateApkUrl + Constants.updateApkFileName;
//                            if (urlExists(apkUrl)) {
//                                if (downloadApk(apkUrl, apkFile.getAbsolutePath())) {
//                                    installApk(apkFile.getAbsolutePath());
//                                }
//                            }
//                        } catch (Exception e1) {
//                            System.out.println("UpdateApk.run ");
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    SystemClock.sleep(10 * 1000);
//                }
//            }
//        });
//
//    }
 
//    public static void installApk(String apkPath) {
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        System.out.println("UpdateApk.installApk " + apkPath);
//        File file = new File(apkPath);
//        Uri apkURI = FileProvider.getUriForFile(BaseApplication.getApplication(),BuildConfig.APPLICATION_ID + ".fileProvider", file);
//        intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
//        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//        BaseApplication.getApplication().activity.startActivity(intent);
//    }
 
    public static void getApk() {
        try {
            String out = new Scanner(new URL(Constants.getApkUrl).openStream(), "UTF-8").useDelimiter("\\A").next();
            Gson gson = new Gson();
            Map map = gson.fromJson(out, Map.class);
            if ("true".equals(map.get("hasNewVersion"))) {
                System.out.println("UpdateApk.getApk 开始下载");
                String newApkName = (String) map.get("newApkName");
                String newApkSize = (String) map.get("newApkSize");
                String newApkNameUrl = Constants.getApkUrl.substring(0, Constants.getApkUrl.lastIndexOf("/")) + "/" + newApkName;
                String dirName = Environment.getExternalStorageDirectory() + "/download/";
                new File(dirName).mkdirs();
                File downloadedFile = new File(dirName, newApkName);
                org.apache.commons.io.FileUtils.copyURLToFile(new URL(newApkNameUrl), downloadedFile);
                if (Integer.parseInt(newApkSize) == downloadedFile.length()) {
                    if (Constants.useAlf) {
                        DeviceControl.startInstallApk(
                                BaseApplication.getApplication(),
                                "com.basic.security",
                                downloadedFile.getAbsolutePath(),
                                true,
                                true
                        );
                    }
                }
                System.out.println("UpdateApk.getApk 安装完成");
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
}