Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/src/main/java/com/leanbitlab/lwidget/AwidgetProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.time.LocalDate
import java.util.concurrent.atomic.AtomicBoolean

enum class UpdateMode {
FULL, TICK, CALENDAR_ONLY, TASKS_ONLY, ALARM_ONLY, STEPS_ONLY
FULL, TICK, CALENDAR_ONLY, TASKS_ONLY, EVENTS_PAGE, ALARM_ONLY, STEPS_ONLY
}

class AwidgetProvider : AppWidgetProvider() {
Expand Down Expand Up @@ -112,6 +112,20 @@ class AwidgetProvider : AppWidgetProvider() {
BreezyWeatherFetcher.saveLatestWeatherData(context, weatherJson)
}

// Tap on the Events/Tasks switch: flip this widget's page and redraw just that block
if (intent.action == ACTION_TOGGLE_EVENTS_PAGE) {
val widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) return
runAsync("toggleEventsPage") {
val widgetPrefs = context.getSharedPreferences("com.leanbitlab.lwidget.PREFS_$widgetId", Context.MODE_PRIVATE)
val globalPrefs = context.getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE)
val current = FallbackPreferences(widgetPrefs, globalPrefs).getInt("events_page", 0)
widgetPrefs.edit().putInt("events_page", if (current == 1) 0 else 1).apply()
updateAppWidget(context, AppWidgetManager.getInstance(context), widgetId, UpdateMode.EVENTS_PAGE)
}
return
}

val isBootOrReplaced = intent.action in listOf(
Intent.ACTION_BOOT_COMPLETED,
Intent.ACTION_MY_PACKAGE_REPLACED,
Expand Down Expand Up @@ -213,6 +227,7 @@ class AwidgetProvider : AppWidgetProvider() {
)
}

const val ACTION_TOGGLE_EVENTS_PAGE = "com.leanbitlab.lwidget.ACTION_TOGGLE_EVENTS_PAGE"
const val ACTION_NOOP = "com.leanbitlab.lwidget.ACTION_NOOP"
/** Gadgetbridge weather broadcast sent by Breezy Weather; payload is the JSON extra. */
const val ACTION_GENERIC_WEATHER = "nodomain.freeyourgadget.gadgetbridge.ACTION_GENERIC_WEATHER"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal fun MainActivity.setupEventsAndTasksSections() {
isContent = true
)

// Mutual Exclusion: Events vs Tasks
// Events and Tasks can both be on: the widget then shows a small switch to flip between them
eventsSwitch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
Expand All @@ -120,10 +120,8 @@ internal fun MainActivity.setupEventsAndTasksSections() {
return@setOnCheckedChangeListener
}
if (checkLimit()) {
tasksSwitch.isChecked = false
prefs.edit().putBoolean("show_events", true).putBoolean("show_tasks", false).apply()
prefs.edit().putBoolean("show_events", true).apply()
updateFeatureRowVisibility(eventsSwitch, true, R.id.row_events_size, R.id.row_events_day_abbr, R.id.row_events_calendar_app)
updateFeatureRowVisibility(tasksSwitch, false, R.id.row_tasks_size)
updateWidget()
updateToggleAvailability()
checkAllPermissions()
Expand Down Expand Up @@ -157,10 +155,8 @@ internal fun MainActivity.setupEventsAndTasksSections() {
ActivityCompat.requestPermissions(this, arrayOf(AwidgetProvider.PERMISSION_READ_TASKS_ORG), 101)
}
if (checkLimit()) {
eventsSwitch.isChecked = false
prefs.edit().putBoolean("show_tasks", true).putBoolean("show_events", false).apply()
prefs.edit().putBoolean("show_tasks", true).apply()
updateFeatureRowVisibility(tasksSwitch, true, R.id.row_tasks_size)
updateFeatureRowVisibility(eventsSwitch, false, R.id.row_events_size, R.id.row_events_day_abbr, R.id.row_events_calendar_app)
updateWidget()
updateToggleAvailability()
checkAllPermissions()
Expand Down
100 changes: 75 additions & 25 deletions app/src/main/java/com/leanbitlab/lwidget/render/WidgetRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,67 @@ object WidgetRenderer {
return fallback
}

/**
* Which of the shared Events/Tasks block to show. With both on, [page] 0 shows events and 1 shows tasks;
* with only one on, that one always shows.
*/
internal fun eventsTasksSelection(showEvents: Boolean, showTasks: Boolean, page: Int): Pair<Boolean, Boolean> =
Pair(showEvents && (!showTasks || page != 1), showTasks && (!showEvents || page == 1))

/** Fills both pages of the Events/Tasks block, picks the visible one, and wires the switch and tap action. */
private fun applyEventsBlock(
context: Context, views: RemoteViews, appWidgetId: Int,
showEvents: Boolean, showTasks: Boolean, showEventsNow: Boolean, canSwitch: Boolean,
sizeEvents: Float, sizeTasks: Float,
primaryColor: WidgetColor, secondaryColor: WidgetColor, prefs: SharedPreferences
) {
views.setViewVisibility(R.id.events_container, if (showEvents || showTasks) View.VISIBLE else View.GONE)
if (showEvents) CalendarFetcher.loadCalendarEvents(context, views, sizeEvents, primaryColor, secondaryColor, prefs)
if (showTasks) TasksFetcher.loadTasks(context, views, sizeTasks, primaryColor)
// An absolute child index, so a replayed update always lands on the right page.
views.setDisplayedChild(R.id.events_flipper, if (showEventsNow) 0 else 1)

// One button pinned to the bottom of the widget; its arrow follows the page.
val showSwitch = canSwitch && appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
views.setViewVisibility(R.id.button_events_switch, if (showSwitch) View.VISIBLE else View.GONE)
if (showSwitch) {
applyEventsSwitchArrow(views, showEventsNow)
secondaryColor.applyColorFilter(views, R.id.button_events_switch)
val switchIntent = Intent(context, AwidgetProvider::class.java).apply {
action = AwidgetProvider.ACTION_TOGGLE_EVENTS_PAGE
data = android.net.Uri.parse("lwidget://widget/$appWidgetId/events_page")
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
}
views.setOnClickPendingIntent(
R.id.button_events_switch,
PendingIntent.getBroadcast(context, 12, switchIntent, PendingIntent.FLAG_IMMUTABLE)
)
}
applyEventsTap(context, views, showEventsNow.not() && showTasks)
}

/** Down arrow while Events show (Tasks are next), up arrow while Tasks show (back to Events). */
private fun applyEventsSwitchArrow(views: RemoteViews, eventsShowing: Boolean) {
views.setImageViewResource(
R.id.button_events_switch,
if (eventsShowing) R.drawable.ic_chevron_down else R.drawable.ic_chevron_up
)
}

/** Tapping the block opens Tasks.org while the tasks page is showing, otherwise it refreshes. */
private fun applyEventsTap(context: Context, views: RemoteViews, tasksPageShowing: Boolean) {
val tasksIntent = if (tasksPageShowing) getLaunchIntent(context, "org.tasks") else null
val tapIntent = if (tasksIntent != null) {
PendingIntent.getActivity(context, 11, tasksIntent, PendingIntent.FLAG_IMMUTABLE)
} else {
val refreshIntent = Intent(context, AwidgetProvider::class.java).apply {
action = SystemStatsFetcher.ACTION_BATTERY_UPDATE
}
PendingIntent.getBroadcast(context, 10, refreshIntent, PendingIntent.FLAG_IMMUTABLE)
}
views.setOnClickPendingIntent(R.id.events_container, tapIntent)
}

private fun getLayout(fontIdx: Int): Int {
return when (fontIdx) {
1 -> R.layout.widget_layout_serif
Expand Down Expand Up @@ -175,6 +236,10 @@ object WidgetRenderer {
}
val sizeTasks = prefs.getFloat("size_tasks", 14f)

// Events and Tasks share one block. When both are on, the saved page decides which one shows.
val (showEventsNow, showTasksNow) = eventsTasksSelection(showEvents, showTasks, prefs.getInt("events_page", 0))
val canSwitchEventsTasks = showEvents && showTasks

val fontStyle = prefs.getInt("font_style", 0)
val bgColorIdx = prefs.getInt("bg_color_idx", 0)
val bgOpacity = prefs.getFloat("bg_opacity", 85f)
Expand Down Expand Up @@ -227,6 +292,13 @@ object WidgetRenderer {
val primaryColor = ColorResolver.resolveWidgetColor(context, prefs, useDynamicColors, textColorPrimaryIdx, isPrimary = true, isLight = useLightTheme, followSystem = followSystem)
if (showTasks) TasksFetcher.loadTasks(context, taskViews, sizeTasks, primaryColor)
return taskViews
} else if (mode == UpdateMode.EVENTS_PAGE) {
// Only the page changes: both pages already hold their own lines.
val pageViews = RemoteViews(context.packageName, layoutId)
pageViews.setDisplayedChild(R.id.events_flipper, if (showEventsNow) 0 else 1)
applyEventsSwitchArrow(pageViews, showEventsNow)
applyEventsTap(context, pageViews, showTasksNow)
return pageViews
} else if (mode == UpdateMode.ALARM_ONLY) {
val alarmViews = RemoteViews(context.packageName, layoutId)
if (showNextAlarm) SystemStatsFetcher.loadNextAlarm(context, alarmViews, sizeNextAlarm, alarmColor, prefs, showDate || showWorldClock)
Expand Down Expand Up @@ -498,7 +570,7 @@ object WidgetRenderer {
val leftHasContent = (showTime || showWorldClock || showDate || showNextAlarm)
if (eventsVisible) {
val topMargin = if (leftHasContent) dpToPx(8f) else {
val size = if (showEvents) sizeEvents else sizeTasks
val size = if (showEventsNow) sizeEvents else sizeTasks
val intrinsicGap = size * 0.18f
maxOf(0, dpToPx(paddingVal - intrinsicGap))
}
Expand Down Expand Up @@ -594,13 +666,8 @@ object WidgetRenderer {
val dataPendingIntent = PendingIntent.getActivity(context, 4, dataIntent, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.text_data_usage, dataPendingIntent)

// Events OR Tasks
views.setViewVisibility(R.id.events_container, if (showEvents || showTasks) View.VISIBLE else View.GONE)
if (showEvents) {
CalendarFetcher.loadCalendarEvents(context, views, sizeEvents, primaryColor, secondaryColor, prefs)
} else if (showTasks) {
TasksFetcher.loadTasks(context, views, sizeTasks, primaryColor)
}
// Events or Tasks (one block; a small switch flips between them when both are on)
applyEventsBlock(context, views, appWidgetId, showEvents, showTasks, showEventsNow, canSwitchEventsTasks, sizeEvents, sizeTasks, primaryColor, secondaryColor, prefs)

// Next Alarm
views.setViewVisibility(R.id.layout_next_alarm, if (showNextAlarm) View.VISIBLE else View.GONE)
Expand All @@ -609,23 +676,6 @@ object WidgetRenderer {
}
views.setOnClickPendingIntent(R.id.layout_next_alarm, alarmPendingIntent)

val refreshIntent = Intent(context, AwidgetProvider::class.java).apply {
action = SystemStatsFetcher.ACTION_BATTERY_UPDATE
}
val refreshPendingIntent = PendingIntent.getBroadcast(context, 10, refreshIntent, PendingIntent.FLAG_IMMUTABLE)

if (showTasks) {
val tasksIntent = getLaunchIntent(context, "org.tasks")
if (tasksIntent != null) {
val tasksPendingIntent = PendingIntent.getActivity(context, 11, tasksIntent, PendingIntent.FLAG_IMMUTABLE)
views.setOnClickPendingIntent(R.id.events_container, tasksPendingIntent)
} else {
views.setOnClickPendingIntent(R.id.events_container, refreshPendingIntent)
}
} else {
views.setOnClickPendingIntent(R.id.events_container, refreshPendingIntent)
}

val settingsIntent = Intent(context, MainActivity::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
data = android.net.Uri.parse("lwidget://widget/$appWidgetId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ object TasksFetcher {

fun loadTasks(context: Context, views: RemoteViews, textSizeSp: Float, primaryColor: WidgetColor) {
val eventViews = listOf(
R.id.text_event_1, R.id.text_event_2, R.id.text_event_3,
R.id.text_event_4, R.id.text_event_5, R.id.text_event_6,
R.id.text_event_7, R.id.text_event_8, R.id.text_event_9,
R.id.text_event_10
R.id.text_task_1, R.id.text_task_2, R.id.text_task_3,
R.id.text_task_4, R.id.text_task_5, R.id.text_task_6,
R.id.text_task_7, R.id.text_task_8, R.id.text_task_9,
R.id.text_task_10
)

val hasPerm = context.checkSelfPermission(PERMISSION_READ_TASKS_ORG) == android.content.pm.PackageManager.PERMISSION_GRANTED ||
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/anim/events_page_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Incoming Events/Tasks page opens out vertically from its middle (same in both directions) -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillBefore="true">
<scale
android:fromXScale="1.0" android:toXScale="1.0"
android:fromYScale="0.0" android:toYScale="1.0"
android:pivotX="50%" android:pivotY="50%"
android:startOffset="150"
android:duration="170"
android:interpolator="@android:interpolator/decelerate_quad" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:startOffset="150" android:duration="170" />
</set>
12 changes: 12 additions & 0 deletions app/src/main/res/anim/events_page_out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Outgoing Events/Tasks page squashes vertically to its middle (same in both directions) -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:fromXScale="1.0" android:toXScale="1.0"
android:fromYScale="1.0" android:toYScale="0.0"
android:pivotX="50%" android:pivotY="50%"
android:duration="150"
android:interpolator="@android:interpolator/accelerate_quad" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="150" />
</set>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_chevron_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>
64 changes: 54 additions & 10 deletions app/src/main/res/layout/widget_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,68 @@
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/layout_battery"
android:layout_above="@+id/button_events_switch"
android:orientation="vertical"
android:layout_below="@id/date_container"
android:layout_marginTop="8dp"
android:clipChildren="true"
android:clipToPadding="true">

<TextView android:id="@+id/text_event_1" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_2" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_3" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_4" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_5" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_6" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_7" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_8" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_9" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_10" style="@style/EventText" android:visibility="gone"/>
<ViewFlipper android:id="@+id/events_flipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inAnimation="@anim/events_page_in"
android:outAnimation="@anim/events_page_out">

<LinearLayout android:id="@+id/events_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/text_event_1" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_2" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_3" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_4" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_5" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_6" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_7" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_8" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_9" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_event_10" style="@style/EventText" android:visibility="gone"/>
</LinearLayout>

<LinearLayout android:id="@+id/tasks_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/text_task_1" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_2" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_3" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_4" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_5" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_6" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_7" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_8" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_9" style="@style/EventText" android:visibility="gone"/>
<TextView android:id="@+id/text_task_10" style="@style/EventText" android:visibility="gone"/>
</LinearLayout>
</ViewFlipper>

</LinearLayout>

<!-- Flips between Events and Tasks when both are on; pinned to the bottom-left of the widget -->
<ImageView
android:id="@+id/button_events_switch"
android:layout_width="56dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingEnd="6dp"
android:scaleType="fitStart"
android:src="@drawable/ic_chevron_down"
android:importantForAccessibility="no"
android:visibility="gone"/>

</RelativeLayout>
</RelativeLayout>
Loading
Loading