Reputation: 31
Several others have found that OnAppWidgetOptionsChanged is not being called when resizing an AppWidget on Galaxy S3s, and the work around is to capture the com.sec.android.widgetapp.APPWIDGET_RESIZE
in onReceive as described here:
How to catch widget size changes on devices where onAppWidgetOptionsChanged not getting called?
In that approach, they get the height and width of the widget from the received intent:
int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
int widgetSpanY = intent.getIntExtra("widgetspany", 0);
And are then setting these into a Bundle as the values for OPTION_APPWIDGET_MIN_HEIGHT and OPTION_APPWIDGET_MIN_WIDTH, and subsequently passing them to a call to OnAppWidgetOptionsChanged.
Now, the problem I have is that the usual OnAppWidgetOptionsChanged callback provides both the min and the max dimensions for a resized widget in its bundle, not just the min. These values correspond to the dimensions for both the landscape and portrait modes of a widget:
For Portrait, the width is OPTION_APPWIDGET_MIN_WIDTH and the height is OPTION_APPWIDGET_MAX_HEIGHT.
For Landscape, the width is OPTION_APPWIDGET_MAX_WIDTH and the height is OPTION_APPWIDGET_MIN_HEIGHT.
The difference between a min and max dimension can be very large, as the given area covered by a landscape cell is much wider and shorter than a portrait cell. I need them both in order to work out the actual aspect ratio of a widget in both modes.
Are there any other values in that intent that might correspond to these min and max values, or are widgetspanx and widgetspany the only integer values available?
Or does the s3 provide another com.sec.android.widgetapp.APPWIDGET_RESIZE
event with different values when you change homescreen mode from portrait to landscape?
Unfortunately I don't have access to an S3 in order to be able to check the intent myself.
Upvotes: 2
Views: 357
Reputation: 31
Ok, I found a workaround.
I still don't have access to an S3 so can't see what else there might be in that com.sec.android.widgetapp.APPWIDGET_RESIZE
intent, but my suspicion is that Touchwiz is only going to provide the number of cells spanning each dimension, rather than the actual pixel bounds in DIPs.
That means I have to adapt the handleTouchWiz
function from here:
How to catch widget size changes on devices where onAppWidgetOptionsChanged not getting called?
and explicitly set the min and max ranges in DIPs myself.
It turns out that the Samsung Galaxy S5 (which I do have) has the same screen aspect ratio as the S3, just proportionally larger, and fortunately the S5 homescreen widget cell size also has the same aspect ratio as the S3.
So I can use the S5 cell dimensions in DIPs for the S3:
Now other launchers may have slight variations in the cell size, particular if they customise the cell grid to say 5x6 rather than the standard Touchwiz 4x4, but it is accurate enough for me.
So the following works fine, allowing me to derive a reasonably accurate widget aspect ratio in the resulting call to OnAppWidgetOptionsChanged
.
private void handleTouchWiz(Context context, Intent intent)
{
// Toast.makeText(context, "Calling handleTouchWiz for Samsung S3", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getIntExtra("widgetId", 0);
int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
int widgetSpanY = intent.getIntExtra("widgetspany", 0);
if(appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0)
{
Bundle newOptions = new Bundle();
// We have to convert these numbers for future use
// For Portrait
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 88);
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, widgetSpanY * 107);
// For Landscape
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, widgetSpanX * 143);
newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 64);
onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
}
Upvotes: 1