Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SingleNoteWidget.java « singlenote « widget « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3637f72d5b2eb9d87f457b75aaba5549d76bdf5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package it.niedermann.owncloud.notes.widget.singlenote;

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.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

import it.niedermann.owncloud.notes.NotesApplication;
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.NotesDatabase;
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
import it.niedermann.owncloud.notes.preferences.DarkModeSetting;

public class SingleNoteWidget extends AppWidgetProvider {

    private static final String TAG = SingleNoteWidget.class.getSimpleName();

    static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidgetIds) {
        final Intent templateIntent = new Intent(context, EditNoteActivity.class);
        final NotesDatabase db = NotesDatabase.getInstance(context);

        for (int appWidgetId : appWidgetIds) {
            new Thread(() -> {
                final SingleNoteWidgetData data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId);
                if (data != null) {
                    templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.getAccountId());

                    final PendingIntent templatePendingIntent = PendingIntent.getActivity(context, appWidgetId, templateIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

                    Intent serviceIntent = new Intent(context, SingleNoteWidgetService.class);
                    serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));

                    RemoteViews views;

                    if (NotesApplication.isDarkThemeActive(context, DarkModeSetting.fromModeID(data.getThemeMode()))) {
                        views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note_dark);
                        views.setPendingIntentTemplate(R.id.single_note_widget_lv_dark, templatePendingIntent);
                        views.setRemoteAdapter(R.id.single_note_widget_lv_dark, serviceIntent);
                        views.setEmptyView(R.id.single_note_widget_lv_dark, R.id.widget_single_note_placeholder_tv_dark);
                        awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv_dark);
                    } else {
                        views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note);
                        views.setPendingIntentTemplate(R.id.single_note_widget_lv, templatePendingIntent);
                        views.setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent);
                        views.setEmptyView(R.id.single_note_widget_lv, R.id.widget_single_note_placeholder_tv);
                        awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv);
                    }
                    awm.updateAppWidget(appWidgetId, views);
                } else {
                    Log.i(TAG, "onUpdate has been triggered before the user finished configuring the widget");
                }
            }).start();
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        updateAppWidget(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        AppWidgetManager awm = AppWidgetManager.getInstance(context);

        updateAppWidget(context, AppWidgetManager.getInstance(context),
                (awm.getAppWidgetIds(new ComponentName(context, SingleNoteWidget.class))));
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        final NotesDatabase db = NotesDatabase.getInstance(context);

        for (int appWidgetId : appWidgetIds) {
            new Thread(() -> db.getWidgetSingleNoteDao().removeSingleNoteWidget(appWidgetId)).start();
        }
        super.onDeleted(context, appWidgetIds);
    }

    /**
     * Update single note widget, if the note data was changed.
     */
    public static void updateSingleNoteWidgets(Context context) {
        context.sendBroadcast(new Intent(context, SingleNoteWidget.class).setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE));
    }
}