Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: bridge pinned notes widget update to native module

kashaf-ansari-dev00a38886c550
diff --git a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java
index ac04cebcb..97a96756b 100644
--- a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java
+++ b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java
@@ -215,6 +215,17 @@ public class RCTNNativeModule extends ReactContextBaseJavaModule {
}
}
+ @ReactMethod
+ public void updatePinnedNotesWidget() {
+ AppWidgetManager wm = AppWidgetManager.getInstance(mContext);
+ int[] ids = wm.getAppWidgetIds(ComponentName.createRelative(mContext.getPackageName(), PinnedNotesWidgetProvider.class.getName()));
+ for (int id: ids) {
+ Log.d("PinnedNotes", "Updating" + id);
+ RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_pinned_notes);
+ PinnedNotesWidgetProvider.updateAppWidget(mContext, wm, id, views);
+ }
+ }
+
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isGestureNavigationEnabled() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
diff --git a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/WidgetUtils.java b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/WidgetUtils.java
index c23d55c44..335108bde 100644
--- a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/WidgetUtils.java
+++ b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/WidgetUtils.java
@@ -11,6 +11,7 @@ import android.os.Build;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
+import android.view.View;
import android.widget.RemoteViews;
import com.google.gson.Gson;
@@ -34,12 +35,14 @@ public class WidgetUtils {
static final String PREFERENCES = "appPreview";
static final String REMINDERS_KEY = "remindersList";
+ static final String PINNED_NOTES_KEY = "pinnedNotesList";
/**
* Every row is serialized into the widget update itself, which has to fit inside a binder
* transaction, so the list cannot grow without bound. Far more than fits on screen anyway.
*/
private static final int MAX_REMINDERS = 50;
+ private static final int MAX_PINNED_NOTES = 50;
/**
* Redraws every widget that currently exists, and drops stored notes for widgets that no
@@ -69,6 +72,12 @@ public class WidgetUtils {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_reminders);
ReminderWidgetProvider.updateAppWidget(context, manager, appWidgetId, views);
}
+
+ for (int appWidgetId : manager.getAppWidgetIds(
+ new ComponentName(context, PinnedNotesWidgetProvider.class))) {
+ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_pinned_notes);
+ PinnedNotesWidgetProvider.updateAppWidget(context, manager, appWidgetId, views);
+ }
}
/**
@@ -186,6 +195,55 @@ public class WidgetUtils {
return views;
}
+ /**
+ * The pinned notes the app last wrote out.
+ */
+ static List<Note> getWidgetPinnedNotes(Context context) {
+ SharedPreferences preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
+ List<Note> stored = null;
+ try {
+ stored = new Gson().fromJson(preferences.getString(PINNED_NOTES_KEY, "[]"),
+ new TypeToken<List<Note>>() {}.getType());
+ } catch (Exception e) {
+ Log.e("PinnedNotes", "Could not read the stored pinned notes list", e);
+ }
+
+ List<Note> active = new ArrayList<>();
+ if (stored == null) return active;
+
+ for (Note note : stored) {
+ if (active.size() >= MAX_PINNED_NOTES) {
+ Log.w("PinnedNotes", "Widget list truncated to " + MAX_PINNED_NOTES + " notes");
+ break;
+ }
+ active.add(note);
+ }
+ return active;
+ }
+
+ /**
+ * Builds a single row of the pinned notes list.
+ */
+ static RemoteViews createPinnedNoteItem(Context context, Note note) {
+ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_pinned_note_item);
+
+ views.setTextViewText(R.id.note_title, note.getTitle());
+ String headline = note.getHeadline();
+ if (headline != null && !headline.isEmpty()) {
+ views.setTextViewText(R.id.note_body, headline);
+ views.setViewVisibility(R.id.note_body, View.VISIBLE);
+ } else {
+ views.setViewVisibility(R.id.note_body, View.GONE);
+ }
+
+ Intent fillInIntent = new Intent();
+ fillInIntent.setData(Uri.parse("nn://note/" + note.getId()));
+ fillInIntent.putExtra(RCTNNativeModule.IntentType, "OpenNote");
+ fillInIntent.putExtra(NotePreviewWidget.OpenNoteId, note.getId());
+ views.setOnClickFillInIntent(R.id.pinned_note_item_btn, fillInIntent);
+ return views;
+ }
+
/**
* Options attached to the PendingIntents our widgets hand to the launcher, opting the creator
* (us) in to background activity starts so a tap on the widget can bring up an activity.
diff --git a/apps/mobile/app/utils/notesnook-module.ts b/apps/mobile/app/utils/notesnook-module.ts
index 0d90c4681..f842baaa5 100644
--- a/apps/mobile/app/utils/notesnook-module.ts
+++ b/apps/mobile/app/utils/notesnook-module.ts
@@ -45,6 +45,7 @@ interface NotesnookModuleInterface {
hasWidgetNote: (noteId: string) => Promise<boolean>;
updateWidgetNote: (noteId: string, data: string) => void;
updateReminderWidget: () => void;
+ updatePinnedNotesWidget: () => void;
refreshWidgets: () => void;
isGestureNavigationEnabled: () => boolean;
addShortcut: (
@@ -82,6 +83,7 @@ export const NotesnookModule: NotesnookModuleInterface = Platform.select({
hasWidgetNote: () => {},
updateWidgetNote: () => {},
updateReminderWidget: () => {},
+ updatePinnedNotesWidget: () => {},
refreshWidgets: () => {},
isGestureNavigationEnabled: () => true,
addShortcut: () => Promise.resolve(false),