mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Widgets: show error message instead of crashing
This commit is contained in:
@@ -107,15 +107,19 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
Habit habit = Habit.get(habitId);
|
Habit habit = Habit.get(habitId);
|
||||||
if(habit == null)
|
if(habit == null)
|
||||||
{
|
{
|
||||||
RemoteViews errorView = new RemoteViews(context.getPackageName(),
|
drawErrorWidget(context, manager, widgetId);
|
||||||
R.layout.widget_error);
|
|
||||||
manager.updateAppWidget(widgetId, errorView);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new RenderWidgetTask(widgetId, context, habit, manager).execute();
|
new RenderWidgetTask(widgetId, context, habit, manager).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void drawErrorWidget(Context context, AppWidgetManager manager, int widgetId)
|
||||||
|
{
|
||||||
|
RemoteViews errorView = new RemoteViews(context.getPackageName(), R.layout.widget_error);
|
||||||
|
manager.updateAppWidget(widgetId, errorView);
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void refreshCustomViewData(View widgetView);
|
protected abstract void refreshCustomViewData(View widgetView);
|
||||||
|
|
||||||
private void savePreview(Context context, int widgetId, Bitmap widgetCache)
|
private void savePreview(Context context, int widgetId, Bitmap widgetCache)
|
||||||
@@ -231,19 +235,28 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid)
|
protected void onPostExecute(Void aVoid)
|
||||||
{
|
{
|
||||||
widgetView.invalidate();
|
try
|
||||||
widgetView.setDrawingCacheEnabled(true);
|
{
|
||||||
widgetView.buildDrawingCache(true);
|
widgetView.invalidate();
|
||||||
Bitmap drawingCache = widgetView.getDrawingCache();
|
widgetView.setDrawingCacheEnabled(true);
|
||||||
remoteViews.setTextViewText(R.id.label, habit.name);
|
widgetView.buildDrawingCache(true);
|
||||||
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
Bitmap drawingCache = widgetView.getDrawingCache();
|
||||||
|
remoteViews.setTextViewText(R.id.label, habit.name);
|
||||||
|
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
||||||
|
|
||||||
//savePreview(context, widgetId, drawingCache);
|
//savePreview(context, widgetId, drawingCache);
|
||||||
|
|
||||||
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
||||||
if(onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView, onClickIntent);
|
if (onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView,
|
||||||
|
onClickIntent);
|
||||||
|
|
||||||
manager.updateAppWidget(widgetId, remoteViews);
|
manager.updateAppWidget(widgetId, remoteViews);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
drawErrorWidget(context, manager, widgetId);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
super.onPostExecute(aVoid);
|
super.onPostExecute(aVoid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:text="Habit not found"
|
android:text="Error drawing widget"
|
||||||
android:textColor="#ffffff"/>
|
android:textColor="#ffffff"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
Reference in New Issue
Block a user