Skip to content

Commit 13c1976

Browse files
newhintonAndyScherzinger
authored andcommitted
render checkboxes in widgets via remote-view-checkboxes instead of markdown
1 parent b0248e6 commit 13c1976

File tree

6 files changed

+129
-38
lines changed

6 files changed

+129
-38
lines changed

app/src/main/AndroidManifest.xml

+31-32
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
android:supportsRtl="true"
2121
android:theme="@style/AppTheme"
2222
tools:targetApi="n">
23+
<receiver
24+
android:name=".reciever.WidgetCheckboxReciever"
25+
android:enabled="true"
26+
android:exported="true" />
2327

2428
<activity
2529
android:name=".main.MainActivity"
30+
android:exported="true"
2631
android:label="@string/app_name"
27-
android:theme="@style/Theme.App.Starting"
28-
android:exported="true">
32+
android:theme="@style/Theme.App.Starting">
2933
<intent-filter>
3034
<action android:name="android.intent.action.MAIN" />
35+
3136
<category android:name="android.intent.category.LAUNCHER" />
37+
3238
<action android:name="android.intent.action.SEARCH" />
3339
</intent-filter>
3440

@@ -39,97 +45,94 @@
3945
android:name="android.app.default_searchable"
4046
android:value=".android.activity.NotesListViewActivity" />
4147
</activity>
42-
4348
<activity
4449
android:name=".importaccount.ImportAccountActivity"
4550
android:label="@string/add_account" />
46-
4751
<activity
4852
android:name=".AppendToNoteActivity"
49-
android:label="@string/append_to_note"
50-
android:exported="true">
53+
android:exported="true"
54+
android:label="@string/append_to_note">
5155
<intent-filter>
5256
<action android:name="android.intent.action.SEND" />
57+
5358
<category android:name="android.intent.category.DEFAULT" />
59+
5460
<data android:mimeType="text/*" />
5561
</intent-filter>
5662
</activity>
57-
5863
<activity
5964
android:name=".exception.ExceptionActivity"
6065
android:label="@string/app_name" />
61-
6266
<activity
6367
android:name=".FormattingHelpActivity"
6468
android:label="@string/action_formatting_help"
6569
android:parentActivityName=".main.MainActivity"
6670
android:windowSoftInputMode="stateHidden" />
67-
6871
<activity
6972
android:name=".manageaccounts.ManageAccountsActivity"
7073
android:label="@string/manage_accounts"
7174
android:parentActivityName=".main.MainActivity"
7275
android:windowSoftInputMode="stateHidden" />
73-
7476
<activity
7577
android:name=".preferences.PreferencesActivity"
7678
android:label="@string/action_settings"
7779
android:parentActivityName=".main.MainActivity"
7880
android:windowSoftInputMode="stateHidden" />
79-
8081
<activity
8182
android:name=".edit.EditNoteActivity"
83+
android:exported="true"
8284
android:label="@string/simple_edit"
8385
android:launchMode="singleTask"
8486
android:parentActivityName=".main.MainActivity"
85-
android:windowSoftInputMode="stateHidden"
86-
android:exported="true">
87+
android:windowSoftInputMode="stateHidden">
8788
<intent-filter android:label="@string/action_create">
8889
<action android:name="android.intent.action.SEND" />
90+
8991
<category android:name="android.intent.category.DEFAULT" />
92+
9093
<data android:mimeType="text/plain" />
9194
</intent-filter>
9295
<!-- Voice command "note to self" in google search -->
9396
<intent-filter android:label="@string/action_create">
9497
<action android:name="com.google.android.gm.action.AUTO_SEND" />
98+
9599
<category android:name="android.intent.category.DEFAULT" />
100+
96101
<data android:mimeType="text/plain" />
97102
</intent-filter>
98103
<intent-filter android:label="@string/app_name">
99104
<action android:name="android.intent.action.VIEW" />
105+
100106
<category android:name="android.intent.category.DEFAULT" />
101107

