-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathNoteListWidgetDark.java
199 lines (173 loc) · 11.3 KB
/
NoteListWidgetDark.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.automattic.simplenote;
import static com.automattic.simplenote.analytics.AnalyticsTracker.CATEGORY_WIDGET;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_BUTTON_TAPPED;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_DELETED;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_FIRST_ADDED;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_LAST_DELETED;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_SIGN_IN_TAPPED;
import static com.automattic.simplenote.analytics.AnalyticsTracker.Stat.NOTE_LIST_WIDGET_TAPPED;
import static com.automattic.simplenote.utils.WidgetUtils.KEY_LIST_WIDGET_CLICK;
import static com.automattic.simplenote.utils.WidgetUtils.MINIMUM_HEIGHT_FOR_BUTTON;
import static com.automattic.simplenote.utils.WidgetUtils.MINIMUM_WIDTH_FOR_BUTTON;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import com.automattic.simplenote.analytics.AnalyticsTracker;
import com.automattic.simplenote.models.Note;
import com.automattic.simplenote.utils.IntentUtils;
import com.automattic.simplenote.utils.PrefUtils;
import com.simperium.Simperium;
import com.simperium.client.Bucket;
import com.simperium.client.Query;
import com.simperium.client.User;
public class NoteListWidgetDark extends AppWidgetProvider {
public static final String KEY_LIST_WIDGET_IDS_DARK = "key_list_widget_ids_dark";
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
RemoteViews views = new RemoteViews(context.getPackageName(), PrefUtils.getLayoutWidgetList(context, false));
if (((Simplenote) context.getApplicationContext()).getSimperium().getUser().getStatus().equals(User.Status.AUTHORIZED)) {
resizeWidget(context, newOptions, views);
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
AnalyticsTracker.track(
NOTE_LIST_WIDGET_DELETED,
CATEGORY_WIDGET,
"note_list_widget_deleted"
);
}
@Override
public void onEnabled(Context context) {
AnalyticsTracker.track(
NOTE_LIST_WIDGET_FIRST_ADDED,
CATEGORY_WIDGET,
"note_list_widget_first_added"
);
}
@Override
public void onDisabled(Context context) {
AnalyticsTracker.track(
NOTE_LIST_WIDGET_LAST_DELETED,
CATEGORY_WIDGET,
"note_list_widget_last_deleted"
);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getExtras() != null && intent.hasExtra(KEY_LIST_WIDGET_IDS_DARK)) {
int[] ids = intent.getExtras().getIntArray(KEY_LIST_WIDGET_IDS_DARK);
this.onUpdate(context, AppWidgetManager.getInstance(context), ids);
} else {
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
Bundle appWidgetOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
updateWidget(context, appWidgetManager, appWidgetId, appWidgetOptions);
}
}
private void resizeWidget(Context context, Bundle appWidgetOptions, RemoteViews views) {
// Show/Hide add button based on widget height and width
if (appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) > MINIMUM_HEIGHT_FOR_BUTTON &&
appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH) > MINIMUM_WIDTH_FOR_BUTTON) {
views.setViewPadding(R.id.widget_list, 0, 0, 0, context.getResources().getDimensionPixelSize(R.dimen.note_list_item_padding_bottom_button_widget));
views.setViewVisibility(R.id.widget_button, View.VISIBLE);
} else {
views.setViewPadding(R.id.widget_list, 0, 0, 0, 0);
views.setViewVisibility(R.id.widget_button, View.GONE);
}
}
@SuppressLint("UnspecifiedImmutableFlag")
private void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle appWidgetOptions) {
RemoteViews views = new RemoteViews(context.getPackageName(), PrefUtils.getLayoutWidgetList(context, false));
resizeWidget(context, appWidgetOptions, views);
// Verify user authentication
Simplenote currentApp = (Simplenote) context.getApplicationContext();
Simperium simperium = currentApp.getSimperium();
User user = simperium.getUser();
if (user.getStatus().equals(User.Status.NOT_AUTHORIZED)) {
// Create intent to navigate to notes activity which redirects to login on widget click
Intent intent = IntentUtils.maybeAliasedIntent(context);
intent.putExtra(KEY_LIST_WIDGET_CLICK, NOTE_LIST_WIDGET_SIGN_IN_TAPPED);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
// Reset intent to navigate to note editor on note list add button click to navigate to notes activity, which redirects to login/signup
Intent intentButton = IntentUtils.maybeAliasedIntent(context);
views.setOnClickPendingIntent(R.id.widget_button, PendingIntent.getActivity(context, appWidgetId, intentButton, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
views.setTextViewText(R.id.widget_text, context.getResources().getString(R.string.log_in_use_widget));
views.setTextColor(R.id.widget_text, context.getResources().getColor(R.color.text_title_dark, context.getTheme()));
views.setViewVisibility(R.id.widget_text, View.VISIBLE);
views.setViewVisibility(R.id.widget_list, View.GONE);
views.setViewVisibility(R.id.widget_button, View.GONE);
} else {
Bucket<Note> notesBucket = currentApp.getNotesBucket();
Query<Note> query = Note.all(notesBucket);
query.include(Note.TITLE_INDEX_NAME, Note.CONTENT_PREVIEW_INDEX_NAME);
PrefUtils.sortNoteQuery(query, context, true);
Bucket.ObjectCursor<Note> cursor = query.execute();
if (cursor.getCount() > 0) {
// Create intent to navigate to notes activity on widget click while loading
Intent intentLoading = IntentUtils.maybeAliasedIntent(context);
intentLoading.putExtra(KEY_LIST_WIDGET_CLICK, NOTE_LIST_WIDGET_TAPPED);
intentLoading.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntentLoading = PendingIntent.getActivity(context, appWidgetId, intentLoading, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntentLoading);
// Create intent for note list widget service
Intent intent = new Intent(context, NoteListWidgetDarkService.class);
intent.putExtra(NoteListWidgetFactory.EXTRA_IS_LIGHT, false);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
views.setRemoteAdapter(R.id.widget_list, intent);
// Create intent to navigate to note editor on note list item click
Intent intentItem = new Intent(context, NoteEditorActivity.class);
PendingIntent pendingIntentItem = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntentItem = PendingIntent.getActivity(context, appWidgetId, intentItem, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
} else {
pendingIntentItem = PendingIntent.getActivity(context, appWidgetId, intentItem, PendingIntent.FLAG_UPDATE_CURRENT);
}
views.setPendingIntentTemplate(R.id.widget_list, pendingIntentItem);
// Create intent to navigate to note editor on note list add button click
Intent intentButton = IntentUtils.maybeAliasedIntent(context);
intentButton.putExtra(KEY_LIST_WIDGET_CLICK, NOTE_LIST_WIDGET_BUTTON_TAPPED);
intentButton.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntentButton = PendingIntent.getActivity(context, appWidgetId, intentButton, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntentButton);
views.setEmptyView(R.id.widget_list, R.id.widget_text);
views.setTextColor(R.id.widget_text, context.getResources().getColor(R.color.text_title_dark, context.getTheme()));
views.setTextViewText(R.id.widget_text, context.getResources().getString(R.string.empty_notes_widget));
views.setViewVisibility(R.id.widget_text, View.GONE);
views.setViewVisibility(R.id.widget_list, View.VISIBLE);
} else {
// Create intent to navigate to notes activity on widget click
Intent intent = IntentUtils.maybeAliasedIntent(context);
intent.putExtra(KEY_LIST_WIDGET_CLICK, NOTE_LIST_WIDGET_TAPPED);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
// Create intent to navigate to note editor on note list add button click
Intent intentButton = IntentUtils.maybeAliasedIntent(context);
intentButton.putExtra(KEY_LIST_WIDGET_CLICK, NOTE_LIST_WIDGET_BUTTON_TAPPED);
intentButton.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntentButton = PendingIntent.getActivity(context, appWidgetId, intentButton, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntentButton);
views.setTextColor(R.id.widget_text, context.getResources().getColor(R.color.text_title_dark, context.getTheme()));
views.setTextViewText(R.id.widget_text, context.getResources().getString(R.string.empty_notes_widget));
views.setViewVisibility(R.id.widget_text, View.VISIBLE);
views.setViewVisibility(R.id.widget_list, View.GONE);
}
}
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}