|
|
@@ -1,37 +1,47 @@
|
|
|
package com.sduduzog.slimlauncher
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
+import android.app.WallpaperManager
|
|
|
import android.content.SharedPreferences
|
|
|
import android.content.res.Resources
|
|
|
+import android.graphics.Bitmap
|
|
|
+import android.graphics.Canvas
|
|
|
import android.os.Bundle
|
|
|
import android.util.Log
|
|
|
import android.view.GestureDetector
|
|
|
import android.view.GestureDetector.SimpleOnGestureListener
|
|
|
import android.view.MotionEvent
|
|
|
import android.view.View
|
|
|
+import androidx.annotation.ColorInt
|
|
|
+import androidx.annotation.StyleRes
|
|
|
+import androidx.annotation.WorkerThread
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
import androidx.constraintlayout.motion.widget.MotionLayout
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import androidx.navigation.NavController
|
|
|
import androidx.navigation.Navigation.findNavController
|
|
|
+import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
|
|
|
import com.sduduzog.slimlauncher.di.MainFragmentFactoryEntryPoint
|
|
|
-import com.sduduzog.slimlauncher.utils.BaseFragment
|
|
|
-import com.sduduzog.slimlauncher.utils.HomeWatcher
|
|
|
-import com.sduduzog.slimlauncher.utils.IPublisher
|
|
|
-import com.sduduzog.slimlauncher.utils.ISubscriber
|
|
|
+import com.sduduzog.slimlauncher.utils.*
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
import dagger.hilt.android.EntryPointAccessors
|
|
|
-import java.lang.reflect.Method
|
|
|
import kotlin.math.absoluteValue
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import java.io.IOException
|
|
|
+import java.lang.reflect.Method
|
|
|
|
|
|
|
|
|
@AndroidEntryPoint
|
|
|
class MainActivity : AppCompatActivity(),
|
|
|
- SharedPreferences.OnSharedPreferenceChangeListener,
|
|
|
- HomeWatcher.OnHomePressedListener, IPublisher {
|
|
|
+ SharedPreferences.OnSharedPreferenceChangeListener,
|
|
|
+ HomeWatcher.OnHomePressedListener, IPublisher {
|
|
|
|
|
|
private lateinit var settings: SharedPreferences
|
|
|
private lateinit var navigator: NavController
|
|
|
private lateinit var homeWatcher: HomeWatcher
|
|
|
+ private lateinit var unlauncherDataSource: UnlauncherDataSource
|
|
|
+
|
|
|
private val subscribers: MutableSet<BaseFragment> = mutableSetOf()
|
|
|
|
|
|
override fun attachSubscriber(s: ISubscriber) {
|
|
|
@@ -84,12 +94,16 @@ class MainActivity : AppCompatActivity(),
|
|
|
homeWatcher.stopWatch()
|
|
|
}
|
|
|
|
|
|
+ override fun onDestroy() {
|
|
|
+ super.onDestroy()
|
|
|
+ settings.unregisterOnSharedPreferenceChangeListener(this)
|
|
|
+ }
|
|
|
+
|
|
|
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
|
|
super.onWindowFocusChanged(hasFocus)
|
|
|
if (hasFocus) toggleStatusBar()
|
|
|
}
|
|
|
|
|
|
-
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, s: String?) {
|
|
|
if (s.equals(getString(R.string.prefs_settings_key_theme), true)) {
|
|
|
recreate()
|
|
|
@@ -99,12 +113,75 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun getTheme(): Resources.Theme {
|
|
|
- val theme = super.getTheme()
|
|
|
+ override fun onApplyThemeResource(theme: Resources.Theme?, @StyleRes resid: Int, first: Boolean) {
|
|
|
+ super.onApplyThemeResource(theme, resid, first)
|
|
|
+ getUnlaucherDataSource().unlauncherAppsRepo.liveData().observe(this, {
|
|
|
+ if (!it.setThemeWallpaper && getUserSelectedThemeRes() == resid) {
|
|
|
+ // only change the wallpaper when user has allowed it and
|
|
|
+ // preventing to change the wallpaper multiple times once it is rechecked in the settings
|
|
|
+ return@observe
|
|
|
+ }
|
|
|
+ @ColorInt val backgroundColor = getThemeBackgroundColor(theme, resid)
|
|
|
+ if (backgroundColor == Int.MIN_VALUE) {
|
|
|
+ return@observe
|
|
|
+ }
|
|
|
+ lifecycleScope.launch(Dispatchers.IO) {
|
|
|
+ setWallpaperBackgroundColor(backgroundColor)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return `Int.MIN_VALUE` if `android.R.attr.colorBackground` of `theme` could not be obtained.
|
|
|
+ */
|
|
|
+ @ColorInt
|
|
|
+ private fun getThemeBackgroundColor(theme: Resources.Theme?, @StyleRes themeRes: Int): Int {
|
|
|
+ val array = theme?.obtainStyledAttributes(themeRes, intArrayOf(android.R.attr.colorBackground))
|
|
|
+ try {
|
|
|
+ return array?.getColor(0, Int.MIN_VALUE) ?: Int.MIN_VALUE
|
|
|
+ } finally {
|
|
|
+ array?.recycle()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Throws(IOException::class)
|
|
|
+ @WorkerThread
|
|
|
+ private fun setWallpaperBackgroundColor(@ColorInt color: Int) {
|
|
|
+ val wallpaperManager = WallpaperManager.getInstance(applicationContext)
|
|
|
+ var width = wallpaperManager.desiredMinimumWidth
|
|
|
+ if (width <= 0) {
|
|
|
+ width = getScreenWidth(this)
|
|
|
+ }
|
|
|
+ var height = wallpaperManager.desiredMinimumHeight
|
|
|
+ if (height <= 0) {
|
|
|
+ height = getScreenHeight(this)
|
|
|
+ }
|
|
|
+ val wallpaperBitmap = createColoredWallpaperBitmap(color, width, height)
|
|
|
+ wallpaperManager.setBitmap(wallpaperBitmap)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun createColoredWallpaperBitmap(@ColorInt color: Int, width: Int, height: Int): Bitmap {
|
|
|
+ val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
|
|
+ val canvas = Canvas(bitmap)
|
|
|
+ canvas.drawColor(color)
|
|
|
+ return bitmap
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun setTheme(resId: Int) {
|
|
|
+ val userThemeId = getUserSelectedThemeRes()
|
|
|
+ val id = if (resId != userThemeId) {
|
|
|
+ userThemeId
|
|
|
+ } else {
|
|
|
+ resId
|
|
|
+ }
|
|
|
+ super.setTheme(id)
|
|
|
+ }
|
|
|
+
|
|
|
+ @StyleRes
|
|
|
+ private fun getUserSelectedThemeRes(): Int {
|
|
|
settings = getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
|
|
|
val active = settings.getInt(getString(R.string.prefs_settings_key_theme), 0)
|
|
|
- theme.applyStyle(resolveTheme(active), true)
|
|
|
- return theme
|
|
|
+ return resolveTheme(active)
|
|
|
}
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
@@ -134,8 +211,16 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun getUnlaucherDataSource(): UnlauncherDataSource {
|
|
|
+ if (!::unlauncherDataSource.isInitialized) {
|
|
|
+ unlauncherDataSource = UnlauncherDataSource(this, lifecycleScope)
|
|
|
+ }
|
|
|
+ return unlauncherDataSource
|
|
|
+ }
|
|
|
+
|
|
|
companion object {
|
|
|
|
|
|
+ @StyleRes
|
|
|
fun resolveTheme(i: Int): Int {
|
|
|
return when (i) {
|
|
|
1 -> R.style.AppThemeDark
|