Compare commits

...

5 Commits

Author SHA1 Message Date
fc402fd81b Minor fix to BarChart 2022-09-03 20:26:21 -05:00
f7c6bc716c Fix ListHabitsScreen.showColorPicker 2022-09-03 19:52:13 -05:00
2535347d5a Always update widgets on launch
Helps mitigate situations where the widgets mysteriously fail to render.
The user can always force a refresh by opening the app.
2022-09-03 19:45:31 -05:00
13af054214 Use different request codes for showNumberPicker pendingIntent
Fixes #1468
2022-09-03 18:46:52 -05:00
fcbb586e80 Use pending intent templates on StackWidget
Fixes #1463
2022-09-03 17:51:55 -05:00
7 changed files with 123 additions and 9 deletions

View File

@@ -85,7 +85,6 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
Thread.setDefaultUncaughtExceptionHandler(BaseExceptionHandler(this)) Thread.setDefaultUncaughtExceptionHandler(BaseExceptionHandler(this))
component.listHabitsBehavior.onStartup() component.listHabitsBehavior.onStartup()
setContentView(rootView) setContentView(rootView)
parseIntents()
} }
override fun onPause() { override fun onPause() {
@@ -103,13 +102,15 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
taskRunner.run { taskRunner.run {
try { try {
AutoBackup(this@ListHabitsActivity).run() AutoBackup(this@ListHabitsActivity).run()
appComponent.widgetUpdater.updateWidgets()
} catch (e: Exception) { } catch (e: Exception) {
Log.e("ListHabitActivity", "AutoBackup task failed", e) Log.e("ListHabitActivity", "TaskRunner failed", e)
} }
} }
if (prefs.theme == THEME_DARK && prefs.isPureBlackEnabled != pureBlack) { if (prefs.theme == THEME_DARK && prefs.isPureBlackEnabled != pureBlack) {
restartWithFade(ListHabitsActivity::class.java) restartWithFade(ListHabitsActivity::class.java)
} }
parseIntents()
super.onResume() super.onResume()
} }
@@ -129,6 +130,7 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
} }
private fun parseIntents() { private fun parseIntents() {
if (intent == null) return
if (intent.action == ACTION_EDIT) { if (intent.action == ACTION_EDIT) {
val habitId = intent.extras?.getLong("habit") val habitId = intent.extras?.getLong("habit")
val timestamp = intent.extras?.getLong("timestamp") val timestamp = intent.extras?.getLong("timestamp")
@@ -137,6 +139,12 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
component.listHabitsBehavior.onEdit(habit, Timestamp(timestamp)) component.listHabitsBehavior.onEdit(habit, Timestamp(timestamp))
} }
} }
intent = null
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
} }
companion object { companion object {

View File

@@ -225,7 +225,7 @@ class ListHabitsScreen
override fun showColorPicker(defaultColor: PaletteColor, callback: OnColorPickedCallback) { override fun showColorPicker(defaultColor: PaletteColor, callback: OnColorPickedCallback) {
val picker = colorPickerFactory.create(defaultColor, themeSwitcher.currentTheme!!) val picker = colorPickerFactory.create(defaultColor, themeSwitcher.currentTheme!!)
picker.setListener(callback) picker.setListener(callback)
picker.dialog?.dismissCurrentAndShow() picker.dismissCurrentAndShow(activity.supportFragmentManager, "picker")
} }
override fun showNumberPopup( override fun showNumberPopup(

View File

@@ -21,13 +21,16 @@ package org.isoron.uhabits.intents
import android.app.PendingIntent import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_MUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.app.PendingIntent.getActivity import android.app.PendingIntent.getActivity
import android.app.PendingIntent.getBroadcast import android.app.PendingIntent.getBroadcast
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import org.isoron.uhabits.activities.habits.list.ListHabitsActivity import org.isoron.uhabits.activities.habits.list.ListHabitsActivity
import org.isoron.uhabits.activities.habits.show.ShowHabitActivity
import org.isoron.uhabits.core.AppScope import org.isoron.uhabits.core.AppScope
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.Timestamp import org.isoron.uhabits.core.models.Timestamp
@@ -89,6 +92,20 @@ class PendingIntentFactory
) )
.getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)!! .getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)!!
fun showHabitTemplate(): PendingIntent {
return getActivity(
context,
0,
Intent(context, ShowHabitActivity::class.java),
getIntentTemplateFlags()
)
}
fun showHabitFillIn(habit: Habit) =
Intent().apply {
data = Uri.parse(habit.uriString)
}
fun showReminder( fun showReminder(
habit: Habit, habit: Habit,
reminderTime: Long?, reminderTime: Long?,
@@ -142,7 +159,7 @@ class PendingIntentFactory
fun showNumberPicker(habit: Habit, timestamp: Timestamp): PendingIntent? { fun showNumberPicker(habit: Habit, timestamp: Timestamp): PendingIntent? {
return getActivity( return getActivity(
context, context,
0, (habit.id!! % Integer.MAX_VALUE).toInt() + 1,
Intent(context, ListHabitsActivity::class.java).apply { Intent(context, ListHabitsActivity::class.java).apply {
action = ListHabitsActivity.ACTION_EDIT action = ListHabitsActivity.ACTION_EDIT
putExtra("habit", habit.id) putExtra("habit", habit.id)
@@ -151,4 +168,43 @@ class PendingIntentFactory
FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
) )
} }
fun showNumberPickerTemplate(): PendingIntent {
return getActivity(
context,
1,
Intent(context, ListHabitsActivity::class.java).apply {
action = ListHabitsActivity.ACTION_EDIT
},
getIntentTemplateFlags()
)
}
fun showNumberPickerFillIn(habit: Habit, timestamp: Timestamp) = Intent().apply {
putExtra("habit", habit.id)
putExtra("timestamp", timestamp.unixTime)
}
private fun getIntentTemplateFlags(): Int {
var flags = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
flags = flags or FLAG_MUTABLE
}
return flags
}
fun toggleCheckmarkTemplate(): PendingIntent =
getBroadcast(
context,
2,
Intent(context, WidgetReceiver::class.java).apply {
action = WidgetReceiver.ACTION_TOGGLE_REPETITION
},
getIntentTemplateFlags()
)
fun toggleCheckmarkFillIn(habit: Habit, timestamp: Timestamp) = Intent().apply {
data = Uri.parse(habit.uriString)
putExtra("timestamp", timestamp.unixTime)
}
} }

View File

@@ -73,6 +73,10 @@ class StackWidget(
StackWidgetType.getStackWidgetAdapterViewId(widgetType), StackWidgetType.getStackWidgetAdapterViewId(widgetType),
StackWidgetType.getStackWidgetEmptyViewId(widgetType) StackWidgetType.getStackWidgetEmptyViewId(widgetType)
) )
remoteViews.setPendingIntentTemplate(
StackWidgetType.getStackWidgetAdapterViewId(widgetType),
StackWidgetType.getPendingIntentTemplate(pendingIntentFactory, widgetType, habits)
)
return remoteViews return remoteViews
} }
} }

