From 4aded3a43656f2eb8483e9b3632a04209ea7c8a7 Mon Sep 17 00:00:00 2001 From: Alan Evans Date: Wed, 27 May 2020 11:09:45 -0300 Subject: [PATCH] Close keyboard on contact list scroll. --- .../securesms/ContactSelectionActivity.java | 28 ++++++++------ .../ContactSelectionListFragment.java | 37 ++++++++++++++----- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionActivity.java b/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionActivity.java index 1f1d3d2d63..ef7932c596 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionActivity.java @@ -19,20 +19,18 @@ package org.thoughtcrime.securesms; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; -import org.thoughtcrime.securesms.logging.Log; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import org.thoughtcrime.securesms.components.ContactFilterToolbar; import org.thoughtcrime.securesms.contacts.ContactsCursorLoader.DisplayMode; import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper; -import org.thoughtcrime.securesms.recipients.Recipient; +import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.recipients.RecipientId; -import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.ServiceUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; -import org.thoughtcrime.securesms.util.ViewUtil; import org.whispersystems.libsignal.util.guava.Optional; import java.io.IOException; @@ -46,14 +44,14 @@ import java.lang.ref.WeakReference; */ public abstract class ContactSelectionActivity extends PassphraseRequiredActionBarActivity implements SwipeRefreshLayout.OnRefreshListener, - ContactSelectionListFragment.OnContactSelectedListener + ContactSelectionListFragment.OnContactSelectedListener, + ContactSelectionListFragment.ScrollCallback { private static final String TAG = ContactSelectionActivity.class.getSimpleName(); public static final String EXTRA_LAYOUT_RES_ID = "layout_res_id"; - private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme(); - private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); + private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme(); protected ContactSelectionListFragment contactsFragment; @@ -62,7 +60,6 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB @Override protected void onPreCreate() { dynamicTheme.onCreate(this); - dynamicLanguage.onCreate(this); } @Override @@ -84,7 +81,6 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB public void onResume() { super.onResume(); dynamicTheme.onResume(this); - dynamicLanguage.onResume(this); } protected ContactFilterToolbar getToolbar() { @@ -95,7 +91,6 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB this.toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - assert getSupportActionBar() != null; getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setIcon(null); @@ -123,6 +118,17 @@ public abstract class ContactSelectionActivity extends PassphraseRequiredActionB @Override public void onContactDeselected(Optional recipientId, String number) {} + @Override + public void onBeginScroll() { + hideKeyboard(); + } + + private void hideKeyboard() { + ServiceUtil.getInputMethodManager(this) + .hideSoftInputFromWindow(toolbar.getWindowToken(), 0); + toolbar.clearFocus(); + } + private static class RefreshDirectoryTask extends AsyncTask { private final WeakReference activity; diff --git a/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionListFragment.java b/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionListFragment.java index 82dc7f2b22..47c4c4ccb3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionListFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ContactSelectionListFragment.java @@ -67,7 +67,6 @@ import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.StickyHeaderDecoration; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.UsernameUtil; -import org.thoughtcrime.securesms.util.ViewUtil; import org.thoughtcrime.securesms.util.adapter.FixedViewsAdapter; import org.thoughtcrime.securesms.util.adapter.RecyclerViewConcatenateAdapterStickyHeader; import org.thoughtcrime.securesms.util.concurrent.SimpleTask; @@ -114,6 +113,7 @@ public final class ContactSelectionListFragment extends Fragment @Nullable private FixedViewsAdapter headerAdapter; @Nullable private FixedViewsAdapter footerAdapter; @Nullable private ListCallback listCallback; + @Nullable private ScrollCallback scrollCallback; private GlideRequests glideRequests; @Override @@ -123,6 +123,10 @@ public final class ContactSelectionListFragment extends Fragment if (context instanceof ListCallback) { listCallback = (ListCallback) context; } + + if (context instanceof ScrollCallback) { + scrollCallback = (ScrollCallback) context; + } } @Override @@ -164,10 +168,10 @@ public final class ContactSelectionListFragment extends Fragment public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.contact_selection_list_fragment, container, false); - emptyText = ViewUtil.findById(view, android.R.id.empty); - recyclerView = ViewUtil.findById(view, R.id.recycler_view); - swipeRefresh = ViewUtil.findById(view, R.id.swipe_refresh); - fastScroller = ViewUtil.findById(view, R.id.fast_scroller); + emptyText = view.findViewById(android.R.id.empty); + recyclerView = view.findViewById(R.id.recycler_view); + swipeRefresh = view.findViewById(R.id.swipe_refresh); + fastScroller = view.findViewById(R.id.fast_scroller); showContactsLayout = view.findViewById(R.id.show_contacts_container); showContactsButton = view.findViewById(R.id.show_contacts_button); showContactsDescription = view.findViewById(R.id.show_contacts_description); @@ -236,6 +240,16 @@ public final class ContactSelectionListFragment extends Fragment recyclerView.setAdapter(concatenateAdapter); recyclerView.addItemDecoration(new StickyHeaderDecoration(concatenateAdapter, true, true)); + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { + if (scrollCallback != null) { + scrollCallback.onBeginScroll(); + } + } + } + }); } private View createInviteActionView(@NonNull ListCallback listCallback) { @@ -292,15 +306,16 @@ public final class ContactSelectionListFragment extends Fragment cursorRecyclerViewAdapter.clearSelectedContacts(); if (!isDetached() && !isRemoving() && getActivity() != null && !getActivity().isFinishing()) { - getLoaderManager().restartLoader(0, null, this); + LoaderManager.getInstance(this).restartLoader(0, null, this); } } @Override public @NonNull Loader onCreateLoader(int id, Bundle args) { - return new ContactsCursorLoader(getActivity(), - getActivity().getIntent().getIntExtra(DISPLAY_MODE, DisplayMode.FLAG_ALL), - cursorFilter, getActivity().getIntent().getBooleanExtra(RECENTS, false)); + FragmentActivity activity = requireActivity(); + return new ContactsCursorLoader(activity, + activity.getIntent().getIntExtra(DISPLAY_MODE, DisplayMode.FLAG_ALL), + cursorFilter, activity.getIntent().getBooleanExtra(RECENTS, false)); } @Override @@ -501,4 +516,8 @@ public final class ContactSelectionListFragment extends Fragment void onInvite(); void onNewGroup(); } + + public interface ScrollCallback { + void onBeginScroll(); + } }