From 50dbd4077f825bb6e594d652745cd5461db3a9df Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 4 Dec 2018 21:01:08 +0100 Subject: open direct search after hitting + in share with screen --- .../ui/AbstractSearchableListItemActivity.java | 27 +++--- .../pixart/messenger/ui/ChooseContactActivity.java | 100 ++++++++++++++------- .../de/pixart/messenger/ui/ShareWithActivity.java | 1 + 3 files changed, 82 insertions(+), 46 deletions(-) (limited to 'src/main/java/de/pixart/messenger') diff --git a/src/main/java/de/pixart/messenger/ui/AbstractSearchableListItemActivity.java b/src/main/java/de/pixart/messenger/ui/AbstractSearchableListItemActivity.java index 91dc52ecf..01246ad92 100644 --- a/src/main/java/de/pixart/messenger/ui/AbstractSearchableListItemActivity.java +++ b/src/main/java/de/pixart/messenger/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 de.pixart.messenger.databinding.ActivityChooseContactBinding; import de.pixart.messenger.entities.ListItem; import de.pixart.messenger.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; @@ -110,6 +106,8 @@ public abstract class AbstractSearchableListItemActivity extends XmppActivity { final View mSearchView = menuSearchView.getActionView(); 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/de/pixart/messenger/ui/ChooseContactActivity.java b/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java index ac1866fec..593dd5645 100644 --- a/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java @@ -3,6 +3,7 @@ package de.pixart.messenger.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 de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.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,22 @@ 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 +150,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 +163,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 +231,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 +244,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 +280,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() { @@ -365,8 +369,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(); } } @@ -375,4 +379,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(); + } } \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java index a5d64c820..0d1a3286d 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java @@ -101,6 +101,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; } -- cgit v1.2.3