package com.ysh.wpc.appupdate;
|
|
import android.Manifest;
|
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.loopj.android.http.TextHttpResponseHandler;
|
import com.ysh.wpc.appupdate.api.AppUpdateAPI;
|
import com.ysh.wpc.appupdate.entity.UpdateBean;
|
import com.ysh.wpc.appupdate.service.DownLoadFileService;
|
import com.ysh.wpc.appupdate.util.PermissionsActivity;
|
import com.ysh.wpc.appupdate.util.PermissionsChecker;
|
import com.ysh.wpc.appupdate.view.CustomDialog;
|
|
import org.apache.http.Header;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
public class AppUpdate {
|
|
// static final String[] PERMISSIONS = new String[]{
|
// Manifest.permission.READ_PHONE_STATE
|
// };
|
private static final int REQUEST_CODE = 0; // 请求码
|
private PermissionsChecker mPermissionsChecker; // 权限检测器
|
private static String mKey;
|
|
private static Activity mActivity;
|
|
private static AppUpdate appUpdate;
|
|
private boolean isWifi = false;
|
// private static final int REQUEST_EXTERNAL_STORAGE = 1;
|
// 所需的全部权限
|
private static String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
|
// public static void verifyStoragePermissions(Activity activity) {
|
// // Check if we have write permission
|
// int permission = ActivityCompat.checkSelfPermission(activity,
|
// Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
//
|
// if (permission != PackageManager.PERMISSION_GRANTED) {
|
// // We don't have permission so prompt the user
|
// ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
|
// REQUEST_EXTERNAL_STORAGE);
|
// }
|
// }
|
|
public AppUpdate(boolean isCheck) {
|
this.isCheck = isCheck;
|
ConnectivityManager connectMgr = (ConnectivityManager) mActivity
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo info = connectMgr.getActiveNetworkInfo();
|
if (info != null) {
|
isWifi = info.getType() == ConnectivityManager.TYPE_WIFI;
|
}
|
mPermissionsChecker = new PermissionsChecker(mActivity);
|
getUpdateInfo();
|
}
|
|
/**
|
* 获取更新信息
|
*/
|
private void getUpdateInfo() {
|
final 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) {
|
deviceId = manager.getDeviceId();// 获取deviceId
|
}// 获取deviceId
|
long cTime = System.currentTimeMillis();// 获取系统当前时间
|
int versionCode = 1;// 版本号
|
try {
|
versionCode = mActivity.getPackageManager().getPackageInfo(
|
mActivity.getPackageName(),
|
PackageManager.GET_CONFIGURATIONS).versionCode;
|
} catch (NameNotFoundException e) {
|
e.printStackTrace();
|
}
|
|
AppUpdateAPI.getAppUpdateInfo(mActivity, deviceId, "Android", cTime
|
+ "", mKey, versionCode + "", new TextHttpResponseHandler() {
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
String responseString) {
|
JSONObject jsonObject = null;
|
try {
|
jsonObject = new JSONObject(responseString);
|
} catch (JSONException e) {
|
e.printStackTrace();
|
}
|
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 {
|
updateDialog(info);// 弹出更新提示框
|
}
|
} else {
|
if (isCheck)
|
Toast.makeText(mActivity, "已经是最新版本", Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
if (isCheck)
|
Toast.makeText(mActivity, "已经是最新版本", Toast.LENGTH_LONG).show();
|
}
|
});
|
}
|
|
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) {
|
|
// if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
|
// startPermissionsActivity();
|
// } else {
|
// 下载
|
try {
|
Intent intent = new Intent(mActivity,
|
DownLoadFileService.class);
|
mActivity.stopService(intent);
|
} catch (Exception e) {
|
}
|
// verifyStoragePermissions(mActivity);
|
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();
|
}
|
|
private void startPermissionsActivity() {
|
PermissionsActivity.startActivityForResult(mActivity, REQUEST_CODE, PERMISSIONS);
|
}
|
|
/**
|
* 获取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) {
|
if (activity != null) {
|
mActivity = activity;
|
}
|
}
|
|
private boolean isCheck = false;
|
|
/**
|
* 初始化
|
*/
|
public static void initAppUpdate(boolean isCheck) {
|
if (appUpdate == null) {
|
appUpdate = new AppUpdate(isCheck);
|
}
|
}
|
|
/**
|
* 销毁appUpdate
|
*/
|
public static void destoryAppUpdate() {
|
if (appUpdate != null) {
|
appUpdate = null;
|
}
|
}
|
}
|