|
|
@@ -1,16 +1,21 @@
|
|
|
package com.uas.hystorage.fragment;
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
+import android.os.Environment;
|
|
|
import android.os.Handler;
|
|
|
import android.os.Message;
|
|
|
+import android.support.annotation.RequiresApi;
|
|
|
import android.support.v4.content.FileProvider;
|
|
|
import android.text.TextUtils;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
@@ -22,7 +27,6 @@ import com.uas.hystorage.util.AndroidUtil;
|
|
|
import com.uas.hystorage.util.CommonUtil;
|
|
|
import com.uas.hystorage.util.Constants;
|
|
|
import com.uas.hystorage.util.FastjsonUtil;
|
|
|
-import com.uas.hystorage.util.FileUtils;
|
|
|
import com.uas.hystorage.util.HttpCallback;
|
|
|
import com.uas.hystorage.util.HttpParams;
|
|
|
import com.uas.hystorage.util.VollyRequest;
|
|
|
@@ -31,10 +35,6 @@ import com.uas.hystorage.util.down.ProgressDownloader;
|
|
|
import com.uas.hystorage.util.down.ProgressResponseBody;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
-import static com.uas.hystorage.util.Constants.CONSTANT.APK_FILE_NAME;
|
|
|
-import static com.uas.hystorage.util.Constants.CONSTANT.APK_FILE_PATH;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -50,6 +50,10 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
private File file;
|
|
|
private String mAttachId;
|
|
|
private StringRequest mStringRequest;
|
|
|
+
|
|
|
+ private File apkFile;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
protected int getLayout() {
|
|
|
return R.layout.fragment_version_upgrade;
|
|
|
@@ -76,20 +80,24 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
mProgressPop.showPopupWindow();
|
|
|
|
|
|
breakPoints = 0L;
|
|
|
- File directory = new File(APK_FILE_PATH);
|
|
|
- if (!directory.exists() && !directory.isDirectory()) {
|
|
|
- boolean mkdirs = directory.mkdirs();
|
|
|
- } else {
|
|
|
- FileUtils.delAllFile(APK_FILE_PATH);
|
|
|
- }
|
|
|
- file = new File(APK_FILE_PATH, APK_FILE_NAME);
|
|
|
- try {
|
|
|
- file.createNewFile();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+// File directory = new File(APK_FILE_PATH);
|
|
|
+// if (!directory.exists() && !directory.isDirectory()) {
|
|
|
+// boolean mkdirs = directory.mkdirs();
|
|
|
+// } else {
|
|
|
+// FileUtils.delAllFile(APK_FILE_PATH);
|
|
|
+// }
|
|
|
+// file = new File(APK_FILE_PATH, APK_FILE_NAME);
|
|
|
+// try {
|
|
|
+// file.createNewFile();
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// String downloadUrl = GloableParams.ADDRESS_COMMON_DOWNLOADBYID + "?id=" + mAttachId;
|
|
|
+// mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
|
|
|
+// mDownloader.download(0L);
|
|
|
+
|
|
|
String downloadUrl = GloableParams.ADDRESS_COMMON_DOWNLOADBYID + "?id=" + mAttachId;
|
|
|
- mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
|
|
|
+ mDownloader = new ProgressDownloader(downloadUrl, apkFile, VersionUpgradeFragment.this);
|
|
|
mDownloader.download(0L);
|
|
|
}
|
|
|
});
|
|
|
@@ -100,8 +108,64 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
mOldVersionTv.setText(AndroidUtil.getVersionName(mActivity));
|
|
|
|
|
|
getNewVersionMsg();
|
|
|
+
|
|
|
+ //不同版本分区处理
|
|
|
+ File downloadDir;
|
|
|
+ if (Build.VERSION.SDK_INT >= 29) {
|
|
|
+ // Android 10+ 使用私有目录
|
|
|
+ downloadDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
|
|
|
+ } else {
|
|
|
+ downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
|
|
+ }
|
|
|
+ if (!downloadDir.exists()) {
|
|
|
+ downloadDir.mkdirs();
|
|
|
+ }
|
|
|
+ String fileName = "update_" + System.currentTimeMillis() + ".apk";
|
|
|
+ apkFile = new File(downloadDir, fileName);
|
|
|
+
|
|
|
+ //从API 26开始需要申请安装权限
|
|
|
+ if (Build.VERSION.SDK_INT >= 26) {
|
|
|
+ if (!isHasInstallPermissionWithO(getActivity())) {
|
|
|
+ startInstallPermissionSettingActivity(getActivity());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检测是否拥有安装未知应用权限canRequestPackageInstalls
|
|
|
+ */
|
|
|
+ @RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
+ private boolean isHasInstallPermissionWithO(Context context){
|
|
|
+ if (context == null){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return context.getPackageManager().canRequestPackageInstalls();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 如果没有权限,则申请安装未知应用权限(打开获取权限界面)
|
|
|
+ */
|
|
|
+ @RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
+ private void startInstallPermissionSettingActivity(Context context) {
|
|
|
+ if (context == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Intent intent = new Intent();
|
|
|
+ //获取当前apk包URI,并设置到intent中(这一步设置,可让“未知应用权限设置界面”只显示当前应用的设置项)
|
|
|
+ Uri packageURI = Uri.parse("package:" + context.getPackageName());
|
|
|
+ intent.setData(packageURI);
|
|
|
+ //设置不同版本跳转未知应用的动作
|
|
|
+ if (Build.VERSION.SDK_INT >= 26) {
|
|
|
+ //intent = new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI);
|
|
|
+ intent.setAction(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
|
|
|
+ } else {
|
|
|
+ intent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS);
|
|
|
+ }
|
|
|
+ ((Activity) context).startActivity(intent);
|
|
|
+ Toast.makeText(getActivity(), "请打开未知应用安装权限", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private void getNewVersionMsg() {
|
|
|
progressDialog.show();
|
|
|
|
|
|
@@ -135,15 +199,9 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
public void onFail(int flag, String failStr) throws Exception {
|
|
|
progressDialog.dismiss();
|
|
|
CommonUtil.toastNoRepeat(mActivity, failStr);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
});
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -171,7 +229,6 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private Handler mHandler = new Handler() {
|
|
|
@Override
|
|
|
public void handleMessage(Message msg) {
|
|
|
@@ -189,20 +246,17 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
|
|
|
}
|
|
|
break;
|
|
|
case Constants.CONSTANT.DOWNLOAD_SUCCESS:
|
|
|
- CommonUtil.toastNoRepeat(mActivity, getResources().getString(R.string.download_completes));
|
|
|
+ CommonUtil.toastNoRepeat(mActivity, "下载完成");
|
|
|
mProgressPop.dismiss();
|
|
|
try {
|
|
|
- File apk = new File(APK_FILE_PATH, APK_FILE_NAME);
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
|
- Uri apkUri = FileProvider.getUriForFile(mActivity,
|
|
|
- "com.uas.hystorage.fileprovider", apk);
|
|
|
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(mActivity, getActivity().getPackageName() +".fileprovider", apkFile);
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
} else {
|
|
|
- intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
|
|
|
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
|
|
|
}
|
|
|
mActivity.startActivity(intent);
|
|
|
} catch (Exception e) {
|