Skip to content

Commit b6e87cc

Browse files
Merge pull request #2887 from nextcloud/fix/single-note-widget
fix: single note widget
2 parents 01f7428 + 9f450f5 commit b6e87cc

File tree

4 files changed

+87
-59
lines changed

4 files changed

+87
-59
lines changed

app/src/main/java/it/niedermann/owncloud/notes/edit/EditNoteActivity.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ protected void onCreate(final Bundle savedInstanceState) {
7777

7878
repo = NotesRepository.getInstance(getApplicationContext());
7979

80-
try {
81-
if (SingleAccountHelper.getCurrentSingleSignOnAccount(this) == null) {
82-
throw new NoCurrentAccountSelectedException(this);
80+
new Thread(() -> {
81+
try {
82+
if (SingleAccountHelper.getCurrentSingleSignOnAccount(EditNoteActivity.this) == null) {
83+
throw new NoCurrentAccountSelectedException(EditNoteActivity.this);
84+
}
85+
} catch (Exception e) {
86+
runOnUiThread(() -> {
87+
Toast.makeText(EditNoteActivity.this, R.string.no_account_configured_yet, Toast.LENGTH_LONG).show();
88+
finish();
89+
});
8390
}
84-
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
85-
Toast.makeText(this, R.string.no_account_configured_yet, Toast.LENGTH_LONG).show();
86-
finish();
87-
return;
88-
}
91+
}).start();
8992

9093
final var preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
9194
new SharedPreferenceBooleanLiveData(preferences, getString(R.string.pref_key_keep_screen_on), true).observe(this, keepScreenOn -> {

app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ class NoteListWidget : AppWidgetProvider() {
5959
)
6060
}
6161

62-
if (intent.extras == null) {
63-
Log.w(TAG, "Intent doesn't have bundle")
64-
return
65-
}
66-
6762
Log.w(TAG, "Update widget via given appWidgetIds")
6863

6964
val appWidgetIds = intArrayOf(intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) ?: -1)

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

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import android.content.ComponentName
1313
import android.content.Context
1414
import android.content.Intent
1515
import android.widget.RemoteViews
16+
import androidx.core.net.toUri
1617
import it.niedermann.owncloud.notes.R
17-
import it.niedermann.owncloud.notes.edit.BaseNoteFragment
1818
import it.niedermann.owncloud.notes.edit.EditNoteActivity
1919
import it.niedermann.owncloud.notes.persistence.NotesRepository
20-
import it.niedermann.owncloud.notes.shared.util.WidgetUtil
2120
import java.util.concurrent.ExecutorService
2221
import java.util.concurrent.Executors
23-
import androidx.core.net.toUri
2422

2523
class SingleNoteWidget : AppWidgetProvider() {
2624
private val executor: ExecutorService = Executors.newCachedThreadPool()
@@ -52,44 +50,69 @@ class SingleNoteWidget : AppWidgetProvider() {
5250
super.onDeleted(context, appWidgetIds)
5351
}
5452

55-
companion object {
56-
private val TAG: String = SingleNoteWidget::class.java.getSimpleName()
57-
fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) {
58-
val templateIntent = Intent(context, EditNoteActivity::class.java)
59-
val repo = NotesRepository.getInstance(context)
53+
private fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) {
54+
val repo = NotesRepository.getInstance(context)
55+
appWidgetIds.forEach { appWidgetId ->
56+
repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
57+
val pendingIntent = getPendingIntent(context, appWidgetId)
58+
val serviceIntent = getServiceIntent(context, appWidgetId)
59+
val views = getRemoteViews(context, pendingIntent, serviceIntent)
60+
awm.run {
61+
updateAppWidget(appWidgetId, views)
62+
notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv)
63+
}
64+
}
65+
}
66+
}
6067

61-
appWidgetIds.forEach { appWidgetId ->
62-
repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
63-
templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.accountId)
68+
private fun getPendingIntent(
69+
context: Context,
70+
id: Int
71+
): PendingIntent {
72+
val intent = Intent(context, EditNoteActivity::class.java).apply {
73+
setPackage(context.packageName)
74+
}
6475

65-
val serviceIntent = Intent(context, SingleNoteWidgetService::class.java).apply {
66-
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
67-
setData(toUri(Intent.URI_INTENT_SCHEME).toUri())
68-
}
76+
val pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT or
77+
PendingIntent.FLAG_MUTABLE or
78+
Intent.FILL_IN_COMPONENT
6979

80+
return PendingIntent.getActivity(
81+
context,
82+
id,
83+
intent,
84+
pendingIntentFlags
85+
)
86+
}
7087

71-
val views = RemoteViews(context.packageName, R.layout.widget_single_note).apply {
72-
setPendingIntentTemplate(
73-
R.id.single_note_widget_lv, PendingIntent.getActivity(
74-
context, appWidgetId, templateIntent,
75-
WidgetUtil.pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT)
76-
)
77-
)
78-
setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent)
79-
setEmptyView(
80-
R.id.single_note_widget_lv,
81-
R.id.widget_single_note_placeholder_tv
82-
)
83-
}
88+
private fun getServiceIntent(context: Context, id: Int): Intent {
89+
return Intent(context, SingleNoteWidgetService::class.java).apply {
90+
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
91+
val dataFlag = toUri(Intent.URI_INTENT_SCHEME) + id
92+
val dataUri = dataFlag.toUri()
93+
setData(dataUri)
94+
}
95+
}
8496

