|
| 1 | +/* |
| 2 | + * Nextcloud Notes - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2017-2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package it.niedermann.owncloud.notes.widget.notelist |
| 8 | + |
| 9 | +import android.app.PendingIntent |
| 10 | +import android.appwidget.AppWidgetManager |
| 11 | +import android.appwidget.AppWidgetProvider |
| 12 | +import android.content.ComponentName |
| 13 | +import android.content.Context |
| 14 | +import android.content.Intent |
| 15 | +import android.net.Uri |
| 16 | +import android.util.Log |
| 17 | +import android.widget.RemoteViews |
| 18 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 19 | +import it.niedermann.owncloud.notes.R |
| 20 | +import it.niedermann.owncloud.notes.edit.EditNoteActivity |
| 21 | +import it.niedermann.owncloud.notes.persistence.NotesRepository |
| 22 | +import it.niedermann.owncloud.notes.shared.util.WidgetUtil |
| 23 | +import java.util.concurrent.ExecutorService |
| 24 | +import java.util.concurrent.Executors |
| 25 | +import androidx.core.net.toUri |
| 26 | + |
| 27 | +class NoteListWidget : AppWidgetProvider() { |
| 28 | + private val executor: ExecutorService = Executors.newCachedThreadPool() |
| 29 | + |
| 30 | + override fun onUpdate( |
| 31 | + context: Context, |
| 32 | + appWidgetManager: AppWidgetManager, |
| 33 | + appWidgetIds: IntArray |
| 34 | + ) { |
| 35 | + super.onUpdate(context, appWidgetManager, appWidgetIds) |
| 36 | + updateAppWidget(context, appWidgetManager, appWidgetIds) |
| 37 | + } |
| 38 | + |
| 39 | + override fun onReceive(context: Context, intent: Intent) { |
| 40 | + super.onReceive(context, intent) |
| 41 | + val awm = AppWidgetManager.getInstance(context) |
| 42 | + |
| 43 | + if (intent.action == null) { |
| 44 | + Log.w(TAG, "Intent action is null") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if (intent.action != AppWidgetManager.ACTION_APPWIDGET_UPDATE) { |
| 49 | + Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE") |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if (!intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) { |
| 54 | + Log.w(TAG, "Update widget via default appWidgetIds") |
| 55 | + updateAppWidget( |
| 56 | + context, |
| 57 | + awm, |
| 58 | + awm.getAppWidgetIds(ComponentName(context, NoteListWidget::class.java)) |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + if (intent.extras == null) { |
| 63 | + Log.w(TAG, "Intent doesn't have bundle") |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + Log.w(TAG, "Update widget via given appWidgetIds") |
| 68 | + |
| 69 | + val appWidgetIds = intArrayOf(intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) ?: -1) |
| 70 | + |
| 71 | + updateAppWidget( |
| 72 | + context, |
| 73 | + awm, |
| 74 | + appWidgetIds |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + override fun onDeleted(context: Context, appWidgetIds: IntArray) { |
| 79 | + super.onDeleted(context, appWidgetIds) |
| 80 | + val repo = NotesRepository.getInstance(context) |
| 81 | + |
| 82 | + for (appWidgetId in appWidgetIds) { |
| 83 | + executor.submit(Runnable { repo.removeNoteListWidget(appWidgetId) }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + companion object { |
| 88 | + private val TAG: String = NoteListWidget::class.java.getSimpleName() |
| 89 | + fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) { |
| 90 | + val repo = NotesRepository.getInstance(context) |
| 91 | + appWidgetIds.forEach { appWidgetId -> |
| 92 | + repo.getNoteListWidgetData(appWidgetId)?.let { data -> |
| 93 | + val serviceIntent = Intent(context, NoteListWidgetService::class.java).apply { |
| 94 | + putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 95 | + setData(toUri(Intent.URI_INTENT_SCHEME).toUri()) |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + Log.v(TAG, "-- data - $data") |
| 100 | + |
| 101 | + val editNoteIntent = Intent(context, EditNoteActivity::class.java).apply { |
| 102 | + setPackage(context.packageName) |
| 103 | + } |
| 104 | + |
| 105 | + val pendingIntentFlags = |
| 106 | + WidgetUtil.pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT or Intent.FILL_IN_COMPONENT) |
| 107 | + val editNotePendingIntent = |
| 108 | + PendingIntent.getActivity(context, 0, editNoteIntent, pendingIntentFlags) |
| 109 | + |
| 110 | + val views = RemoteViews(context.packageName, R.layout.widget_note_list).apply { |
| 111 | + setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent) |
| 112 | + setPendingIntentTemplate(R.id.note_list_widget_lv, editNotePendingIntent) |
| 113 | + setEmptyView( |
| 114 | + R.id.note_list_widget_lv, |
| 115 | + R.id.widget_note_list_placeholder_tv |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + awm.run { |
| 120 | + updateAppWidget(appWidgetId, views) |
| 121 | + notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @JvmStatic |
| 128 | + fun updateNoteListWidgets(context: Context) { |
| 129 | + val intent = Intent(context, NoteListWidget::class.java).apply { |
| 130 | + setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE) |
| 131 | + } |
| 132 | + context.sendBroadcast(intent) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments