From 37aab60ddc7addd10b1469cc1f8cbaa1dc86ec28 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+huancheng65@users.noreply.github.com> Date: Fri, 10 Jun 2022 10:38:18 +0800 Subject: [PATCH] pref: Target SDK 31 --- app/build.gradle | 17 ++-- app/src/main/AndroidManifest.xml | 7 +- .../com/huanchengfly/tieba/post/Extensions.kt | 20 ++++- .../post/components/workers/OKSignWorker.kt | 23 ----- .../tieba/post/fragments/SettingsFragment.kt | 85 +++++++++++++++---- .../receivers/BootCompleteSignReceiver.kt | 14 +-- .../tieba/post/services/NotifyJobService.kt | 13 ++- .../tieba/post/services/OKSignService.kt | 30 ++++--- .../tieba/post/utils/CrashUtil.java | 4 +- .../huanchengfly/tieba/post/utils/OKSigner.kt | 41 +++++++++ .../tieba/post/utils/TiebaUtil.kt | 7 +- .../res/drawable/ic_round_battery_alert.xml | 10 +++ app/src/main/res/values/strings.xml | 7 +- app/src/main/res/xml/preferences.xml | 6 ++ build.gradle | 4 +- 15 files changed, 204 insertions(+), 84 deletions(-) delete mode 100644 app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWorker.kt create mode 100644 app/src/main/res/drawable/ic_round_battery_alert.xml diff --git a/app/build.gradle b/app/build.gradle index 3221ae2e..564f97e6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,7 +27,7 @@ android { applicationId "com.huanchengfly.tieba.post" minSdkVersion 21 //noinspection OldTargetApi - targetSdkVersion 30 + targetSdkVersion 31 versionCode applicationVersionCode versionName applicationVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -64,7 +64,6 @@ android { } kotlinOptions { jvmTarget = '1.8' - useIR = true } packagingOptions { resources { @@ -74,11 +73,11 @@ android { } dependencies { - implementation "org.jetbrains:annotations:19.0.0" + implementation 'org.jetbrains:annotations:23.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2" - api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2" + api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2' + api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' @@ -126,13 +125,13 @@ dependencies { implementation "com.squareup.retrofit2:retrofit:$retrofit_version" implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" - implementation "com.google.code.gson:gson:2.8.7" + implementation 'com.google.code.gson:gson:2.9.0' implementation "cn.dreamtobe.kpswitch:library:1.6.2" implementation "org.litepal.android:kotlin:3.0.0" implementation "com.github.SheHuan:RecyclerViewAdapter:1.2.9" implementation "com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0" implementation "com.bm.photoview:library:2.0.7" - implementation "cn.jzvd:jiaozivideoplayer:7.7.0" + implementation 'cn.jzvd:jiaozivideoplayer:7.7.2.3300' implementation "com.jrummyapps:colorpicker:2.1.7" implementation "com.scwang.smart:refresh-layout-kernel:2.0.1" @@ -149,11 +148,11 @@ dependencies { implementation "com.jakewharton:butterknife:10.2.3" kapt "com.jakewharton:butterknife-compiler:10.2.3" - implementation ("com.alibaba.android:vlayout:1.2.31@aar") { + implementation ('com.alibaba.android:vlayout:1.3.0@aar') { transitive = true } - def appCenterSdkVersion = '4.3.1' + def appCenterSdkVersion = '4.4.3' implementation "com.microsoft.appcenter:appcenter-analytics:${appCenterSdkVersion}" implementation "com.microsoft.appcenter:appcenter-crashes:${appCenterSdkVersion}" implementation "com.microsoft.appcenter:appcenter-distribute:${appCenterSdkVersion}" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6b0191b8..ebf2a427 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,6 +24,7 @@ + @@ -101,6 +102,7 @@ android:configChanges="screenSize|screenLayout|orientation|smallestScreenSize|keyboardHidden" android:resizeableActivity="true" android:theme="@style/AppTheme.Splash" + android:exported="true" android:windowSoftInputMode="adjustResize"> @@ -156,7 +158,9 @@ android:name=".activities.AboutActivity" android:configChanges="screenSize|screenLayout|orientation|smallestScreenSize|keyboardHidden" android:windowSoftInputMode="adjustResize" /> - + = Configuration.SCREENLAYOUT_SIZE_LARGE val Context.isTablet: Boolean - get() = resources.configuration.isTablet \ No newline at end of file + get() = resources.configuration.isTablet + +fun pendingIntentFlagMutable(): Int { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + PendingIntent.FLAG_MUTABLE + } else { + 0 + } +} + +fun pendingIntentFlagImmutable(): Int { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + PendingIntent.FLAG_IMMUTABLE + } else { + 0 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWorker.kt b/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWorker.kt deleted file mode 100644 index ec706c75..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/components/workers/OKSignWorker.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.huanchengfly.tieba.post.components.workers - -import android.content.Context -import androidx.work.Worker -import androidx.work.WorkerParameters -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlin.coroutines.CoroutineContext - -class OKSignWorker( - context: Context, - workerParams: WorkerParameters -) : Worker(context, workerParams), CoroutineScope { - override fun doWork(): Result { - return Result.success() - } - - val job = Job() - - override val coroutineContext: CoroutineContext - get() = Dispatchers.Main + job -} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/SettingsFragment.kt b/app/src/main/java/com/huanchengfly/tieba/post/fragments/SettingsFragment.kt index b6c69db9..d2e6fbd7 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/SettingsFragment.kt @@ -1,10 +1,14 @@ package com.huanchengfly.tieba.post.fragments import android.annotation.SuppressLint +import android.content.Context import android.content.DialogInterface import android.content.Intent +import android.net.Uri import android.os.Build import android.os.Bundle +import android.os.PowerManager +import android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS import android.view.View import android.widget.Toast import androidx.core.content.ContextCompat @@ -20,9 +24,9 @@ import com.huanchengfly.tieba.post.components.prefs.TimePickerPreference import com.huanchengfly.tieba.post.fragments.preference.PreferencesFragment import com.huanchengfly.tieba.post.models.database.Account import com.huanchengfly.tieba.post.models.database.Block +import com.huanchengfly.tieba.post.toastShort import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils import com.huanchengfly.tieba.post.utils.* -import java.util.* class SettingsFragment : PreferencesFragment() { private var loginInfo: Account? = null @@ -31,6 +35,18 @@ class SettingsFragment : PreferencesFragment() { refresh() } + //忽略电池优化 + private fun ignoreBatteryOptimization() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + val powerManager = attachContext.getSystemService(Context.POWER_SERVICE) as PowerManager + if (!powerManager.isIgnoringBatteryOptimizations(attachContext.packageName)) { + val intent = Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) + intent.data = Uri.parse("package:${attachContext.packageName}") + startActivity(intent) + } + } + } + private fun refresh() { loginInfo = AccountUtil.getLoginInfo(attachContext) val accounts = AccountUtil.allAccounts @@ -56,23 +72,62 @@ class SettingsFragment : PreferencesFragment() { preferenceManager.sharedPreferencesName = "settings" addPreferencesFromResource(R.xml.preferences) val accountsPreference = findPreference("switch_account") - accountsPreference!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any? -> - if (AccountUtil.switchUser(attachContext, Integer.valueOf((newValue as String?)!!))) { - refresh() - Toast.makeText(attachContext, R.string.toast_switch_success, Toast.LENGTH_SHORT).show() + accountsPreference!!.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any? -> + if (AccountUtil.switchUser( + attachContext, + Integer.valueOf((newValue as String?)!!) + ) + ) { + refresh() + Toast.makeText(attachContext, R.string.toast_switch_success, Toast.LENGTH_SHORT) + .show() + } + false } - false - } - findPreference("copy_bduss")!!.onPreferenceClickListener = Preference.OnPreferenceClickListener { - val account = AccountUtil.getLoginInfo(attachContext) - if (account != null) { - TiebaUtil.copyText(attachContext, account.bduss) + findPreference("ignore_battery_optimization")?.let { + val powerManager = attachContext.getSystemService(Context.POWER_SERVICE) as PowerManager + it.onPreferenceClickListener = Preference.OnPreferenceClickListener { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (!powerManager.isIgnoringBatteryOptimizations(attachContext.packageName)) { + ignoreBatteryOptimization() + } else { + attachContext.toastShort(R.string.toast_ignore_battery_optimization_already) + } + } + true + } + it.isEnabled = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !powerManager.isIgnoringBatteryOptimizations( + attachContext.packageName + ) + it.setSummaryProvider { + when { + Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> { + attachContext.getString(R.string.summary_battery_optimization_old_android_version) + } + powerManager.isIgnoringBatteryOptimizations(attachContext.packageName) -> { + attachContext.getString(R.string.summary_battery_optimization_ignored) + } + else -> { + attachContext.getString(R.string.summary_ignore_battery_optimization) + } + } } - true } - findPreference("exit_account")!!.isEnabled = AccountUtil.isLoggedIn(attachContext) - findPreference("exit_account")!!.onPreferenceClickListener = Preference.OnPreferenceClickListener { - DialogUtil.build(attachContext) + findPreference("copy_bduss")!!.onPreferenceClickListener = + Preference.OnPreferenceClickListener { + val account = AccountUtil.getLoginInfo(attachContext) + if (account != null) { + TiebaUtil.copyText(attachContext, account.bduss) + } + true + } + findPreference("exit_account")!!.isEnabled = + AccountUtil.isLoggedIn(attachContext) + findPreference("exit_account")!!.onPreferenceClickListener = + Preference.OnPreferenceClickListener { + DialogUtil.build(attachContext) .setMessage(R.string.title_dialog_exit_account) .setPositiveButton(R.string.button_sure_default) { _: DialogInterface?, _: Int -> AccountUtil.exit(attachContext) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/receivers/BootCompleteSignReceiver.kt b/app/src/main/java/com/huanchengfly/tieba/post/receivers/BootCompleteSignReceiver.kt index 1f5d5878..bf9569fd 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/receivers/BootCompleteSignReceiver.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/receivers/BootCompleteSignReceiver.kt @@ -5,7 +5,7 @@ import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.os.Build +import com.huanchengfly.tieba.post.pendingIntentFlagImmutable import com.huanchengfly.tieba.post.utils.TiebaUtil import com.huanchengfly.tieba.post.utils.Util import com.huanchengfly.tieba.post.utils.appPreferences @@ -28,12 +28,12 @@ class BootCompleteSignReceiver : BroadcastReceiver() { val time = Util.time2Calendar(autoSignTimeStr).apply { add(Calendar.DAY_OF_MONTH, 1) }.timeInMillis - val pendingIntent = PendingIntent.getBroadcast(context, 0, Intent(context, AutoSignAlarm::class.java), - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PendingIntent.FLAG_IMMUTABLE - } else { - 0 - }) + val pendingIntent = PendingIntent.getBroadcast( + context, + 0, + Intent(context, AutoSignAlarm::class.java), + pendingIntentFlagImmutable() + ) alarmManager.setInexactRepeating(AlarmManager.RTC, time, AlarmManager.INTERVAL_DAY, pendingIntent) } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/services/NotifyJobService.kt b/app/src/main/java/com/huanchengfly/tieba/post/services/NotifyJobService.kt index 52b48dc4..7ec98221 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/services/NotifyJobService.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/services/NotifyJobService.kt @@ -16,6 +16,7 @@ import com.huanchengfly.tieba.post.activities.MessageActivity import com.huanchengfly.tieba.post.api.TiebaApi import com.huanchengfly.tieba.post.api.models.MsgBean import com.huanchengfly.tieba.post.fragments.MessageFragment +import com.huanchengfly.tieba.post.pendingIntentFlagImmutable import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils import retrofit2.Call import retrofit2.Callback @@ -104,14 +105,10 @@ class NotifyJobService : JobService() { .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent( PendingIntent.getActivity( - this, - 0, - intent, - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PendingIntent.FLAG_IMMUTABLE - } else { - 0 - } + this, + 0, + intent, + pendingIntentFlagImmutable() ) ) .setColor(ThemeUtils.getColorByAttr(this, R.attr.colorPrimary)) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/services/OKSignService.kt b/app/src/main/java/com/huanchengfly/tieba/post/services/OKSignService.kt index 832245ae..76846ad1 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/services/OKSignService.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/services/OKSignService.kt @@ -6,7 +6,7 @@ import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent -import android.os.* +import android.os.Build import android.widget.Toast import androidx.core.app.NotificationCompat import com.huanchengfly.tieba.post.R @@ -14,11 +14,17 @@ import com.huanchengfly.tieba.post.activities.LoginActivity import com.huanchengfly.tieba.post.activities.MainActivity import com.huanchengfly.tieba.post.api.models.SignResultBean import com.huanchengfly.tieba.post.models.SignDataBean +import com.huanchengfly.tieba.post.pendingIntentFlagImmutable import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils -import com.huanchengfly.tieba.post.utils.* -import kotlinx.coroutines.* +import com.huanchengfly.tieba.post.utils.AccountUtil +import com.huanchengfly.tieba.post.utils.ProgressListener +import com.huanchengfly.tieba.post.utils.SingleAccountSigner +import com.huanchengfly.tieba.post.utils.addFlag +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.Main -import java.util.* +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel +import kotlinx.coroutines.runBlocking import kotlin.coroutines.CoroutineContext class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener { @@ -54,13 +60,15 @@ class OKSignService : IntentService(TAG), CoroutineScope, ProgressListener { private fun updateNotification(title: String, text: String, intent: Intent) { manager.notify(1, - buildNotification(title, text) - .setContentIntent(PendingIntent.getActivity(this, 0, intent, - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PendingIntent.FLAG_IMMUTABLE - } else { - 0 - })) + buildNotification(title, text) + .setContentIntent( + PendingIntent.getActivity( + this, + 0, + intent, + pendingIntentFlagImmutable() + ) + ) .build()) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/CrashUtil.java b/app/src/main/java/com/huanchengfly/tieba/post/utils/CrashUtil.java index 177661fc..468d59c1 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/CrashUtil.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/CrashUtil.java @@ -1,5 +1,7 @@ package com.huanchengfly.tieba.post.utils; +import static com.huanchengfly.tieba.post.ExtensionsKt.pendingIntentFlagMutable; + import android.annotation.SuppressLint; import android.app.AlarmManager; import android.app.PendingIntent; @@ -114,7 +116,7 @@ public class CrashUtil { context.getApplicationContext(), 0, intent, - 0 + pendingIntentFlagMutable() ); mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/OKSigner.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/OKSigner.kt index f2423d7e..fe30818d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/OKSigner.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/OKSigner.kt @@ -25,6 +25,8 @@ abstract class IOKSigner( abstract suspend fun start(): Boolean + abstract suspend fun startSync(): Boolean + suspend fun sign(signDataBean: SignDataBean): ApiResult { return TiebaApi.getInstance() .signAsync(signDataBean.forumName, signDataBean.tbs) @@ -98,12 +100,50 @@ class SingleAccountSigner( private var successCount = 0 private var totalCount = 0 + var lastFailure: Throwable? = null + private var mProgressListener: ProgressListener? = null fun setProgressListener(listener: ProgressListener?) { mProgressListener = listener } + override suspend fun startSync(): Boolean { + var result = false + signData.clear() + var userName: String by Delegates.notNull() + var tbs: String by Delegates.notNull() + AccountUtil.updateUserInfoAsync(coroutineScope, account.bduss) + .await() + .fetchIfSuccess { + userName = it.data.name + tbs = it.data.itbTbs + TiebaApi.getInstance().forumRecommendAsync().getData() + } + .doIfSuccess { forumRecommend -> + signData.addAll(forumRecommend.likeForum.filter { it.isSign != "1" } + .map { SignDataBean(it.forumName, userName, tbs) }) + totalCount = signData.size + mProgressListener?.onStart(totalCount) + if (signData.isNotEmpty()) { + result = sign(0) + } else { + mProgressListener?.onFinish(true, 0, 0) + } + } + .doIfFailure { + lastFailure = it + mProgressListener?.onFailure( + 0, + 0, + it.getErrorCode(), + it.getErrorMessage() + ) + throw it + } + return result + } + override suspend fun start(): Boolean { var result = false signData.clear() @@ -128,6 +168,7 @@ class SingleAccountSigner( } } .doIfFailure { + lastFailure = it mProgressListener?.onFailure( 0, 0, diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt index b90fc8b2..542f2c26 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/TiebaUtil.kt @@ -12,6 +12,7 @@ import com.huanchengfly.tieba.post.activities.WebViewActivity import com.huanchengfly.tieba.post.api.TiebaApi import com.huanchengfly.tieba.post.api.models.CheckReportBean import com.huanchengfly.tieba.post.components.dialogs.LoadingDialog +import com.huanchengfly.tieba.post.pendingIntentFlagImmutable import com.huanchengfly.tieba.post.receivers.AutoSignAlarm import com.huanchengfly.tieba.post.services.OKSignService import com.huanchengfly.tieba.post.toastShort @@ -41,11 +42,7 @@ object TiebaUtil { context, 0, Intent(context, AutoSignAlarm::class.java), - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PendingIntent.FLAG_IMMUTABLE - } else { - 0 - } + pendingIntentFlagImmutable() ) if (autoSign) { val autoSignTimeStr = context.appPreferences.autoSignTime!! diff --git a/app/src/main/res/drawable/ic_round_battery_alert.xml b/app/src/main/res/drawable/ic_round_battery_alert.xml new file mode 100644 index 00000000..df6632e5 --- /dev/null +++ b/app/src/main/res/drawable/ic_round_battery_alert.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 460e20b2..9f287f8a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -201,7 +201,7 @@ 开启后,将每天定时运行一键签到 自动签到已开启 当前设置为 %1$s - 提示:请您允许贴吧 Lite 的自启动,否则可能无法正常唤醒签到。 + 提示:请允许贴吧 Lite 的自启动,否则可能无法正常唤醒签到。\n若使用的是 MIUI 等国产定制 UI,你还需要在系统设置中进行相应修改。 点击查看完整更新日志 %1$s吧 编辑资料 @@ -498,4 +498,9 @@ 未安装官方版客户端 使用 PC 网页版发贴 发贴回贴风险提示 + 请忽略“电池优化”以允许应用启动自动签到 + 忽略“电池优化” + 旧版 Android 无“电池优化” + “电池优化”已忽略 + 已忽略电池优化 diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index cdf5c579..8516306f 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -228,6 +228,12 @@ android:key="auto_sign_time" android:title="@string/title_auto_sign_time" /> + + diff --git a/build.gradle b/build.gradle index d5674a6e..f59d20f1 100644 --- a/build.gradle +++ b/build.gradle @@ -2,9 +2,9 @@ buildscript { ext { - glide_version = "4.12.0" + glide_version = '4.13.2' retrofit_version = "2.9.0" - kotlin_version = '1.5.31' + kotlin_version = '1.7.0' } repositories { google()