102108
<data android:scheme="content" />
103109
<data android:host="*" />
104110
<data android:mimeType="text/*" />
105111
</intent-filter>
106112
</activity>
107-
108113
<activity
109114
android:name=".about.AboutActivity"
110115
android:label="@string/simple_about"
111116
android:parentActivityName=".main.MainActivity" />
112-
113-
<activity android:name=".widget.singlenote.SingleNoteWidgetConfigurationActivity"
117+
<activity
118+
android:name=".widget.singlenote.SingleNoteWidgetConfigurationActivity"
114119
android:exported="true">
115120
<intent-filter>
116121
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
117122
</intent-filter>
118123
</activity>
119-
120-
<activity android:name=".widget.notelist.NoteListWidgetConfigurationActivity"
124+
<activity
125+
android:name=".widget.notelist.NoteListWidgetConfigurationActivity"
121126
android:exported="true">
122-
123127
<intent-filter>
124128
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
125129
</intent-filter>
126130
</activity>
127131

128132
<receiver
129133
android:name=".widget.singlenote.SingleNoteWidget"
130-
android:label="@string/widget_single_note_title"
131-
android:exported="true">
132-
134+
android:exported="true"
135+
android:label="@string/widget_single_note_title">
133136
<intent-filter>
134137
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
135138
</intent-filter>
@@ -138,12 +141,10 @@
138141
android:name="android.appwidget.provider"
139142
android:resource="@xml/single_note_widget_provider_info" />
140143
</receiver>
141-
142144
<receiver
143145
android:name=".widget.notelist.NoteListWidget"
144-
android:label="@string/widget_note_list_title"
145-
android:exported="true">
146-
146+
android:exported="true"
147+
android:label="@string/widget_note_list_title">
147148
<intent-filter>
148149
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
149150
</intent-filter>
@@ -156,22 +157,20 @@
156157
<service
157158
android:name=".widget.singlenote.SingleNoteWidgetService"
158159
android:permission="android.permission.BIND_REMOTEVIEWS" />
159-
160160
<service
161161
android:name=".widget.notelist.NoteListWidgetService"
162162
android:permission="android.permission.BIND_REMOTEVIEWS" />
163-
164163
<service
165164
android:name=".quicksettings.NewNoteTileService"
166165
android:description="@string/action_create"
166+
android:exported="true"
167167
android:icon="@drawable/ic_launcher_foreground_full"
168168
android:label="@string/action_create"
169-
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
170-
android:exported="true">
169+
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
171170
<intent-filter>
172171
<action android:name="android.service.quicksettings.action.QS_TILE" />
173172
</intent-filter>
174173
</service>
175-
176174
</application>
177-
</manifest>
175+
176+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package it.niedermann.owncloud.notes.reciever
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.util.Log
7+
8+
class WidgetCheckboxReciever : BroadcastReceiver() {
9+
10+
override fun onReceive(context: Context, intent: Intent) {
11+
// This method is called when the BroadcastReceiver is receiving an Intent broadcast.
12+
Log.e("RECIEVER", "Action: ${intent.action}")
13+
}
14+
}

app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
package it.niedermann.owncloud.notes.widget.singlenote;
22

3+
import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_CHECKBOX_CHECKED;
4+
import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_CHECKBOX_UNCHECKED;
5+
import static it.niedermann.android.markdown.remoteviews.RemoteViewElement.TYPE_TEXT;
6+
7+
import android.app.PendingIntent;
38
import android.appwidget.AppWidgetManager;
49
import android.content.Context;
510
import android.content.Intent;
11+
import android.os.Build;
612
import android.os.Bundle;
713
import android.util.Log;
14+
import android.view.View;
815
import android.widget.RemoteViews;
916
import android.widget.RemoteViewsService;
17+
import android.widget.TextView;
1018

1119
import androidx.annotation.Nullable;
1220

21+
import java.util.ArrayList;
22+
1323
import it.niedermann.android.markdown.MarkdownUtil;
24+
import it.niedermann.android.markdown.remoteviews.RemoteViewElement;
1425
import it.niedermann.owncloud.notes.R;
1526
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
1627
import it.niedermann.owncloud.notes.persistence.NotesRepository;
1728
import it.niedermann.owncloud.notes.persistence.entity.Note;
1829
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
30+
import it.niedermann.owncloud.notes.reciever.WidgetCheckboxReciever;
1931

