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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
package it.niedermann.owncloud.notes.android.activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.model.Item;
import it.niedermann.owncloud.notes.model.ItemAdapter;
import it.niedermann.owncloud.notes.model.Note;
import it.niedermann.owncloud.notes.model.SectionItem;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.util.ICallback;
public class NotesListViewActivity extends AppCompatActivity implements
ItemAdapter.NoteClickListener, View.OnClickListener {
public final static String SELECTED_NOTE = "it.niedermann.owncloud.notes.clicked_note";
public final static String CREATED_NOTE = "it.niedermann.owncloud.notes.created_notes";
public final static String SELECTED_NOTE_POSITION = "it.niedermann.owncloud.notes.clicked_note_position";
public final static String CREDENTIALS_CHANGED = "it.niedermann.owncloud.notes.CREDENTIALS_CHANGED";
private final static int create_note_cmd = 0;
private final static int show_single_note_cmd = 1;
private final static int server_settings = 2;
private final static int about = 3;
private RecyclerView listView = null;
private ItemAdapter adapter = null;
private ActionMode mActionMode;
private SwipeRefreshLayout swipeRefreshLayout = null;
private NoteSQLiteOpenHelper db = null;
private SearchView searchView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First Run Wizard
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (preferences.getBoolean(SettingsActivity.SETTINGS_FIRST_RUN, true)) {
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivityForResult(settingsIntent, server_settings);
}
setContentView(R.layout.activity_notes_list_view);
// Display Data
db = new NoteSQLiteOpenHelper(this);
db.synchronizeWithServer();
setListView(db.getNotes());
// Pull to Refresh
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
db.getNoteServerSyncHelper().addCallback(new ICallback() {
@Override
public void onFinish() {
swipeRefreshLayout.setRefreshing(false);
adapter.clearSelection();
if (mActionMode != null) {
mActionMode.finish();
}
setListView(db.getNotes());
}
});
db.synchronizeWithServer();
}
});
// Floating Action Button
findViewById(R.id.fab_create).setOnClickListener(this);
}
/**
* Click listener for <strong>Floating Action Button</strong>
* <p/>
* Creates a new Instance of CreateNoteActivity.
*
* @param v View
*/
@Override
public void onClick(View v) {
Intent createIntent = new Intent(this, CreateNoteActivity.class);
startActivityForResult(createIntent, create_note_cmd);
}
/**
* Allows other classes to set a List of Notes.
*
* @param noteList List<Note>
*/
@SuppressWarnings("WeakerAccess")
public void setListView(List<Note> noteList) {
List<Item> itemList = new ArrayList<>();
// #12 Create Sections depending on Time
// TODO Move to ItemAdapter?
boolean todaySet, yesterdaySet, weekSet, monthSet, earlierSet;
todaySet = yesterdaySet = weekSet = monthSet = earlierSet = false;
Calendar recent = Calendar.getInstance();
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar yesterday = Calendar.getInstance();
yesterday.set(Calendar.DAY_OF_YEAR, yesterday.get(Calendar.DAY_OF_YEAR) - 1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
yesterday.set(Calendar.MILLISECOND, 0);
Calendar week = Calendar.getInstance();
week.set(Calendar.DAY_OF_WEEK, week.getFirstDayOfWeek());
week.set(Calendar.HOUR_OF_DAY, 0);
week.set(Calendar.MINUTE, 0);
week.set(Calendar.SECOND, 0);
week.set(Calendar.MILLISECOND, 0);
Calendar month = Calendar.getInstance();
month.set(Calendar.DAY_OF_MONTH, 0);
month.set(Calendar.HOUR_OF_DAY, 0);
month.set(Calendar.MINUTE, 0);
month.set(Calendar.SECOND, 0);
month.set(Calendar.MILLISECOND, 0);
for (int i = 0; i < noteList.size(); i++) {
Note currentNote = noteList.get(i);
if (!todaySet && recent.getTimeInMillis() - currentNote.getModified().getTimeInMillis() >= 600000 && currentNote.getModified().getTimeInMillis() >= today.getTimeInMillis()) {
// < 10 minutes but after 00:00 today
if (i > 0) {
//itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_today)));
}
todaySet = true;
} else if (!yesterdaySet && currentNote.getModified().getTimeInMillis() < today.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= yesterday.getTimeInMillis()) {
// between today 00:00 and yesterday 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_yesterday)));
}
yesterdaySet = true;
} else if (!weekSet && currentNote.getModified().getTimeInMillis() < yesterday.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= week.getTimeInMillis()) {
// between yesterday 00:00 and start of the week 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_week)));
}
weekSet = true;
} else if (!monthSet && currentNote.getModified().getTimeInMillis() < week.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= month.getTimeInMillis()) {
// between start of the week 00:00 and start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_month)));
}
monthSet = true;
} else if (!earlierSet && currentNote.getModified().getTimeInMillis() < month.getTimeInMillis()) {
// before start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_earlier)));
}
earlierSet = true;
}
itemList.add(currentNote);
}
adapter = new ItemAdapter(itemList);
ItemAdapter.setNoteClickListener(this);
listView = (RecyclerView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setLayoutManager(new LinearLayoutManager(this));
}
/**
* Adds the Menu Items to the Action Bar.
*
* @param menu Menu
* @return boolean
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list_view, menu);
// Associate searchable configuration with the SearchView
final MenuItem item = menu.findItem(R.id.search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
search(newText.trim());
return true;
}
});
return true;
}
private void search(final String query) {
new Thread() {
@Override
public void run() {
if (query.length() > 0) {
setListView(db.searchNotes(query));
} else {
setListView(db.getNotes());
}
listView.scrollToPosition(0);
}
}.run();
}
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
search(intent.getStringExtra(SearchManager.QUERY));
}
super.onNewIntent(intent);
}
/**
* Handels click events on the Buttons in the Action Bar.
*
* @param item MenuItem - the clicked menu item
* @return boolean
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivityForResult(settingsIntent, server_settings);
return true;
case R.id.action_about:
Intent aboutIntent = new Intent(this, AboutActivity.class);
startActivityForResult(aboutIntent, about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Handles the Results of started Sub Activities (Created Note, Edited Note)
*
* @param requestCode int to distinguish between the different Sub Activities
* @param resultCode int Return Code
* @param data Intent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == create_note_cmd) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//not need because of db.synchronisation in createActivity
Note createdNote = (Note) data.getExtras().getSerializable(
CREATED_NOTE);
adapter.add(createdNote);
//setListView(db.getNotes());
}
} else if (requestCode == show_single_note_cmd) {
if (resultCode == RESULT_OK || resultCode == RESULT_FIRST_USER) {
int notePosition = data.getExtras().getInt(
SELECTED_NOTE_POSITION);
adapter.remove(adapter.getItem(notePosition));
if (resultCode == RESULT_OK) {
Note editedNote = (Note) data.getExtras().getSerializable(
NoteActivity.EDIT_NOTE);
adapter.add(editedNote);
}
}
} else if (requestCode == server_settings) {
// Create new Instance with new URL and credentials
db = new NoteSQLiteOpenHelper(this);
db.getNoteServerSyncHelper().addCallback(new ICallback() {
@Override
public void onFinish() {
setListView(db.getNotes());
}
});
db.synchronizeWithServer();
}
}
@Override
public void onNoteClick(int position, View v) {
if (mActionMode != null) {
if (!adapter.select(position)) {
v.setSelected(false);
adapter.deselect(position);
} else {
v.setSelected(true);
}
mActionMode.setTitle(String.valueOf(adapter.getSelected().size())
+ " " + getString(R.string.ab_selected));
int checkedItemCount = adapter.getSelected().size();
boolean hasCheckedItems = checkedItemCount > 0;
if (hasCheckedItems && mActionMode == null) {
// TODO differ if one or more items are selected
// if (checkedItemCount == 1) {
// mActionMode = startActionMode(new
// SingleSelectedActionModeCallback());
// } else {
// there are some selected items, start the actionMode
mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback());
// }
} else if (!hasCheckedItems && mActionMode != null) {
// there no selected items, finish the actionMode
mActionMode.finish();
}
} else {
Intent intent = new Intent(getApplicationContext(),
NoteActivity.class);
Item item = adapter.getItem(position);
intent.putExtra(SELECTED_NOTE, (Note) item);
intent.putExtra(SELECTED_NOTE_POSITION, position);
Log.v("Note",
"notePosition | NotesListViewActivity wurde abgesendet "
+ position);
startActivityForResult(intent, show_single_note_cmd);
}
}
@Override
public boolean onNoteLongClick(int position, View v) {
boolean selected = adapter.select(position);
if (selected) {
v.setSelected(selected);
mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback());
int checkedItemCount = adapter.getSelected().size();
mActionMode.setTitle(String.valueOf(checkedItemCount)
+ " " + getString(R.string.ab_selected));
}
return selected;
}
@Override
public void onBackPressed() {
if (searchView.isIconified()) {
super.onBackPressed();
} else {
searchView.setIconified(true);
}
}
/**
* Handler for the MultiSelect Actions
*/
private class MultiSelectedActionModeCallback implements
ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate contextual menu
mode.getMenuInflater().inflate(R.menu.menu_list_context_multiple,
menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
/**
* @param mode ActionMode - used to close the Action Bar after all work is done.
* @param item MenuItem - the item in the List that contains the Node
* @return boolean
*/
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
List<Integer> selection = adapter.getSelected();
for (Integer i : selection) {
Note note = (Note) adapter.getItem(i);
db.deleteNoteAndSync(note.getId());
// Not needed because of dbsync
//adapter.remove(note);
}
mode.finish(); // Action picked, so close the CAB
//after delete selection has to be cleared
searchView.setIconified(true);
setListView(db.getNotes());
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
adapter.clearSelection();
mActionMode = null;
adapter.notifyDataSetChanged();
}
}
}
|