@Override public void onReceive(Context context, Intent intent) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); int id = intent.getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); String date = intent.getStringExtra(EXTRA_DATE); switch (intent.getAction()) { case ACTION_PREVIOUS_CLICK: RemoteViews remoteViews = getRemoteViews(context, id, date); if (remoteViews != null) { mSharedPreferenceHelper.setDateForWidget(date); appWidgetManager.updateAppWidget(id, remoteViews); } break; case ACTION_NEXT_CLICK: RemoteViews remoteViews1 = getRemoteViews(context, id, date); if (remoteViews1 != null) { appWidgetManager.updateAppWidget(id, remoteViews1); } break; case ACTION_TODAY_CLICK: RemoteViews remoteViews2 = getRemoteViews(context, id, date); if (remoteViews2 != null) { mSharedPreferenceHelper.setDateForWidget(date); appWidgetManager.updateAppWidget(id, remoteViews2); } break; case ACTION_UPDATE: int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); setRemotes(context, AppWidgetManager.getInstance(context), appWidgetIds); break; case ACTION_UPDATE_NOTE: appWidgetManager.notifyAppWidgetViewDataChanged( intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), R.id.list_widget); break; case ACTION_HEADER_CLICK: Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); break; } super.onReceive(context, intent); }
public RemoteViews getRemoteViews(Context context, int id, String date) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_timetable); Application.getComponent(context).inject(this); User user = mSharedPreferenceHelper.getUser(); mSharedPreferenceHelper.setDateForWidget(date); remoteViews.setTextViewText(R.id.value_widget, user.getTitleValue()); if (user.getType() < 2) { if (TimetableUtils.getDayOfWeek(date) == TimetableUtils.getDayOfWeek(TimetableUtils.todayDate()) && TimetableUtils.getParity(date) == TimetableUtils.getParity(TimetableUtils.todayDate())) remoteViews.setTextViewText( R.id.day_widget, String.format(context.getString(R.string.today), TimetableUtils.getDayOfWeek(date))); else remoteViews.setTextViewText(R.id.day_widget, TimetableUtils.getDayOfWeek(date)); remoteViews.setTextViewText(R.id.date_widget, TimetableUtils.getParity(date) + " неделя"); } else { if (TimetableUtils.todayDate().equals(date)) remoteViews.setTextViewText( R.id.day_widget, String.format(context.getString(R.string.today), TimetableUtils.getDayOfWeek(date))); else remoteViews.setTextViewText(R.id.day_widget, TimetableUtils.getDayOfWeek(date)); remoteViews.setTextViewText(R.id.date_widget, TimetableUtils.getDayAndMonth(date)); } setUpPreviousButton(context, remoteViews, id, date); setUpNextButton(context, remoteViews, id, date); setUpTodayButton(context, remoteViews, id); setUpAdapter(context, remoteViews, id, date); setUpHeaderClick(context, remoteViews, id); return remoteViews; }
private void setUpAdapter(Context context, RemoteViews remoteViews, int id, String date) { Intent intent = new Intent(context, TimetableWidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id); mSharedPreferenceHelper.setDateForWidget(date); remoteViews.setRemoteAdapter(R.id.list_widget, intent); remoteViews.setEmptyView(R.id.list_widget, R.id.empty_view); AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(id, R.id.list_widget); }