forked from github/szkolny
[Widgets/LuckyNumber] Implement a widget. Not knowing if it works or not, sorry.
This commit is contained in:
parent
1abb9ac378
commit
19c446d267
@ -98,7 +98,7 @@
|
||||
<service android:name=".ui.widgets.notifications.WidgetNotificationsService"
|
||||
android:permission="android.permission.BIND_REMOTEVIEWS" />
|
||||
<!-- LUCKY NUMBER -->
|
||||
<receiver android:name=".widgets.luckynumber.WidgetLuckyNumber"
|
||||
<receiver android:name=".ui.widgets.luckynumber.WidgetLuckyNumberProvider"
|
||||
android:label="@string/widget_lucky_number_title">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
|
@ -27,9 +27,9 @@ import pl.szczodrzynski.edziennik.App;
|
||||
import pl.szczodrzynski.edziennik.R;
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Profile;
|
||||
import pl.szczodrzynski.edziennik.databinding.DialogWidgetConfigBinding;
|
||||
import pl.szczodrzynski.edziennik.ui.widgets.luckynumber.WidgetLuckyNumberProvider;
|
||||
import pl.szczodrzynski.edziennik.ui.widgets.notifications.WidgetNotificationsProvider;
|
||||
import pl.szczodrzynski.edziennik.ui.widgets.timetable.WidgetTimetableProvider;
|
||||
import pl.szczodrzynski.edziennik.widgets.luckynumber.WidgetLuckyNumber;
|
||||
|
||||
import static pl.szczodrzynski.edziennik.ExtensionsKt.filterOutArchived;
|
||||
|
||||
@ -150,7 +150,7 @@ public class WidgetConfigActivity extends Activity {
|
||||
refreshIntent = new Intent(app.getContext(), WidgetNotificationsProvider.class);
|
||||
break;
|
||||
case WIDGET_LUCKY_NUMBER:
|
||||
refreshIntent = new Intent(app.getContext(), WidgetLuckyNumber.class);
|
||||
refreshIntent = new Intent(app.getContext(), WidgetLuckyNumberProvider.class);
|
||||
break;
|
||||
}
|
||||
refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
||||
|
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) Kuba Szczodrzyński 2020-1-7.
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.ui.widgets.luckynumber
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import pl.szczodrzynski.edziennik.App
|
||||
import pl.szczodrzynski.edziennik.MainActivity
|
||||
import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.getJsonObject
|
||||
import pl.szczodrzynski.edziennik.ui.widgets.WidgetConfig
|
||||
import pl.szczodrzynski.edziennik.utils.Utils
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
|
||||
class WidgetLuckyNumberProvider : AppWidgetProvider() {
|
||||
companion object {
|
||||
private const val TAG = "WidgetLuckyNumberProvider"
|
||||
}
|
||||
|
||||
private fun getRemoteViews(app: App, config: WidgetConfig): RemoteViews {
|
||||
return if (config.bigStyle) {
|
||||
RemoteViews(app.packageName, if (config.darkTheme) R.layout.widget_lucky_number_dark_big else R.layout.widget_lucky_number_big)
|
||||
} else {
|
||||
RemoteViews(app.packageName, if (config.darkTheme) R.layout.widget_lucky_number_dark else R.layout.widget_lucky_number)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
val app = context.applicationContext as App
|
||||
val widgetConfigs = app.config.widgetConfigs
|
||||
for (appWidgetId in appWidgetIds) {
|
||||
val config = widgetConfigs.getJsonObject(appWidgetId.toString())?.let { app.gson.fromJson(it, WidgetConfig::class.java) } ?: continue
|
||||
|
||||
val views = getRemoteViews(app, config)
|
||||
|
||||
val today = Date.getToday()
|
||||
val todayValue = today.value
|
||||
|
||||
val tomorrow = Date.getToday().stepForward(0, 0, 1)
|
||||
val tomorrowValue = tomorrow.value
|
||||
|
||||
val profile = app.db.profileDao().getByIdNow(config.profileId)
|
||||
val luckyNumber = app.db.luckyNumberDao().getNearestFutureNow(config.profileId, todayValue)
|
||||
val isYours = luckyNumber?.number == profile?.studentNumber
|
||||
|
||||
var noNumberText = false
|
||||
|
||||
if (profile != null) {
|
||||
views.setTextViewText(R.id.widgetLuckyNumberProfileRight, profile.name)
|
||||
views.setTextViewText(R.id.widgetLuckyNumberProfileBottom, profile.name)
|
||||
}
|
||||
|
||||
if (profile == null || luckyNumber == null || luckyNumber.number == -1) {
|
||||
noNumberText = true
|
||||
views.setTextViewText(R.id.widgetLuckyNumberTextRight, null)
|
||||
views.setTextViewText(R.id.widgetLuckyNumberTextBottom, null)
|
||||
}
|
||||
else {
|
||||
views.setTextViewText(R.id.widgetLuckyNumberTextRight, luckyNumber.number.toString())
|
||||
views.setTextViewText(R.id.widgetLuckyNumberTextBottom, luckyNumber.number.toString())
|
||||
}
|
||||
|
||||
val drawableRes = when {
|
||||
luckyNumber == null || luckyNumber.number == -1 -> R.drawable.emoji_sad
|
||||
isYours -> R.drawable.emoji_glasses
|
||||
!isYours -> R.drawable.emoji_smiling
|
||||
else -> R.drawable.emoji_no_face
|
||||
}
|
||||
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextRightLayout, if (noNumberText) View.GONE else View.VISIBLE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextBottomLayout, if (noNumberText) View.GONE else View.VISIBLE)
|
||||
views.setImageViewResource(R.id.widgetLuckyNumberIcon, drawableRes)
|
||||
|
||||
updateLayout(config, views, appWidgetManager, appWidgetId)
|
||||
|
||||
val openIntent = Intent(context, MainActivity::class.java)
|
||||
openIntent.action = Intent.ACTION_MAIN
|
||||
openIntent.putExtra("fragmentId", MainActivity.DRAWER_ITEM_HOME)
|
||||
val openPendingIntent = PendingIntent.getActivity(context, 0, openIntent, 0)
|
||||
views.setOnClickPendingIntent(R.id.widgetLuckyNumberRoot, openPendingIntent)
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLayout(config: WidgetConfig, views: RemoteViews, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
|
||||
val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
|
||||
val minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
|
||||
val minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
|
||||
val width = Utils.getCellsForSize(minWidth)
|
||||
val height = Utils.getCellsForSize(minHeight)
|
||||
when (width) {
|
||||
1 -> {
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, View.GONE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, View.GONE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.GONE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.VISIBLE)
|
||||
}
|
||||
2 -> {
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, View.GONE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.GONE)
|
||||
}
|
||||
else -> {
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileRight, if (config.bigStyle) View.GONE else View.VISIBLE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberProfileBottom, if (config.bigStyle) View.VISIBLE else View.GONE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextRight, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.widgetLuckyNumberTextBottom, View.GONE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle?) {
|
||||
val app = context.applicationContext as App
|
||||
val widgetConfigs = app.config.widgetConfigs
|
||||
val config = widgetConfigs.getJsonObject(appWidgetId.toString())?.let { app.gson.fromJson(it, WidgetConfig::class.java) } ?: return
|
||||
val views: RemoteViews = getRemoteViews(app, config)
|
||||
updateLayout(config, views, appWidgetManager, appWidgetId)
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
val app = context.applicationContext as App
|
||||
val widgetConfigs = app.config.widgetConfigs
|
||||
appWidgetIds.forEach {
|
||||
widgetConfigs.remove(it.toString())
|
||||
}
|
||||
app.config.widgetConfigs = widgetConfigs
|
||||
}
|
||||
}
|
@ -6,7 +6,10 @@
|
||||
android:layout_height="match_parent"
|
||||
android:background="@drawable/widget_background"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
android:orientation="vertical"
|
||||
tools:layout_width="200dp"
|
||||
tools:layout_height="100dp"
|
||||
tools:layout_gravity="center">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
@ -18,7 +21,7 @@
|
||||
android:id="@+id/widgetLuckyNumberIcon"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
tools:srcCompat="@android:drawable/btn_star_big_on" />
|
||||
tools:srcCompat="@drawable/emoji_no_face" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
|
Loading…
x
Reference in New Issue
Block a user