private static void updateWidget( Context context, AppWidgetManager appWidgetManager, WidgetModel widgetModel, ArrayList<ArrivalModel> arrivals) { // Set the base layout to use int layoutResId = widgetModel.getSize() == WidgetUtils.SIZE_1X1 ? R.layout.widget_layout_1x1 : R.layout.widget_layout_2x1; RemoteViews updatedViews = new RemoteViews(context.getPackageName(), layoutResId); // Set widget background colors updatedViews.setInt( R.id.tv_widget_title, SET_BACKGROUND_COLOR, WidgetUtils.getPrimaryColorInt(context, widgetModel.getBgColor())); updatedViews.setInt( R.id.layout_widget_arrivals, SET_BACKGROUND_COLOR, WidgetUtils.getSecondaryColorInt(context, widgetModel.getBgColor())); // If the user set the widget color to white, we have to change the colors of // some UI elements so that they show properly. if (widgetModel.getBgColor() == WidgetUtils.BG_WHITE) { updatedViews.setTextColor(R.id.tv_widget_title, Color.DKGRAY); updatedViews.setTextColor(R.id.tv_widget_arrival_1, Color.DKGRAY); updatedViews.setTextColor(R.id.tv_widget_arrival_2, Color.DKGRAY); updatedViews.setInt(R.id.iv_widget_divider_1, SET_BACKGROUND_COLOR, Color.DKGRAY); if (getNumArrivalsToShow(widgetModel) == 3) { // There is a third arrival text to update updatedViews.setTextColor(R.id.tv_widget_arrival_3, Color.DKGRAY); updatedViews.setInt(R.id.iv_widget_divider_2, SET_BACKGROUND_COLOR, Color.DKGRAY); } } // Set the widget title updatedViews.setTextViewText(R.id.tv_widget_title, widgetModel.getTitle()); // Update the shown arrival times for (int i = 0; i < Math.min(arrivals.size(), getNumArrivalsToShow(widgetModel)); i++) { updatedViews.setTextViewText( ARRIVAL_TEXTVIEW_RES_ID[i], String.valueOf(arrivals.get(i).getMinsUntilArrival())); } // Set the widget to refresh on click updatedViews.setOnClickPendingIntent( R.id.tv_widget_title, createRefreshIntent(context, widgetModel.getWidgetId())); updatedViews.setOnClickPendingIntent( R.id.layout_widget_arrivals, createRefreshIntent(context, widgetModel.getWidgetId())); // Update the widget view appWidgetManager.updateAppWidget(widgetModel.getWidgetId(), updatedViews); }
private static int getNumArrivalsToShow(WidgetModel widgetModel) { return widgetModel.getSize() == WidgetUtils.SIZE_1X1 ? 2 : 3; }