2032
public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
2133

@@ -26,6 +38,8 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa
2638
@Nullable
2739
private Note note;
2840

41+
private ArrayList<RemoteViewElement> noteElements = new ArrayList<>();
42+
2943
private static final String TAG = SingleNoteWidget.class.getSimpleName();
3044

3145
SingleNoteWidgetFactory(Context context, Intent intent) {
@@ -53,6 +67,7 @@ public void onDataSetChanged() {
5367
} else {
5468
Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet.");
5569
}
70+
noteElements = MarkdownUtil.getRenderedElementsForRemoteView(context, note.getContent());
5671
}
5772

5873
@Override
@@ -66,7 +81,7 @@ public void onDestroy() {
6681
*/
6782
@Override
6883
public int getCount() {
69-
return (note != null) ? 1 : 0;
84+
return (note != null) ? this.noteElements.size() : 0;
7085
}
7186

7287
/**
@@ -89,9 +104,29 @@ public RemoteViews getViewAt(int position) {
89104
args.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
90105
fillInIntent.putExtras(args);
91106

107+
var item = noteElements.get(position);
108+
92109
final var note_content = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content);
93-
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
94-
note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, note.getContent()));
110+
var content = item.getCurrentLineBlock();
111+
int type = item.getType();
112+
113+
114+
if (type == TYPE_TEXT || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
115+
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
116+
note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, content).toString().trim());
117+
note_content.setViewVisibility(R.id.single_note_content_tv, View.VISIBLE);
118+
}
119+
120+
if (type == TYPE_CHECKBOX_CHECKED || type == TYPE_CHECKBOX_UNCHECKED ) {
121+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
122+
note_content.setTextViewText(R.id.single_note_content_cb, content);
123+
if(type == TYPE_CHECKBOX_CHECKED) {
124+
note_content.setCompoundButtonChecked(R.id.single_note_content_cb, true);
125+
}
126+
note_content.setViewVisibility(R.id.single_note_content_cb, View.VISIBLE);
127+
note_content.setOnClickPendingIntent(R.id.single_note_content_cb, getPendingIntent());
128+
}
129+
}
95130

96131
return note_content;
97132
}
@@ -117,4 +152,12 @@ public long getItemId(int position) {
117152
public boolean hasStableIds() {
118153
return true;
119154
}
155+
156+
private PendingIntent getPendingIntent() {
157+
Intent checkboxIntent = new Intent(context, WidgetCheckboxReciever.class);
158+
checkboxIntent.setAction("toggle");
159+
return PendingIntent.getBroadcast(context, 0, checkboxIntent, PendingIntent.FLAG_IMMUTABLE);
160+
161+
}
162+
120163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:orientation="vertical">
7+
8+
<TextView
9+
android:id="@+id/single_note_content_tv"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:lineSpacingMultiplier="@dimen/note_line_spacing"
13+
android:textColor="@color/widget_foreground"
14+
android:visibility="gone"
15+
tools:text="@tools:sample/lorem/random" />
16+
17+
<CheckBox
18+
android:id="@+id/single_note_content_cb"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:layout_weight="1"
22+
android:visibility="gone"
23+
android:text="CheckBox" />
24+
25+
</LinearLayout>

app/src/main/res/layout/widget_single_note.xml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
android:layout_height="match_parent"
1212
android:paddingVertical="@dimen/widget_inner_padding_vertical"
1313
android:paddingHorizontal="@dimen/widget_inner_padding_horizontal"
14+
android:dividerHeight="0dp"
15+
android:divider="@null"
1416
tools:listitem="@layout/widget_single_note_content" />
1517

1618
<TextView
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:orientation="vertical">
7+
8+
<TextView
49
android:id="@+id/single_note_content_tv"
510
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
11+
android:layout_height="wrap_content"
712
android:lineSpacingMultiplier="@dimen/note_line_spacing"
813
android:textColor="@color/widget_foreground"
14+
android:visibility="gone"
915
tools:text="@tools:sample/lorem/random" />
16+
17+
</LinearLayout>

0 commit comments

Comments
 (0)