|
|
@@ -13,18 +13,14 @@ import android.content.ServiceConnection;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.pm.PackageInstaller;
|
|
|
import android.net.Uri;
|
|
|
-import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
-import android.os.Environment;
|
|
|
import android.os.IBinder;
|
|
|
+import android.provider.Settings;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.TextView;
|
|
|
-import android.widget.Toast;
|
|
|
|
|
|
-import androidx.annotation.NonNull;
|
|
|
-import androidx.annotation.Nullable;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
import androidx.core.content.FileProvider;
|
|
|
|
|
|
@@ -32,31 +28,23 @@ import com.blankj.utilcode.constant.PermissionConstants;
|
|
|
import com.blankj.utilcode.util.AppUtils;
|
|
|
import com.blankj.utilcode.util.PathUtils;
|
|
|
import com.blankj.utilcode.util.PermissionUtils;
|
|
|
+import com.blankj.utilcode.util.ToastUtils;
|
|
|
import com.blankj.utilcode.util.UtilsTransActivity;
|
|
|
import com.ht.gate.domain.QueryUpdate;
|
|
|
import com.ht.gate.domain.Room;
|
|
|
import com.ht.gate.domain.UpdateInfo;
|
|
|
import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
|
+import com.liulishuo.filedownloader.DownloadTask;
|
|
|
import com.liulishuo.filedownloader.FileDownloadListener;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadSampleListener;
|
|
|
import com.liulishuo.filedownloader.FileDownloader;
|
|
|
-import com.liulishuo.okdownload.DownloadListener;
|
|
|
-import com.liulishuo.okdownload.DownloadTask;
|
|
|
-import com.liulishuo.okdownload.StatusUtil;
|
|
|
-import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
|
|
|
-import com.liulishuo.okdownload.core.cause.EndCause;
|
|
|
-import com.liulishuo.okdownload.core.cause.ResumeFailedCause;
|
|
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
-import org.xutils.x;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
-import java.nio.file.Paths;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
@@ -86,6 +74,8 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
private SharedPreferences sharedPreferences;
|
|
|
private DoorService.DoorBinder mDoorBinder;
|
|
|
|
|
|
+ private ProgressDialog progressDialog;
|
|
|
+
|
|
|
private ServiceConnection connection = new ServiceConnection() {
|
|
|
@Override
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
@@ -107,6 +97,13 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
ButterKnife.bind(this);
|
|
|
sharedPreferences = getSharedPreferences(PREF_NAME_SETTINGS, MODE_PRIVATE);
|
|
|
bindService(new Intent(this, DoorService.class), connection, BIND_AUTO_CREATE);
|
|
|
+
|
|
|
+ progressDialog = new ProgressDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
|
|
|
+ progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
+ progressDialog.setCanceledOnTouchOutside(false);
|
|
|
+ progressDialog.setCancelable(false);
|
|
|
+ progressDialog.setMessage("下载中");
|
|
|
+ progressDialog.setProgress(0);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -184,140 +181,168 @@ public class SettingsActivity extends AppCompatActivity {
|
|
|
|
|
|
@OnClick(R.id.btn_update)
|
|
|
void checkUpdate() {
|
|
|
-// try {
|
|
|
-//
|
|
|
-// Room room = Room.fromBase64(sharedPreferences.getString(Constants.PREF_ROOM_INFO, null));
|
|
|
-// RetrofitManager.getInstance().getRetrofit().create(ApiService.class)
|
|
|
-// .checkUpdate(new QueryUpdate(room.getSiteCode(), "02", AppUtils.getAppVersionCode() + ""))
|
|
|
-// .enqueue(new Callback<UpdateInfo>() {
|
|
|
-// @Override
|
|
|
-// public void onResponse(Call<UpdateInfo> call, Response<UpdateInfo> response) {
|
|
|
-// if (response.code() == 200) {
|
|
|
-// if (response.body() == null) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// if (response.body().getResultData().isNeedUpdate()) {
|
|
|
-// PermissionUtils.permission(PermissionConstants.STORAGE).rationale(new PermissionUtils.OnRationaleListener() {
|
|
|
-// @Override
|
|
|
-// public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
|
|
|
-// }
|
|
|
-// }).callback(new PermissionUtils.SimpleCallback() {
|
|
|
-// @Override
|
|
|
-// public void onGranted() {
|
|
|
-// startDownload(RetrofitManager.BASE_URL.replaceAll("/$", "") + response.body().getResultData().getSoftwareUrl());
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void onDenied() {
|
|
|
-//
|
|
|
-// }
|
|
|
-// }).request();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
+ try {
|
|
|
+ Room room = Room.fromBase64(sharedPreferences.getString(Constants.PREF_ROOM_INFO, null));
|
|
|
+ RetrofitManager.getInstance().getRetrofit().create(ApiService.class)
|
|
|
+ .checkUpdate(new QueryUpdate(room.getSiteCode(), "02", AppUtils.getAppVersionCode() + ""))
|
|
|
+ .enqueue(new Callback<UpdateInfo>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(Call<UpdateInfo> call, Response<UpdateInfo> response) {
|
|
|
+ if (response.code() == 200) {
|
|
|
+ if (response.body() == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (response.body().getResultData().isNeedUpdate()) {
|
|
|
+ new AlertDialog.Builder(SettingsActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
|
|
|
+ .setTitle("新版本")
|
|
|
+ .setMessage("检测到新版本" + response.body().getResultData().getNewestVersion() + ",是否立即下载更新?")
|
|
|
+ .setNegativeButton("取消", (dialog0, which0) -> {
|
|
|
+ dialog0.dismiss();
|
|
|
+ })
|
|
|
+ .setPositiveButton("下载", (dialog0, which0) -> {
|
|
|
+ dialog0.dismiss();
|
|
|
+ PermissionUtils.permission(PermissionConstants.STORAGE)
|
|
|
+ .rationale((activity, shouldRequest) -> new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
|
|
|
+ .setTitle("无写入权限")
|
|
|
+ .setMessage("请前往设置打开存储权限")
|
|
|
+ .setNegativeButton("取消", (dialog, which) -> {
|
|
|
+ dialog.dismiss();
|
|
|
+ activity.finish();
|
|
|
+ })
|
|
|
+ .setPositiveButton("打开设置", (dialog, which) -> {
|
|
|
+ dialog.dismiss();
|
|
|
+ Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
+ Uri uri = Uri.fromParts("package", getPackageName(), null);
|
|
|
+ intent.setData(uri);
|
|
|
+ intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
|
|
+ activity.startActivity(intent);
|
|
|
+ activity.finish();
|
|
|
+ }).show())
|
|
|
+ .callback(new PermissionUtils.SimpleCallback() {
|
|
|
+ @Override
|
|
|
+ public void onGranted() {
|
|
|
+ String url;
|
|
|
+ if (response.body().getResultData().getSoftwareUrl().startsWith("http")) {
|
|
|
+ url = response.body().getResultData().getSoftwareUrl();
|
|
|
+ } else {
|
|
|
+ url = RetrofitManager.BASE_URL.replaceAll("/$", "") + response.body().getResultData().getSoftwareUrl();
|
|
|
+ }
|
|
|
+ startDownload(url);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDenied() {
|
|
|
+
|
|
|
+ }
|
|
|
+ }).request();
|
|
|
+ }).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Call<UpdateInfo> call, Throwable t) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// PermissionUtils.permission(PermissionConstants.STORAGE)
|
|
|
+// .rationale((activity, shouldRequest) -> new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
|
|
|
+// .setTitle("无写入权限")
|
|
|
+// .setMessage("请前往设置打开存储权限")
|
|
|
+// .setNegativeButton("取消", (dialog, which) -> {
|
|
|
+// dialog.dismiss();
|
|
|
+// activity.finish();
|
|
|
+// })
|
|
|
+// .setPositiveButton("打开设置", (dialog, which) -> {
|
|
|
+// dialog.dismiss();
|
|
|
+// Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
+// Uri uri = Uri.fromParts("package", getPackageName(), null);
|
|
|
+// intent.setData(uri);
|
|
|
+// intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
+// intent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
|
+// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+// intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
|
+// intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
|
|
+// activity.startActivity(intent);
|
|
|
+// activity.finish();
|
|
|
+// }).show())
|
|
|
+// .callback(new PermissionUtils.SimpleCallback() {
|
|
|
+// @Override
|
|
|
+// public void onGranted() {
|
|
|
+// startDownload("https://beautiful-nanjing.oss-cn-hangzhou.aliyuncs.com/Coolapk-v10.2-2005181-coolapk-app-release.apk");
|
|
|
+// }
|
|
|
//
|
|
|
-// @Override
|
|
|
-// public void onFailure(Call<UpdateInfo> call, Throwable t) {
|
|
|
+// @Override
|
|
|
+// public void onDenied() {
|
|
|
//
|
|
|
-// }
|
|
|
-// });
|
|
|
-// } catch (Exception e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// startDownload("http://dl-cdn.coolapkmarket.com/down/apk_file/2020/0518/Coolapk-v10.2-2005181-coolapk-app-release.apk");
|
|
|
- installApk(new File("file:///sdcard/Android/data/com.ht.gate/files/Download/1589872246972.apk"));
|
|
|
+// }
|
|
|
+// }).request();
|
|
|
}
|
|
|
|
|
|
private void startDownload(String url) {
|
|
|
FileDownloader.setup(SettingsActivity.this);
|
|
|
- ProgressDialog progressDialog = new ProgressDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
|
|
|
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
- progressDialog.setCanceledOnTouchOutside(false);
|
|
|
- progressDialog.setCancelable(false);
|
|
|
- progressDialog.setMessage("下载中");
|
|
|
- progressDialog.setProgress(0);
|
|
|
String ext = FilenameUtils.getExtension(url);
|
|
|
String fileName = System.currentTimeMillis() + "." + ext;
|
|
|
- progressDialog.show();
|
|
|
- new DownloadTask.Builder(url, new File(PathUtils.getExternalAppDownloadPath()))
|
|
|
- .setFilename(fileName)
|
|
|
- // the minimal interval millisecond for callback progress
|
|
|
- .setMinIntervalMillisCallbackProcess(16)
|
|
|
- // ignore the same task has already completed in the past.
|
|
|
- .setPassIfAlreadyCompleted(false)
|
|
|
- .build()
|
|
|
- .enqueue(new DownloadListener() {
|
|
|
- @Override
|
|
|
- public void taskStart(@NonNull DownloadTask task) {
|
|
|
+ String path = PathUtils.getExternalAppDownloadPath() + "/" + fileName;
|
|
|
+ BaseDownloadTask downloadTask = FileDownloader.getImpl().create(url).setPath(path).setListener(new FileDownloadListener() {
|
|
|
+ @Override
|
|
|
+ protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void connectTrialStart(@NonNull DownloadTask task, @NonNull Map<String, List<String>> requestHeaderFields) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void connectTrialEnd(@NonNull DownloadTask task, int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void downloadFromBeginning(@NonNull DownloadTask task, @NonNull BreakpointInfo info, @NonNull ResumeFailedCause cause) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void downloadFromBreakpoint(@NonNull DownloadTask task, @NonNull BreakpointInfo info) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void connectStart(@NonNull DownloadTask task, int blockIndex, @NonNull Map<String, List<String>> requestHeaderFields) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void connectEnd(@NonNull DownloadTask task, int blockIndex, int responseCode, @NonNull Map<String, List<String>> responseHeaderFields) {
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void fetchStart(@NonNull DownloadTask task, int blockIndex, long contentLength) {
|
|
|
+ @Override
|
|
|
+ protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
+ progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
+ progressDialog.setProgress((int) (soFarBytes * 100L / totalBytes));
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void completed(BaseDownloadTask task) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ ToastUtils.showShort("下载完成");
|
|
|
+ installApk(new File(task.getPath()));
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void fetchProgress(@NonNull DownloadTask task, int blockIndex, long increaseBytes) {
|
|
|
- long total = StatusUtil.getCurrentInfo(task).getTotalLength();
|
|
|
- long current = StatusUtil.getCurrentInfo(task).getTotalOffset();
|
|
|
- if (total != 0)
|
|
|
- progressDialog.setProgress((int) (current * 100 / total));
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void fetchEnd(@NonNull DownloadTask task, int blockIndex, long contentLength) {
|
|
|
- progressDialog.dismiss();
|
|
|
- Log.d("xxxx", "fetchEnd");
|
|
|
+ @Override
|
|
|
+ protected void error(BaseDownloadTask task, Throwable e) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ ToastUtils.showShort(e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
- installApk(new File(PathUtils.getExternalAppDownloadPath() + fileName));
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void warn(BaseDownloadTask task) {
|
|
|
|
|
|
- @Override
|
|
|
- public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause) {
|
|
|
- progressDialog.dismiss();
|
|
|
- Log.d("xxxx", "taskEnd");
|
|
|
- }
|
|
|
- });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消下载", (dialog, which) -> {
|
|
|
+ downloadTask.pause();
|
|
|
+ progressDialog.dismiss();
|
|
|
+ });
|
|
|
+ progressDialog.setProgress(0);
|
|
|
+ progressDialog.show();
|
|
|
+ downloadTask.start();
|
|
|
}
|
|
|
|
|
|
private void installApk(File apkFile) {
|
|
|
- Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
|
- intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
- intent.setDataAndType(FileProvider.getUriForFile(SettingsActivity.this,
|
|
|
- "com.ht.gate.fileProvider", apkFile), "application/vnd.android.package-archive");
|
|
|
- startActivity(intent);
|
|
|
-// AppUtils.installApp(apkFile);
|
|
|
+// Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
|
+// intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+// intent.setDataAndType(FileProvider.getUriForFile(SettingsActivity.this,
|
|
|
+// "com.ht.gate.fileProvider", apkFile), "application/vnd.android.package-archive");
|
|
|
+// startActivity(intent);
|
|
|
+ AppUtils.installApp(apkFile);
|
|
|
// try {
|
|
|
// installPackage(apkFile.getPath());
|
|
|
// } catch (IOException e) {
|