1
1
package com .nitramite .apcupsdmonitor ;
2
2
3
+ import android .annotation .SuppressLint ;
3
4
import android .app .PendingIntent ;
4
5
import android .appwidget .AppWidgetManager ;
5
6
import android .appwidget .AppWidgetProvider ;
24
25
25
26
public class Widget extends AppWidgetProvider {
26
27
27
- // Logging
28
28
private static final String TAG = Widget .class .getSimpleName ();
29
29
30
-
31
30
@ Override
32
31
public void onReceive (Context context , Intent intent ) {
33
32
super .onReceive (context , intent );
@@ -39,32 +38,31 @@ public void onReceive(Context context, Intent intent) {
39
38
onUpdate (context , appWidgetManager , appWidgetIds );
40
39
}
41
40
42
- // onUpdate
41
+ @ SuppressLint ("UnspecifiedImmutableFlag" )
42
+ @ Override
43
43
public void onUpdate (Context context , AppWidgetManager appWidgetManager , int [] appWidgetIds ) {
44
- super .onUpdate (context , appWidgetManager , appWidgetIds );
45
44
//noinspection ForLoopReplaceableByForEach
46
45
for (int i = 0 ; i < appWidgetIds .length ; i ++) {
47
46
try {
48
- Intent intent = new Intent (context , MainMenu .class );
49
- PendingIntent pendingIntent = PendingIntent .getActivity (context , 0 , intent , 0 );
47
+ Log .i (TAG , "Widget on update event" );
50
48
DatabaseHelper databaseHelper = new DatabaseHelper (context );
51
49
ArrayList <UPS > upsArrayList = getUpsData (databaseHelper );
52
-
53
50
RemoteViews rv = new RemoteViews (context .getPackageName (), R .layout .widget );
54
51
setBitmap (rv , R .id .upsStatusImage , createUpsViewBitmap (context , upsArrayList ));
55
52
56
53
57
- // On click refresh trigger method
58
- Log .i (TAG , "Widget on update event" );
59
- Intent updateIntent = new Intent (context , Widget .class );
60
- updateIntent .setAction (AppWidgetManager .ACTION_APPWIDGET_UPDATE );
61
- PendingIntent pendingUpdate = PendingIntent .getBroadcast (context , 0 , updateIntent , PendingIntent .FLAG_UPDATE_CURRENT );
62
- rv .setOnClickPendingIntent (R .id .upsStatusImage , pendingUpdate );
54
+ // intent to open app on widget click
55
+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .M ) {
56
+ Intent intentSync = new Intent (context , MainMenu .class );
57
+ intentSync .setAction (AppWidgetManager .ACTION_APPWIDGET_UPDATE );
58
+ PendingIntent pendingSync = PendingIntent .getActivity (
59
+ context , 0 , intentSync , PendingIntent .FLAG_IMMUTABLE
60
+ );
61
+ rv .setOnClickPendingIntent (R .id .upsStatusImage , pendingSync );
62
+ }
63
63
64
- // Finish
65
- rv .setOnClickPendingIntent (R .id .upsStatusImage , pendingIntent );
66
- appWidgetManager .updateAppWidget (appWidgetIds [i ], rv );
67
64
65
+ appWidgetManager .updateAppWidget (appWidgetIds [i ], rv );
68
66
AppWidgetManager .getInstance (context ).updateAppWidget (appWidgetIds [i ], rv );
69
67
} catch (Exception e ) {
70
68
Log .e (TAG , e .toString ());
@@ -132,6 +130,16 @@ private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList)
132
130
return getBitmapFromView (mainLinearLayout );
133
131
}
134
132
133
+ @ Override
134
+ public void onEnabled (Context context ) {
135
+ // Enter relevant functionality for when the first widget is created
136
+ }
137
+
138
+ @ Override
139
+ public void onDisabled (Context context ) {
140
+ // Enter relevant functionality for when the last widget is disabled
141
+ }
142
+
135
143
136
144
private static Bitmap getBitmapFromView (View view ) {
137
145
try {
@@ -161,4 +169,5 @@ private void setBitmap(RemoteViews views, int resId, Bitmap bitmap) throws Runti
161
169
views .setImageViewBitmap (resId , proxy );
162
170
}
163
171
164
- } // End of class
172
+
173
+ }
0 commit comments