From 53f61893018403047c87e3d9618c82e9f03b0c55 Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Thu, 6 Mar 2014 02:24:13 +0530 Subject: [PATCH 1/3] Implement search for KeyListPublicFragment --- .../keychain/ui/KeyListPublicFragment.java | 47 ++++++++++++++++-- .../res/drawable-mdpi/ic_action_search.png | Bin 0 -> 2349 bytes .../src/main/res/menu/key_list_public.xml | 7 ++- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 OpenPGP-Keychain/src/main/res/drawable-mdpi/ic_action_search.png diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java index 0afa556cb..af5d946eb 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java @@ -46,9 +46,13 @@ import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; +import android.support.v4.view.MenuItemCompat; +import android.support.v7.widget.SearchView; +import android.text.TextUtils; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; @@ -64,23 +68,25 @@ import com.beardedhen.androidbootstrap.BootstrapButton; * Public key list with sticky list headers. It does _not_ extend ListFragment because it uses * StickyListHeaders library which does not extend upon ListView. */ -public class KeyListPublicFragment extends Fragment implements AdapterView.OnItemClickListener, +public class KeyListPublicFragment extends Fragment implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks { private KeyListPublicAdapter mAdapter; private StickyListHeadersListView mStickyList; - + private String mCurQuery; + private SearchView mSearchView; // empty list layout private BootstrapButton mButtonEmptyCreate; private BootstrapButton mButtonEmptyImport; + /** * Load custom layout with StickyListView from library */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.key_list_public_fragment, container, false); - + setHasOptionsMenu(true); mButtonEmptyCreate = (BootstrapButton) view.findViewById(R.id.key_list_empty_button_create); mButtonEmptyCreate.setOnClickListener(new OnClickListener() { @@ -232,10 +238,13 @@ public class KeyListPublicFragment extends Fragment implements AdapterView.OnIte // This is called when a new Loader needs to be created. This // sample only has one Loader, so we don't care about the ID. Uri baseUri = KeyRings.buildPublicKeyRingsUri(); + String where = null; + if(mCurQuery != null) + where = KeychainContract.UserIds.USER_ID + " LIKE \"" + mCurQuery + "%\""; // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. - return new CursorLoader(getActivity(), baseUri, PROJECTION, null, null, SORT_ORDER); + return new CursorLoader(getActivity(), baseUri, PROJECTION, where, null, SORT_ORDER); } @Override @@ -335,4 +344,34 @@ public class KeyListPublicFragment extends Fragment implements AdapterView.OnIte deleteKeyDialog.show(getActivity().getSupportFragmentManager(), "deleteKeyDialog"); } + + @Override + public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { + + // Get the searchview + MenuItem searchItem = menu.findItem(R.id.menu_key_list_public_search); + mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); + + // Execute this when searching + mSearchView.setOnQueryTextListener(this); + + super.onCreateOptionsMenu(menu, inflater); + + } + + @Override + public boolean onQueryTextSubmit(String s) { + return true; + } + + @Override + public boolean onQueryTextChange(String s) { + // Called when the action bar search text has changed. Update + // the search filter, and restart the loader to do a new query + // with this filter. + String newQuery = !TextUtils.isEmpty(s) ? s : null; + mCurQuery = newQuery; + getLoaderManager().restartLoader(0, null, this); + return true; + } } diff --git a/OpenPGP-Keychain/src/main/res/drawable-mdpi/ic_action_search.png b/OpenPGP-Keychain/src/main/res/drawable-mdpi/ic_action_search.png new file mode 100644 index 0000000000000000000000000000000000000000..f6719d228a614e6090fd20281eda453d5002f424 GIT binary patch literal 2349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<2c$4`+Lv!%##CeUqUZe z7%S_E(aQ3zkE;98`Qi0L@H#OZPkV777zv&*8W+E#%*X^e=p@t z;>FAvj@JXV2k9afjfgpYc6054X6^p|V?)^{2PdnZk6j;GJo`uw_<2f&cBR9}Q7zZ$ zp=n^>!(#Pc`)hVU5425{MBOc>mf|3&ZoeALGs59^rnwWmkU;6?rJ9W1|A`S{1w_KS4>#d^GHBD+#@>1^^ zuaSLpI689XNp+$;q=)94=eMODQ@&vExQqts1w0>06r?|fKO=a%-21d5kcT+L>wl_6j8z1&6N(yte(zc#P$`)_*N(|%? zJ|=E&%0O&z-v&lAszb2sIB(ssw|n)$Lv}3X=VrOychNpNzx-IU!jw?F&mH&~{Ep&}Cr}GX&9VlnxN@MBy4(Ein zjZ-Yv52D`hW#+KzEUcew+gR!LPpU*$c=GJ5;U>kM5(_D6Jnee68Dm`wiLNZWY(D75 zoeb`Csz$44o;YHrndG0Gek*dE*<=sjrgd9O6$*mj!2P0@^~uqem-i>Mu4Qsda*|ue zvMJKUQEpD_nv@a9$}K0Rx$xwU1jClX+gGmIqiI1#S9)H)NVUnrMYP(v7~X5D$(K7> zM;Y>>5CuDqHCW(!3Lj+I?5sTjlWa2^>w@a!Pxgs!(7m55ORNepi+{Zw(>}4 - + \ No newline at end of file From 7932dd8a81345f4f4cc10a4618c8f0856d9f45d1 Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Thu, 6 Mar 2014 03:37:04 +0530 Subject: [PATCH 2/3] Implement search for SelectPublicKeyFragment --- .../keychain/ui/SelectPublicKeyFragment.java | 36 ++++++++++++++++--- .../res/layout/select_public_key_activity.xml | 8 +++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java index 59b46dd00..e3e5c55fb 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java @@ -38,17 +38,22 @@ import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.widget.EditText; import android.widget.ListView; -public class SelectPublicKeyFragment extends ListFragmentWorkaround implements +public class SelectPublicKeyFragment extends ListFragmentWorkaround implements TextWatcher, LoaderManager.LoaderCallbacks { public static final String ARG_PRESELECTED_KEY_IDS = "preselected_key_ids"; private Activity mActivity; private SelectKeyCursorAdapter mAdapter; private ListView mListView; - + private EditText mSearchView; private long mSelectedMasterKeyIds[]; + private String mCurQuery; /** * Creates new instance of this fragment @@ -67,7 +72,8 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + mSearchView = (EditText)getActivity().findViewById(R.id.select_public_key_search); + mSearchView.addTextChangedListener(this); mSelectedMasterKeyIds = getArguments().getLongArray(ARG_PRESELECTED_KEY_IDS); } @@ -82,7 +88,6 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements mListView = getListView(); mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); - // Give some text to display if there is no data. In a real // application this would come from a resource. setEmptyText(getString(R.string.list_empty)); @@ -220,10 +225,14 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements // sort by selected master keys orderBy = inMasterKeyList + " DESC, " + orderBy; } + String where = null; + if(mCurQuery != null) + where = UserIds.USER_ID + " LIKE \"" + mCurQuery + "%\""; + // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. - return new CursorLoader(getActivity(), baseUri, projection, null, null, orderBy); + return new CursorLoader(getActivity(), baseUri, projection, where, null, orderBy); } @Override @@ -250,4 +259,21 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements // longer using it. mAdapter.swapCursor(null); } + + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + + } + + @Override + public void afterTextChanged(Editable editable) { + String newQuery = !TextUtils.isEmpty(editable.toString()) ? editable.toString() : null; + mCurQuery = newQuery; + getLoaderManager().restartLoader(0, null, this); + } } diff --git a/OpenPGP-Keychain/src/main/res/layout/select_public_key_activity.xml b/OpenPGP-Keychain/src/main/res/layout/select_public_key_activity.xml index a18ce46fc..5337433c6 100644 --- a/OpenPGP-Keychain/src/main/res/layout/select_public_key_activity.xml +++ b/OpenPGP-Keychain/src/main/res/layout/select_public_key_activity.xml @@ -4,8 +4,16 @@ android:layout_height="match_parent" android:layout_centerHorizontal="true" > + + From fab549c2b5cf637a4afc94c38f7745bfa733b316 Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Thu, 6 Mar 2014 16:27:35 +0530 Subject: [PATCH 3/3] Use selectionArgs parameter to pass mCurquery. --- .../keychain/ui/KeyListPublicFragment.java | 10 ++++++---- .../keychain/ui/SelectPublicKeyFragment.java | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java index af5d946eb..95267e01f 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListPublicFragment.java @@ -239,12 +239,14 @@ public class KeyListPublicFragment extends Fragment implements SearchView.OnQuer // sample only has one Loader, so we don't care about the ID. Uri baseUri = KeyRings.buildPublicKeyRingsUri(); String where = null; - if(mCurQuery != null) - where = KeychainContract.UserIds.USER_ID + " LIKE \"" + mCurQuery + "%\""; - + String whereArgs[] = null; + if(mCurQuery != null){ + where = KeychainContract.UserIds.USER_ID + " LIKE ?"; + whereArgs = new String[]{mCurQuery+"%"}; + } // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. - return new CursorLoader(getActivity(), baseUri, PROJECTION, where, null, SORT_ORDER); + return new CursorLoader(getActivity(), baseUri, PROJECTION, where, whereArgs, SORT_ORDER); } @Override diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java index e3e5c55fb..a43c84cf4 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java @@ -226,13 +226,15 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T orderBy = inMasterKeyList + " DESC, " + orderBy; } String where = null; - if(mCurQuery != null) - where = UserIds.USER_ID + " LIKE \"" + mCurQuery + "%\""; - + String whereArgs[] = null; + if(mCurQuery != null){ + where = UserIds.USER_ID + " LIKE ?"; + whereArgs = new String[]{mCurQuery+"%"}; + } // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. - return new CursorLoader(getActivity(), baseUri, projection, where, null, orderBy); + return new CursorLoader(getActivity(), baseUri, projection, where, whereArgs, orderBy); } @Override