@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private int getWidgetWidthFromOptions(AppWidgetManager appWidgetManager, int appWidgetId) { Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId); if (options.containsKey(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)) { int minWidthDp = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); // The width returned is in dp, but we'll convert it to pixels to match the other widths DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, minWidthDp, displayMetrics); } return getResources().getDimensionPixelSize(R.dimen.widget_score_default_width); }
@SuppressLint("NewApi") @Override public void onUpdate(final Context context, final AppWidgetManager manager, final int[] ids) { for (int id : ids) { final Bundle bundle; if (AppConstants.Versions.feature16Plus) { bundle = manager.getAppWidgetOptions(id); } else { bundle = null; } addView(manager, context, id, bundle); } super.onUpdate(context, manager, ids); }
@Override protected void onHandleIntent(Intent intent) { mAppWidgetManager = AppWidgetManager.getInstance(this); mAppWidgetIds = mAppWidgetManager.getAppWidgetIds( new ComponentName(this, TodaysScoresWidgetProvider.class)); final int N = mAppWidgetIds.length; int weatherFart = R.drawable.manchester_city; String descr = "Clear"; double maxTemp = 24; Date fragmentdate = new Date(System.currentTimeMillis()); SimpleDateFormat mformat = new SimpleDateFormat("yyyy-MM-dd"); fragmentDateArray[0] = mformat.format(fragmentdate); Context context = getApplicationContext(); Uri scoreWithDate = DatabaseContract.scores_table.buildScoreWithDate(); // we'll query our contentProvider, as always Cursor cursor = context .getContentResolver() .query(scoreWithDate, TODAY__SCORES_PROJECTION, null, fragmentDateArray, null); if (cursor.moveToFirst()) { String awayTeam = cursor.getString(INDEX_AWAY); String awayGoals = cursor.getString(INDEX_AWAY_GOALS); String homeTeam = cursor.getString(INDEX_HOME); String homeGoals = cursor.getString(INDEX_HOME_GOALS); // On Honeycomb and higher devices, we can retrieve the size of the large icon // Prior to that, we use a fixed size String title = context.getString(R.string.app_name); for (int i = 0; i < N; i++) { int appWidgetId = mAppWidgetIds[i]; int layOutIdWidge; Bundle options = mAppWidgetManager.getAppWidgetOptions(appWidgetId); // TODO base case for now expand later if (options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) <= 110) { layOutIdWidge = R.layout.widget_today_scores; } else if (options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) >= 220) { layOutIdWidge = R.layout.widget_today_scores_large; } else { layOutIdWidge = R.layout.widget_today_scores_med; } RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), layOutIdWidge); remoteViews.setImageViewResource(R.id.widget_icon, R.drawable.ic_launcher); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { setRemoteContentDescription(remoteViews, homeTeam + " v " + awayTeam); } remoteViews.setTextViewText(R.id.home_name, homeTeam); remoteViews.setTextViewText(R.id.score_textview, homeGoals + ":" + awayGoals); remoteViews.setTextViewText(R.id.away_name, awayTeam); Intent fartTent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, fartTent, 0); remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent); mAppWidgetManager.updateAppWidget(appWidgetId, remoteViews); } } }