mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Remove fields from BaseWidgetProvider
This caused a bug when multiple widgets had different sizes. The size of the last widget would be used in all of them.
This commit is contained in:
@@ -42,12 +42,15 @@ import org.isoron.uhabits.tasks.BaseTask;
|
|||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public abstract class BaseWidgetProvider extends AppWidgetProvider
|
public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||||
{
|
{
|
||||||
|
private class WidgetDimensions
|
||||||
private int portraitWidth, portraitHeight;
|
{
|
||||||
private int landscapeWidth, landscapeHeight;
|
public int portraitWidth, portraitHeight;
|
||||||
|
public int landscapeWidth, landscapeHeight;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract int getDefaultHeight();
|
protected abstract int getDefaultHeight();
|
||||||
|
|
||||||
@@ -98,7 +101,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
private void updateWidget(Context context, AppWidgetManager manager,
|
private void updateWidget(Context context, AppWidgetManager manager,
|
||||||
int widgetId, Bundle options)
|
int widgetId, Bundle options)
|
||||||
{
|
{
|
||||||
updateWidgetSize(context, options);
|
WidgetDimensions dim = getWidgetDimensions(context, options);
|
||||||
|
|
||||||
Context appContext = context.getApplicationContext();
|
Context appContext = context.getApplicationContext();
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
|
||||||
@@ -113,7 +116,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new RenderWidgetTask(widgetId, context, habit, manager).execute();
|
new RenderWidgetTask(widgetId, context, habit, dim, manager).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawErrorWidget(Context context, AppWidgetManager manager, int widgetId)
|
private void drawErrorWidget(Context context, AppWidgetManager manager, int widgetId)
|
||||||
@@ -159,7 +162,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateWidgetSize(Context context, Bundle options)
|
private WidgetDimensions getWidgetDimensions(Context context, Bundle options)
|
||||||
{
|
{
|
||||||
int maxWidth = getDefaultWidth();
|
int maxWidth = getDefaultWidth();
|
||||||
int minWidth = getDefaultWidth();
|
int minWidth = getDefaultWidth();
|
||||||
@@ -178,11 +181,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT));
|
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT));
|
||||||
}
|
}
|
||||||
|
|
||||||
portraitWidth = minWidth;
|
WidgetDimensions ws = new WidgetDimensions();
|
||||||
portraitHeight = maxHeight;
|
ws.portraitWidth = minWidth;
|
||||||
|
ws.portraitHeight = maxHeight;
|
||||||
landscapeWidth = maxWidth;
|
ws.landscapeWidth = maxWidth;
|
||||||
landscapeHeight = minHeight;
|
ws.landscapeHeight = minHeight;
|
||||||
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void measureCustomView(Context context, int w, int h, View customView)
|
private void measureCustomView(Context context, int w, int h, View customView)
|
||||||
@@ -212,16 +216,18 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
private final Context context;
|
private final Context context;
|
||||||
private final Habit habit;
|
private final Habit habit;
|
||||||
private final AppWidgetManager manager;
|
private final AppWidgetManager manager;
|
||||||
public RemoteViews portraitRemoteViews, landscapeRemoteViews;
|
private RemoteViews portraitRemoteViews, landscapeRemoteViews;
|
||||||
public View portraitWidgetView, landscapeWidgetView;
|
private View portraitWidgetView, landscapeWidgetView;
|
||||||
|
private WidgetDimensions dim;
|
||||||
|
|
||||||
public RenderWidgetTask(int widgetId, Context context, Habit habit,
|
public RenderWidgetTask(int widgetId, Context context, Habit habit, WidgetDimensions ws,
|
||||||
AppWidgetManager manager)
|
AppWidgetManager manager)
|
||||||
{
|
{
|
||||||
this.widgetId = widgetId;
|
this.widgetId = widgetId;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.habit = habit;
|
this.habit = habit;
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
this.dim = ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -232,11 +238,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
|
|
||||||
portraitRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
portraitRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||||
portraitWidgetView = buildCustomView(context, habit);
|
portraitWidgetView = buildCustomView(context, habit);
|
||||||
measureCustomView(context, portraitWidth, portraitHeight, portraitWidgetView);
|
measureCustomView(context, dim.portraitWidth, dim.portraitHeight, portraitWidgetView);
|
||||||
|
|
||||||
landscapeRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
landscapeRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||||
landscapeWidgetView = buildCustomView(context, habit);
|
landscapeWidgetView = buildCustomView(context, habit);
|
||||||
measureCustomView(context, landscapeWidth, landscapeHeight, landscapeWidgetView);
|
measureCustomView(context, dim.landscapeWidth, dim.landscapeHeight,
|
||||||
|
landscapeWidgetView);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateAppWidget()
|
private void updateAppWidget()
|
||||||
@@ -260,8 +267,10 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
buildRemoteViews(portraitWidgetView, portraitRemoteViews, portraitWidth, portraitHeight);
|
buildRemoteViews(portraitWidgetView, portraitRemoteViews,
|
||||||
buildRemoteViews(landscapeWidgetView, landscapeRemoteViews, landscapeWidth, landscapeHeight);
|
dim.portraitWidth, dim.portraitHeight);
|
||||||
|
buildRemoteViews(landscapeWidgetView, landscapeRemoteViews,
|
||||||
|
dim.landscapeWidth, dim.landscapeHeight);
|
||||||
updateAppWidget();
|
updateAppWidget();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -273,8 +282,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
super.onPostExecute(aVoid);
|
super.onPostExecute(aVoid);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildRemoteViews(View widgetView, RemoteViews remoteViews, int width, int height)
|
private void buildRemoteViews(View widgetView, RemoteViews remoteViews, int width,
|
||||||
|
int height)
|
||||||
{
|
{
|
||||||
|
if(habit.getId() == 4)
|
||||||
|
Log.d("BaseWidgetProvider", String.format("width=%d height=%d\n", width, height));
|
||||||
|
|
||||||
widgetView.invalidate();
|
widgetView.invalidate();
|
||||||
widgetView.setDrawingCacheEnabled(true);
|
widgetView.setDrawingCacheEnabled(true);
|
||||||
widgetView.buildDrawingCache(true);
|
widgetView.buildDrawingCache(true);
|
||||||
@@ -287,7 +300,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
int imageWidth = widgetView.getMeasuredWidth();
|
int imageWidth = widgetView.getMeasuredWidth();
|
||||||
int imageHeight = widgetView.getMeasuredHeight();
|
int imageHeight = widgetView.getMeasuredHeight();
|
||||||
int p[] = getPadding(width, height, imageWidth, imageHeight);
|
int p[] = getPadding(width, height, imageWidth, imageHeight);
|
||||||
|
if(habit.getId() == 4) Log.d("BaseWidgetProvider", Arrays.toString(p));
|
||||||
remoteViews.setViewPadding(R.id.buttonOverlay, p[0], p[1], p[2], p[3]);
|
remoteViews.setViewPadding(R.id.buttonOverlay, p[0], p[1], p[2], p[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user