137 lines
No EOL
5.3 KiB
Kotlin
137 lines
No EOL
5.3 KiB
Kotlin
package ani.dantotsu.widgets
|
|
|
|
import android.app.PendingIntent
|
|
import android.appwidget.AppWidgetManager
|
|
import android.appwidget.AppWidgetProvider
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.graphics.Bitmap
|
|
import android.graphics.Canvas
|
|
import android.graphics.Color
|
|
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
|
|
|
|
/**
|
|
* Implementation of App Widget functionality.
|
|
* App Widget Configuration implemented in [UpcomingWidgetConfigureActivity]
|
|
*/
|
|
class UpcomingWidget : AppWidgetProvider() {
|
|
override fun onUpdate(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetIds: IntArray
|
|
) {
|
|
appWidgetIds.forEach { appWidgetId ->
|
|
val rv = updateAppWidget(context, appWidgetId)
|
|
appWidgetManager.updateAppWidget(appWidgetId, rv)
|
|
}
|
|
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
|
}
|
|
|
|
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
|
for (appWidgetId in appWidgetIds) {
|
|
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().apply()
|
|
}
|
|
super.onDeleted(context, appWidgetIds)
|
|
}
|
|
|
|
override fun onEnabled(context: Context) {
|
|
super.onEnabled(context)
|
|
}
|
|
|
|
override fun onDisabled(context: Context) {
|
|
super.onDisabled(context)
|
|
}
|
|
|
|
|
|
companion object {
|
|
fun updateAppWidget(
|
|
context: Context,
|
|
appWidgetId: Int,
|
|
): 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)
|
|
|
|
val intent = Intent(context, UpcomingRemoteViewsService::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(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)
|
|
|
|
val views = RemoteViews(context.packageName, R.layout.upcoming_widget).apply {
|
|
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, UpcomingWidgetConfigureActivity::class.java).apply {
|
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
|
},
|
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
|
)
|
|
)
|
|
}
|
|
|
|
return views
|
|
|
|
}
|
|
|
|
private fun convertDrawableToBitmap(drawable: Drawable): Bitmap {
|
|
val bitmap = Bitmap.createBitmap(100, 300, Bitmap.Config.ARGB_8888)
|
|
val canvas = Canvas(bitmap)
|
|
drawable.setBounds(0, 0, canvas.width, canvas.height)
|
|
drawable.draw(canvas)
|
|
return bitmap
|
|
}
|
|
|
|
const val PREFS_NAME = "ani.dantotsu.widgets.UpcomingWidget"
|
|
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"
|
|
}
|
|
}
|
|
|
|
internal fun updateAppWidget(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetId: Int
|
|
) {
|
|
val views = UpcomingWidget.updateAppWidget(context, appWidgetId)
|
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widgetListView)
|
|
} |