diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-05-12 11:49:49 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-05-12 11:49:49 +0300 |
commit | 2b7cda9fb101d60f18ae33a874f646927bb820f5 (patch) | |
tree | ce229ecbdfdc994471d50414622234efba296c51 /app/src/main/java/it/niedermann/nextcloud/deck/ui/widget | |
parent | 7b6786158c06bb9d4332208372922002ccd7f382 (diff) |
Widget Test
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/widget')
3 files changed, 204 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidget.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidget.java new file mode 100644 index 000000000..cba5e5fdb --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidget.java @@ -0,0 +1,70 @@ +package it.niedermann.nextcloud.deck.ui.widget.singlecard; + +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.widget.RemoteViews; + +import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; +import it.niedermann.nextcloud.deck.ui.card.EditActivity; +import it.niedermann.owncloud.notes.R; + +public class SingleCardWidget extends AppWidgetProvider { + + void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidgetIds) { + final SyncManager syncManager = new SyncManager(context); + + for (int appWidgetId : appWidgetIds) { + syncManager.getSingleCardWidgetModel(appWidgetId).observe(this, (model) -> { + + Intent templateIntent = EditActivity.createEditCardIntent(context, model.getAccount(), model.getBoardLocalId(), model.getCardLocalId()); + + PendingIntent templatePendingIntent = PendingIntent.getActivity(context, appWidgetId, templateIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + Intent serviceIntent = new Intent(context, SingleCardWidgetService.class); + serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); + + RemoteViews views; + + views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note); + views.setPendingIntentTemplate(R.id.single_note_widget_lv, templatePendingIntent); + views.setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent); + views.setEmptyView(R.id.single_note_widget_lv, R.id.widget_single_note_placeholder_tv); + awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv); + awm.updateAppWidget(appWidgetId, views); + }); + } + } + + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.onUpdate(context, appWidgetManager, appWidgetIds); + updateAppWidget(context, appWidgetManager, appWidgetIds); + } + + @Override + public void onReceive(Context context, Intent intent) { + super.onReceive(context, intent); + AppWidgetManager awm = AppWidgetManager.getInstance(context); + + updateAppWidget(context, AppWidgetManager.getInstance(context), + (awm.getAppWidgetIds(new ComponentName(context, SingleCardWidget.class)))); + } + + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + final SyncManager syncManager = new SyncManager(context); + + for (int appWidgetId : appWidgetIds) { + syncManager.deleteSingleCardWidgetModel(appWidgetId); + } + + super.onDeleted(context, appWidgetIds); + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetFactory.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetFactory.java new file mode 100644 index 000000000..9ca6d476a --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetFactory.java @@ -0,0 +1,123 @@ +package it.niedermann.nextcloud.deck.ui.widget.singlecard; + +import android.appwidget.AppWidgetManager; +import android.content.Context; +import android.content.Intent; +import android.util.Log; +import android.widget.RemoteViews; +import android.widget.RemoteViewsService; + +import it.niedermann.nextcloud.deck.model.SingleCardWidgetModel; +import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; +import it.niedermann.nextcloud.deck.ui.card.EditActivity; +import it.niedermann.owncloud.notes.R; + +public class SingleCardWidgetFactory implements RemoteViewsService.RemoteViewsFactory { + + private final Context context; + private final int appWidgetId; + + private SyncManager syncManager; + private SingleCardWidgetModel singleCardWidgetModel; + + private static final String TAG = SingleCardWidget.class.getSimpleName(); + + SingleCardWidgetFactory(Context context, Intent intent) { + this.context = context; + appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + } + + @Override + public void onCreate() { + syncManager = new SyncManager(context); + } + + + @Override + public void onDataSetChanged() { + syncManager.getSingleCardWidgetModel(appWidgetId).observe(this, (model) -> { + long noteID = sp.getLong(SingleCardWidget.WIDGET_KEY + appWidgetId, -1); + + if (noteID >= 0) { + Log.v(TAG, "Fetch note for account " + SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId); + Log.v(TAG, "Fetch note for account " + sp.getLong(SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId, -1)); + Log.v(TAG, "Fetch note with id " + noteID); + singleCardWidgetModel = syncManager.getNote(sp.getLong(SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId, -1), noteID); + + if (singleCardWidgetModel == null) { + Log.e(TAG, "Error: note not found"); + } + } + }); + } + + @Override + public void onDestroy() { + //NoOp + } + + /** + * Returns the number of items in the data set. In this case, always 1 as a single note is + * being displayed. Will return 0 when the note can't be displayed. + */ + @Override + public int getCount() { + return (singleCardWidgetModel != null) ? 1 : 0; + } + + /** + * Returns a RemoteView containing the note content in a TextView and + * a fillInIntent to handle the user tapping on the item in the list view. + * + * @param position The position of the item in the list + * @return The RemoteView at the specified position in the list + */ + @Override + public RemoteViews getViewAt(int position) { + if (singleCardWidgetModel == null) { + return null; + } + long noteID = sp.getLong(SingleCardWidget.WIDGET_KEY + appWidgetId, -1); + + if (noteID >= 0) { + Log.v(TAG, "Fetch note for account " + SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId); + Log.v(TAG, "Fetch note for account " + sp.getLong(SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId, -1)); + Log.v(TAG, "Fetch note with id " + noteID); + singleCardWidgetModel = syncManager.getNote(sp.getLong(SingleCardWidget.ACCOUNT_ID_KEY + appWidgetId, -1), noteID); + + if (singleCardWidgetModel == null) { + Log.e(TAG, "Error: note not found"); + } + } + + RemoteViews widgetRemoteView; + final Intent fillInIntent = EditActivity.createEditCardIntent(context, singleCardWidgetModel.getAccount(), singleCardWidgetModel.getBoardLocalId(), singleCardWidgetModel.getCardLocalId().getLocalId()); + widgetRemoteView = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content); + widgetRemoteView.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent); + widgetRemoteView.setTextViewText(R.id.single_note_content_tv, singleCardWidgetModel.getCardLocalId().getCard().getTitle()); + + return widgetRemoteView; + } + + + // TODO Set loading view + @Override + public RemoteViews getLoadingView() { + return null; + } + + @Override + public int getViewTypeCount() { + return 1; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public boolean hasStableIds() { + return true; + } +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetService.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetService.java new file mode 100644 index 000000000..d938e383a --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/widget/singlecard/SingleCardWidgetService.java @@ -0,0 +1,11 @@ +package it.niedermann.nextcloud.deck.ui.widget.singlecard; + +import android.content.Intent; +import android.widget.RemoteViewsService; + +public class SingleCardWidgetService extends RemoteViewsService { + @Override + public RemoteViewsFactory onGetViewFactory(Intent intent) { + return new SingleCardWidgetFactory(this.getApplicationContext(), intent); + } +} |