admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
package com.yeshi.appupdate;
 
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
 
import androidx.core.content.ContextCompat;
 
import android.telephony.TelephonyManager;
import android.text.Html;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.util.common.DeviceUtil;
import com.loopj.android.http.TextHttpResponseHandler;
import com.yeshi.appupdate.api.AppUpdateAPI;
import com.yeshi.appupdate.entity.UpdateBean;
import com.yeshi.appupdate.service.DownLoadFileService;
import com.yeshi.appupdate.util.StringUtils;
import com.yeshi.appupdate.view.CustomDialog;
 
public class AppUpdate {
 
    private static String mKey;
 
    private static Activity mActivity;
 
    private static AppUpdate appUpdate;
 
    private boolean isWifi = false;
 
    private static boolean isCheck = false;
 
    public AppUpdate() {
        ConnectivityManager connectMgr = (ConnectivityManager) mActivity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info != null) {
            isWifi = info.getType() == ConnectivityManager.TYPE_WIFI ? true
                    : false;
        }
        getUpdateInfo();
    }
 
    /**
     * 获取更新信息
     */
    private void getUpdateInfo() {
        TelephonyManager manager = (TelephonyManager) mActivity
                .getSystemService(Activity.TELEPHONY_SERVICE);
        String deviceId = "";
        if (ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            try {
                deviceId = DeviceUtil.getImeiCache(mActivity);
            } catch (Exception e) {
 
            }
        }
        int versionCode = 1;// 版本号
        try {
            versionCode = mActivity.getPackageManager().getPackageInfo(
                    mActivity.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS).versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
 
        AppUpdateAPI.getAppUpdateInfo(mActivity, deviceId, "Android", mKey,
                versionCode + "", new TextHttpResponseHandler() {
 
                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                                          String responseString) {
                        if (StringUtils.isEmpty(responseString)) {
                            return;
                        }
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(responseString);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (jsonObject == null)
                            return;
                        if (Integer.parseInt(jsonObject.optString("code")) == 0) {
                            Gson gson = new GsonBuilder().setPrettyPrinting()
                                    .create();
                            UpdateBean info = null;
                            try {
                                info = gson.fromJson(
                                        jsonObject.getJSONObject("data")
                                                .toString(),
                                        new TypeToken<UpdateBean>() {
                                        }.getType());
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            if (info.isSilent() && isWifi) {
                                // 自动开始下载
                                try {
                                    Intent intent = new Intent(mActivity,
                                            DownLoadFileService.class);
                                    mActivity.stopService(intent);
                                } catch (Exception e) {
                                }
                                try {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("downloadurl",
                                            info.getLink());
                                    Intent intent = new Intent(mActivity,
                                            DownLoadFileService.class);
                                    intent.putExtras(bundle);
                                    mActivity.startService(intent);
                                } catch (Exception e) {
                                }
                            } else {
                                if (!mActivity.isFinishing())
                                    updateDialog(info);// 弹出更新提示框
                            }
                        } else if (jsonObject.optInt("code") == 1) {
                            if (isCheck)
                                Toast.makeText(mActivity, "当前版本为最新版本", Toast.LENGTH_LONG).show();
                        } else {
                            if (isCheck)
                                Toast.makeText(mActivity, jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                                          String responseString, Throwable throwable) {
                    }
                });
    }
 
    private void updateDialog(final UpdateBean info) {
        CustomDialog.Builder builder = new CustomDialog.Builder(mActivity);
        builder.setTitle("软件更新提示");
        builder.setVersion(info.getVersion());
        builder.setSize(info.getSize());
        builder.setMessage(Html.fromHtml(info.getIntroduction()) + "");
        builder.setNoti_WIFI(isWifi);
        builder.setPositiveButton("立即升级", new OnClickListener() {
 
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // 下载
                try {
                    Intent intent = new Intent(mActivity,
                            DownLoadFileService.class);
                    mActivity.stopService(intent);
                } catch (Exception e) {
                }
                try {
                    Bundle bundle = new Bundle();
                    bundle.putString("downloadurl", info.getLink());
                    Intent intent = new Intent(mActivity
                            .getApplicationContext(), DownLoadFileService.class);
                    intent.putExtras(bundle);
                    mActivity.startService(intent);
                } catch (Exception e) {
                }
                arg0.dismiss();
            }
        });
        builder.setNegativeButton("暂不升级", new OnClickListener() {
 
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                if (info.isForce()) {
                    mActivity.finish();
                }
                arg0.dismiss();
            }
        });
        builder.create().show();
    }
 
    /**
     * 获取key
     *
     * @param key
     */
    public static void setAppUpdateKey(String key) {
        if (key != null && key.length() > 0) {
            mKey = key;
        }
    }
 
    /**
     * 获取activity
     *
     * @param activity
     */
    public static void setAppUpdateActivity(Activity activity, boolean check) {
        if (activity != null) {
            mActivity = activity;
            isCheck = check;
        }
    }
 
    /**
     * 初始化
     */
    public static void initAppUpdate() {
        if (appUpdate == null) {
            appUpdate = new AppUpdate();
        }
    }
 
    /**
     * 销毁appUpdate
     */
    public static void destoryAppUpdate() {
        if (appUpdate != null) {
            appUpdate = null;
        }
    }
}