Skip to content

Commit

Permalink
[feature]支持媒体定时自动检查更新
Browse files Browse the repository at this point in the history
  • Loading branch information
RyensX committed Jul 23, 2022
1 parent a910cd6 commit 7de1dfd
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 19 deletions.
24 changes: 21 additions & 3 deletions app/src/main/java/com/su/mediabox/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ import com.su.mediabox.pluginapi.util.PluginPreferenceIns
import com.su.mediabox.pluginapi.util.WebUtilIns
import com.su.mediabox.util.Util.getResColor
import com.su.mediabox.plugin.WebUtilImpl
import com.su.mediabox.util.appCoroutineScope
import com.su.mediabox.util.logD
import com.su.mediabox.util.release
import com.su.mediabox.util.showToast
import com.su.mediabox.work.launchMediaUpdateCheckWorker
import com.su.mediabox.work.stopMediaUpdateCheckWorker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class App : Application() {
Expand All @@ -29,6 +36,11 @@ class App : Application() {
super.onCreate()
context = this

release {
// Crash提示
AppCatchException.bindCrashHandler(this)
}

//插件工具初始化
AppUtil.init(this)
AppAction.init()
Expand All @@ -50,9 +62,15 @@ class App : Application() {

Analytics.trackEvent("应用启动")

release {
// Crash提示
AppCatchException.bindCrashHandler(this)
//媒体检查更新服务
appCoroutineScope.launch(Dispatchers.Default) {
Pref.mediaUpdateCheck.collect {
logD("媒体检查更新", "状态:$it")
if (it)
launchMediaUpdateCheckWorker()
else
stopMediaUpdateCheckWorker()
}
}

FileDownloader.setup(this)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/su/mediabox/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ object Pref {
Const.Setting.PLAY_ACTION_DEFAULT_CORE,
Exo2PlayerManager::class.java.name
)
val mediaUpdateCheck by lazyDataStoreStateFlow(Const.Setting.MEDIA_UPDATE_CHECK, true)
}
1 change: 1 addition & 0 deletions app/src/main/java/com/su/mediabox/config/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface Setting {
const val SHOW_PLAY_BOTTOM_BAR = "show_play_bottom_bar"
const val APP_LAUNCH_COUNT = "app_launch_count"
const val PLAY_ACTION_DEFAULT_CORE = "play_action_default_core"
const val MEDIA_UPDATE_CHECK = "media_update_check"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,31 @@ class SettingsPageFragment : PreferenceFragmentCompat(), Preference.OnPreference
}

preferenceCategory {
title = "媒体"
checkPreference {
isEnabled = false
titleRes(R.string.media_update_check_category)
val auto = switchPreference {
setDefaultValue(Pref.mediaUpdateCheck.value)
key = Const.Setting.MEDIA_UPDATE_CHECK
setIcon(R.drawable.ic_update_main_color_2_24_skin)
titleRes(R.string.media_update_check_name)
summaryRes(R.string.media_update_check_desc)
}
preference {
//TODO 开启时启动周期,关闭时取消
summaryRes(R.string.media_update_check_alert)
}
val now = preference {
titleRes(R.string.media_update_check_pref_now_name)
mediaUpdateCheckWorkerIsRunning.observe(this@SettingsPageFragment) {
isEnabled = !it
summary = if (it)
App.context.getString(R.string.media_update_check_pref_now_summary) else ""
}
setOnPreferenceClickListener {
launchMediaUpdateCheckWorkerNow()
true
}
}

mediaUpdateCheckWorkerIsRunning.observe(this@SettingsPageFragment) {
auto.isEnabled = !it
now.isEnabled = !it
now.summary = if (it)
App.context.getString(R.string.media_update_check_pref_now_summary) else ""
}
}

preferenceCategory {
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/com/su/mediabox/work/MediaUpdateCheckWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ internal class MediaUpdateCheckWorker(context: Context, workerParameters: Worker
return ForegroundInfo(2, notification)
}

override suspend fun getForegroundInfo(): ForegroundInfo = createForegroundInfo()

@FlowPreview
override suspend fun doWork(): Result {
if (mediaUpdateCheckWorkerIsRunning.value == true)
return Result.failure()
setForeground(createForegroundInfo())
runCatching {
setForeground(createForegroundInfo())
}
withContext(Dispatchers.IO + CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace()
logD(TAG, "发生错误:${throwable.message}")
Expand Down Expand Up @@ -198,10 +202,10 @@ fun launchMediaUpdateCheckWorker() {
.setRequiresBatteryNotLow(true)//电量充足才检查
.build()

val request = PeriodicWorkRequestBuilder<MediaUpdateCheckWorker>(24, TimeUnit.HOURS)
//TODO 自定义间隔
val request = PeriodicWorkRequestBuilder<MediaUpdateCheckWorker>(2, TimeUnit.HOURS)
.addTag(MEDIA_UPDATE_CHECK_WORKER_TAG)
.setConstraints(constraints)
//TODO 差异延迟+定时
.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
Expand All @@ -217,6 +221,10 @@ fun launchMediaUpdateCheckWorker() {
)
}

fun stopMediaUpdateCheckWorker() {
WorkManager.getInstance(App.context).cancelUniqueWork(MEDIA_UPDATE_CHECK_WORKER_ID)
}

fun launchMediaUpdateCheckWorkerNow() {
val request = OneTimeWorkRequestBuilder<MediaUpdateCheckWorker>()
.addTag(MEDIA_UPDATE_CHECK_WORKER_TAG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/foreground_main_color_2_skin"
android:fillColor="?colorPrimary"
android:pathData="M11,8.75v3.68c0,0.35 0.19,0.68 0.49,0.86l3.12,1.85c0.36,0.21 0.82,0.09 1.03,-0.26c0.21,-0.36 0.1,-0.82 -0.26,-1.03l-2.87,-1.71v-3.4C12.5,8.34 12.16,8 11.75,8S11,8.34 11,8.75zM21,9.5V4.21c0,-0.45 -0.54,-0.67 -0.85,-0.35l-1.78,1.78c-1.81,-1.81 -4.39,-2.85 -7.21,-2.6c-4.19,0.38 -7.64,3.75 -8.1,7.94C2.46,16.4 6.69,21 12,21c4.59,0 8.38,-3.44 8.93,-7.88c0.07,-0.6 -0.4,-1.12 -1,-1.12c-0.5,0 -0.92,0.37 -0.98,0.86c-0.43,3.49 -3.44,6.19 -7.05,6.14c-3.71,-0.05 -6.84,-3.18 -6.9,-6.9C4.94,8.2 8.11,5 12,5c1.93,0 3.68,0.79 4.95,2.05l-2.09,2.09C14.54,9.46 14.76,10 15.21,10h5.29C20.78,10 21,9.78 21,9.5z" />
</vector>
4 changes: 3 additions & 1 deletion app/src/main/res/values/pref_values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
<string name="open_source_watch">关注开发进度</string>
<string name="user_notice_summary">点击查看</string>
<!--媒体检查更新-->
<string name="media_update_check_category">媒体</string>
<string name="media_update_check_name">媒体自动检查更新</string>
<string name="media_update_check_result">共计检查%d个插件,发现%d项的媒体有更新</string>
<string name="media_update_check_hint">如果希望不自动检查,可以打开设置全局关闭或长按插件单独关闭</string>
<string name="media_update_check_desc">定时检查所有实现了媒体更新数据组件的插件的媒体更新</string>
<string name="media_update_check_alert">注意:在一些系统上可能无法在应用关闭后继续保持定时执行,建议为应用关闭电池优化和保持后台运行。</string>
<string name="media_update_check_pref_now_name">立即检查所有媒体更新</string>
<string name="media_update_check_pref_now_summary">正在检查中</string>
<string name="media_update_check_pref_now_summary">正在检查中</string>
<string name="media_update_check_plugin_result">发现%d个媒体有更新</string>

<string-array name="play_action_core_text">
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@ if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
exec "$JAVACMD" "$@"
1 change: 1 addition & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ exit /b 1
if "%OS%"=="Windows_NT" endlocal

:omega

0 comments on commit 7de1dfd

Please sign in to comment.