private NoteWidget getNoteWidgetForId(int appWidgetId) { NoteWidget noteWidget; synchronized (mWidgets) { noteWidget = mWidgets.get(appWidgetId); if (noteWidget == null) { noteWidget = new NoteWidget(appWidgetId); noteWidget.view = createNoteView(); mWidgets.put(appWidgetId, noteWidget); } } return noteWidget; }
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.w(TAG, "onConfigurationChanged: " + newConfig.orientation); mViewContext = new ContextThemeWrapper(this, getApplicationTheme(this)); synchronized (mWidgets) { for (int i = 0; i < mWidgets.size(); i++) { NoteWidget noteWidget = mWidgets.valueAt(i); noteWidget.view = createNoteView(); updateNoteWidgetSize(noteWidget.id); updateWidget(noteWidget.id); } } }
@SuppressLint("NewApi") private void updateNoteWidgetSize(int appWidgetId) { DisplayMetrics metrics = getResources().getDisplayMetrics(); int width, height; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { Bundle options = getAppWidgetManager().getAppWidgetOptions(appWidgetId); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { width = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT); } else { width = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH); height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); } } else { AppWidgetProviderInfo info = getAppWidgetManager().getAppWidgetInfo(appWidgetId); width = info.minWidth; height = info.minHeight; } NoteWidget noteWidget = getNoteWidgetForId(appWidgetId); noteWidget.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, metrics); noteWidget.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, metrics); }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Note note = data.get(position); // write data to preference SharedPreferences sp = getSharedPreferences(WIDGET_PREF, MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString(WIDGET_TITLE + widgetID, note.getTitle()); editor.apply(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(activity); NoteWidget.updateWidget(activity, appWidgetManager, sp, widgetID); // positive result setResult(RESULT_OK, resultValue); finish(); }