3
3
import android .app .PendingIntent ;
4
4
import android .appwidget .AppWidgetManager ;
5
5
import android .appwidget .AppWidgetProvider ;
6
- import androidx .lifecycle .LiveData ;
7
- import androidx .lifecycle .Observer ;
8
6
import android .content .Context ;
9
7
import android .content .Intent ;
10
8
import android .graphics .Bitmap ;
9
+ import android .os .Build ;
11
10
import android .view .View ;
12
11
import android .widget .RemoteViews ;
13
12
14
- import com .bumptech .glide .request .RequestOptions ;
13
+ import androidx .annotation .NonNull ;
14
+
15
15
import com .bumptech .glide .request .target .AppWidgetTarget ;
16
16
import com .bumptech .glide .request .transition .Transition ;
17
17
import com .example .abhishek .newsapp .R ;
@@ -47,7 +47,7 @@ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
47
47
48
48
AppWidgetTarget appWidgetTarget = new AppWidgetTarget (context , R .id .iv_news_image , views , appWidgetId ) {
49
49
@ Override
50
- public void onResourceReady (Bitmap resource , Transition <? super Bitmap > transition ) {
50
+ public void onResourceReady (@ NonNull Bitmap resource , Transition <? super Bitmap > transition ) {
51
51
super .onResourceReady (resource , transition );
52
52
}
53
53
};
@@ -62,18 +62,18 @@ public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transiti
62
62
Intent nextIntent = new Intent (context , SavedNewsService .class );
63
63
nextIntent .setAction (SavedNewsService .ACTION_GET_NEXT );
64
64
nextIntent .putExtra (SavedNewsService .PARAM_CURRENT , selected );
65
- PendingIntent nextPendingIntent = PendingIntent . getService (context , 0 , nextIntent , PendingIntent . FLAG_UPDATE_CURRENT );
65
+ PendingIntent nextPendingIntent = getPendingIntent (context , nextIntent , false );
66
66
views .setOnClickPendingIntent (R .id .ib_next , nextPendingIntent );
67
67
68
68
Intent previousIntent = new Intent (context , SavedNewsService .class );
69
69
previousIntent .setAction (SavedNewsService .ACTION_GET_PREVIOUS );
70
70
previousIntent .putExtra (SavedNewsService .PARAM_CURRENT , selected );
71
- PendingIntent previousPendingIntent = PendingIntent . getService (context , 0 , previousIntent , PendingIntent . FLAG_UPDATE_CURRENT );
71
+ PendingIntent previousPendingIntent = getPendingIntent (context , previousIntent , false );
72
72
views .setOnClickPendingIntent (R .id .ib_previous , previousPendingIntent );
73
73
74
74
Intent detail = new Intent (context , DetailActivity .class );
75
75
detail .putExtra (DetailActivity .PARAM_ARTICLE , articles .get (selected ));
76
- PendingIntent pendingIntent = PendingIntent . getActivity (context , 0 , detail , PendingIntent . FLAG_UPDATE_CURRENT );
76
+ PendingIntent pendingIntent = getPendingIntent (context , detail , true );
77
77
views .setOnClickPendingIntent (R .id .widget_parent , pendingIntent );
78
78
79
79
} else {
@@ -85,7 +85,7 @@ public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transiti
85
85
views .setViewVisibility (R .id .tv_error , View .VISIBLE );
86
86
87
87
Intent home = new Intent (context , MainActivity .class );
88
- PendingIntent pendingIntent = PendingIntent . getActivity (context , 0 , home , PendingIntent . FLAG_UPDATE_CURRENT );
88
+ PendingIntent pendingIntent = getPendingIntent (context , home , true );
89
89
views .setOnClickPendingIntent (R .id .widget_parent , pendingIntent );
90
90
}
91
91
// Instruct the widget manager to update the widget
@@ -97,7 +97,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
97
97
Intent nextIntent = new Intent (context , SavedNewsService .class );
98
98
nextIntent .setAction (SavedNewsService .ACTION_GET_NEXT );
99
99
nextIntent .putExtra (SavedNewsService .PARAM_CURRENT , -1 );
100
- PendingIntent nextPendingIntent = PendingIntent . getService (context , 0 , nextIntent , PendingIntent . FLAG_UPDATE_CURRENT );
100
+ PendingIntent nextPendingIntent = getPendingIntent (context , nextIntent , false );
101
101
try {
102
102
nextPendingIntent .send ();
103
103
} catch (PendingIntent .CanceledException e ) {
@@ -106,13 +106,6 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
106
106
107
107
}
108
108
109
- public static void updateNewsWidgets (Context context , AppWidgetManager appWidgetManager , List <Article > articles , int selected , int [] appWidgetIds ) {
110
- // There may be multiple widgets active, so update all of them
111
- for (int appWidgetId : appWidgetIds ) {
112
- updateAppWidget (context , appWidgetManager , articles , selected , appWidgetId );
113
- }
114
- }
115
-
116
109
@ Override
117
110
public void onEnabled (Context context ) {
118
111
// Enter relevant functionality for when the first widget is created
@@ -122,5 +115,24 @@ public void onEnabled(Context context) {
122
115
public void onDisabled (Context context ) {
123
116
// Enter relevant functionality for when the last widget is disabled
124
117
}
118
+
119
+ public static void updateNewsWidgets (Context context , AppWidgetManager appWidgetManager , List <Article > articles , int selected , int [] appWidgetIds ) {
120
+ // There may be multiple widgets active, so update all of them
121
+ for (int appWidgetId : appWidgetIds ) {
122
+ updateAppWidget (context , appWidgetManager , articles , selected , appWidgetId );
123
+ }
124
+ }
125
+
126
+ private static PendingIntent getPendingIntent (Context context , Intent intent , boolean isActivity ) {
127
+ int flag = PendingIntent .FLAG_UPDATE_CURRENT ;
128
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
129
+ flag = PendingIntent .FLAG_IMMUTABLE ;
130
+ }
131
+ if (isActivity ) {
132
+ return PendingIntent .getActivity (context , 0 , intent , flag );
133
+
134
+ }
135
+ return PendingIntent .getService (context , 0 , intent , flag );
136
+ }
125
137
}
126
138
0 commit comments