Skip to content

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

mobile: add android layouts and provider for pinned notes widget

kashaf-ansari-dev820532c8df7c
diff --git a/apps/mobile/android/app/src/main/AndroidManifest.xml b/apps/mobile/android/app/src/main/AndroidManifest.xml
index a3267c669..b5a66b2b1 100644
--- a/apps/mobile/android/app/src/main/AndroidManifest.xml
+++ b/apps/mobile/android/app/src/main/AndroidManifest.xml
@@ -117,6 +117,18 @@
android:resource="@xml/widget_reminders_info" />
</receiver>
+ <receiver
+ android:name=".PinnedNotesWidgetProvider"
+ android:exported="false"
+ android:label="@string/pinned_notes_title">
+ <intent-filter>
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+ </intent-filter>
+ <meta-data
+ android:name="android.appwidget.provider"
+ android:resource="@xml/widget_pinned_notes_info" />
+ </receiver>
+
<activity
android:name=".NotePreviewConfigureActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
diff --git a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/PinnedNotesWidgetProvider.java b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/PinnedNotesWidgetProvider.java
new file mode 100644
index 000000000..f8a3a51e6
--- /dev/null
+++ b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/PinnedNotesWidgetProvider.java
@@ -0,0 +1,65 @@
+package com.streetwriters.notesnook;
+
+import android.app.PendingIntent;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.Context;
+import android.content.Intent;
+import android.widget.RemoteViews;
+
+import androidx.core.widget.RemoteViewsCompat;
+
+import com.streetwriters.notesnook.datatypes.Note;
+
+import java.util.List;
+
+/**
+ * Implementation of App Widget functionality for Pinned Notes.
+ * Displays a scrollable collection of pinned notes on the home screen.
+ */
+public class PinnedNotesWidgetProvider extends AppWidgetProvider {
+
+ @Override
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
+ for (int appWidgetId : appWidgetIds) {
+ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_pinned_notes);
+ updateAppWidget(context, appWidgetManager, appWidgetId, views);
+ }
+ }
+
+ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, RemoteViews views) {
+ // PendingIntent template for list items: tapping an individual note row fills in the note ID
+ // and opens it directly in MainActivity / note editor.
+ Intent listview_intent_template = new Intent(context, MainActivity.class);
+ listview_intent_template.setAction(Intent.ACTION_VIEW);
+ PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, listview_intent_template, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_MUTABLE, WidgetUtils.getActivityOptionsBundle());
+ views.setPendingIntentTemplate(R.id.widget_list_view, pendingIntent);
+
+ // Header '+' button opens the quick note composer (ShareActivity) without loading the full app.
+ Intent new_note_intent = new Intent(context, ShareActivity.class);
+ PendingIntent pendingIntent2 = PendingIntent.getActivity(context, appWidgetId, new_note_intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, WidgetUtils.getActivityOptionsBundle());
+ views.setOnClickPendingIntent(R.id.add_button, pendingIntent2);
+
+ // Build list items using RemoteCollectionItems: rows travel directly inside the widget update,
+ // so no bound RemoteViewsService is required and updates are completely self-contained.
+ List<Note> pinnedNotes = WidgetUtils.getWidgetPinnedNotes(context);
+ RemoteViewsCompat.RemoteCollectionItems.Builder items =
+ new RemoteViewsCompat.RemoteCollectionItems.Builder();
+ for (Note note : pinnedNotes) {
+ items.addItem(getItemId(note), WidgetUtils.createPinnedNoteItem(context, note));
+ }
+ items.setViewTypeCount(1);
+ items.setHasStableIds(true);
+
+ RemoteViewsCompat.setRemoteAdapter(context, views, appWidgetId, R.id.widget_list_view, items.build());
+ views.setEmptyView(R.id.widget_list_view, R.id.empty_view);
+ appWidgetManager.updateAppWidget(appWidgetId, views);
+ }
+
+ /**
+ * Stable ID for each note row so rows retain identity when list order shifts.
+ */
+ private static long getItemId(Note note) {
+ return note.getId() == null ? 0 : note.getId().hashCode();
+ }
+}
diff --git a/apps/mobile/android/app/src/main/res/layout/widget_pinned_note_item.xml b/apps/mobile/android/app/src/main/res/layout/widget_pinned_note_item.xml
new file mode 100644
index 000000000..17b7b3cae
--- /dev/null
+++ b/apps/mobile/android/app/src/main/res/layout/widget_pinned_note_item.xml
@@ -0,0 +1,28 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:paddingVertical="10dp"
+ android:id="@+id/pinned_note_item_btn">
+
+ <TextView
+ android:id="@+id/note_title"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:textStyle="bold"
+ android:textColor="@color/text"
+ android:textSize="15sp"
+ android:maxLines="1"
+ android:ellipsize="end" />
+
+ <TextView
+ android:id="@+id/note_body"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:textColor="@color/text"
+ android:textSize="13sp"
+ android:maxLines="2"
+ android:ellipsize="end"
+ android:layout_marginTop="2dp" />
+
+</LinearLayout>
diff --git a/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes.xml b/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes.xml
new file mode 100644
index 000000000..8ff45a57f
--- /dev/null
+++ b/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes.xml
@@ -0,0 +1,64 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@drawable/layout_bg">
+
+ <RelativeLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal"
+ android:paddingHorizontal="12dp"
+ android:layout_gravity="center"
+ android:paddingTop="8dp"
+ android:paddingBottom="8dp">
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_marginTop="2dp"
+ android:textSize="15sp"
+ android:textStyle="bold"
+ android:textColor="@color/text"
+ android:text="@string/pinned_notes_title"/>
+
+ <ImageButton
+ android:layout_width="25dp"
+ android:id="@+id/add_button"
+ android:layout_height="25dp"
+ android:layout_alignParentRight="true"
+ android:background="@drawable/ic_newnote"
+ android:contentDescription="@string/add_widget" />
+ </RelativeLayout>
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="1dp"
+ android:layout_marginTop="2dp"
+ android:layout_marginBottom="8dp"
+ android:background="@color/border" />
+
+ <ListView
+ android:id="@+id/widget_list_view"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:divider="@color/border"
+ android:paddingBottom="12dp"
+ android:paddingHorizontal="12dp"
+ android:dividerHeight="1dp" />
+
+ <LinearLayout
+ android:id="@+id/empty_view"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+ <TextView
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:textAlignment="center"
+ android:gravity="center"
+ android:textColor="@color/text"
+ android:text="@string/widget_pinned_notes_empty"/>
+ </LinearLayout>
+
+</LinearLayout>
diff --git a/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes_preview.xml b/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes_preview.xml
new file mode 100644
index 000000000..d311d5add
--- /dev/null
+++ b/apps/mobile/android/app/src/main/res/layout/widget_pinned_notes_preview.xml
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Shown in the widget picker only. Mirrors widget_pinned_notes.xml, but with the list replaced by
+ sample rows, since an adapter-backed list renders empty in the picker.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@drawable/layout_bg"
+ android:orientation="vertical">
+
+ <RelativeLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:paddingHorizontal="12dp"
+ android:paddingTop="12dp">
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_marginTop="2dp"
+ android:text="@string/pinned_notes_title"
+ android:textColor="@color/text"
+ android:textSize="15sp"
+ android:textStyle="bold" />
+
+ <ImageButton
+ android:layout_width="25dp"
+ android:layout_height="25dp"
+ android:layout_alignParentRight="true"
+ android:background="@drawable/ic_newnote"
+ android:contentDescription="@string/add_widget" />
+ </RelativeLayout>
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="1dp"
+ android:layout_marginTop="2dp"
+ android:layout_marginBottom="8dp"
+ android:background="@color/border" />
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ android:paddingHorizontal="12dp"
+ android:paddingBottom="12dp">
+
+ <TextView
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/widget_preview_note_title"
+ android:textColor="@color/text"
+ android:textSize="15sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="2dp"
+ android:text="@string/widget_preview_note_body"
+ android:textColor="@color/text"
+ android:textSize="13sp" />
+ </LinearLayout>
+
+</LinearLayout>
diff --git a/apps/mobile/android/app/src/main/res/values/strings.xml b/apps/mobile/android/app/src/main/res/values/strings.xml
index d5b719bcd..fd8b2862d 100644
--- a/apps/mobile/android/app/src/main/res/values/strings.xml
+++ b/apps/mobile/android/app/src/main/res/values/strings.xml
@@ -10,6 +10,9 @@
<string name="note_description">Add a note to home screen</string>
<string name="quick_note">Quick note</string>
<string name="widget_reminders_empty">Tap + to add a reminder</string>
+ <string name="pinned_notes_title">Pinned notes</string>
+ <string name="pinned_notes_description">Quick overview of pinned notes</string>
+ <string name="widget_pinned_notes_empty">No pinned notes</string>
<!-- Sample content, only ever shown in the widget picker's preview. -->
<string name="widget_preview_note_title">Meeting notes</string>
diff --git a/apps/mobile/android/app/src/main/res/xml-v36/widget_pinned_notes_info.xml b/apps/mobile/android/app/src/main/res/xml-v36/widget_pinned_notes_info.xml
new file mode 100644
index 000000000..79699923e
--- /dev/null
+++ b/apps/mobile/android/app/src/main/res/xml-v36/widget_pinned_notes_info.xml
@@ -0,0 +1,15 @@
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:initialLayout="@layout/widget_pinned_notes"
+ android:minWidth="250dp"
+ android:minHeight="110dp"
+ android:minResizeWidth="180dp"
+ android:minResizeHeight="110dp"
+ android:description="@string/pinned_notes_description"
+ android:targetCellWidth="4"
+ android:targetCellHeight="2"
+ android:resizeMode="horizontal|vertical"
+ android:previewImage="@drawable/note_widget_preview"
+ android:previewLayout="@layout/widget_pinned_notes_preview"
+ android:updatePeriodMillis="1800000"
+ android:widgetCategory="home_screen|not_keyguard"
+ />
diff --git a/apps/mobile/android/app/src/main/res/xml/widget_pinned_notes_info.xml b/apps/mobile/android/app/src/main/res/xml/widget_pinned_notes_info.xml
new file mode 100644
index 000000000..703000c94
--- /dev/null
+++ b/apps/mobile/android/app/src/main/res/xml/widget_pinned_notes_info.xml
@@ -0,0 +1,15 @@
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:initialLayout="@layout/widget_pinned_notes"
+ android:minWidth="250dp"
+ android:minHeight="110dp"
+ android:minResizeWidth="180dp"
+ android:minResizeHeight="110dp"
+ android:description="@string/pinned_notes_description"
+ android:targetCellWidth="4"
+ android:targetCellHeight="2"
+ android:resizeMode="horizontal|vertical"
+ android:previewImage="@drawable/note_widget_preview"
+ android:previewLayout="@layout/widget_pinned_notes_preview"
+ android:updatePeriodMillis="1800000"
+ android:widgetCategory="home_screen"
+ />