|
|
@@ -1,35 +1,25 @@
|
|
|
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.*
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
import dagger.hilt.android.EntryPointAccessors
|
|
|
-import kotlin.math.absoluteValue
|
|
|
-import kotlinx.coroutines.Dispatchers
|
|
|
-import kotlinx.coroutines.launch
|
|
|
-import java.io.IOException
|
|
|
import java.lang.reflect.Method
|
|
|
+import kotlin.math.absoluteValue
|
|
|
|
|
|
|
|
|
@AndroidEntryPoint
|
|
|
@@ -37,10 +27,11 @@ class MainActivity : AppCompatActivity(),
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener,
|
|
|
HomeWatcher.OnHomePressedListener, IPublisher {
|
|
|
|
|
|
+ private val wallpaperManager = WallpaperManager(this)
|
|
|
+
|
|
|
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()
|
|
|
|
|
|
@@ -115,70 +106,15 @@ class MainActivity : AppCompatActivity(),
|
|
|
|
|
|
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
|
|
|
+ wallpaperManager.onApplyThemeResource(theme, resid)
|
|
|
}
|
|
|
|
|
|
override fun setTheme(resId: Int) {
|
|
|
- val userThemeId = getUserSelectedThemeRes()
|
|
|
- val id = if (resId != userThemeId) {
|
|
|
- userThemeId
|
|
|
- } else {
|
|
|
- resId
|
|
|
- }
|
|
|
- super.setTheme(id)
|
|
|
+ super.setTheme(getUserSelectedThemeRes())
|
|
|
}
|
|
|
|
|
|
@StyleRes
|
|
|
- private fun getUserSelectedThemeRes(): Int {
|
|
|
+ fun getUserSelectedThemeRes(): Int {
|
|
|
settings = getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
|
|
|
val active = settings.getInt(getString(R.string.prefs_settings_key_theme), 0)
|
|
|
return resolveTheme(active)
|
|
|
@@ -211,15 +147,7 @@ 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) {
|