1616
1717package com .pranavpandey .android .dynamic .theme .receiver ;
1818
19+ import android .appwidget .AppWidgetManager ;
1920import android .content .BroadcastReceiver ;
2021import android .content .Context ;
2122import 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