From fdbed9cf58200ba3a2ee175b0456ebc85a575549 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 1 Dec 2018 13:27:00 +0100 Subject: [PATCH] open direct search after hitting + in share with screen --- .../AbstractSearchableListItemActivity.java | 29 ++--- .../ui/ChooseContactActivity.java | 102 ++++++++++++------ .../conversations/ui/ShareWithActivity.java | 1 + .../res/layout/activity_choose_contact.xml | 1 + 4 files changed, 86 insertions(+), 47 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/AbstractSearchableListItemActivity.java b/src/main/java/eu/siacs/conversations/ui/AbstractSearchableListItemActivity.java index bafcac0ea..78d1fb694 100644 --- a/src/main/java/eu/siacs/conversations/ui/AbstractSearchableListItemActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/AbstractSearchableListItemActivity.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -13,6 +14,7 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; +import android.widget.TextView; import java.util.ArrayList; import java.util.List; @@ -22,7 +24,7 @@ import eu.siacs.conversations.databinding.ActivityChooseContactBinding; import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.ui.adapter.ListItemAdapter; -public abstract class AbstractSearchableListItemActivity extends XmppActivity { +public abstract class AbstractSearchableListItemActivity extends XmppActivity implements TextView.OnEditorActionListener { protected ActivityChooseContactBinding binding; private final List listItems = new ArrayList<>(); private ArrayAdapter mListItemsAdapter; @@ -33,15 +35,10 @@ public abstract class AbstractSearchableListItemActivity extends XmppActivity { @Override public boolean onMenuItemActionExpand(final MenuItem item) { - mSearchEditText.post(new Runnable() { - - @Override - public void run() { - mSearchEditText.requestFocus(); - final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.showSoftInput(mSearchEditText, - InputMethodManager.SHOW_IMPLICIT); - } + mSearchEditText.post(() -> { + mSearchEditText.requestFocus(); + final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT); }); return true; @@ -50,8 +47,7 @@ public abstract class AbstractSearchableListItemActivity extends XmppActivity { @Override public boolean onMenuItemActionCollapse(final MenuItem item) { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(), - InputMethodManager.HIDE_IMPLICIT_ONLY); + imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); mSearchEditText.setText(""); filterContacts(); return true; @@ -108,8 +104,10 @@ public abstract class AbstractSearchableListItemActivity extends XmppActivity { getMenuInflater().inflate(R.menu.choose_contact, menu); final MenuItem menuSearchView = menu.findItem(R.id.action_search); final View mSearchView = menuSearchView.getActionView(); - mSearchEditText = (EditText) mSearchView.findViewById(R.id.search_field); + mSearchEditText = mSearchView.findViewById(R.id.search_field); mSearchEditText.addTextChangedListener(mSearchTextWatcher); + mSearchEditText.setHint(R.string.search_contacts); + mSearchEditText.setOnEditorActionListener(this); menuSearchView.setOnActionExpandListener(mOnActionExpandListener); return true; } @@ -129,4 +127,9 @@ public abstract class AbstractSearchableListItemActivity extends XmppActivity { void onBackendConnected() { filterContacts(); } + + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + return false; + } } diff --git a/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java b/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java index 3fce16b38..facc3f32f 100644 --- a/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ChooseContactActivity.java @@ -3,6 +3,7 @@ package eu.siacs.conversations.ui; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.StringRes; @@ -10,12 +11,15 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.view.ActionMode; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AbsListView.MultiChoiceModeListener; +import android.widget.AdapterView; import android.widget.ListView; +import android.widget.TextView; import java.util.ArrayList; import java.util.Arrays; @@ -37,7 +41,7 @@ import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.utils.XmppUri; import rocks.xmpp.addr.Jid; -public class ChooseContactActivity extends AbstractSearchableListItemActivity implements MultiChoiceModeListener { +public class ChooseContactActivity extends AbstractSearchableListItemActivity implements MultiChoiceModeListener, AdapterView.OnItemClickListener { public static final String EXTRA_TITLE_RES_ID = "extra_title_res_id"; public static final String EXTRA_GROUP_CHAT_NAME = "extra_group_chat_name"; public static final String EXTRA_SELECT_MULTIPLE = "extra_select_multiple"; @@ -49,6 +53,8 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im private Set filterContacts; private boolean showEnterJid = false; + private boolean startSearching = false; + private boolean multiple = false; private PendingItem postponedActivityResult = new PendingItem<>(); @@ -109,42 +115,24 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im Intent intent = getIntent(); - final boolean multiple = intent.getBooleanExtra(EXTRA_SELECT_MULTIPLE, false); + multiple = intent.getBooleanExtra(EXTRA_SELECT_MULTIPLE, false); if (multiple) { getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); getListView().setMultiChoiceModeListener(this); } - getListView().setOnItemClickListener((parent, view, position, id) -> { - if (multiple) { - startActionMode(this); - getListView().setItemChecked(position, true); - return; - } - final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); - final Intent request = getIntent(); - final Intent data = new Intent(); - final ListItem mListItem = getListItems().get(position); - data.putExtra("contact", mListItem.getJid().toString()); - String account = request.getStringExtra(EXTRA_ACCOUNT); - if (account == null && mListItem instanceof Contact) { - account = ((Contact) mListItem).getAccount().getJid().asBareJid().toString(); - } - data.putExtra(EXTRA_ACCOUNT, account); - data.putExtra(EXTRA_SELECT_MULTIPLE, false); - copy(request, data); - setResult(RESULT_OK, data); - finish(); - }); - final Intent i = getIntent(); - this.showEnterJid = i != null && i.getBooleanExtra(EXTRA_SHOW_ENTER_JID, false); + getListView().setOnItemClickListener(this); + this.showEnterJid = intent.getBooleanExtra(EXTRA_SHOW_ENTER_JID, false); this.binding.fab.setOnClickListener(this::onFabClicked); if (this.showEnterJid) { - this.binding.fab.setVisibility(View.VISIBLE); + this.binding.fab.show(); } else { - this.binding.fab.setVisibility(View.GONE); + binding.fab.setImageResource(R.drawable.ic_forward_white_24dp); } + + final SharedPreferences preferences = getPreferences(); + this.startSearching = intent.getBooleanExtra("direct_search", false) && preferences.getBoolean("start_searching", getResources().getBoolean(R.bool.start_searching)); + } private void onFabClicked(View v) { @@ -164,7 +152,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.setTitle(getTitleFromIntent()); binding.fab.setImageResource(R.drawable.ic_forward_white_24dp); - binding.fab.setVisibility(View.VISIBLE); + binding.fab.show(); final View view = getSearchEditText(); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (view != null && imm != null) { @@ -177,9 +165,9 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im public void onDestroyActionMode(ActionMode mode) { this.binding.fab.setImageResource(R.drawable.ic_person_add_white_24dp); if (this.showEnterJid) { - this.binding.fab.setVisibility(View.VISIBLE); + this.binding.fab.show(); } else { - this.binding.fab.setVisibility(View.GONE); + this.binding.fab.hide(); } selected.clear(); } @@ -245,6 +233,10 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im final Intent i = getIntent(); boolean showEnterJid = i != null && i.getBooleanExtra(EXTRA_SHOW_ENTER_JID, false); menu.findItem(R.id.action_scan_qr_code).setVisible(isCameraFeatureAvailable() && showEnterJid); + MenuItem mMenuSearchView = menu.findItem(R.id.action_search); + if (startSearching) { + mMenuSearchView.expandActionView(); + } return true; } @@ -254,6 +246,20 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im super.onSaveInstanceState(savedInstanceState); } + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (multiple) { + return false; + } else { + List items = getListItems(); + if (items.size() == 1) { + onListItemClicked(items.get(0)); + return true; + } + return false; + } + } + protected void filterContacts(final String needle) { getListItems().clear(); if (xmppConnectionService == null) { @@ -276,7 +282,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im } private String[] getSelectedContactJids() { - return selected.toArray(new String[selected.size()]); + return selected.toArray(new String[0]); } public void refreshUiReal() { @@ -364,8 +370,8 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im if (activityResult != null) { handleActivityResult(activityResult); } - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG); - if (fragment != null && fragment instanceof OnBackendConnected) { + final Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG); + if (fragment instanceof OnBackendConnected) { ((OnBackendConnected) fragment).onBackendConnected(); } } @@ -374,4 +380,32 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { ScanActivity.onRequestPermissionResult(this, requestCode, grantResults); } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + if (multiple) { + startActionMode(this); + getListView().setItemChecked(position, true); + return; + } + final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); + final ListItem mListItem = getListItems().get(position); + onListItemClicked(mListItem); + } + + private void onListItemClicked(ListItem item) { + final Intent request = getIntent(); + final Intent data = new Intent(); + data.putExtra("contact", item.getJid().toString()); + String account = request.getStringExtra(EXTRA_ACCOUNT); + if (account == null && item instanceof Contact) { + account = ((Contact) item).getAccount().getJid().asBareJid().toString(); + } + data.putExtra(EXTRA_ACCOUNT, account); + data.putExtra(EXTRA_SELECT_MULTIPLE, false); + copy(request, data); + setResult(RESULT_OK, data); + finish(); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index bcd783ed2..ef9289b83 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -112,6 +112,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer switch (item.getItemId()) { case R.id.action_add: final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class); + intent.putExtra("direct_search",true); startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION); return true; } diff --git a/src/main/res/layout/activity_choose_contact.xml b/src/main/res/layout/activity_choose_contact.xml index 13487b1ea..f44ef3509 100644 --- a/src/main/res/layout/activity_choose_contact.xml +++ b/src/main/res/layout/activity_choose_contact.xml @@ -24,6 +24,7 @@