From 54bfbc1326a0d3cfe7b165f7da76d91842a5a7fd Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 24 Sep 2022 20:48:40 +0800 Subject: [PATCH] ***2022/09/24*** * self update files clear --- app/src/main/assets/updateLog.md | 1 + .../jack/bookshelf/constant/AppConstant.java | 11 ----------- .../bookshelf/help/update/UpdateManager.java | 18 ++++++++++++++++++ .../java/com/jack/bookshelf/utils/FileUtils.kt | 1 - .../bookshelf/view/activity/MainActivity.java | 6 +++++- .../view/activity/WelcomeActivity.java | 7 ++----- .../view/fragment/GeneralSettingFragment.java | 3 +++ 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index bfeff4e..f5908ef 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -7,6 +7,7 @@ ***2022/09/24*** * 整合并优化了布局文件 +* 加入了更新文件自动清理的功能 ***2022/09/23*** * 优化了所有主布局加载速度 diff --git a/app/src/main/java/com/jack/bookshelf/constant/AppConstant.java b/app/src/main/java/com/jack/bookshelf/constant/AppConstant.java index 106b644..df8caaa 100644 --- a/app/src/main/java/com/jack/bookshelf/constant/AppConstant.java +++ b/app/src/main/java/com/jack/bookshelf/constant/AppConstant.java @@ -1,8 +1,5 @@ package com.jack.bookshelf.constant; -import android.content.Context; -import android.provider.Settings; - import com.google.gson.reflect.TypeToken; import com.jack.bookshelf.BuildConfig; import com.jack.bookshelf.help.FileHelp; @@ -15,8 +12,6 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; -import okhttp3.MediaType; - public class AppConstant { public static final String ActionStartService = "startService"; public static final String ActionDoneService = "doneService"; @@ -40,10 +35,4 @@ public class AppConstant { public static final Pattern EXP_PATTERN = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}"); public static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("rhino"); - - public static final MediaType jsonMediaType = MediaType.parse("Content-Type, application/json"); - - static public String androidId(Context context) { - return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); - } } \ No newline at end of file diff --git a/app/src/main/java/com/jack/bookshelf/help/update/UpdateManager.java b/app/src/main/java/com/jack/bookshelf/help/update/UpdateManager.java index fe02ffd..ec6d7b7 100644 --- a/app/src/main/java/com/jack/bookshelf/help/update/UpdateManager.java +++ b/app/src/main/java/com/jack/bookshelf/help/update/UpdateManager.java @@ -88,6 +88,7 @@ public void forPositiveButton() { startUpdate(context, updateInfo); } }).show(mainView); + clearApkClear(); } else { callBack.showDialog(/*UpdateDownloadTask.getLastProgress()*/0); } @@ -210,6 +211,23 @@ private String getApkPath(String fileName) { return Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).getPath() + File.separator + fileName + ".apk"; } + public void clearApkClear() { + String thisVersion = MApplication.getVersionName().split("\\s")[0]; + File[] files = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).listFiles(); + if(files == null) return; + for (File file : files) { + if (file.isFile()) { + String fileName = file.getName(); + if (fileName.endsWith(".apk") && fileName.startsWith(StringUtils.getString(R.string.app_name))) { + String otherVersion = fileName.substring(2, fileName.length()-4); + if (!StringUtils.compareVersion(otherVersion, thisVersion)) { + file.delete(); + } + } + } + } + } + public interface CallBack { void setProgress(int progress); diff --git a/app/src/main/java/com/jack/bookshelf/utils/FileUtils.kt b/app/src/main/java/com/jack/bookshelf/utils/FileUtils.kt index 6fda86e..c5b5246 100644 --- a/app/src/main/java/com/jack/bookshelf/utils/FileUtils.kt +++ b/app/src/main/java/com/jack/bookshelf/utils/FileUtils.kt @@ -391,7 +391,6 @@ object FileUtils { @JvmOverloads fun delete(path: String, deleteRootDir: Boolean = false): Boolean { val file = File(path) - return if (file.exists()) { delete(file, deleteRootDir) } else false diff --git a/app/src/main/java/com/jack/bookshelf/view/activity/MainActivity.java b/app/src/main/java/com/jack/bookshelf/view/activity/MainActivity.java index 86f03ff..e8cff32 100644 --- a/app/src/main/java/com/jack/bookshelf/view/activity/MainActivity.java +++ b/app/src/main/java/com/jack/bookshelf/view/activity/MainActivity.java @@ -26,6 +26,7 @@ import com.jack.bookshelf.help.permission.Permissions; import com.jack.bookshelf.help.permission.PermissionsCompat; import com.jack.bookshelf.help.storage.BackupRestoreUi; +import com.jack.bookshelf.help.update.UpdateManager; import com.jack.bookshelf.model.UpLastChapterModel; import com.jack.bookshelf.presenter.MainPresenter; import com.jack.bookshelf.presenter.contract.MainContract; @@ -116,7 +117,10 @@ public void delete(String value) {} } @Override - protected void initData() {} + protected void initData() { + // 清除下载缓存 + UpdateManager.getInstance(this).clearApkClear(); + } @Override public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); } diff --git a/app/src/main/java/com/jack/bookshelf/view/activity/WelcomeActivity.java b/app/src/main/java/com/jack/bookshelf/view/activity/WelcomeActivity.java index 7fd4368..8806893 100644 --- a/app/src/main/java/com/jack/bookshelf/view/activity/WelcomeActivity.java +++ b/app/src/main/java/com/jack/bookshelf/view/activity/WelcomeActivity.java @@ -74,8 +74,7 @@ private void initData() { if (!preferences.getBoolean("importDefaultBookSource", false)) { String json = null; try { - InputStream inputStream = MApplication.getInstance().getAssets() - .open("default/bookSource.json"); + InputStream inputStream = MApplication.getInstance().getAssets().open("default/bookSource.json"); json = IOUtils.toString(inputStream); inputStream.close(); } catch (IOException e) { @@ -102,9 +101,7 @@ private void initData() { if (ruleDefaultList != null) { DbHelper.getDaoSession().getTxtChapterRuleBeanDao().insertOrReplaceInTx(ruleDefaultList); } - preferences.edit() - .putBoolean("importDefaultTxtRule", ruleDefaultList != null) - .apply(); + preferences.edit().putBoolean("importDefaultTxtRule", ruleDefaultList != null).apply(); } } } \ No newline at end of file diff --git a/app/src/main/java/com/jack/bookshelf/view/fragment/GeneralSettingFragment.java b/app/src/main/java/com/jack/bookshelf/view/fragment/GeneralSettingFragment.java index 832450a..76966f7 100644 --- a/app/src/main/java/com/jack/bookshelf/view/fragment/GeneralSettingFragment.java +++ b/app/src/main/java/com/jack/bookshelf/view/fragment/GeneralSettingFragment.java @@ -14,6 +14,7 @@ import com.jack.bookshelf.R; import com.jack.bookshelf.databinding.FragmentGeneralSettingBinding; import com.jack.bookshelf.help.BookshelfHelp; +import com.jack.bookshelf.help.update.UpdateManager; import com.jack.bookshelf.service.WebService; import com.jack.bookshelf.view.activity.SettingActivity; import com.jack.bookshelf.widget.dialog.NumberPickerDialog; @@ -92,11 +93,13 @@ private void bindView() { @Override public void forNegativeButton() { BookshelfHelp.clearCaches(false); + UpdateManager.getInstance(settingActivity).clearApkClear(); } @Override public void forPositiveButton() { BookshelfHelp.clearCaches(true); + UpdateManager.getInstance(settingActivity).clearApkClear(); }}) .show(settingActivity.getRoot())); binding.tvAboutRead.setOnClickListener(v -> openAboutFragment());