From 04f766d82220de099918e800b36342c61a78fbe4 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Wed, 3 Feb 2016 14:58:44 +0100 Subject: [PATCH] #19 Local Search --- app/app.iml | 1 + app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 10 ++++- .../activity/NotesListViewActivity.java | 37 +++++++++++++++++++ .../owncloud/notes/model/ItemAdapter.java | 5 +++ .../persistence/NoteSQLiteOpenHelper.java | 28 +++++++++++++- app/src/main/res/menu/menu_list_view.xml | 17 ++++++--- app/src/main/res/xml/searchable.xml | 5 +++ 8 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/xml/searchable.xml diff --git a/app/app.iml b/app/app.iml index 73cecb61..42a25ccc 100644 --- a/app/app.iml +++ b/app/app.iml @@ -77,6 +77,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index 9304fa20..85ca3d95 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "it.niedermann.owncloud.notes" - minSdkVersion 10 + minSdkVersion 11 targetSdkVersion 23 versionCode 8 versionName "0.6.0" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fe5dd302..4386d452 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,7 @@ android:versionName="0.6.0"> @@ -28,7 +28,15 @@ + + + + 0) { + setListView(db.searchNotes(query)); + } else { + setListView(db.getNotes()); + } + listView.scrollToPosition(0); + } + + @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. * @@ -352,6 +388,7 @@ public class NotesListViewActivity extends AppCompatActivity implements } mode.finish(); // Action picked, so close the CAB //after delete selection has to be cleared + searchView.setIconified(true); setListView(db.getNotes()); return true; default: diff --git a/app/src/main/java/it/niedermann/owncloud/notes/model/ItemAdapter.java b/app/src/main/java/it/niedermann/owncloud/notes/model/ItemAdapter.java index 03681963..ce84c85a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/model/ItemAdapter.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/model/ItemAdapter.java @@ -1,6 +1,7 @@ package it.niedermann.owncloud.notes.model; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -88,14 +89,18 @@ public class ItemAdapter extends RecyclerView.Adapter { } public boolean deselect(Integer position) { + Log.v("Note", "Position: " + position); for (int i = 0; i < selected.size(); i++) { + Log.v("Note", "Compare: " + selected.get(i) + " == " + position); if (selected.get(i) == position) { //position was selected and removed selected.remove(i); + Log.v("Note", "Return: true"); return true; } } // position was not selected + Log.v("Note", "Return: false"); return false; } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java index 25aea41a..82aa3049 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java @@ -146,7 +146,7 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper { */ public List getNotes() { List notes = new ArrayList<>(); - SQLiteDatabase db = this.getWritableDatabase(); + SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + table_notes + " WHERE " + key_status + " != ? ORDER BY " + key_modified + " DESC", new String[]{DBStatus.LOCAL_DELETED.getTitle()}); if (cursor.moveToFirst()) { do { @@ -165,6 +165,32 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper { return notes; } + /** + * Returns a list of all Notes in the Database + * + * @return List<Note> + */ + public List searchNotes(String query) { + List notes = new ArrayList<>(); + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.rawQuery("SELECT * FROM " + table_notes + " WHERE " + key_status + " != ? AND " + key_content + " LIKE ? ORDER BY " + key_modified + " DESC", new String[]{DBStatus.LOCAL_DELETED.getTitle(), "%" + query + "%"}); + if (cursor.moveToFirst()) { + do { + Calendar modified = Calendar.getInstance(); + try { + String modifiedStr = cursor.getString(3); + if (modifiedStr != null) + modified.setTime(new SimpleDateFormat(DATE_FORMAT, Locale.GERMANY).parse(modifiedStr)); + } catch (ParseException e) { + e.printStackTrace(); + } + notes.add(new Note(Long.valueOf(cursor.getString(0)), modified, cursor.getString(2), cursor.getString(4))); + } while (cursor.moveToNext()); + } + cursor.close(); + return notes; + } + /** * Returns a list of all Notes in the Database with a sepcial status, e.g. Edited, Deleted,... * diff --git a/app/src/main/res/menu/menu_list_view.xml b/app/src/main/res/menu/menu_list_view.xml index f3c48bbe..163f96a8 100644 --- a/app/src/main/res/menu/menu_list_view.xml +++ b/app/src/main/res/menu/menu_list_view.xml @@ -1,18 +1,23 @@ + tools:context="com.example.owncloudnotes.NotesListViewActivity"> + + android:title="@string/action_settings" + app:showAsAction="never"/> + android:title="@string/action_about" + app:showAsAction="never"/> \ No newline at end of file diff --git a/app/src/main/res/xml/searchable.xml b/app/src/main/res/xml/searchable.xml new file mode 100644 index 00000000..eb7b01fd --- /dev/null +++ b/app/src/main/res/xml/searchable.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file