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.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
{
|
||||
|
||||
private int portraitWidth, portraitHeight;
|
||||
private int landscapeWidth, landscapeHeight;
|
||||
private class WidgetDimensions
|
||||
{
|
||||
public int portraitWidth, portraitHeight;
|
||||
public int landscapeWidth, landscapeHeight;
|
||||
}
|
||||
|
||||
protected abstract int getDefaultHeight();
|
||||
|
||||
@@ -98,7 +101,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
private void updateWidget(Context context, AppWidgetManager manager,
|
||||
int widgetId, Bundle options)
|
||||
{
|
||||
updateWidgetSize(context, options);
|
||||
WidgetDimensions dim = getWidgetDimensions(context, options);
|
||||
|
||||
Context appContext = context.getApplicationContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
|
||||
@@ -113,7 +116,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
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)
|
||||
@@ -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 minWidth = getDefaultWidth();
|
||||
@@ -178,11 +181,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT));
|
||||
}
|
||||
|
||||
portraitWidth = minWidth;
|
||||
portraitHeight = maxHeight;
|
||||
|
||||
landscapeWidth = maxWidth;
|
||||
landscapeHeight = minHeight;
|
||||
WidgetDimensions ws = new WidgetDimensions();
|
||||
ws.portraitWidth = minWidth;
|
||||
ws.portraitHeight = maxHeight;
|
||||
ws.landscapeWidth = maxWidth;
|
||||
ws.landscapeHeight = minHeight;
|
||||
return ws;
|
||||
}
|
||||
|
||||
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 Habit habit;
|
||||
private final AppWidgetManager manager;
|
||||
public RemoteViews portraitRemoteViews, landscapeRemoteViews;
|
||||
public View portraitWidgetView, landscapeWidgetView;
|
||||
private RemoteViews portraitRemoteViews, landscapeRemoteViews;
|
||||
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)
|
||||
{
|
||||
this.widgetId = widgetId;
|
||||
this.context = context;
|
||||
this.habit = habit;
|
||||
this.manager = manager;
|
||||
this.dim = ws;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -232,11 +238,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
|
||||
portraitRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||
portraitWidgetView = buildCustomView(context, habit);
|
||||
measureCustomView(context, portraitWidth, portraitHeight, portraitWidgetView);
|
||||
measureCustomView(context, dim.portraitWidth, dim.portraitHeight, portraitWidgetView);
|
||||
|
||||
landscapeRemoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||
landscapeWidgetView = buildCustomView(context, habit);
|
||||
measureCustomView(context, landscapeWidth, landscapeHeight, landscapeWidgetView);
|
||||
measureCustomView(context, dim.landscapeWidth, dim.landscapeHeight,
|
||||
landscapeWidgetView);
|
||||
}
|
||||
|
||||
private void updateAppWidget()
|
||||
@@ -260,8 +267,10 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
{
|
||||
try
|
||||
{
|
||||
buildRemoteViews(portraitWidgetView, portraitRemoteViews, portraitWidth, portraitHeight);
|
||||
buildRemoteViews(landscapeWidgetView, landscapeRemoteViews, landscapeWidth, landscapeHeight);
|
||||
buildRemoteViews(portraitWidgetView, portraitRemoteViews,
|
||||
dim.portraitWidth, dim.portraitHeight);
|
||||
buildRemoteViews(landscapeWidgetView, landscapeRemoteViews,
|
||||
dim.landscapeWidth, dim.landscapeHeight);
|
||||
updateAppWidget();
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -273,8 +282,12 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
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.setDrawingCacheEnabled(true);
|
||||
widgetView.buildDrawingCache(true);
|
||||
@@ -287,7 +300,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||
int imageWidth = widgetView.getMeasuredWidth();
|
||||
int imageHeight = widgetView.getMeasuredHeight();
|
||||
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]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user