In App Widget, only update the size if it has changed.

This commit is contained in:
Jimmy Shih
2013-04-11 13:27:52 -07:00
parent 1ebe7baf7c
commit 19020f686f
@@ -82,30 +82,29 @@ public class TrackWidgetProvider extends AppWidgetProvider {
|| context.getString(R.string.track_stopped_broadcast_action).equals(action) || context.getString(R.string.track_stopped_broadcast_action).equals(action)
|| context.getString(R.string.track_update_broadcast_action).equals(action)) { || context.getString(R.string.track_update_broadcast_action).equals(action)) {
long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L); long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L);
updateAllWidgets(context, trackId); updateAllAppWidgets(context, trackId);
} }
} }
@Override @Override
public void onEnabled(Context context) { public void onEnabled(Context context) {
super.onEnabled(context);
// Need to update all widgets after phone reboot // Need to update all widgets after phone reboot
updateAllWidgets(context, -1L); updateAllAppWidgets(context, -1L);
} }
/** /**
* Updates all widgets. * Updates all app widgets.
* *
* @param context the context * @param context the context
* @param trackId track id * @param trackId track id
*/ */
private void updateAllWidgets(Context context, long trackId) { private void updateAllAppWidgets(Context context, long trackId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds( int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, TrackWidgetProvider.class)); new ComponentName(context, TrackWidgetProvider.class));
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
int size = ApiAdapterFactory.getApiAdapter().getAppWidgetSize(appWidgetManager, appWidgetId); updateAppWidget(context, appWidgetManager, appWidgetId);
RemoteViews remoteViews = getRemoteViews(context, trackId, size);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
} }
} }
@@ -113,28 +112,31 @@ public class TrackWidgetProvider extends AppWidgetProvider {
@Override @Override
public void onAppWidgetOptionsChanged( public void onAppWidgetOptionsChanged(
Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
if (newOptions != null) { if (newOptions != null) {
int size; int newSize;
if (newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1) if (newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1)
== AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) { == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
size = 1; newSize = 1;
} else { } else {
int height = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); int height = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
if (height == 0) { if (height == 0) {
size = 2; newSize = 2;
} else if (height >= FOUR_CELLS) { } else if (height >= FOUR_CELLS) {
size = 4; newSize = 4;
} else if (height >= THREE_CELLS) { } else if (height >= THREE_CELLS) {
size = 3; newSize = 3;
} else if (height >= TWO_CELLS) { } else if (height >= TWO_CELLS) {
size = 2; newSize = 2;
} else { } else {
size = 1; newSize = 1;
} }
} }
ApiAdapterFactory.getApiAdapter().setAppWidgetSize(appWidgetManager, appWidgetId, size); int size = ApiAdapterFactory.getApiAdapter().getAppWidgetSize(appWidgetManager, appWidgetId);
RemoteViews remoteViews = getRemoteViews(context, -1L, size); if (size != newSize) {
appWidgetManager.updateAppWidget(appWidgetId, remoteViews); ApiAdapterFactory.getApiAdapter().setAppWidgetSize(appWidgetManager, appWidgetId, newSize);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
} }
} }
@@ -148,7 +150,6 @@ public class TrackWidgetProvider extends AppWidgetProvider {
public static void updateAppWidget( public static void updateAppWidget(
Context context, AppWidgetManager appWidgetManager, int appWidgetId) { Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
int size = ApiAdapterFactory.getApiAdapter().getAppWidgetSize(appWidgetManager, appWidgetId); int size = ApiAdapterFactory.getApiAdapter().getAppWidgetSize(appWidgetManager, appWidgetId);
ApiAdapterFactory.getApiAdapter().setAppWidgetSize(appWidgetManager, appWidgetId, size);
RemoteViews remoteViews = getRemoteViews(context, -1L, size); RemoteViews remoteViews = getRemoteViews(context, -1L, size);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews); appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
} }