add a broadcastreceiver for activating and updating the shared theme

This commit is contained in:
tibbi 2017-11-29 15:34:55 +01:00
parent fa0c15bdb5
commit 9eddd7e215
8 changed files with 80 additions and 9 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 16 propMinSdkVersion = 16
propTargetSdkVersion = propCompileSdkVersion propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1 propVersionCode = 1
propVersionName = '2.41.19' propVersionName = '2.42.2'
kotlin_version = '1.2.0' kotlin_version = '1.2.0'
support_libs = '27.0.1' support_libs = '27.0.1'
} }

View file

@ -1,2 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest package="com.simplemobiletools.commons"/> <manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemobiletools.commons">
<application>
<receiver
android:name=".receivers.SharedThemeReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.simplemobiletools.commons.SHARED_THEME_ACTIVATED"/>
<action android:name="com.simplemobiletools.commons.SHARED_THEME_UPDATED"/>
</intent-filter>
</receiver>
</application>
</manifest>

View file

@ -1,11 +1,13 @@
package com.simplemobiletools.commons.activities package com.simplemobiletools.commons.activities
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.commons.R import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.dialogs.* import com.simplemobiletools.commons.dialogs.*
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.MyContentProvider
import com.simplemobiletools.commons.models.MyTheme import com.simplemobiletools.commons.models.MyTheme
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.models.SharedTheme import com.simplemobiletools.commons.models.SharedTheme
@ -85,7 +87,7 @@ class CustomizationActivity : BaseSimpleActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.save -> saveChanges() R.id.save -> saveChanges(true)
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)
} }
return true return true
@ -193,7 +195,7 @@ class CustomizationActivity : BaseSimpleActivity() {
private fun promptSaveDiscard() { private fun promptSaveDiscard() {
ConfirmationAdvancedDialog(this, "", R.string.save_before_closing, R.string.save, R.string.discard) { ConfirmationAdvancedDialog(this, "", R.string.save_before_closing, R.string.save, R.string.discard) {
if (it) { if (it) {
saveChanges() saveChanges(true)
} else { } else {
resetColors() resetColors()
finish() finish()
@ -201,7 +203,7 @@ class CustomizationActivity : BaseSimpleActivity() {
} }
} }
private fun saveChanges() { private fun saveChanges(finishAfterSave: Boolean) {
baseConfig.apply { baseConfig.apply {
textColor = curTextColor textColor = curTextColor
backgroundColor = curBackgroundColor backgroundColor = curBackgroundColor
@ -211,10 +213,16 @@ class CustomizationActivity : BaseSimpleActivity() {
if (curSelectedThemeId == THEME_SHARED) { if (curSelectedThemeId == THEME_SHARED) {
val newSharedTheme = SharedTheme(curTextColor, curBackgroundColor, curPrimaryColor) val newSharedTheme = SharedTheme(curTextColor, curBackgroundColor, curPrimaryColor)
updateSharedTheme(newSharedTheme) updateSharedTheme(newSharedTheme)
Intent().apply {
action = MyContentProvider.SHARED_THEME_UPDATED
sendBroadcast(this)
}
} }
baseConfig.isUsingSharedTheme = curSelectedThemeId == THEME_SHARED baseConfig.isUsingSharedTheme = curSelectedThemeId == THEME_SHARED
hasUnsavedChanges = false hasUnsavedChanges = false
finish() if (finishAfterSave) {
finish()
}
} }
private fun resetColors() { private fun resetColors() {
@ -308,8 +316,14 @@ class CustomizationActivity : BaseSimpleActivity() {
ConfirmationDialog(this, "", R.string.share_colors_success, R.string.ok, 0) { ConfirmationDialog(this, "", R.string.share_colors_success, R.string.ok, 0) {
baseConfig.wasSharedThemeEverActivated = true baseConfig.wasSharedThemeEverActivated = true
apply_to_all_holder.beGone() apply_to_all_holder.beGone()
predefinedThemes.put(THEME_SHARED, MyTheme(R.string.shared, 0, 0, 0))
updateColorTheme(THEME_SHARED) updateColorTheme(THEME_SHARED)
saveChanges(false)
predefinedThemes.put(THEME_SHARED, MyTheme(R.string.shared, 0, 0, 0))
Intent().apply {
action = MyContentProvider.SHARED_THEME_ACTIVATED
sendBroadcast(this)
}
} }
} else { } else {
PurchaseThankYouDialog(this) PurchaseThankYouDialog(this)

View file

@ -2,8 +2,6 @@ package com.simplemobiletools.commons.extensions
import android.Manifest import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.ContentUris import android.content.ContentUris
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager

View file

@ -116,4 +116,8 @@ open class BaseConfig(val context: Context) {
var wasCustomThemeSwitchDescriptionShown: Boolean var wasCustomThemeSwitchDescriptionShown: Boolean
get() = prefs.getBoolean(WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN, false) get() = prefs.getBoolean(WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN, false)
set(wasCustomThemeSwitchDescriptionShown) = prefs.edit().putBoolean(WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN, wasCustomThemeSwitchDescriptionShown).apply() set(wasCustomThemeSwitchDescriptionShown) = prefs.edit().putBoolean(WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN, wasCustomThemeSwitchDescriptionShown).apply()
var wasSharedThemeForced: Boolean
get() = prefs.getBoolean(WAS_SHARED_THEME_FORCED, false)
set(wasSharedThemeForced) = prefs.edit().putBoolean(WAS_SHARED_THEME_FORCED, wasSharedThemeForced).apply()
} }

View file

@ -32,6 +32,7 @@ val USE_ENGLISH = "use_english"
val WAS_USE_ENGLISH_TOGGLED = "was_use_english_toggled" val WAS_USE_ENGLISH_TOGGLED = "was_use_english_toggled"
val WAS_SHARED_THEME_EVER_ACTIVATED = "was_shared_theme_ever_activated" val WAS_SHARED_THEME_EVER_ACTIVATED = "was_shared_theme_ever_activated"
val IS_USING_SHARED_THEME = "is_using_shared_theme" val IS_USING_SHARED_THEME = "is_using_shared_theme"
val WAS_SHARED_THEME_FORCED = "was_shared_theme_forced"
val WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN = "was_custom_theme_switch_description_shown" val WAS_CUSTOM_THEME_SWITCH_DESCRIPTION_SHOWN = "was_custom_theme_switch_description_shown"
// licenses // licenses

View file

@ -8,6 +8,8 @@ class MyContentProvider {
companion object { companion object {
val AUTHORITY = "com.simplemobiletools.commons.provider" val AUTHORITY = "com.simplemobiletools.commons.provider"
val CONTENT_URI = Uri.parse("content://$AUTHORITY/themes") val CONTENT_URI = Uri.parse("content://$AUTHORITY/themes")
val SHARED_THEME_ACTIVATED = "com.simplemobiletools.commons.SHARED_THEME_ACTIVATED"
val SHARED_THEME_UPDATED = "com.simplemobiletools.commons.SHARED_THEME_UPDATED"
val COL_ID = "_id" val COL_ID = "_id"
val COL_TEXT_COLOR = "text_color" val COL_TEXT_COLOR = "text_color"

View file

@ -0,0 +1,38 @@
package com.simplemobiletools.commons.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.getSharedTheme
import com.simplemobiletools.commons.helpers.MyContentProvider
class SharedThemeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == MyContentProvider.SHARED_THEME_ACTIVATED) {
context.baseConfig.apply {
if (!wasSharedThemeForced) {
wasSharedThemeForced = true
isUsingSharedTheme = true
wasSharedThemeEverActivated = true
context.getSharedTheme {
textColor = it.textColor
backgroundColor = it.backgroundColor
primaryColor = it.primaryColor
}
}
}
} else if (intent.action == MyContentProvider.SHARED_THEME_UPDATED) {
context.baseConfig.apply {
if (isUsingSharedTheme) {
context.getSharedTheme {
textColor = it.textColor
backgroundColor = it.backgroundColor
primaryColor = it.primaryColor
}
}
}
}
}
}