a
554325746@qq.com
2020-01-15 956063ff14bc75e3a2a97c7bcaa06b9edc84ad24
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
package com.basic.security.manager;
 
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
 
import com.alfeye.a1io.DeviceControl;
import com.basic.security.activity.MainActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Upgrade;
import com.basic.security.utils.Constants;
import com.basic.security.utils.ExceptionUtil;
import com.basic.security.utils.RUtils;
import com.basic.security.utils.ToastUtil;
import com.basic.security.widget.UpgradeDialog;
import com.google.gson.Gson;
 
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;
 
public class UpgradeManager extends BaseManager {
    static ModelAdapter upgrade;
 
    public static void initUpgrade() {
        upgrade = findById(Upgrade.tableName, Upgrade.tableName);
        if (upgrade == null) {
            upgrade = new ModelAdapter();
            upgrade.setString(Upgrade.id, Upgrade.tableName);
            upgrade.setString(Upgrade.device_id, DeviceManager.getDeviceId());
            upgrade.setString(Upgrade.company_id, CompanyManager.getCompanyId());
            upgrade.setString(Upgrade.del_flag, "0");
            upgrade.setString(Upgrade.upgrade_version, "");
            upgrade.setString(Upgrade.apk_path, "");
        }
        upgrade.setString(Upgrade.table, Upgrade.tableName);
        upgrade.setString(Upgrade.current_version, getCurrentVersion());
        save(upgrade);
    }
 
    public static String getCurrentVersion() {
        String url = Constants.getApkUrl;
        String currentVersion = "";
        try {
            if (url.contains("=")) {
                currentVersion = url.substring(url.lastIndexOf("=") + 1);
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        return currentVersion;
    }
 
    public static void saveUpgrade(ModelAdapter upgrade) {
        upgrade.setString(Constants.TABLE, Upgrade.tableName);
        UpgradeManager.upgrade = upgrade;
        save(upgrade);
    }
 
    public static ModelAdapter getUpgrade() {
        if (upgrade == null) {
            initUpgrade();
        }
        return upgrade;
    }
 
    public static void upgrade() {
        if (!hasUpgrade()) {
            return;
        }
        installNewApk();
    }
 
    public static void installNewApk() {
        ModelAdapter upgrade = getUpgrade();
        String apkPath = upgrade.getString(Upgrade.apk_path);
        File file = new File(apkPath);
        if (file.exists()) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = null;
                    try {
                        contentUri = FileProvider.getUriForFile(BaseApplication.getApplication().activity, RUtils.applicationID + ".fileProvider", file);
                    } catch (Exception e) {
                        ExceptionUtil.printException(e);
                    }
                    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                } else {
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "application/vnd.android.package-archive");
                }
                BaseApplication.getApplication().activity.startActivity(intent);
            } catch (Exception e) {
                ExceptionUtil.printException(e);
                ToastUtil.show("安装失败" + e.getMessage());
            }
        } else {
            ToastUtil.show("文件出错");
        }
    }
 
    public static void showUpgradeDialog() {
        if (hasUpgrade()) {
            final UpgradeDialog confirmDialog = new UpgradeDialog(BaseApplication.getApplication().activity, "升级", "安装", "取消");
            confirmDialog.show();
            confirmDialog.setClickListener(new UpgradeDialog.ClickListenerInterface() {
                public void doConfirm() {
                    installNewApk();
                    confirmDialog.dismiss();
                }
 
                public void doCancel() {
                    confirmDialog.dismiss();
                }
            });
        }
    }
 
    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"))) {
                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);
                if (downloadedFile.exists() && Integer.parseInt(newApkSize) == downloadedFile.length()) {
                    updateUpgrade(newApkName, downloadedFile);
                    return;
                }
                org.apache.commons.io.FileUtils.copyURLToFile(new URL(newApkNameUrl), downloadedFile);
                if (Integer.parseInt(newApkSize) == downloadedFile.length()) {
                    updateUpgrade(newApkName, downloadedFile);
                    MainActivity mainActivity = BaseApplication.getApplication().activity;
                    if (mainActivity.currentFragment == mainActivity.fragment_upgrade) {
                        mainActivity.fragment_upgrade.initDataToUI();
                    }
                    if (Constants.useAlf) {
                        DeviceControl.startInstallApk(
                                BaseApplication.getApplication(),
                                RUtils.applicationID,
                                downloadedFile.getAbsolutePath(),
                                true,
                                true
                        );
                    }
                }
            }
            System1.out.println("UpdateApk.getApk 安装完成");
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
    }
 
    private static void updateUpgrade(String newApkName, File downloadedFile) {
        ModelAdapter upgrade = getUpgrade();
        String newVersion = Integer.parseInt(newApkName.replace(Constants.apkName, "").replace(".apk", "")) + "";
        String new_apk_path = downloadedFile.getAbsolutePath();
        if (upgrade.getString(Upgrade.upgrade_version).equals(newVersion)
                && upgrade.getString(Upgrade.apk_path).equals(new_apk_path)
                ) {
            return;
        }
        upgrade.setString(Upgrade.upgrade_version, "" + newVersion);
        upgrade.setString(Upgrade.apk_path, new_apk_path);
        upgrade.setString(Upgrade.upgraded, Constants.FALSE);
        upgrade.setString(Upgrade.prompt_time, "");
        upgrade.setString(Upgrade.prompt_return, "");
        saveUpgrade(upgrade);
    }
 
    public static boolean hasUpgrade() {
        try {
            ModelAdapter upgrade = getUpgrade();
            if (upgrade == null) {
                return false;
            }
            if (TextUtils.isEmpty(upgrade.getString(Upgrade.upgrade_version))) {
                return false;
            }
            if (Integer.parseInt(upgrade.getString(Upgrade.current_version)) >=
                    Integer.parseInt(upgrade.getString(Upgrade.upgrade_version))) {
                return false;
            }
            if (TextUtils.isEmpty(upgrade.getString(Upgrade.apk_path))) {
                return false;
            }
            if (!new File(upgrade.getString(Upgrade.apk_path)).exists()) {
                return false;
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
            return false;
        }
        return true;
    }
 
}