Skip to content

Commit 17354bf

Browse files
authored
Merge pull request #129 from norkator/issue/104
Issue/104 - widget not loading or rendering
2 parents 95283e2 + 3267e94 commit 17354bf

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

app/src/main/java/com/nitramite/apcupsdmonitor/Widget.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nitramite.apcupsdmonitor;
22

3+
import android.annotation.SuppressLint;
34
import android.app.PendingIntent;
45
import android.appwidget.AppWidgetManager;
56
import android.appwidget.AppWidgetProvider;
@@ -24,10 +25,8 @@
2425

2526
public class Widget extends AppWidgetProvider {
2627

27-
// Logging
2828
private static final String TAG = Widget.class.getSimpleName();
2929

30-
3130
@Override
3231
public void onReceive(Context context, Intent intent) {
3332
super.onReceive(context, intent);
@@ -39,32 +38,31 @@ public void onReceive(Context context, Intent intent) {
3938
onUpdate(context, appWidgetManager, appWidgetIds);
4039
}
4140

42-
// onUpdate
41+
@SuppressLint("UnspecifiedImmutableFlag")
42+
@Override
4343
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
44-
super.onUpdate(context, appWidgetManager, appWidgetIds);
4544
//noinspection ForLoopReplaceableByForEach
4645
for (int i = 0; i < appWidgetIds.length; i++) {
4746
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");
5048
DatabaseHelper databaseHelper = new DatabaseHelper(context);
5149
ArrayList<UPS> upsArrayList = getUpsData(databaseHelper);
52-
5350
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget);
5451
setBitmap(rv, R.id.upsStatusImage, createUpsViewBitmap(context, upsArrayList));
5552

5653

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+
}
6363

64-
// Finish
65-
rv.setOnClickPendingIntent(R.id.upsStatusImage, pendingIntent);
66-
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
6764

65+
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
6866
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetIds[i], rv);
6967
} catch (Exception e) {
7068
Log.e(TAG, e.toString());
@@ -132,6 +130,16 @@ private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList)
132130
return getBitmapFromView(mainLinearLayout);
133131
}
134132

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+
135143

136144
private static Bitmap getBitmapFromView(View view) {
137145
try {
@@ -161,4 +169,5 @@ private void setBitmap(RemoteViews views, int resId, Bitmap bitmap) throws Runti
161169
views.setImageViewBitmap(resId, proxy);
162170
}
163171

164-
} // End of class
172+
173+
}

app/src/main/res/values-ru/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,7 @@
204204
<string name="donate_description">Пожертвования помогают поддерживать это приложение в актуальном состоянии и помогают в разработке новых функций.</string>
205205
<string name="nut_fqdn_error">Адрес Nut-сервера не в формате FQDN. Должно быть что-то вроде \"name@192.168.1.1\". </string>
206206

207+
<!-- Widget -->
208+
<string name="widget_description">Виджет статуса</string>
209+
207210
</resources>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,7 @@
204204
<string name="donate_description">Donations helps to keep this app up to date and helps with developing new features.</string>
205205
<string name="nut_fqdn_error">Nut server address not in FQDN format. Must be something like \"name@192.168.1.1\". </string>
206206

207+
<!-- Widget -->
208+
<string name="widget_description">Status widget</string>
209+
207210
</resources>

app/src/main/res/xml/widget_info.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:description="@string/widget_description"
34
android:initialKeyguardLayout="@layout/widget"
45
android:initialLayout="@layout/widget"
5-
android:minHeight="30dp"
66
android:minWidth="50dp"
7+
android:minHeight="30dp"
78
android:previewImage="@drawable/widget_preview"
89
android:resizeMode="horizontal|vertical"
10+
android:targetCellWidth="2"
11+
android:targetCellHeight="1"
912
android:updatePeriodMillis="3600000"
1013
android:widgetCategory="home_screen" />
1114

12-
<!-- android:minHeight="60dp" -->
15+
<!-- android:minHeight="60dp" -->

0 commit comments

Comments
 (0)