From 13c1976da81c4edb4f2bbd6e6a2f67d04758030f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Mon, 2 Oct 2023 23:52:14 +0200 Subject: [PATCH] render checkboxes in widgets via remote-view-checkboxes instead of markdown --- app/src/main/AndroidManifest.xml | 63 +++++++++---------- .../notes/reciever/WidgetCheckboxReciever.kt | 14 +++++ .../singlenote/SingleNoteWidgetFactory.java | 49 ++++++++++++++- .../layout-v31/widget_single_note_content.xml | 25 ++++++++ .../main/res/layout/widget_single_note.xml | 2 + .../res/layout/widget_single_note_content.xml | 14 ++++- 6 files changed, 129 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/reciever/WidgetCheckboxReciever.kt create mode 100644 app/src/main/res/layout-v31/widget_single_note_content.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5b23bd715..5eaf78f1e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,15 +20,21 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:targetApi="n"> + + android:theme="@style/Theme.App.Starting"> + + @@ -39,64 +45,64 @@ android:name="android.app.default_searchable" android:value=".android.activity.NotesListViewActivity" /> - - + android:exported="true" + android:label="@string/append_to_note"> + + - - - - - + android:windowSoftInputMode="stateHidden"> + + + + + @@ -104,22 +110,20 @@ - - - - - - @@ -127,9 +131,8 @@ - + android:exported="true" + android:label="@string/widget_single_note_title"> @@ -138,12 +141,10 @@ android:name="android.appwidget.provider" android:resource="@xml/single_note_widget_provider_info" /> - - + android:exported="true" + android:label="@string/widget_note_list_title"> @@ -156,22 +157,20 @@ - - + android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"> - - + + \ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/reciever/WidgetCheckboxReciever.kt b/app/src/main/java/it/niedermann/owncloud/notes/reciever/WidgetCheckboxReciever.kt new file mode 100644 index 000000000..b7ab6e8df --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/reciever/WidgetCheckboxReciever.kt @@ -0,0 +1,14 @@ +package it.niedermann.owncloud.notes.reciever + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.util.Log + +class WidgetCheckboxReciever : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + // This method is called when the BroadcastReceiver is receiving an Intent broadcast. + Log.e("RECIEVER", "Action: ${intent.action}") + } +} \ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java index 5369d3c70..49fcb640d 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java @@ -1,21 +1,33 @@ package it.niedermann.owncloud.notes.widget.singlenote; +import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_CHECKBOX_CHECKED; +import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_CHECKBOX_UNCHECKED; +import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_TEXT; + +import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.util.Log; +import android.view.View; import android.widget.RemoteViews; import android.widget.RemoteViewsService; +import android.widget.TextView; import androidx.annotation.Nullable; +import java.util.ArrayList; + import it.niedermann.android.markdown.MarkdownUtil; +import it.niedermann.android.markdown.remoteviews.RemoteViewElement; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.persistence.NotesRepository; import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData; +import it.niedermann.owncloud.notes.reciever.WidgetCheckboxReciever; public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFactory { @@ -26,6 +38,8 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa @Nullable private Note note; + private ArrayList noteElements = new ArrayList<>(); + private static final String TAG = SingleNoteWidget.class.getSimpleName(); SingleNoteWidgetFactory(Context context, Intent intent) { @@ -53,6 +67,7 @@ public void onDataSetChanged() { } else { Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet."); } + noteElements = MarkdownUtil.getRenderedElementsForRemoteView(context, note.getContent()); } @Override @@ -66,7 +81,7 @@ public void onDestroy() { */ @Override public int getCount() { - return (note != null) ? 1 : 0; + return (note != null) ? this.noteElements.size() : 0; } /** @@ -89,9 +104,29 @@ public RemoteViews getViewAt(int position) { args.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId()); fillInIntent.putExtras(args); + var item = noteElements.get(position); + final var note_content = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content); - note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent); - note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, note.getContent())); + var content = item.getCurrentLineBlock(); + int type = item.getType(); + + + if (type == TYPE_TEXT || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { + note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent); + note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, content).toString().trim()); + note_content.setViewVisibility(R.id.single_note_content_tv, View.VISIBLE); + } + + if (type == TYPE_CHECKBOX_CHECKED || type == TYPE_CHECKBOX_UNCHECKED ) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + note_content.setTextViewText(R.id.single_note_content_cb, content); + if(type == TYPE_CHECKBOX_CHECKED) { + note_content.setCompoundButtonChecked(R.id.single_note_content_cb, true); + } + note_content.setViewVisibility(R.id.single_note_content_cb, View.VISIBLE); + note_content.setOnClickPendingIntent(R.id.single_note_content_cb, getPendingIntent()); + } + } return note_content; } @@ -117,4 +152,12 @@ public long getItemId(int position) { public boolean hasStableIds() { return true; } + + private PendingIntent getPendingIntent() { + Intent checkboxIntent = new Intent(context, WidgetCheckboxReciever.class); + checkboxIntent.setAction("toggle"); + return PendingIntent.getBroadcast(context, 0, checkboxIntent, PendingIntent.FLAG_IMMUTABLE); + + } + } diff --git a/app/src/main/res/layout-v31/widget_single_note_content.xml b/app/src/main/res/layout-v31/widget_single_note_content.xml new file mode 100644 index 000000000..5ca089f17 --- /dev/null +++ b/app/src/main/res/layout-v31/widget_single_note_content.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/widget_single_note.xml b/app/src/main/res/layout/widget_single_note.xml index ac1370835..62d39a40a 100644 --- a/app/src/main/res/layout/widget_single_note.xml +++ b/app/src/main/res/layout/widget_single_note.xml @@ -11,6 +11,8 @@ android:layout_height="match_parent" android:paddingVertical="@dimen/widget_inner_padding_vertical" android:paddingHorizontal="@dimen/widget_inner_padding_horizontal" + android:dividerHeight="0dp" + android:divider="@null" tools:listitem="@layout/widget_single_note_content" /> - + + + + \ No newline at end of file