85-
awm.run {
86-
updateAppWidget(appWidgetId, views)
87-
notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv)
88-
}
89-
}
90-
}
97+
private fun getRemoteViews(
98+
context: Context,
99+
pendingIntent: PendingIntent,
100+
serviceIntent: Intent
101+
): RemoteViews {
102+
return RemoteViews(
103+
context.packageName,
104+
R.layout.widget_single_note
105+
).apply {
106+
setPendingIntentTemplate(R.id.single_note_widget_lv, pendingIntent)
107+
setEmptyView(
108+
R.id.single_note_widget_lv,
109+
R.id.widget_single_note_placeholder_tv
110+
)
111+
setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent)
91112
}
113+
}
92114

115+
companion object {
93116
@JvmStatic
94117
fun updateSingleNoteWidgets(context: Context) {
95118
val intent = Intent(context, SingleNoteWidget::class.java).apply {

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
import android.appwidget.AppWidgetManager;
1010
import android.content.Context;
1111
import android.content.Intent;
12-
import android.os.Bundle;
1312
import android.util.Log;
1413
import android.widget.RemoteViews;
1514
import android.widget.RemoteViewsService;
1615

1716
import androidx.annotation.Nullable;
1817

18+
import com.owncloud.android.lib.common.utils.Log_OC;
19+
1920
import it.niedermann.android.markdown.MarkdownUtil;
2021
import it.niedermann.owncloud.notes.R;
2122
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
2223
import it.niedermann.owncloud.notes.persistence.NotesRepository;
2324
import it.niedermann.owncloud.notes.persistence.entity.Note;
24-
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
2525

2626
public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
2727

@@ -89,17 +89,24 @@ public RemoteViews getViewAt(int position) {
8989
}
9090

9191
final var fillInIntent = new Intent();
92-
final var args = new Bundle();
93-
94-
args.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
95-
args.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
96-
fillInIntent.putExtras(args);
92+
fillInIntent.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId());
93+
fillInIntent.putExtra(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
9794

98-
final var note_content = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content);
99-
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
100-
note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, note.getContent()));
95+
final var noteContent = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content);
96+
noteContent.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
10197

102-
return note_content;
98+
CharSequence rendered;
99+
try {
100+
rendered = MarkdownUtil.renderForRemoteView(context, note.getContent());
101+
if (rendered == null) {
102+
rendered = "";
103+
}
104+
} catch (Exception e) {
105+
Log_OC.e(TAG, "Markdown rendering failed", e);
106+
rendered = note.getContent();
107+
}
108+
noteContent.setTextViewText(R.id.single_note_content_tv, rendered);
109+
return noteContent;
103110
}
104111

105112

@@ -116,7 +123,7 @@ public int getViewTypeCount() {
116123

117124
@Override
118125
public long getItemId(int position) {
119-
return position;
126+
return note != null ? note.getId() : position;
120127
}
121128

122129
@Override

0 commit comments

Comments
 (0)