|
| 1 | +/* |
| 2 | + * Nextcloud Notes - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package it.niedermann.owncloud.notes.widget.interactivelist |
| 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.content.res.ColorStateList |
| 16 | +import android.os.Build |
| 17 | +import android.os.Bundle |
| 18 | +import android.util.Log |
| 19 | +import android.widget.RemoteViews |
| 20 | +import androidx.core.net.toUri |
| 21 | +import it.niedermann.owncloud.notes.R |
| 22 | +import it.niedermann.owncloud.notes.branding.BrandingUtil |
| 23 | +import it.niedermann.owncloud.notes.edit.EditNoteActivity |
| 24 | +import it.niedermann.owncloud.notes.persistence.NotesRepository |
| 25 | +import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData |
| 26 | +import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType |
| 27 | +import it.niedermann.owncloud.notes.shared.model.NavigationCategory |
| 28 | +import it.niedermann.owncloud.notes.shared.util.WidgetUtil |
| 29 | +import java.util.concurrent.ExecutorService |
| 30 | +import java.util.concurrent.Executors |
| 31 | + |
| 32 | +class InteractiveNoteListWidget : AppWidgetProvider() { |
| 33 | + private val executor: ExecutorService = Executors.newCachedThreadPool() |
| 34 | + |
| 35 | + override fun onUpdate( |
| 36 | + context: Context, |
| 37 | + appWidgetManager: AppWidgetManager, |
| 38 | + appWidgetIds: IntArray |
| 39 | + ) { |
| 40 | + super.onUpdate(context, appWidgetManager, appWidgetIds) |
| 41 | + updateAppWidget(context, appWidgetManager, appWidgetIds) |
| 42 | + } |
| 43 | + |
| 44 | + override fun onReceive(context: Context, intent: Intent) { |
| 45 | + super.onReceive(context, intent) |
| 46 | + val awm = AppWidgetManager.getInstance(context) |
| 47 | + |
| 48 | + if (intent.action == null) { |
| 49 | + Log.w(TAG, "Intent action is null") |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if (intent.action != AppWidgetManager.ACTION_APPWIDGET_UPDATE) { |
| 54 | + Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE") |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if (!intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) { |
| 59 | + updateAppWidget( |
| 60 | + context, |
| 61 | + awm, |
| 62 | + awm.getAppWidgetIds(ComponentName(context, InteractiveNoteListWidget::class.java)) |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + val appWidgetIds = intArrayOf(intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) ?: -1) |
| 67 | + |
| 68 | + updateAppWidget( |
| 69 | + context, |
| 70 | + awm, |
| 71 | + appWidgetIds |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + override fun onDeleted(context: Context, appWidgetIds: IntArray) { |
| 76 | + super.onDeleted(context, appWidgetIds) |
| 77 | + val repo = NotesRepository.getInstance(context) |
| 78 | + |
| 79 | + for (appWidgetId in appWidgetIds) { |
| 80 | + executor.execute { |
| 81 | + repo.removeNoteListWidget(appWidgetId) |
| 82 | + InteractiveWidgetPreferences.remove(context, appWidgetId) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + companion object { |
| 88 | + private val TAG: String = InteractiveNoteListWidget::class.java.simpleName |
| 89 | + |
| 90 | + fun updateAppWidget(context: Context, awm: AppWidgetManager, appWidgetIds: IntArray) { |
| 91 | + val repo = NotesRepository.getInstance(context) |
| 92 | + appWidgetIds.forEach { appWidgetId -> |
| 93 | + repo.getNoteListWidgetData(appWidgetId)?.let { data -> |
| 94 | + val serviceIntent = Intent(context, InteractiveNoteListWidgetService::class.java).apply { |
| 95 | + putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 96 | + setData(toUri(Intent.URI_INTENT_SCHEME).toUri()) |
| 97 | + } |
| 98 | + |
| 99 | + val openTemplateIntent = Intent(context, EditNoteActivity::class.java).apply { |
| 100 | + setPackage(context.packageName) |
| 101 | + } |
| 102 | + val openTemplateFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 103 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE |
| 104 | + } else { |
| 105 | + PendingIntent.FLAG_UPDATE_CURRENT |
| 106 | + } |
| 107 | + val openTemplatePendingIntent = |
| 108 | + PendingIntent.getActivity(context, 0, openTemplateIntent, openTemplateFlags) |
| 109 | + |
| 110 | + val createNotePendingIntent = |
| 111 | + PendingIntent.getActivity( |
| 112 | + context, |
| 113 | + appWidgetId, |
| 114 | + createNoteIntent(context, data), |
| 115 | + WidgetUtil.pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT) |
| 116 | + ) |
| 117 | + |
| 118 | + val views = RemoteViews(context.packageName, R.layout.widget_interactive_note_list).apply { |
| 119 | + setRemoteAdapter(R.id.interactive_note_list_lv, serviceIntent) |
| 120 | + setPendingIntentTemplate(R.id.interactive_note_list_lv, openTemplatePendingIntent) |
| 121 | + setEmptyView( |
| 122 | + R.id.interactive_note_list_lv, |
| 123 | + R.id.interactive_note_list_placeholder_tv |
| 124 | + ) |
| 125 | + setOnClickPendingIntent(R.id.interactive_create_note_button, createNotePendingIntent) |
| 126 | + |
| 127 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 128 | + setColorStateList( |
| 129 | + R.id.interactive_create_note_button, |
| 130 | + "setBackgroundTintList", |
| 131 | + ColorStateList.valueOf(BrandingUtil.readBrandMainColor(context)) |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + awm.run { |
| 137 | + updateAppWidget(appWidgetId, views) |
| 138 | + notifyAppWidgetViewDataChanged(intArrayOf(appWidgetId), R.id.interactive_note_list_lv) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private fun createNoteIntent(context: Context, data: NotesListWidgetData): Intent { |
| 145 | + val navigationCategory = if (data.mode == NotesListWidgetData.MODE_DISPLAY_STARRED) { |
| 146 | + NavigationCategory(ENavigationCategoryType.FAVORITES) |
| 147 | + } else { |
| 148 | + NavigationCategory(data.accountId, data.category) |
| 149 | + } |
| 150 | + |
| 151 | + val bundle = Bundle().apply { |
| 152 | + putSerializable(EditNoteActivity.PARAM_CATEGORY, navigationCategory) |
| 153 | + putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.accountId) |
| 154 | + } |
| 155 | + |
| 156 | + return Intent(context, EditNoteActivity::class.java).apply { |
| 157 | + setPackage(context.packageName) |
| 158 | + putExtras(bundle) |
| 159 | + setData("interactive-create://${data.id}".toUri()) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @JvmStatic |
| 164 | + fun updateInteractiveNoteListWidgets(context: Context) { |
| 165 | + val intent = Intent(context, InteractiveNoteListWidget::class.java).apply { |
| 166 | + action = AppWidgetManager.ACTION_APPWIDGET_UPDATE |
| 167 | + } |
| 168 | + context.sendBroadcast(intent) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments