feat: currently airing widget
This commit is contained in:
parent
95cddbd409
commit
df23b2f62f
15 changed files with 446 additions and 182 deletions
|
@ -1,12 +1,24 @@
|
|||
package ani.dantotsu.widgets
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.BitmapShader
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Shader
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.RemoteViewsService
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.connections.anilist.Anilist
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.util.Logger
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.io.InputStream
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
@ -14,59 +26,47 @@ import java.net.URL
|
|||
class CurrentlyAiringRemoteViewsFactory(private val context: Context) :
|
||||
RemoteViewsService.RemoteViewsFactory {
|
||||
private var widgetItems = mutableListOf<WidgetItem>()
|
||||
private var refreshing = false
|
||||
private val prefs =
|
||||
context.getSharedPreferences(CurrentlyAiringWidget.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
override fun onCreate() {
|
||||
// 4 items for testing
|
||||
widgetItems.clear()
|
||||
Logger.log("CurrentlyAiringRemoteViewsFactory onCreate")
|
||||
widgetItems = List(4) {
|
||||
WidgetItem(
|
||||
"Show $it",
|
||||
"$it days $it hours $it minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
}.toMutableList()
|
||||
fillWidgetItems()
|
||||
}
|
||||
|
||||
private fun timeUntil(timeUntil: Long): String {
|
||||
val days = timeUntil / (1000 * 60 * 60 * 24)
|
||||
val hours = (timeUntil % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
|
||||
val minutes = ((timeUntil % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60)
|
||||
return "$days days $hours hours $minutes minutes"
|
||||
}
|
||||
|
||||
override fun onDataSetChanged() {
|
||||
// 4 items for testing
|
||||
if (refreshing) return
|
||||
Logger.log("CurrentlyAiringRemoteViewsFactory onDataSetChanged")
|
||||
widgetItems.clear()
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
"Show 1",
|
||||
"1 day 2 hours 3 minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
)
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
"Show 2",
|
||||
"2 days 3 hours 4 minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
)
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
"Show 3",
|
||||
"3 days 4 hours 5 minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
)
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
"Show 4",
|
||||
"4 days 5 hours 6 minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
)
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
"Show 5",
|
||||
"5 days 6 hours 7 minutes",
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx14741-alxqoP4yx6WF.jpg"
|
||||
)
|
||||
)
|
||||
fillWidgetItems()
|
||||
|
||||
}
|
||||
|
||||
private fun fillWidgetItems() {
|
||||
refreshing = true
|
||||
val userId = PrefManager.getVal<String>(PrefName.AnilistUserId)
|
||||
runBlocking(Dispatchers.IO) {
|
||||
val upcoming = Anilist.query.getUpcomingAnime(userId)
|
||||
upcoming.forEach {
|
||||
widgetItems.add(
|
||||
WidgetItem(
|
||||
it.userPreferredName,
|
||||
timeUntil(it.timeUntilAiring ?: 0),
|
||||
it.cover ?: "",
|
||||
it.id
|
||||
)
|
||||
)
|
||||
}
|
||||
refreshing = false
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
@ -80,12 +80,20 @@ class CurrentlyAiringRemoteViewsFactory(private val context: Context) :
|
|||
override fun getViewAt(position: Int): RemoteViews {
|
||||
Logger.log("CurrentlyAiringRemoteViewsFactory getViewAt")
|
||||
val item = widgetItems[position]
|
||||
val titleTextColor = prefs.getInt(CurrentlyAiringWidget.PREF_TITLE_TEXT_COLOR, Color.WHITE)
|
||||
val countdownTextColor =
|
||||
prefs.getInt(CurrentlyAiringWidget.PREF_COUNTDOWN_TEXT_COLOR, Color.WHITE)
|
||||
val rv = RemoteViews(context.packageName, R.layout.item_currently_airing_widget).apply {
|
||||
setTextViewText(R.id.text_show_title, item.title)
|
||||
setTextViewText(R.id.text_show_countdown, item.countdown)
|
||||
setTextColor(R.id.text_show_title, titleTextColor)
|
||||
setTextColor(R.id.text_show_countdown, countdownTextColor)
|
||||
val bitmap = downloadImageAsBitmap(item.image)
|
||||
//setImageViewUri(R.id.image_show_icon, Uri.parse(item.image))
|
||||
setImageViewBitmap(R.id.image_show_icon, bitmap)
|
||||
val fillInIntent = Intent().apply {
|
||||
putExtra("mediaId", item.id)
|
||||
}
|
||||
setOnClickFillInIntent(R.id.widget_item, fillInIntent)
|
||||
}
|
||||
|
||||
return rv
|
||||
|
@ -108,16 +116,27 @@ class CurrentlyAiringRemoteViewsFactory(private val context: Context) :
|
|||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
// Handle the error according to your needs
|
||||
} finally {
|
||||
// Clean up resources
|
||||
inputStream?.close()
|
||||
urlConnection?.disconnect()
|
||||
}
|
||||
|
||||
return bitmap
|
||||
return bitmap?.let { roundCorners(it) }
|
||||
}
|
||||
|
||||
private fun roundCorners(bitmap: Bitmap): Bitmap {
|
||||
val cornerRadius = 20f
|
||||
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(output)
|
||||
val paint = Paint()
|
||||
paint.isAntiAlias = true
|
||||
paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
|
||||
val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())
|
||||
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
|
||||
override fun getLoadingView(): RemoteViews {
|
||||
return RemoteViews(context.packageName, R.layout.item_currently_airing_widget)
|
||||
}
|
||||
|
@ -135,4 +154,4 @@ class CurrentlyAiringRemoteViewsFactory(private val context: Context) :
|
|||
}
|
||||
}
|
||||
|
||||
data class WidgetItem(val title: String, val countdown: String, val image: String)
|
||||
data class WidgetItem(val title: String, val countdown: String, val image: String, val id: Int)
|
|
@ -12,7 +12,9 @@ import android.graphics.drawable.Drawable
|
|||
import android.graphics.drawable.GradientDrawable
|
||||
import android.net.Uri
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import ani.dantotsu.MainActivity
|
||||
import ani.dantotsu.R
|
||||
|
||||
/**
|
||||
|
@ -26,25 +28,15 @@ class CurrentlyAiringWidget : AppWidgetProvider() {
|
|||
appWidgetIds: IntArray
|
||||
) {
|
||||
appWidgetIds.forEach { appWidgetId ->
|
||||
val intent = Intent(context, CurrentlyAiringRemoteViewsService::class.java).apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
|
||||
}
|
||||
|
||||
val rv = RemoteViews(context.packageName, R.layout.currently_airing_widget).apply {
|
||||
setRemoteAdapter(R.id.widgetListView, intent)
|
||||
setEmptyView(R.id.widgetListView, R.id.empty_view)
|
||||
}
|
||||
|
||||
val rv = updateAppWidget(context, appWidgetId)
|
||||
appWidgetManager.updateAppWidget(appWidgetId, rv)
|
||||
}
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
// When the user deletes the widget, delete the preference associated with it.
|
||||
for (appWidgetId in appWidgetIds) {
|
||||
deleteTitlePref(context, appWidgetId)
|
||||
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().apply()
|
||||
}
|
||||
super.onDeleted(context, appWidgetIds)
|
||||
}
|
||||
|
@ -57,34 +49,65 @@ class CurrentlyAiringWidget : AppWidgetProvider() {
|
|||
super.onDisabled(context)
|
||||
}
|
||||
|
||||
|
||||
companion object {
|
||||
fun updateAppWidget(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetId: Int,
|
||||
color: Int
|
||||
) {
|
||||
// Create an intent to launch the configuration activity when the widget is clicked
|
||||
val intent = Intent(context, CurrentlyAiringWidgetConfigureActivity::class.java)
|
||||
val pendingIntent =
|
||||
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
): RemoteViews {
|
||||
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
val backgroundColor =
|
||||
prefs.getInt(PREF_BACKGROUND_COLOR, ContextCompat.getColor(context, R.color.theme))
|
||||
val backgroundFade = prefs.getInt(PREF_BACKGROUND_FADE, Color.GRAY)
|
||||
val titleTextColor = prefs.getInt(PREF_TITLE_TEXT_COLOR, Color.WHITE)
|
||||
val countdownTextColor = prefs.getInt(PREF_COUNTDOWN_TEXT_COLOR, Color.WHITE)
|
||||
|
||||
// Get the gradient drawable resource and update its start color with the user-selected color
|
||||
val intent = Intent(context, CurrentlyAiringRemoteViewsService::class.java).apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
data = Uri.parse(toUri(Intent.URI_INTENT_SCHEME))
|
||||
}
|
||||
val gradientDrawable = ResourcesCompat.getDrawable(
|
||||
context.resources,
|
||||
R.drawable.gradient_background,
|
||||
null
|
||||
) as GradientDrawable
|
||||
gradientDrawable.colors = intArrayOf(color, Color.GRAY) // End color is gray.
|
||||
gradientDrawable.colors = intArrayOf(backgroundColor, backgroundFade)
|
||||
|
||||
val intentTemplate = Intent(context, MainActivity::class.java)
|
||||
intentTemplate.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
intentTemplate.putExtra("fromWidget", true)
|
||||
|
||||
// Create the RemoteViews object and set the background
|
||||
val views = RemoteViews(context.packageName, R.layout.currently_airing_widget).apply {
|
||||
//setImageViewBitmap(R.id.backgroundView, convertDrawableToBitmap(gradientDrawable))
|
||||
//setOnClickPendingIntent(R.id.backgroundView, pendingIntent)
|
||||
setImageViewBitmap(R.id.backgroundView, convertDrawableToBitmap(gradientDrawable))
|
||||
setTextColor(R.id.text_show_title, titleTextColor)
|
||||
setTextColor(R.id.text_show_countdown, countdownTextColor)
|
||||
setTextColor(R.id.widgetTitle, titleTextColor)
|
||||
setRemoteAdapter(R.id.widgetListView, intent)
|
||||
setEmptyView(R.id.widgetListView, R.id.empty_view)
|
||||
setPendingIntentTemplate(
|
||||
R.id.widgetListView,
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
0,
|
||||
intentTemplate,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
)
|
||||
)
|
||||
setOnClickPendingIntent(
|
||||
R.id.logoView,
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
1,
|
||||
Intent(context, CurrentlyAiringWidgetConfigureActivity::class.java).apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
},
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// Instruct the widget manager to update the widget
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
return views
|
||||
|
||||
}
|
||||
|
||||
private fun convertDrawableToBitmap(drawable: Drawable): Bitmap {
|
||||
|
@ -94,6 +117,12 @@ class CurrentlyAiringWidget : AppWidgetProvider() {
|
|||
drawable.draw(canvas)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
const val PREFS_NAME = "ani.dantotsu.widgets.CurrentlyAiringWidget"
|
||||
const val PREF_BACKGROUND_COLOR = "background_color"
|
||||
const val PREF_BACKGROUND_FADE = "background_fade"
|
||||
const val PREF_TITLE_TEXT_COLOR = "title_text_color"
|
||||
const val PREF_COUNTDOWN_TEXT_COLOR = "countdown_text_color"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,11 +131,7 @@ internal fun updateAppWidget(
|
|||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetId: Int
|
||||
) {
|
||||
val widgetText = loadTitlePref(context, appWidgetId)
|
||||
// Construct the RemoteViews object
|
||||
val views = RemoteViews(context.packageName, R.layout.currently_airing_widget)
|
||||
views.setTextViewText(R.id.appwidget_text, widgetText)
|
||||
|
||||
// Instruct the widget manager to update the widget
|
||||
val views = CurrentlyAiringWidget.updateAppWidget(context, appWidgetId)
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widgetListView)
|
||||
}
|
|
@ -1,42 +1,36 @@
|
|||
package ani.dantotsu.widgets
|
||||
|
||||
import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.EditText
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.databinding.CurrentlyAiringWidgetConfigureBinding
|
||||
import ani.dantotsu.themes.ThemeManager
|
||||
import eltos.simpledialogfragment.SimpleDialog
|
||||
import eltos.simpledialogfragment.color.SimpleColorDialog
|
||||
|
||||
/**
|
||||
* The configuration screen for the [CurrentlyAiringWidget] AppWidget.
|
||||
*/
|
||||
class CurrentlyAiringWidgetConfigureActivity : Activity() {
|
||||
class CurrentlyAiringWidgetConfigureActivity : AppCompatActivity(),
|
||||
SimpleDialog.OnDialogResultListener {
|
||||
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
private lateinit var appWidgetText: EditText
|
||||
|
||||
private var onClickListener = View.OnClickListener {
|
||||
val context = this@CurrentlyAiringWidgetConfigureActivity
|
||||
|
||||
// When the button is clicked, store the string locally
|
||||
val widgetText = appWidgetText.text.toString()
|
||||
saveTitlePref(context, appWidgetId, widgetText)
|
||||
|
||||
// It is the responsibility of the configuration activity to update the app widget
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
//updateAppWidget(context, appWidgetManager, appWidgetId)
|
||||
|
||||
|
||||
CurrentlyAiringWidget.updateAppWidget(
|
||||
updateAppWidget(
|
||||
context,
|
||||
appWidgetManager,
|
||||
appWidgetId,
|
||||
-1
|
||||
)
|
||||
|
||||
// Make sure we pass back the original appWidgetId
|
||||
val resultValue = Intent()
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
setResult(RESULT_OK, resultValue)
|
||||
|
@ -45,21 +39,87 @@ class CurrentlyAiringWidgetConfigureActivity : Activity() {
|
|||
private lateinit var binding: CurrentlyAiringWidgetConfigureBinding
|
||||
|
||||
public override fun onCreate(icicle: Bundle?) {
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
super.onCreate(icicle)
|
||||
|
||||
// Set the result to CANCELED. This will cause the widget host to cancel
|
||||
// out of the widget placement if the user presses the back button.
|
||||
setResult(RESULT_CANCELED)
|
||||
|
||||
binding = CurrentlyAiringWidgetConfigureBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
val prefs = getSharedPreferences(CurrentlyAiringWidget.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
binding.topBackgroundButton.setOnClickListener {
|
||||
val tag = CurrentlyAiringWidget.PREF_BACKGROUND_COLOR
|
||||
SimpleColorDialog().title(R.string.custom_theme)
|
||||
.colorPreset(
|
||||
prefs.getInt(
|
||||
CurrentlyAiringWidget.PREF_BACKGROUND_COLOR,
|
||||
ContextCompat.getColor(this, R.color.theme)
|
||||
)
|
||||
)
|
||||
.colors(
|
||||
this@CurrentlyAiringWidgetConfigureActivity,
|
||||
SimpleColorDialog.MATERIAL_COLOR_PALLET
|
||||
)
|
||||
.allowCustom(true)
|
||||
.showOutline(0x46000000)
|
||||
.gridNumColumn(5)
|
||||
.choiceMode(SimpleColorDialog.SINGLE_CHOICE)
|
||||
.neg()
|
||||
.show(this@CurrentlyAiringWidgetConfigureActivity, tag)
|
||||
}
|
||||
binding.bottomBackgroundButton.setOnClickListener {
|
||||
val tag = CurrentlyAiringWidget.PREF_BACKGROUND_FADE
|
||||
SimpleColorDialog().title(R.string.custom_theme)
|
||||
.colorPreset(prefs.getInt(CurrentlyAiringWidget.PREF_BACKGROUND_FADE, Color.GRAY))
|
||||
.colors(
|
||||
this@CurrentlyAiringWidgetConfigureActivity,
|
||||
SimpleColorDialog.MATERIAL_COLOR_PALLET
|
||||
)
|
||||
.allowCustom(true)
|
||||
.showOutline(0x46000000)
|
||||
.gridNumColumn(5)
|
||||
.choiceMode(SimpleColorDialog.SINGLE_CHOICE)
|
||||
.neg()
|
||||
.show(this@CurrentlyAiringWidgetConfigureActivity, tag)
|
||||
}
|
||||
binding.titleColorButton.setOnClickListener {
|
||||
val tag = CurrentlyAiringWidget.PREF_TITLE_TEXT_COLOR
|
||||
SimpleColorDialog().title(R.string.custom_theme)
|
||||
.colorPreset(prefs.getInt(CurrentlyAiringWidget.PREF_TITLE_TEXT_COLOR, Color.WHITE))
|
||||
.colors(
|
||||
this@CurrentlyAiringWidgetConfigureActivity,
|
||||
SimpleColorDialog.MATERIAL_COLOR_PALLET
|
||||
)
|
||||
.allowCustom(true)
|
||||
.showOutline(0x46000000)
|
||||
.gridNumColumn(5)
|
||||
.choiceMode(SimpleColorDialog.SINGLE_CHOICE)
|
||||
.neg()
|
||||
.show(this@CurrentlyAiringWidgetConfigureActivity, tag)
|
||||
}
|
||||
binding.countdownColorButton.setOnClickListener {
|
||||
val tag = CurrentlyAiringWidget.PREF_COUNTDOWN_TEXT_COLOR
|
||||
SimpleColorDialog().title(R.string.custom_theme)
|
||||
.colorPreset(
|
||||
prefs.getInt(
|
||||
CurrentlyAiringWidget.PREF_COUNTDOWN_TEXT_COLOR,
|
||||
Color.WHITE
|
||||
)
|
||||
)
|
||||
.colors(
|
||||
this@CurrentlyAiringWidgetConfigureActivity,
|
||||
SimpleColorDialog.MATERIAL_COLOR_PALLET
|
||||
)
|
||||
.allowCustom(true)
|
||||
.showOutline(0x46000000)
|
||||
.gridNumColumn(5)
|
||||
.choiceMode(SimpleColorDialog.SINGLE_CHOICE)
|
||||
.neg()
|
||||
.show(this@CurrentlyAiringWidgetConfigureActivity, tag)
|
||||
}
|
||||
|
||||
appWidgetText = binding.appwidgetText
|
||||
binding.addButton.setOnClickListener(onClickListener)
|
||||
|
||||
// Find the widget id from the intent.
|
||||
val intent = intent
|
||||
val extras = intent.extras
|
||||
if (extras != null) {
|
||||
|
@ -68,43 +128,66 @@ class CurrentlyAiringWidgetConfigureActivity : Activity() {
|
|||
)
|
||||
}
|
||||
|
||||
// If this activity was started with an intent without an app widget ID, finish with an error.
|
||||
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
appWidgetText.setText(
|
||||
loadTitlePref(
|
||||
this@CurrentlyAiringWidgetConfigureActivity,
|
||||
appWidgetId
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
override fun onResult(dialogTag: String, which: Int, extras: Bundle): Boolean {
|
||||
if (which == SimpleDialog.OnDialogResultListener.BUTTON_POSITIVE) {
|
||||
when (dialogTag) {
|
||||
CurrentlyAiringWidget.PREF_BACKGROUND_COLOR -> {
|
||||
getSharedPreferences(
|
||||
CurrentlyAiringWidget.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putInt(
|
||||
CurrentlyAiringWidget.PREF_BACKGROUND_COLOR,
|
||||
extras.getInt(SimpleColorDialog.COLOR)
|
||||
)
|
||||
.apply()
|
||||
}
|
||||
|
||||
private const val PREFS_NAME = "ani.dantotsu.parsers.CurrentlyAiringWidget"
|
||||
private const val PREF_PREFIX_KEY = "appwidget_"
|
||||
CurrentlyAiringWidget.PREF_BACKGROUND_FADE -> {
|
||||
getSharedPreferences(
|
||||
CurrentlyAiringWidget.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putInt(
|
||||
CurrentlyAiringWidget.PREF_BACKGROUND_FADE,
|
||||
extras.getInt(SimpleColorDialog.COLOR)
|
||||
)
|
||||
.apply()
|
||||
}
|
||||
|
||||
// Write the prefix to the SharedPreferences object for this widget
|
||||
internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String) {
|
||||
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
|
||||
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text)
|
||||
prefs.apply()
|
||||
}
|
||||
CurrentlyAiringWidget.PREF_TITLE_TEXT_COLOR -> {
|
||||
getSharedPreferences(
|
||||
CurrentlyAiringWidget.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putInt(
|
||||
CurrentlyAiringWidget.PREF_TITLE_TEXT_COLOR,
|
||||
extras.getInt(SimpleColorDialog.COLOR)
|
||||
)
|
||||
.apply()
|
||||
}
|
||||
|
||||
// Read the prefix from the SharedPreferences object for this widget.
|
||||
// If there is no preference saved, get the default from a resource
|
||||
internal fun loadTitlePref(context: Context, appWidgetId: Int): String {
|
||||
val prefs = context.getSharedPreferences(PREFS_NAME, 0)
|
||||
val titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null)
|
||||
return titleValue ?: context.getString(R.string.appwidget_text)
|
||||
}
|
||||
CurrentlyAiringWidget.PREF_COUNTDOWN_TEXT_COLOR -> {
|
||||
getSharedPreferences(
|
||||
CurrentlyAiringWidget.PREFS_NAME,
|
||||
Context.MODE_PRIVATE
|
||||
).edit()
|
||||
.putInt(
|
||||
CurrentlyAiringWidget.PREF_COUNTDOWN_TEXT_COLOR,
|
||||
extras.getInt(SimpleColorDialog.COLOR)
|
||||
)
|
||||
.apply()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
internal fun deleteTitlePref(context: Context, appWidgetId: Int) {
|
||||
val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit()
|
||||
prefs.remove(PREF_PREFIX_KEY + appWidgetId)
|
||||
prefs.apply()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue