Skip to content

Commit c96373f

Browse files
committed
Add app widget support for theme receiver
1 parent cfc2768 commit c96373f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

dynamic-theme/src/main/java/com/pranavpandey/android/dynamic/theme/Theme.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,21 @@
314314
*/
315315
String ACTION = "com.pranavpandey.theme.intent.action.DYNAMIC_THEME";
316316

317+
/**
318+
* Intent action constant for the app widget.
319+
*/
320+
String ACTION_APP_WIDGET = "com.pranavpandey.theme.intent.action.APP_WIDGET";
321+
317322
/**
318323
* Intent action constant to capture the dynamic theme.
319324
*/
320325
String ACTION_CAPTURE = DynamicIntentUtils.ACTION_MATRIX_CAPTURE_RESULT;
321326

327+
/**
328+
* Intent extra key for the theme data.
329+
*/
330+
String EXTRA_PREFS = "adt_extra_prefs";
331+
322332
/**
323333
* Intent extra key for the theme data.
324334
*/

dynamic-theme/src/main/java/com/pranavpandey/android/dynamic/theme/receiver/DynamicThemeReceiver.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.pranavpandey.android.dynamic.theme.receiver;
1818

19+
import android.appwidget.AppWidgetManager;
1920
import android.content.BroadcastReceiver;
2021
import android.content.Context;
2122
import android.content.Intent;
@@ -35,13 +36,26 @@ public abstract class DynamicThemeReceiver extends BroadcastReceiver {
3536

3637
@Override
3738
public void onReceive(final @NonNull Context context, @Nullable Intent intent) {
38-
if (intent != null && Theme.Intent.ACTION.equals(intent.getAction())) {
39+
if (intent == null) {
40+
return;
41+
}
42+
43+
if (Theme.Intent.ACTION.equals(intent.getAction())) {
3944
if (intent.hasExtra(Theme.Intent.EXTRA_THEME)
4045
|| intent.hasExtra(Theme.Intent.EXTRA_DATA)) {
4146
onReceiveTheme(intent.getStringExtra(Theme.Intent.EXTRA_THEME),
4247
intent.getStringExtra(Theme.Intent.EXTRA_VALUE),
4348
intent.getStringExtra(Theme.Intent.EXTRA_DATA));
4449
}
50+
} else if (Theme.Intent.ACTION_APP_WIDGET.equals(intent.getAction())
51+
|| intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
52+
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
53+
AppWidgetManager.INVALID_APPWIDGET_ID);
54+
55+
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
56+
onAppWidget(appWidgetId, intent.getStringExtra(Theme.Intent.EXTRA_PREFS),
57+
intent.getStringExtra(Theme.Intent.EXTRA_THEME));
58+
}
4559
}
4660
}
4761

@@ -54,4 +68,14 @@ public void onReceive(final @NonNull Context context, @Nullable Intent intent) {
5468
*/
5569
protected abstract void onReceiveTheme(@Nullable @Theme.ToString String theme,
5670
@Nullable @Theme.ToString String value, @Nullable String data);
71+
72+
/**
73+
* This method will be called when an app widget is added via pinning.
74+
*
75+
* @param appWidgetId The generated app widget id.
76+
* @param prefs The received shared preferences name.
77+
* @param theme The received widget theme or settings.
78+
*/
79+
protected void onAppWidget(int appWidgetId,
80+
@Nullable String prefs, @Nullable String theme) { }
5781
}

0 commit comments

Comments
 (0)