Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ protected void onCreate(final Bundle savedInstanceState) {

repo = NotesRepository.getInstance(getApplicationContext());

try {
if (SingleAccountHelper.getCurrentSingleSignOnAccount(this) == null) {
throw new NoCurrentAccountSelectedException(this);
new Thread(() -> {
try {
if (SingleAccountHelper.getCurrentSingleSignOnAccount(EditNoteActivity.this) == null) {
throw new NoCurrentAccountSelectedException(EditNoteActivity.this);
}
} catch (Exception e) {
runOnUiThread(() -> {
Toast.makeText(EditNoteActivity.this, R.string.no_account_configured_yet, Toast.LENGTH_LONG).show();
finish();
});
}
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
Toast.makeText(this, R.string.no_account_configured_yet, Toast.LENGTH_LONG).show();
finish();
return;
}
}).start();

final var preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
new SharedPreferenceBooleanLiveData(preferences, getString(R.string.pref_key_keep_screen_on), true).observe(this, keepScreenOn -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ class NoteListWidget : AppWidgetProvider() {
)
}

if (intent.extras == null) {
Log.w(TAG, "Intent doesn't have bundle")
return
}

Log.w(TAG, "Update widget via given appWidgetIds")

val appWidgetIds = intArrayOf(intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) ?: -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import androidx.core.net.toUri
import it.niedermann.owncloud.notes.R
import it.niedermann.owncloud.notes.edit.BaseNoteFragment
import it.niedermann.owncloud.notes.edit.EditNoteActivity
import it.niedermann.owncloud.notes.persistence.NotesRepository
import it.niedermann.owncloud.notes.shared.util.WidgetUtil
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import androidx.core.net.toUri

class SingleNoteWidget : AppWidgetProvider() {
private val executor: ExecutorService = Executors.newCachedThreadPool()
Expand Down Expand Up @@ -52,44 +50,69 @@ class SingleNoteWidget : AppWidgetProvider() {
super.onDeleted(context, appWidgetIds)
}

companion object {
private val TAG: String = SingleNoteWidget::class.java.getSimpleName()
fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) {
val templateIntent = Intent(context, EditNoteActivity::class.java)
val repo = NotesRepository.getInstance(context)
private fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) {
val repo = NotesRepository.getInstance(context)
appWidgetIds.forEach { appWidgetId ->
repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
val pendingIntent = getPendingIntent(context, appWidgetId)
val serviceIntent = getServiceIntent(context, appWidgetId)
val views = getRemoteViews(context, pendingIntent, serviceIntent)
awm.run {
updateAppWidget(appWidgetId, views)
notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv)
}
}
}
}

appWidgetIds.forEach { appWidgetId ->
repo.getSingleNoteWidgetData(appWidgetId)?.let { data ->
templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.accountId)
private fun getPendingIntent(
context: Context,
id: Int
): PendingIntent {
val intent = Intent(context, EditNoteActivity::class.java).apply {
setPackage(context.packageName)
}

val serviceIntent = Intent(context, SingleNoteWidgetService::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
setData(toUri(Intent.URI_INTENT_SCHEME).toUri())
}
val pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT or
PendingIntent.FLAG_MUTABLE or
Intent.FILL_IN_COMPONENT

return PendingIntent.getActivity(
context,
id,
intent,
pendingIntentFlags
)
}

val views = RemoteViews(context.packageName, R.layout.widget_single_note).apply {
setPendingIntentTemplate(
R.id.single_note_widget_lv, PendingIntent.getActivity(
context, appWidgetId, templateIntent,
WidgetUtil.pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT)
)
)
setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent)
setEmptyView(
R.id.single_note_widget_lv,
R.id.widget_single_note_placeholder_tv
)
}
private fun getServiceIntent(context: Context, id: Int): Intent {
return Intent(context, SingleNoteWidgetService::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
val dataFlag = toUri(Intent.URI_INTENT_SCHEME) + id
val dataUri = dataFlag.toUri()
setData(dataUri)
}
}

awm.run {
updateAppWidget(appWidgetId, views)
notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv)
}
}
}
private fun getRemoteViews(
context: Context,
pendingIntent: PendingIntent,
serviceIntent: Intent
): RemoteViews {
return RemoteViews(
context.packageName,
R.layout.widget_single_note
).apply {
setPendingIntentTemplate(R.id.single_note_widget_lv, pendingIntent)
setEmptyView(
R.id.single_note_widget_lv,
R.id.widget_single_note_placeholder_tv
)
setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent)
}
}

companion object {
@JvmStatic
fun updateSingleNoteWidgets(context: Context) {
val intent = Intent(context, SingleNoteWidget::class.java).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import androidx.annotation.Nullable;

import com.owncloud.android.lib.common.utils.Log_OC;

import it.niedermann.android.markdown.MarkdownUtil;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.edit.EditNoteActivity;
import it.niedermann.owncloud.notes.persistence.NotesRepository;
import it.niedermann.owncloud.notes.persistence.entity.Note;
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;

public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFactory {

Expand Down Expand Up @@ -89,17 +89,24 @@ public RemoteViews getViewAt(int position) {
}

final var fillInIntent = new Intent();
final var args = new Bundle();

args.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
args.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
fillInIntent.putExtras(args);
fillInIntent.putExtra(EditNoteActivity.PARAM_NOTE_ID, note.getId());
fillInIntent.putExtra(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());

final var note_content = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content);
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
note_content.setTextViewText(R.id.single_note_content_tv, MarkdownUtil.renderForRemoteView(context, note.getContent()));
final var noteContent = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_content);
noteContent.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);

return note_content;
CharSequence rendered;
try {
rendered = MarkdownUtil.renderForRemoteView(context, note.getContent());
if (rendered == null) {
rendered = "";
}
} catch (Exception e) {
Log_OC.e(TAG, "Markdown rendering failed", e);
rendered = note.getContent();
}
noteContent.setTextViewText(R.id.single_note_content_tv, rendered);
return noteContent;
}


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

@Override
public long getItemId(int position) {
return position;
return note != null ? note.getId() : position;
}

@Override
Expand Down
Loading