|
1 | 1 | /* |
2 | 2 | * Nextcloud Notes - Android Client |
3 | 3 | * |
4 | | - * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-FileCopyrightText: 2017-2025 Nextcloud GmbH and Nextcloud contributors |
5 | 5 | * SPDX-License-Identifier: GPL-3.0-or-later |
6 | 6 | */ |
7 | | -package it.niedermann.owncloud.notes.widget.notelist; |
8 | | - |
9 | | -import static it.niedermann.owncloud.notes.shared.util.WidgetUtil.pendingIntentFlagCompat; |
10 | | - |
11 | | -import android.app.PendingIntent; |
12 | | -import android.appwidget.AppWidgetManager; |
13 | | -import android.appwidget.AppWidgetProvider; |
14 | | -import android.content.ComponentName; |
15 | | -import android.content.Context; |
16 | | -import android.content.Intent; |
17 | | -import android.net.Uri; |
18 | | -import android.util.Log; |
19 | | -import android.widget.RemoteViews; |
20 | | - |
21 | | -import java.util.NoSuchElementException; |
22 | | -import java.util.concurrent.ExecutorService; |
23 | | -import java.util.concurrent.Executors; |
24 | | - |
25 | | -import it.niedermann.owncloud.notes.R; |
26 | | -import it.niedermann.owncloud.notes.edit.EditNoteActivity; |
27 | | -import it.niedermann.owncloud.notes.persistence.NotesRepository; |
28 | | - |
29 | | -public class NoteListWidget extends AppWidgetProvider { |
30 | | - private static final String TAG = NoteListWidget.class.getSimpleName(); |
31 | | - private final ExecutorService executor = Executors.newCachedThreadPool(); |
32 | | - |
33 | | - static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidgetIds) { |
34 | | - final var repo = NotesRepository.getInstance(context); |
35 | | - |
36 | | - RemoteViews views; |
37 | | - |
38 | | - for (int appWidgetId : appWidgetIds) { |
39 | | - try { |
40 | | - final var data = repo.getNoteListWidgetData(appWidgetId); |
41 | | - |
42 | | - final var serviceIntent = new Intent(context, NoteListWidgetService.class); |
43 | | - serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
44 | | - serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); |
45 | | - |
46 | | - Log.v(TAG, "-- data - " + data); |
47 | | - |
48 | | - Intent editNoteIntent = new Intent(context, EditNoteActivity.class); |
49 | | - editNoteIntent.setPackage(context.getPackageName()); |
50 | | - |
51 | | - int pendingIntentFlags = pendingIntentFlagCompat(PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_COMPONENT); |
52 | | - PendingIntent editNotePendingIntent = PendingIntent.getActivity(context, 0, editNoteIntent, pendingIntentFlags); |
53 | | - |
54 | | - views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list); |
55 | | - views.setRemoteAdapter(R.id.note_list_widget_lv, serviceIntent); |
56 | | - views.setPendingIntentTemplate(R.id.note_list_widget_lv, editNotePendingIntent); |
57 | | - views.setEmptyView(R.id.note_list_widget_lv, R.id.widget_note_list_placeholder_tv); |
58 | | - |
59 | | - awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.note_list_widget_lv); |
60 | | - awm.updateAppWidget(appWidgetId, views); |
61 | | - } catch (NoSuchElementException e) { |
62 | | - Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget"); |
63 | | - } |
64 | | - } |
| 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) |
65 | 37 | } |
66 | 38 |
|
67 | | - @Override |
68 | | - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
69 | | - super.onUpdate(context, appWidgetManager, appWidgetIds); |
70 | | - updateAppWidget(context, appWidgetManager, appWidgetIds); |
71 | | - } |
72 | | - |
73 | | - @Override |
74 | | - public void onReceive(Context context, Intent intent) { |
75 | | - super.onReceive(context, intent); |
76 | | - final var awm = AppWidgetManager.getInstance(context); |
| 39 | + override fun onReceive(context: Context, intent: Intent) { |
| 40 | + super.onReceive(context, intent) |
| 41 | + val awm = AppWidgetManager.getInstance(context) |
77 | 42 |
|
78 | | - if (intent.getAction() == null) { |
79 | | - Log.w(TAG, "Intent action is null"); |
80 | | - return; |
| 43 | + if (intent.action == null) { |
| 44 | + Log.w(TAG, "Intent action is null") |
| 45 | + return |
81 | 46 | } |
82 | 47 |
|
83 | | - if (!intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) { |
84 | | - Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE"); |
85 | | - return; |
| 48 | + if (intent.action != AppWidgetManager.ACTION_APPWIDGET_UPDATE) { |
| 49 | + Log.w(TAG, "Intent action is not ACTION_APPWIDGET_UPDATE") |
| 50 | + return |
86 | 51 | } |
87 | 52 |
|
88 | 53 | if (!intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) { |
89 | | - Log.w(TAG,"Update widget via default appWidgetIds"); |
90 | | - updateAppWidget(context, awm, awm.getAppWidgetIds(new ComponentName(context, NoteListWidget.class))); |
| 54 | + Log.w(TAG, "Update widget via default appWidgetIds") |
| 55 | + updateAppWidget( |
| 56 | + context, |
| 57 | + awm, |
| 58 | + awm.getAppWidgetIds(ComponentName(context, NoteListWidget::class.java)) |
| 59 | + ) |
91 | 60 | } |
92 | 61 |
|
93 | | - if (intent.getExtras() == null) { |
94 | | - Log.w(TAG, "Intent doesn't have bundle"); |
95 | | - return; |
| 62 | + if (intent.extras == null) { |
| 63 | + Log.w(TAG, "Intent doesn't have bundle") |
| 64 | + return |
96 | 65 | } |
97 | 66 |
|
98 | | - Log.w(TAG,"Update widget via given appWidgetIds"); |
99 | | - updateAppWidget(context, awm, new int[]{intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)}); |
| 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 | + ) |
100 | 76 | } |
101 | 77 |
|
102 | | - @Override |
103 | | - public void onDeleted(Context context, int[] appWidgetIds) { |
104 | | - super.onDeleted(context, appWidgetIds); |
105 | | - final var repo = NotesRepository.getInstance(context); |
| 78 | + override fun onDeleted(context: Context, appWidgetIds: IntArray) { |
| 79 | + super.onDeleted(context, appWidgetIds) |
| 80 | + val repo = NotesRepository.getInstance(context) |
106 | 81 |
|
107 | | - for (final int appWidgetId : appWidgetIds) { |
108 | | - executor.submit(() -> repo.removeNoteListWidget(appWidgetId)); |
| 82 | + for (appWidgetId in appWidgetIds) { |
| 83 | + executor.submit(Runnable { repo.removeNoteListWidget(appWidgetId) }) |
109 | 84 | } |
110 | 85 | } |
111 | 86 |
|
112 | | - /** |
113 | | - * Update note list widgets, if the note data was changed. |
114 | | - */ |
115 | | - public static void updateNoteListWidgets(Context context) { |
116 | | - context.sendBroadcast(new Intent(context, NoteListWidget.class).setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)); |
| 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 | + } |
117 | 134 | } |
118 | 135 | } |
0 commit comments