Skip to content

Commit f97c6a7

Browse files
committed
Widgets: workaround showNext action for forecasts
* For some reason, if we send a partial update with the 'showNext' action it doesn't work * The action only works with a 'full' update (updateAppWidget) * 'updateAppWidget' shouldn't be used here since this is not a 'full' update * Workaround this by explicitly requesting to show some child view in the ViewFlipper using a persisted index which flips between the numbers 0-1
1 parent 99a688a commit f97c6a7

File tree

7 files changed

+88
-8
lines changed

7 files changed

+88
-8
lines changed

Diff for: app/src/main/java/com/thewizrd/simpleweather/widgets/WeatherWidgetProvider4x1.kt

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@ import android.widget.RemoteViews
77
import com.thewizrd.simpleweather.R
88

99
class WeatherWidgetProvider4x1 : WeatherWidgetProvider() {
10+
companion object {
11+
private fun getNextIndex(index: Int): Int {
12+
return (index + 1) % 2
13+
}
14+
}
15+
1016
override val info: WidgetProviderInfo by lazy { Info.getInstance() }
1117

1218
override fun onReceive(context: Context, intent: Intent?) {
1319
val action = intent?.action
1420
if (ACTION_SHOWNEXTFORECAST == action) {
1521
val appWidgetManager = AppWidgetManager.getInstance(context)
16-
val views = RemoteViews(context.packageName, info.widgetLayoutId)
17-
views.showNext(R.id.forecast_layout)
1822
val appWidgetId =
1923
intent.getIntExtra(EXTRA_WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
24+
25+
// Note: workaround; possibly temporary?
26+
val views = RemoteViews(context.packageName, info.widgetLayoutId)
27+
val index = getNextIndex(WidgetUtils.getDisplayedChild(appWidgetId))
28+
views.setInt(R.id.forecast_layout, "setDisplayedChild", index)
29+
WidgetUtils.setDisplayedChild(appWidgetId, index)
2030
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views)
2131
} else {
2232
super.onReceive(context, intent)

Diff for: app/src/main/java/com/thewizrd/simpleweather/widgets/WeatherWidgetProvider4x2.kt

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,38 @@ import android.widget.RemoteViews
77
import com.thewizrd.simpleweather.R
88

99
class WeatherWidgetProvider4x2 : WeatherWidgetProvider() {
10+
companion object {
11+
private fun getNextIndex(index: Int): Int {
12+
return (index + 1) % 2
13+
}
14+
}
15+
1016
override val info: WidgetProviderInfo by lazy { Info.getInstance() }
1117

1218
override fun onReceive(context: Context, intent: Intent?) {
1319
val action = intent?.action
1420
if (Intent.ACTION_TIME_CHANGED == action || Intent.ACTION_TIMEZONE_CHANGED == action) {
15-
WeatherWidgetService.enqueueWork(context, Intent(context, WeatherWidgetService::class.java)
21+
WeatherWidgetService.enqueueWork(
22+
context, Intent(context, WeatherWidgetService::class.java)
1623
.setAction(WeatherWidgetService.ACTION_UPDATECLOCK)
1724
.putExtra(EXTRA_WIDGET_IDS, info.appWidgetIds)
18-
.putExtra(EXTRA_WIDGET_TYPE, info.widgetType.value))
25+
.putExtra(EXTRA_WIDGET_TYPE, info.widgetType.value)
26+
)
1927

2028
WeatherWidgetService.enqueueWork(context, Intent(context, WeatherWidgetService::class.java)
2129
.setAction(WeatherWidgetService.ACTION_UPDATEDATE)
2230
.putExtra(EXTRA_WIDGET_IDS, info.appWidgetIds)
2331
.putExtra(EXTRA_WIDGET_TYPE, info.widgetType.value))
2432
} else if (ACTION_SHOWNEXTFORECAST == action) {
2533
val appWidgetManager = AppWidgetManager.getInstance(context)
34+
val appWidgetId =
35+
intent.getIntExtra(EXTRA_WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
36+
37+
// Note: workaround; possibly temporary?
2638
val views = RemoteViews(context.packageName, info.widgetLayoutId)
27-
views.showNext(R.id.forecast_layout)
28-
val appWidgetId = intent.getIntExtra(EXTRA_WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
39+
val index = getNextIndex(WidgetUtils.getDisplayedChild(appWidgetId))
40+
views.setInt(R.id.forecast_layout, "setDisplayedChild", index)
41+
WidgetUtils.setDisplayedChild(appWidgetId, index)
2942
appWidgetManager.partiallyUpdateAppWidget(appWidgetId, views)
3043
} else {
3144
super.onReceive(context, intent)

Diff for: app/src/main/java/com/thewizrd/simpleweather/widgets/WidgetUtils.kt

+11
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,15 @@ object WidgetUtils {
947947
putString(KEY_LOCATIONS, locations?.joinToString(separator = ";separator;") { it })
948948
}
949949
}
950+
951+
fun getDisplayedChild(appWidgetId: Int): Int {
952+
val prefs = getPreferences(appWidgetId)
953+
return prefs.getInt("setDisplayedChild", 0)
954+
}
955+
956+
fun setDisplayedChild(appWidgetId: Int, index: Int) {
957+
getPreferences(appWidgetId).edit(false) {
958+
putInt("setDisplayedChild", index)
959+
}
960+
}
950961
}

Diff for: app/src/main/res/anim/widget_fade_in.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
/*
3+
** Copyright 2010, The Android Open Source Project
4+
**
5+
** Licensed under the Apache License, Version 2.0 (the "License");
6+
** you may not use this file except in compliance with the License.
7+
** You may obtain a copy of the License at
8+
**
9+
** http://www.apache.org/licenses/LICENSE-2.0
10+
**
11+
** Unless required by applicable law or agreed to in writing, software
12+
** distributed under the License is distributed on an "AS IS" BASIS,
13+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
** See the License for the specific language governing permissions and
15+
** limitations under the License.
16+
*/
17+
-->
18+
19+
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:fromAlpha="0.0"
21+
android:toAlpha="1.0"
22+
android:duration="@android:integer/config_shortAnimTime" />

Diff for: app/src/main/res/anim/widget_fade_out.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
/*
3+
** Copyright 2010, The Android Open Source Project
4+
**
5+
** Licensed under the Apache License, Version 2.0 (the "License");
6+
** you may not use this file except in compliance with the License.
7+
** You may obtain a copy of the License at
8+
**
9+
** http://www.apache.org/licenses/LICENSE-2.0
10+
**
11+
** Unless required by applicable law or agreed to in writing, software
12+
** distributed under the License is distributed on an "AS IS" BASIS,
13+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
** See the License for the specific language governing permissions and
15+
** limitations under the License.
16+
*/
17+
-->
18+
19+
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:fromAlpha="1.0"
21+
android:toAlpha="0.0"
22+
android:duration="@android:integer/config_shortAnimTime" />

Diff for: app/src/main/res/layout/app_widget_4x1.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
android:layout_gravity="center_vertical"
2525
android:autoStart="true"
2626
android:flipInterval="10000"
27-
android:loopViews="true" />
27+
android:inAnimation="@anim/widget_fade_in"
28+
android:outAnimation="@anim/widget_fade_out" />
2829

2930
</LinearLayout>
3031

Diff for: app/src/main/res/layout/app_widget_4x2.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
android:layout_alignParentBottom="true"
186186
android:autoStart="true"
187187
android:flipInterval="10000"
188-
android:loopViews="true" />
188+
android:inAnimation="@anim/widget_fade_in"
189+
android:outAnimation="@anim/widget_fade_out" />
189190

190191
</RelativeLayout>

0 commit comments

Comments
 (0)