Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create launcher widget for todo, closes #1997 #2379

Merged
merged 21 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@
android:name="other.writeily.widget.WrFilesWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />

<receiver
android:name=".widget.TodoWidgetProvider"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/todo_widget" />
</receiver>

<service
android:name=".widget.TodoWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.markor.web.MarkorWebViewClient;
import net.gsantner.markor.widget.TodoWidgetProvider;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserOptions;
import net.gsantner.opoc.frontend.settings.GsFontPreferenceCompat;
import net.gsantner.opoc.frontend.textview.TextViewUndoRedo;
Expand Down Expand Up @@ -287,6 +288,11 @@ public void onPause() {
_appSettings.addRecentFile(_document.getFile());
_appSettings.setDocumentPreviewState(_document.getPath(), _isPreviewVisible);
_appSettings.setLastEditPosition(_document.getPath(), _hlEditor.getSelectionStart());

if(_document.getPath().equals(_appSettings.getTodoFile().getPath()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make an appSettings function called isTodoFile or something

I would also make sure this does not slow down opening / closing the todo file

Copy link
Owner

@gsantner gsantner Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any other usage like this (""istodo file check"), so suggest to keep it like that. Just bloats a new method with 1 usage then.

Opening we should be fine by usual file changed detection rules, as long widget writes immedeatly when not editing anymore.

Closing not sure maybe the call can be called in a new started thread

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Fair.

Either way one should absolute paths

Copy link
Owner

@gsantner gsantner Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roughly right or do you have something in mind which needs special attention?

guess the most important part is that the widget actually writes & reads filesystem at the right times and not resets it to older state the widget loaded in the past

{
TodoWidgetProvider.updateTodoWidgets();
}
super.onPause();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.markor.widget.TodoWidgetProvider;
import net.gsantner.opoc.format.GsSimpleMarkdownParser;
import net.gsantner.opoc.frontend.base.GsFragmentBase;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserFragment;
Expand Down Expand Up @@ -503,6 +504,7 @@ public GsFileBrowserFragment getNotebook() {
protected void onPause() {
super.onPause();
WrMarkorWidgetProvider.updateLauncherWidgets();
TodoWidgetProvider.updateTodoWidgets();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.util.BackupUtils;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.markor.widget.TodoWidgetProvider;
import net.gsantner.opoc.frontend.base.GsActivityBase;
import net.gsantner.opoc.frontend.base.GsPreferenceFragmentBase;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserOptions;
Expand Down Expand Up @@ -231,6 +232,7 @@ protected void onPreferenceChanged(final SharedPreferences prefs, final String k
}
} else if (eq(key, R.string.pref_key__notebook_directory, R.string.pref_key__quicknote_filepath, R.string.pref_key__todo_filepath)) {
WrMarkorWidgetProvider.updateLauncherWidgets();
TodoWidgetProvider.updateTodoWidgets();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import net.gsantner.markor.activity.DocumentActivity;
import net.gsantner.markor.activity.MarkorBaseActivity;
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;

import java.io.File;
Expand All @@ -30,7 +31,7 @@ protected void onNewIntent(final Intent intent) {
private void launchActivityAndFinish(Intent intent) {
final File file = MarkorContextUtils.getIntentFile(intent, null);
if (file != null) {
DocumentActivity.launch(this, file, null, null);
DocumentActivity.launch(this, file, null, intent.getIntExtra(Document.EXTRA_FILE_LINE_NUMBER, -1));
gsantner marked this conversation as resolved.
Show resolved Hide resolved
}
finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ public List<String> getProjects() {
}

public String getCreationDate() {
return getCreationaDate("");
return getCreationDate("");
}

public String getCreationaDate(final String defaultValue) {
public String getCreationDate(final String defaultValue) {
if (creationDate == null) {
creationDate = parseOneValueOrDefault(line, PATTERN_CREATION_DATE, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.gsantner.markor.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.activity.openeditor.OpenFromShortcutOrWidgetActivity;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.model.Document;

public class TodoWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

int requestCode = 1;
final AppSettings appSettings = ApplicationObject.settings();

final int staticFlags = PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0);
final int mutableFlags = PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);

for (int appWidgetId : appWidgetIds) {

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.todo_widget_layout);

final Intent intent = new Intent(context, TodoWidgetService.class);
views.setRemoteAdapter(R.id.todo_widget_list_view, intent);
views.setEmptyView(R.id.todo_widget_list_view, R.id.todo_widget_empty_view);
views.setInt(R.id.todo_widget_list_view, "setBackgroundColor", appSettings.getEditorBackgroundColor());

final Intent openTodo = new Intent(context, OpenFromShortcutOrWidgetActivity.class)
.setAction(Intent.ACTION_EDIT)
.putExtra(Document.EXTRA_FILE, appSettings.getTodoFile());
views.setPendingIntentTemplate(R.id.todo_widget_list_view, PendingIntent.getActivity(context, requestCode++, openTodo, mutableFlags));
views.setOnClickPendingIntent(R.id.todo_widget_container, PendingIntent.getActivity(context, requestCode++, openTodo, staticFlags));

// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}

super.onUpdate(context, appWidgetManager, appWidgetIds);
}

// Update all widget lists and shortcuts for all widgets
public static void updateTodoWidgets() {
final Context context = ApplicationObject.get().getApplicationContext();
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetManager == null) {
// The device does not support widgets.
return;
}
final ComponentName comp = new ComponentName(context, TodoWidgetProvider.class);
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(comp);

// Update List
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.todo_widget_list_view);

// Trigger remote views update
context.sendBroadcast(new Intent(context, TodoWidgetProvider.class)
.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package net.gsantner.markor.widget;

import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.format.todotxt.TodoTxtTask;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.model.Document;

import java.util.ArrayList;
import java.util.List;

public class TodoWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

private final Context _context;
private final AppSettings _appSettings;
private final Document _document;
private final List<TodoTxtTask> _tasks;

public TodoWidgetRemoteViewsFactory(Context context, Intent intent) {
_context = context;
_appSettings = ApplicationObject.settings();
_document = new Document(_appSettings.getTodoFile());
_tasks = new ArrayList<>();
}

@Override
public void onCreate() {
onDataSetChanged();
}

@Override
public void onDataSetChanged() {
_tasks.clear();
final String content = _document.loadContent(_context);
if (content == null) {
return;
}
List<TodoTxtTask> tasks = TodoTxtTask.getAllTasks(content);
_tasks.addAll(tasks);
}

@Override
public void onDestroy() {
_tasks.clear();
}

@Override
public int getCount() {
return _tasks.size();
}

@Override
public RemoteViews getViewAt(int position) {
RemoteViews views = new RemoteViews(_context.getPackageName(), R.layout.todo_widget_list_item);
views.setTextViewText(R.id.todo_widget_item_text, _tasks.get(position).getDescription());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really good to also highlight this.

Can do this pretty easily. See getSttHighlighter in MarkorDialogFactory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it needs highlighting.

String text = _tasks.get(position).getLine().replaceFirst(TodoTxtTask.PT_DATE + " ", "");
Spannable s = new SpannableString(text);
GsCallback.a1<Spannable> highlight = getSttHighlighter();
highlight.callback(s);
views.setTextViewText(R.id.todo_widget_item_text, s);

I have tried, but could not get it to work.
Using getSttHighlighter is seems to be limited to just highlighting patterns, which is good for editable text. I might be wrong.
A SpannableStringBuilder with TodoTxtTask would allow for parsing, choosing or changing elements shown.

Copy link
Owner

@gsantner gsantner Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plaintext/no highlight would be enough for the first version. Especially if you anyway think about further changes

The widget is not meant to replicate whole-Markor-editor-in-a-widget.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plaintext/no highlight would be enough for the first version.
Especially if you anyway think about further changes

Do you mean, that I should merge this PR and make further changes in another?
I also plan on making another widget for regular notes which would take some time.

Copy link
Owner

@gsantner gsantner Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan would be to get a initial version that works well enough / not makes crashes. Preferred over full featured and half stuff bare works 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... It works well on my machine 😅
I cannot find other ways to improve it beyond what I mentioned. Maybe you might find something?

views.setInt(R.id.todo_widget_item_text, "setTextColor", _appSettings.getEditorForegroundColor());

final Intent fillInIntent = new Intent()
.putExtra(Document.EXTRA_FILE_LINE_NUMBER, position);
views.setOnClickFillInIntent(R.id.todo_widget_item_text, fillInIntent);

return views;
}

@Override
public RemoteViews getLoadingView() {
return null;
}

@Override
public int getViewTypeCount() {
return 1;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public boolean hasStableIds() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package net.gsantner.markor.widget;

import android.content.Intent;
import android.widget.RemoteViewsService;

public class TodoWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new TodoWidgetRemoteViewsFactory(getApplicationContext(), intent));
}
}
23 changes: 23 additions & 0 deletions app/src/main/res/layout/todo_widget_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/todo_widget_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">

<ListView
android:id="@+id/todo_widget_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/grey"
android:dividerHeight="0.5dp" />

<TextView
android:id="@+id/todo_widget_empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Empty"
android:gravity="center"
android:visibility="gone" />
</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/todo_widget_list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/todo_widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:textSize="15sp"
android:textColor="@color/primary_text" />
11 changes: 11 additions & 0 deletions app/src/main/res/xml/todo_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/todo_widget_layout"
android:minWidth="200dp"
android:minHeight="100dp"
android:targetCellHeight="2"
android:targetCellWidth="2"
android:minResizeWidth="80dp"
android:minResizeHeight="40dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="1800000" />
Loading