From e9233e56401049832c639f1322da90a08e9e70b4 Mon Sep 17 00:00:00 2001 From: F0x1d Date: Fri, 24 Feb 2023 18:07:01 +0500 Subject: [PATCH] Fixed theme changes --- app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt | 8 ++++++++ .../f0x1d/logfox/extensions/ContextExtensions.kt | 14 +++++++++++++- .../logfox/extensions/PendingIntentsExtensions.kt | 1 + .../com/f0x1d/logfox/service/LoggingService.kt | 7 +++---- .../f0x1d/logfox/ui/activity/base/BaseActivity.kt | 6 ------ .../ui/fragment/settings/SettingsUIFragment.kt | 2 +- app/src/main/res/values-night/themes.xml | 2 +- app/src/main/res/values/themes.xml | 2 +- 8 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt b/app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt index da8cd4aa..a6c71463 100644 --- a/app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt +++ b/app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt @@ -3,12 +3,15 @@ package com.f0x1d.logfox import android.app.Application import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationManagerCompat +import com.f0x1d.logfox.extensions.applyTheme import com.f0x1d.logfox.extensions.notificationManagerCompat +import com.f0x1d.logfox.utils.preferences.AppPreferences import com.f0x1d.logfox.utils.view.FontsInterceptor import com.google.android.material.color.DynamicColors import dagger.hilt.android.HiltAndroidApp import io.github.inflationx.viewpump.ViewPump import kotlinx.coroutines.MainScope +import javax.inject.Inject @HiltAndroidApp class LogFoxApp: Application() { @@ -22,10 +25,15 @@ class LogFoxApp: Application() { lateinit var instance: LogFoxApp } + @Inject + lateinit var appPreferences: AppPreferences + override fun onCreate() { super.onCreate() instance = this + applyTheme(appPreferences.nightTheme) + ViewPump.init( ViewPump.builder() .addInterceptor(FontsInterceptor(this)) diff --git a/app/src/main/java/com/f0x1d/logfox/extensions/ContextExtensions.kt b/app/src/main/java/com/f0x1d/logfox/extensions/ContextExtensions.kt index 131b54fa..aaee930b 100644 --- a/app/src/main/java/com/f0x1d/logfox/extensions/ContextExtensions.kt +++ b/app/src/main/java/com/f0x1d/logfox/extensions/ContextExtensions.kt @@ -3,6 +3,7 @@ package com.f0x1d.logfox.extensions import android.Manifest import android.app.ActivityManager import android.app.NotificationManager +import android.app.UiModeManager import android.content.ClipData import android.content.ClipboardManager import android.content.Context @@ -10,6 +11,7 @@ import android.content.Intent import android.content.pm.PackageManager import android.os.Build import android.widget.Toast +import androidx.appcompat.app.AppCompatDelegate import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat import androidx.core.content.FileProvider @@ -32,6 +34,7 @@ fun Context.hasPermissionToReadLogs() = ContextCompat.checkSelfPermission( val Context.notificationManagerCompat get() = NotificationManagerCompat.from(this) val Context.notificationManager get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val Context.activityManager get() = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager +val Context.uiModeManager get() = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager fun Context.startLoggingAndService(loggingRepository: LoggingRepository, appPreferences: AppPreferences, force: Boolean = false) { loggingRepository.startLoggingIfNot() @@ -108,4 +111,13 @@ else fun Context.doIfPermitted(block: NotificationManagerCompat.() -> Unit) = if (hasNotificationsPermission()) block.invoke(notificationManagerCompat) else - Unit \ No newline at end of file + Unit + +fun Context.applyTheme(nightMode: Int, force: Boolean = false) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (force) uiModeManager.setApplicationNightMode( + if (nightMode != 0) nightMode else UiModeManager.MODE_NIGHT_CUSTOM + ) + } else + AppCompatDelegate.setDefaultNightMode(if (nightMode != 0) nightMode else AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) +} \ No newline at end of file diff --git a/app/src/main/java/com/f0x1d/logfox/extensions/PendingIntentsExtensions.kt b/app/src/main/java/com/f0x1d/logfox/extensions/PendingIntentsExtensions.kt index dcd15d42..06594145 100644 --- a/app/src/main/java/com/f0x1d/logfox/extensions/PendingIntentsExtensions.kt +++ b/app/src/main/java/com/f0x1d/logfox/extensions/PendingIntentsExtensions.kt @@ -16,6 +16,7 @@ const val EXIT_APP_INTENT_ID = 3 const val PAUSE_RECORDING_INTENT_ID = 4 const val RESUME_RECORDING_INTENT_ID = 5 const val STOP_RECORDING_INTENT_ID = 6 +const val OPEN_APP_INTENT_ID = 7 val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT diff --git a/app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt b/app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt index 41116dc0..4007a232 100644 --- a/app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt +++ b/app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt @@ -7,11 +7,9 @@ import androidx.core.app.NotificationCompat import com.f0x1d.logfox.BuildConfig import com.f0x1d.logfox.LogFoxApp import com.f0x1d.logfox.R -import com.f0x1d.logfox.extensions.EXIT_APP_INTENT_ID -import com.f0x1d.logfox.extensions.STOP_LOGGING_SERVICE_INTENT_ID -import com.f0x1d.logfox.extensions.activityManager -import com.f0x1d.logfox.extensions.makeServicePendingIntent +import com.f0x1d.logfox.extensions.* import com.f0x1d.logfox.repository.logging.LoggingRepository +import com.f0x1d.logfox.ui.activity.MainActivity import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.update import javax.inject.Inject @@ -47,6 +45,7 @@ class LoggingService: Service() { private fun notification() = NotificationCompat.Builder(this, LogFoxApp.LOGGING_STATUS_CHANNEL_ID) .setContentTitle(getString(R.string.logging)) .setSmallIcon(R.drawable.ic_bug_notification) + .setContentIntent(makeActivityPendingIntent(OPEN_APP_INTENT_ID, MainActivity::class.java)) .addAction(R.drawable.ic_stop, getString(R.string.stop_service), makeServicePendingIntent(STOP_LOGGING_SERVICE_INTENT_ID, LoggingService::class.java) { action = ACTION_STOP_SERVICE }) diff --git a/app/src/main/java/com/f0x1d/logfox/ui/activity/base/BaseActivity.kt b/app/src/main/java/com/f0x1d/logfox/ui/activity/base/BaseActivity.kt index 62f23690..bec3e572 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/activity/base/BaseActivity.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/activity/base/BaseActivity.kt @@ -4,14 +4,12 @@ import android.content.Context import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.app.AppCompatDelegate import androidx.core.view.WindowCompat import androidx.viewbinding.ViewBinding import com.f0x1d.logfox.extensions.snackbar import com.f0x1d.logfox.utils.preferences.AppPreferences import dagger.hilt.EntryPoint import dagger.hilt.InstallIn -import dagger.hilt.android.EntryPointAccessors import dagger.hilt.components.SingletonComponent import io.github.inflationx.viewpump.ViewPumpContextWrapper @@ -24,10 +22,6 @@ abstract class BaseActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { WindowCompat.setDecorFitsSystemWindows(window, false) - EntryPointAccessors.fromApplication(applicationContext, BaseActivityEntryPoint::class.java).appPreferences().nightTheme.also { nightInt -> - AppCompatDelegate.setDefaultNightMode(if (nightInt != 0) nightInt else AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - } - super.onCreate(savedInstanceState) inflateBinding()?.also { diff --git a/app/src/main/java/com/f0x1d/logfox/ui/fragment/settings/SettingsUIFragment.kt b/app/src/main/java/com/f0x1d/logfox/ui/fragment/settings/SettingsUIFragment.kt index 5863926b..3000b890 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/fragment/settings/SettingsUIFragment.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/fragment/settings/SettingsUIFragment.kt @@ -40,7 +40,7 @@ class SettingsUIFragment: BaseSettingsWrapperFragment() { appPreferences.nightTheme ) { appPreferences.nightTheme = it - requireActivity().recreate() + requireActivity().applyTheme(it, true) } observeAndUpdateSummaryForList(appPreferences, this@SettingsUIWrappedFragment, 0, filledThemeSettings) diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml index 2add5b9f..ee235536 100644 --- a/app/src/main/res/values-night/themes.xml +++ b/app/src/main/res/values-night/themes.xml @@ -1,6 +1,6 @@ -