Skip to content

Commit abbf284

Browse files
committed
Refactor NoteListWidgetFactory for better debugging and code readability
Signed-off-by: alperozturk <[email protected]>
1 parent 0169490 commit abbf284

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
*/
77
package it.niedermann.owncloud.notes.widget.notelist;
88

9+
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
10+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
11+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
12+
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
13+
914
import android.appwidget.AppWidgetManager;
1015
import android.content.ComponentName;
1116
import android.content.Context;
@@ -33,11 +38,6 @@
3338
import it.niedermann.owncloud.notes.shared.model.NavigationCategory;
3439
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil;
3540

36-
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.PARAM_CATEGORY;
37-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_ALL;
38-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_CATEGORY;
39-
import static it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData.MODE_DISPLAY_STARRED;
40-
4141
public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
4242
private static final String TAG = NoteListWidgetFactory.class.getSimpleName();
4343

@@ -79,7 +79,7 @@ public void onDataSetChanged() {
7979
}
8080
}
8181
} catch (IllegalArgumentException e) {
82-
e.printStackTrace();
82+
Log.w(TAG, "Error caught at onDataSetChanged: " + e);
8383
}
8484
}
8585

@@ -93,25 +93,44 @@ public int getCount() {
9393
return dbNotes.size() + 1;
9494
}
9595

96+
private Intent getCreateNoteIntent(Account localAccount ) {
97+
final Bundle bundle = new Bundle();
98+
bundle.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
99+
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
100+
101+
final Intent intent = new Intent(context, EditNoteActivity.class);
102+
intent.setPackage(context.getPackageName());
103+
intent.putExtras(bundle);
104+
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
105+
106+
return intent;
107+
}
108+
109+
private Intent getOpenNoteIntent(Note note) {
110+
final Bundle bundle = new Bundle();
111+
bundle.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
112+
bundle.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
113+
114+
final Intent intent = new Intent(context, EditNoteActivity.class);
115+
intent.putExtras(bundle);
116+
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
117+
118+
return intent;
119+
}
120+
96121
@Override
97122
public RemoteViews getViewAt(int position) {
98123
final RemoteViews note_content;
99124

100125
if (position == 0) {
101126
final Account localAccount = repo.getAccountById(data.getAccountId());
102-
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
103-
final Intent createIntent = new Intent(context, EditNoteActivity.class);
104-
final Bundle extras = new Bundle();
105-
106-
extras.putSerializable(PARAM_CATEGORY, data.getMode() == MODE_DISPLAY_STARRED ? new NavigationCategory(ENavigationCategoryType.FAVORITES) : new NavigationCategory(localAccount.getId(), data.getCategory()));
107-
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, data.getAccountId());
108127

109-
createIntent.putExtras(extras);
110-
createIntent.setData(Uri.parse(createIntent.toUri(Intent.URI_INTENT_SCHEME)));
128+
final Intent createNoteIntent = getCreateNoteIntent(localAccount);
129+
final Intent openIntent = new Intent(Intent.ACTION_MAIN).setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
111130

112131
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry_add);
113132
note_content.setOnClickFillInIntent(R.id.widget_entry_content_tv, openIntent);
114-
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createIntent);
133+
note_content.setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent);
115134
note_content.setTextViewText(R.id.widget_entry_content_tv, getCategoryTitle(context, data.getMode(), data.getCategory()));
116135
note_content.setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp);
117136
note_content.setInt(R.id.widget_entry_fav_icon, "setColorFilter", NotesColorUtil.contrastRatioIsSufficient(ContextCompat.getColor(context, R.color.widget_background), localAccount.getColor())
@@ -125,16 +144,10 @@ public RemoteViews getViewAt(int position) {
125144
}
126145

127146
final Note note = dbNotes.get(position);
128-
final Intent fillInIntent = new Intent(context, EditNoteActivity.class);
129-
final Bundle extras = new Bundle();
130-
extras.putLong(EditNoteActivity.PARAM_NOTE_ID, note.getId());
131-
extras.putLong(EditNoteActivity.PARAM_ACCOUNT_ID, note.getAccountId());
132-
133-
fillInIntent.putExtras(extras);
134-
fillInIntent.setData(Uri.parse(fillInIntent.toUri(Intent.URI_INTENT_SCHEME)));
147+
final Intent openNoteIntent = getOpenNoteIntent(note);
135148

136149
note_content = new RemoteViews(context.getPackageName(), R.layout.widget_entry);
137-
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, fillInIntent);
150+
note_content.setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent);
138151
note_content.setTextViewText(R.id.widget_entry_content_tv, note.getTitle());
139152
note_content.setImageViewResource(R.id.widget_entry_fav_icon, note.getFavorite()
140153
? R.drawable.ic_star_yellow_24dp

0 commit comments

Comments
 (0)