|
|
@@ -0,0 +1,239 @@
|
|
|
+package com.tongyu.luck.happywork.ui.widget.dialog;
|
|
|
+
|
|
|
+import android.app.Dialog;
|
|
|
+import android.app.DownloadManager;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.PackageInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.database.ContentObserver;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Environment;
|
|
|
+import android.os.Handler;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
+import android.text.Html;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.webkit.MimeTypeMap;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.tongyu.luck.happywork.R;
|
|
|
+import com.tongyu.luck.happywork.baselibrary.utils.AppUtils;
|
|
|
+import com.tongyu.luck.happywork.baselibrary.utils.SharedPreferencesUtils;
|
|
|
+import com.tongyu.luck.happywork.baselibrary.utils.ToastUtils;
|
|
|
+import com.tongyu.luck.happywork.bean.VersionBean;
|
|
|
+import com.tongyu.luck.happywork.ui.base.BaseViewHolder;
|
|
|
+import com.tongyu.luck.happywork.utils.FileUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import butterknife.OnClick;
|
|
|
+
|
|
|
+public class UpdateAppDialog {
|
|
|
+ private Context mContext;
|
|
|
+ private Dialog mDialog;
|
|
|
+ private ViewHolder mViewHolder;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数(初始化ViewHolder,Dialog)
|
|
|
+ *
|
|
|
+ * @param context 上下文
|
|
|
+ */
|
|
|
+ public UpdateAppDialog(final Context context, VersionBean mBean) {
|
|
|
+ this.mContext = context;
|
|
|
+ mDialog = new Dialog(mContext, R.style.HappyJobDialog);
|
|
|
+ mViewHolder = new ViewHolder(mContext, mBean);
|
|
|
+ mDialog.setContentView(mViewHolder.getView());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 显示弹窗
|
|
|
+ */
|
|
|
+ public void show() {
|
|
|
+ if (mDialog != null && !mDialog.isShowing())
|
|
|
+ mDialog.show();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Dialog ViewHolder
|
|
|
+ */
|
|
|
+ class ViewHolder extends BaseViewHolder {
|
|
|
+
|
|
|
+
|
|
|
+ @BindView(R.id.tv_content)
|
|
|
+ TextView tvContent;
|
|
|
+ @BindView(R.id.btn_update)
|
|
|
+ Button btnUpdate;
|
|
|
+
|
|
|
+ private VersionBean mBean;
|
|
|
+ private String mDownloadFileName;
|
|
|
+ private DownloadManager mManager;
|
|
|
+ private DownloadChangeObserver downloadObserver;
|
|
|
+ private final String DOWNLOAD_DIR_TYPE = Environment.DIRECTORY_DOWNLOADS;
|
|
|
+ private final Uri CONTENT_URI = Uri.parse("content://downloads/my_downloads");
|
|
|
+
|
|
|
+ public ViewHolder(Context context, VersionBean mBean) {
|
|
|
+ super(context);
|
|
|
+ this.mBean = mBean;
|
|
|
+ initView();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int bindViewLayoutId() {
|
|
|
+ return R.layout.dialog_update_app;
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick(R.id.btn_update)
|
|
|
+ void onClick(View view) {
|
|
|
+ updateApk();
|
|
|
+ if (!mBean.isForceUpdate())
|
|
|
+ mDialog.dismiss();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void initView() {
|
|
|
+ if (mBean != null) {
|
|
|
+ mDialog.setCancelable(!mBean.isForceUpdate());
|
|
|
+ mDialog.setCanceledOnTouchOutside(!mBean.isForceUpdate());
|
|
|
+ if (!TextUtils.isEmpty(mBean.getDescription()))
|
|
|
+ tvContent.setText(Html.fromHtml(mBean.getDescription()));
|
|
|
+ else
|
|
|
+ tvContent.setText("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 安装
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public void doInstall(File file) {
|
|
|
+ if (Build.VERSION.SDK_INT >= 24) {
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(mContext, AppUtils.getPackageName(mContext) + ".update", file);
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ mContext.grantUriPermission(AppUtils.getPackageName(mContext), Uri.fromFile(file), Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
+ mContext.startActivity(intent);
|
|
|
+ } else {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ mContext.startActivity(intent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查文件是否下载
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isInstall
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean doCheckFile(File file, boolean isInstall) {
|
|
|
+ PackageManager pm = mContext.getPackageManager();
|
|
|
+ String packageName = mContext.getPackageName();
|
|
|
+ PackageInfo piNew = pm.getPackageArchiveInfo(file.getPath(), 0);
|
|
|
+ if (piNew != null) {
|
|
|
+ if (piNew.packageName.equals(packageName)
|
|
|
+ && piNew.versionName.equals(mBean.getVersion())) {
|
|
|
+ if (isInstall)
|
|
|
+ doInstall(file);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ file.delete();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新APK
|
|
|
+ */
|
|
|
+ private void updateApk() {
|
|
|
+ mDownloadFileName = mContext.getString(R.string.install_name) + ".apk";
|
|
|
+ String url = mBean.getDownUrl();
|
|
|
+ if (!TextUtils.isEmpty(mBean.getDownUrl())) {
|
|
|
+ String file = getDownloadFileName();
|
|
|
+ if (FileUtils.isFileExit(file)) {
|
|
|
+ if (doCheckFile(new File(file), true)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (TextUtils.isEmpty(url)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ mManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
+ downloadObserver = new DownloadChangeObserver(null);
|
|
|
+ long downloadId = SharedPreferencesUtils.getAppUpdateDownloadId(mContext);
|
|
|
+ if (downloadId != 0) {
|
|
|
+ if (mManager != null) {
|
|
|
+ mManager.remove(downloadId);
|
|
|
+ }
|
|
|
+ SharedPreferencesUtils.remove(mContext, SharedPreferencesUtils.APP_UPDATE_DOWNLOAD_ID);
|
|
|
+ }
|
|
|
+ Uri res = Uri.parse(url);
|
|
|
+ DownloadManager.Request request = new DownloadManager.Request(res);
|
|
|
+ request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
|
|
|
+ request.setAllowedOverRoaming(false);
|
|
|
+
|
|
|
+ // 设置文件类型
|
|
|
+ MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
|
|
+ String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap
|
|
|
+ .getFileExtensionFromUrl(url));
|
|
|
+ request.setMimeType(mimeString);
|
|
|
+ // 在通知栏中显示
|
|
|
+ request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
|
|
|
+ request.setVisibleInDownloadsUi(true);
|
|
|
+ request.allowScanningByMediaScanner();
|
|
|
+ // sdcard的目录下的download文件夹
|
|
|
+ request.setDestinationInExternalPublicDir(DOWNLOAD_DIR_TYPE,
|
|
|
+ mDownloadFileName);
|
|
|
+ String title = mContext.getString(R.string.app_name);
|
|
|
+ request.setTitle(title);
|
|
|
+ try {
|
|
|
+ long id = mManager.enqueue(request);
|
|
|
+ SharedPreferencesUtils.setAppUpdateDownloadId(mContext, id);
|
|
|
+ ToastUtils.show(mContext, R.string.download_start);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ ToastUtils.show(mContext, R.string.download_disabled_hint);
|
|
|
+ }
|
|
|
+ mContext.getContentResolver().registerContentObserver(CONTENT_URI, true, downloadObserver);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件名
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getDownloadFileName() {
|
|
|
+ File f = Environment
|
|
|
+ .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
|
|
+ if (!f.exists()) {
|
|
|
+ f.mkdirs();
|
|
|
+ }
|
|
|
+ return f.getAbsolutePath() + File.separator + mContext.getString(R.string.install_name) + ".apk";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载状态改变回调
|
|
|
+ */
|
|
|
+ class DownloadChangeObserver extends ContentObserver {
|
|
|
+
|
|
|
+ public DownloadChangeObserver(Handler handler) {
|
|
|
+ super(handler);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onChange(boolean selfChange) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|