mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Render widgets in background
This commit is contained in:
@@ -178,9 +178,7 @@ public class CheckmarkView extends View implements HabitDataView
|
|||||||
padding = 8 * leftMargin;
|
padding = 8 * leftMargin;
|
||||||
textPaint.setTextSize(0.15f * width);
|
textPaint.setTextSize(0.15f * width);
|
||||||
|
|
||||||
labelLayout = new StaticLayout(label, textPaint,
|
updateLabel();
|
||||||
(int) (width - 2 * leftMargin - 2 * padding),
|
|
||||||
Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void refreshData()
|
public void refreshData()
|
||||||
@@ -190,8 +188,15 @@ public class CheckmarkView extends View implements HabitDataView
|
|||||||
Color.blue(habit.color));
|
Color.blue(habit.color));
|
||||||
this.label = habit.name;
|
this.label = habit.name;
|
||||||
|
|
||||||
textPaint.setColor(Color.WHITE);
|
updateLabel();
|
||||||
postInvalidate();
|
postInvalidate();
|
||||||
requestLayout();
|
}
|
||||||
|
|
||||||
|
private void updateLabel()
|
||||||
|
{
|
||||||
|
textPaint.setColor(Color.WHITE);
|
||||||
|
labelLayout = new StaticLayout(label, textPaint,
|
||||||
|
(int) (width - 2 * leftMargin - 2 * padding),
|
||||||
|
Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ import android.view.View;
|
|||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.UIHelper;
|
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -92,12 +93,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateWidget(Context context, AppWidgetManager manager, int widgetId, Bundle options)
|
private void updateWidget(Context context, AppWidgetManager manager,
|
||||||
|
int widgetId, Bundle options)
|
||||||
{
|
{
|
||||||
updateWidgetSize(context, options);
|
updateWidgetSize(context, options);
|
||||||
|
|
||||||
Context appContext = context.getApplicationContext();
|
Context appContext = context.getApplicationContext();
|
||||||
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
|
||||||
|
|
||||||
Long habitId = prefs.getLong(getHabitIdKey(widgetId), -1L);
|
Long habitId = prefs.getLong(getHabitIdKey(widgetId), -1L);
|
||||||
@@ -112,24 +113,11 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
View widgetView = buildCustomView(context, habit);
|
new RenderWidgetTask(widgetId, context, habit, manager).execute();
|
||||||
measureCustomView(context, width, height, widgetView);
|
|
||||||
|
|
||||||
widgetView.setDrawingCacheEnabled(true);
|
|
||||||
widgetView.buildDrawingCache(true);
|
|
||||||
Bitmap drawingCache = widgetView.getDrawingCache();
|
|
||||||
|
|
||||||
remoteViews.setTextViewText(R.id.label, habit.name);
|
|
||||||
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
|
||||||
|
|
||||||
//savePreview(context, widgetId, drawingCache);
|
|
||||||
|
|
||||||
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
|
||||||
if(onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView, onClickIntent);
|
|
||||||
|
|
||||||
manager.updateAppWidget(widgetId, remoteViews);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract void refreshCustomViewData(View widgetView);
|
||||||
|
|
||||||
private void savePreview(Context context, int widgetId, Bitmap widgetCache)
|
private void savePreview(Context context, int widgetId, Bitmap widgetCache)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -204,4 +192,61 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
customView.measure(specWidth, specHeight);
|
customView.measure(specWidth, specHeight);
|
||||||
customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight());
|
customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class RenderWidgetTask extends BaseTask
|
||||||
|
{
|
||||||
|
private final int widgetId;
|
||||||
|
private final Context context;
|
||||||
|
private final Habit habit;
|
||||||
|
private final AppWidgetManager manager;
|
||||||
|
public RemoteViews remoteViews;
|
||||||
|
public View widgetView;
|
||||||
|
|
||||||
|
public RenderWidgetTask(int widgetId, Context context, Habit habit,
|
||||||
|
AppWidgetManager manager)
|
||||||
|
{
|
||||||
|
this.widgetId = widgetId;
|
||||||
|
this.context = context;
|
||||||
|
this.habit = habit;
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute()
|
||||||
|
{
|
||||||
|
super.onPreExecute();
|
||||||
|
|
||||||
|
remoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||||
|
widgetView = buildCustomView(context, habit);
|
||||||
|
measureCustomView(context, width, height, widgetView);
|
||||||
|
manager.updateAppWidget(widgetId, remoteViews);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
refreshCustomViewData(widgetView);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid)
|
||||||
|
{
|
||||||
|
widgetView.invalidate();
|
||||||
|
widgetView.setDrawingCacheEnabled(true);
|
||||||
|
widgetView.buildDrawingCache(true);
|
||||||
|
Bitmap drawingCache = widgetView.getDrawingCache();
|
||||||
|
remoteViews.setTextViewText(R.id.label, habit.name);
|
||||||
|
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
||||||
|
|
||||||
|
savePreview(context, widgetId, drawingCache);
|
||||||
|
|
||||||
|
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
||||||
|
if(onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView, onClickIntent);
|
||||||
|
|
||||||
|
manager.updateAppWidget(widgetId, remoteViews);
|
||||||
|
|
||||||
|
super.onPostExecute(aVoid);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,9 @@ import org.isoron.uhabits.HabitBroadcastReceiver;
|
|||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
import org.isoron.uhabits.views.CheckmarkView;
|
import org.isoron.uhabits.views.CheckmarkView;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
|
|
||||||
public class CheckmarkWidgetProvider extends BaseWidgetProvider
|
public class CheckmarkWidgetProvider extends BaseWidgetProvider
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected View buildCustomView(Context context, Habit habit)
|
protected View buildCustomView(Context context, Habit habit)
|
||||||
@@ -37,6 +38,12 @@ public class CheckmarkWidgetProvider extends BaseWidgetProvider
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void refreshCustomViewData(View view)
|
||||||
|
{
|
||||||
|
((HabitDataView) view).refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
||||||
{
|
{
|
||||||
@@ -60,4 +67,6 @@ public class CheckmarkWidgetProvider extends BaseWidgetProvider
|
|||||||
{
|
{
|
||||||
return R.layout.widget_checkmark;
|
return R.layout.widget_checkmark;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import android.view.View;
|
|||||||
import org.isoron.uhabits.HabitBroadcastReceiver;
|
import org.isoron.uhabits.HabitBroadcastReceiver;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
import org.isoron.uhabits.views.HabitFrequencyView;
|
import org.isoron.uhabits.views.HabitFrequencyView;
|
||||||
|
|
||||||
public class FrequencyWidgetProvider extends BaseWidgetProvider
|
public class FrequencyWidgetProvider extends BaseWidgetProvider
|
||||||
@@ -39,6 +40,12 @@ public class FrequencyWidgetProvider extends BaseWidgetProvider
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void refreshCustomViewData(View view)
|
||||||
|
{
|
||||||
|
((HabitDataView) view).refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import android.view.View;
|
|||||||
import org.isoron.uhabits.HabitBroadcastReceiver;
|
import org.isoron.uhabits.HabitBroadcastReceiver;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
import org.isoron.uhabits.views.HabitHistoryView;
|
import org.isoron.uhabits.views.HabitHistoryView;
|
||||||
|
|
||||||
public class HistoryWidgetProvider extends BaseWidgetProvider
|
public class HistoryWidgetProvider extends BaseWidgetProvider
|
||||||
@@ -38,6 +39,12 @@ public class HistoryWidgetProvider extends BaseWidgetProvider
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void refreshCustomViewData(View view)
|
||||||
|
{
|
||||||
|
((HabitDataView) view).refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import android.view.View;
|
|||||||
import org.isoron.uhabits.HabitBroadcastReceiver;
|
import org.isoron.uhabits.HabitBroadcastReceiver;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
import org.isoron.uhabits.views.HabitScoreView;
|
import org.isoron.uhabits.views.HabitScoreView;
|
||||||
|
|
||||||
public class ScoreWidgetProvider extends BaseWidgetProvider
|
public class ScoreWidgetProvider extends BaseWidgetProvider
|
||||||
@@ -38,6 +39,12 @@ public class ScoreWidgetProvider extends BaseWidgetProvider
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void refreshCustomViewData(View view)
|
||||||
|
{
|
||||||
|
((HabitDataView) view).refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import android.view.View;
|
|||||||
import org.isoron.uhabits.HabitBroadcastReceiver;
|
import org.isoron.uhabits.HabitBroadcastReceiver;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.views.HabitDataView;
|
||||||
import org.isoron.uhabits.views.HabitStreakView;
|
import org.isoron.uhabits.views.HabitStreakView;
|
||||||
|
|
||||||
public class StreakWidgetProvider extends BaseWidgetProvider
|
public class StreakWidgetProvider extends BaseWidgetProvider
|
||||||
@@ -38,6 +39,12 @@ public class StreakWidgetProvider extends BaseWidgetProvider
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void refreshCustomViewData(View view)
|
||||||
|
{
|
||||||
|
((HabitDataView) view).refreshData();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
protected PendingIntent getOnClickPendingIntent(Context context, Habit habit)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user