From a57eaf63037dfdb5a78e645bc717c86e5d3cb4cb Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 24 Oct 2021 21:02:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/config/OtherConfigFragment.kt | 8 +----- .../io/legado/app/utils/ContextExtensions.kt | 28 +++++++++++++------ .../java/io/legado/app/utils/SystemUtils.kt | 16 ----------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index 38eda3429..5322e8967 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -2,11 +2,9 @@ package io.legado.app.ui.config import android.annotation.SuppressLint import android.content.ComponentName -import android.content.Intent import android.content.SharedPreferences import android.content.pm.PackageManager import android.os.Bundle -import android.os.Process import android.view.View import androidx.preference.ListPreference import androidx.preference.Preference @@ -21,7 +19,6 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.primaryColor import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.service.WebService -import io.legado.app.ui.main.MainActivity import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.utils.* import splitties.init.appCtx @@ -113,10 +110,7 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.showDiscovery, PreferKey.showRss -> postEvent(EventBus.NOTIFY_MAIN, true) PreferKey.language -> listView.postDelayed({ - val intent = Intent(appCtx, MainActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) - appCtx.startActivity(intent) - Process.killProcess(Process.myPid()) + appCtx.restart() }, 1000) PreferKey.userAgent -> listView.post { upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 00546c8ad..3e2985c9e 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -15,6 +15,7 @@ import android.graphics.drawable.Drawable import android.net.Uri import android.os.BatteryManager import android.os.Build +import android.os.Process import android.provider.Settings import android.widget.Toast import androidx.annotation.ColorRes @@ -148,19 +149,30 @@ fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? = ContextCompat.g fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? = ContextCompat.getColorStateList(this, id) +fun Context.restart() { + val intent: Intent? = packageManager.getLaunchIntentForPackage(packageName) + intent?.let { + intent.addFlags( + Intent.FLAG_ACTIVITY_NEW_TASK + or Intent.FLAG_ACTIVITY_CLEAR_TASK + or Intent.FLAG_ACTIVITY_CLEAR_TOP + ) + startActivity(intent) + //杀掉以前进程 + Process.killProcess(Process.myPid()) + } +} + /** * 系统息屏时间 */ val Context.sysScreenOffTime: Int get() { - var screenOffTime = 0 - try { - screenOffTime = - Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT) - } catch (e: Exception) { - Timber.e(e) - } - return screenOffTime + return kotlin.runCatching { + Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT) + }.onFailure { + Timber.e(it) + }.getOrDefault(0) } val Context.statusBarHeight: Int diff --git a/app/src/main/java/io/legado/app/utils/SystemUtils.kt b/app/src/main/java/io/legado/app/utils/SystemUtils.kt index af953f294..1b6df2df6 100644 --- a/app/src/main/java/io/legado/app/utils/SystemUtils.kt +++ b/app/src/main/java/io/legado/app/utils/SystemUtils.kt @@ -2,32 +2,16 @@ package io.legado.app.utils import android.annotation.SuppressLint import android.app.Activity -import android.content.Context import android.content.Context.POWER_SERVICE import android.content.Intent import android.net.Uri import android.os.PowerManager import android.provider.Settings -import timber.log.Timber @Suppress("unused") object SystemUtils { - fun getScreenOffTime(context: Context): Int { - var screenOffTime = 0 - kotlin.runCatching { - screenOffTime = Settings.System.getInt( - context.contentResolver, - Settings.System.SCREEN_OFF_TIMEOUT - ) - }.onFailure { - Timber.e(it) - } - - return screenOffTime - } - fun ignoreBatteryOptimization(activity: Activity) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) return