View File

@@ -29,11 +29,14 @@ import android.widget.RemoteViewsService
import android.widget.RemoteViewsService.RemoteViewsFactory import android.widget.RemoteViewsService.RemoteViewsFactory
import org.isoron.platform.utils.StringUtils.Companion.splitLongs import org.isoron.platform.utils.StringUtils.Companion.splitLongs
import org.isoron.uhabits.HabitsApplication import org.isoron.uhabits.HabitsApplication
import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitNotFoundException import org.isoron.uhabits.core.models.HabitNotFoundException
import org.isoron.uhabits.core.preferences.Preferences import org.isoron.uhabits.core.preferences.Preferences
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.isoron.uhabits.intents.IntentFactory
import org.isoron.uhabits.intents.PendingIntentFactory
import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels
import java.util.ArrayList
class StackWidgetService : RemoteViewsService() { class StackWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
@@ -138,14 +141,18 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
val options = AppWidgetManager.getInstance(context).getAppWidgetOptions(widgetId) val options = AppWidgetManager.getInstance(context).getAppWidgetOptions(widgetId)
val newRemoteViews = ArrayList<RemoteViews>() val newRemoteViews = ArrayList<RemoteViews>()
if (Looper.myLooper() == null) Looper.prepare() if (Looper.myLooper() == null) Looper.prepare()
for (id in habitIds) { val habits = habitIds.map { habitList.getById(it) ?: throw HabitNotFoundException() }
val h = habitList.getById(id) ?: throw HabitNotFoundException() for (h in habits) {
val widget = constructWidget(h, prefs) val widget = constructWidget(h, prefs)
widget.setDimensions(getDimensionsFromOptions(context, options)) widget.setDimensions(getDimensionsFromOptions(context, options))
val landscapeViews = widget.landscapeRemoteViews val landscapeViews = widget.landscapeRemoteViews
val portraitViews = widget.portraitRemoteViews val portraitViews = widget.portraitRemoteViews
val factory = PendingIntentFactory(context, IntentFactory())
val intent = StackWidgetType.getIntentFillIn(factory, widgetType, h, habits, getToday())
landscapeViews.setOnClickFillInIntent(R.id.button, intent)
portraitViews.setOnClickFillInIntent(R.id.button, intent)
newRemoteViews.add(RemoteViews(landscapeViews, portraitViews)) newRemoteViews.add(RemoteViews(landscapeViews, portraitViews))
Log.i("StackRemoteViewsFactory", "onDataSetChanged constructed widget $id") Log.i("StackRemoteViewsFactory", "onDataSetChanged constructed widget ${h.id}")
} }
remoteViews = newRemoteViews remoteViews = newRemoteViews
Log.i("StackRemoteViewsFactory", "onDataSetChanged ended") Log.i("StackRemoteViewsFactory", "onDataSetChanged ended")

View File

@@ -18,7 +18,12 @@
*/ */
package org.isoron.uhabits.widgets package org.isoron.uhabits.widgets
import android.app.PendingIntent
import android.content.Intent
import org.isoron.uhabits.R import org.isoron.uhabits.R
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.intents.PendingIntentFactory
import java.lang.IllegalStateException import java.lang.IllegalStateException
enum class StackWidgetType(val value: Int) { enum class StackWidgetType(val value: Int) {
@@ -73,5 +78,39 @@ enum class StackWidgetType(val value: Int) {
else -> throw IllegalStateException() else -> throw IllegalStateException()
} }
} }
fun getPendingIntentTemplate(
factory: PendingIntentFactory,
widgetType: StackWidgetType,
habits: List<Habit>
): PendingIntent {
val containsNumerical = habits.any { it.isNumerical }
return when (widgetType) {
CHECKMARK -> if (containsNumerical) {
factory.showNumberPickerTemplate()
} else {
factory.toggleCheckmarkTemplate()
}
FREQUENCY, SCORE, HISTORY, STREAKS, TARGET -> factory.showHabitTemplate()
}
}
fun getIntentFillIn(
factory: PendingIntentFactory,
widgetType: StackWidgetType,
habit: Habit,
allHabitsInStackWidget: List<Habit>,
timestamp: Timestamp
): Intent {
val containsNumerical = allHabitsInStackWidget.any { it.isNumerical }
return when (widgetType) {
CHECKMARK -> if (containsNumerical) {
factory.showNumberPickerFillIn(habit, timestamp)
} else {
factory.toggleCheckmarkFillIn(habit, timestamp)
}
FREQUENCY, SCORE, HISTORY, STREAKS, TARGET -> factory.showHabitFillIn(habit)
}
}
} }
} }

View File

@@ -92,7 +92,7 @@ class BarChart(
val r = round(barWidth * 0.15) val r = round(barWidth * 0.15)
if (2 * r < barHeight) { if (2 * r < barHeight) {
canvas.fillRect(x, y + r, barWidth, barHeight - r) canvas.fillRect(x, y + r, barWidth, barHeight - r)
canvas.fillRect(x + r, y, barWidth - 2 * r, r) canvas.fillRect(x + r, y, barWidth - 2 * r, r + 1)
canvas.fillCircle(x + r, y + r, r) canvas.fillCircle(x + r, y + r, r)
canvas.fillCircle(x + barWidth - r, y + r, r) canvas.fillCircle(x + barWidth - r, y + r, r)
} else { } else {