|
1 | 1 | package co.tinode.tindroid;
|
2 | 2 |
|
| 3 | +import android.app.Activity; |
| 4 | +import android.app.SearchManager; |
3 | 5 | import android.content.Intent;
|
| 6 | +import android.net.Uri; |
4 | 7 | import android.os.Bundle;
|
| 8 | +import android.os.Handler; |
| 9 | +import android.text.TextUtils; |
5 | 10 | import android.util.Log;
|
| 11 | +import android.view.Menu; |
| 12 | +import android.view.MenuInflater; |
| 13 | +import android.view.MenuItem; |
| 14 | +import android.widget.SearchView; |
6 | 15 |
|
| 16 | +import java.util.Locale; |
| 17 | + |
| 18 | +import androidx.annotation.NonNull; |
7 | 19 | import androidx.appcompat.app.ActionBar;
|
8 | 20 | import androidx.appcompat.app.AppCompatActivity;
|
9 | 21 | import androidx.appcompat.widget.Toolbar;
|
| 22 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 23 | +import androidx.recyclerview.widget.RecyclerView; |
| 24 | +import co.tinode.tindroid.widgets.HorizontalListDivider; |
| 25 | +import co.tinode.tinodesdk.Topic; |
10 | 26 |
|
11 |
| -public class SendToActivity extends AppCompatActivity |
12 |
| - implements FindFragment.ReadContactsPermissionChecker { |
| 27 | +public class SendToActivity extends AppCompatActivity { |
13 | 28 | private static final String TAG = "SendToActivity";
|
14 | 29 |
|
15 |
| - // Limit the number of times permissions are requested per session. |
16 |
| - private boolean mReadContactsPermissionsAlreadyRequested = false; |
| 30 | + // Delay in milliseconds between the last keystroke and time when the query is sent to the server. |
| 31 | + private static final int SEARCH_REQUEST_DELAY = 1000; |
| 32 | + |
| 33 | + private ChatsAdapter mAdapter = null; |
| 34 | + private String mSearchTerm; // Stores the current search query term |
17 | 35 |
|
18 | 36 | @Override
|
19 | 37 | public void onCreate(Bundle savedInstanceState) {
|
@@ -46,17 +64,144 @@ public void onCreate(Bundle savedInstanceState) {
|
46 | 64 | });
|
47 | 65 | }
|
48 | 66 |
|
49 |
| - getSupportFragmentManager() |
50 |
| - .beginTransaction() |
51 |
| - .add(R.id.contentFragment, new FindFragment(), "contacts") |
52 |
| - .commitAllowingStateLoss(); |
| 67 | + RecyclerView rv = findViewById(R.id.chat_list); |
| 68 | + rv.setLayoutManager(new LinearLayoutManager(this)); |
| 69 | + rv.setHasFixedSize(true); |
| 70 | + rv.addItemDecoration(new HorizontalListDivider(this)); |
| 71 | + mAdapter = new ChatsAdapter(this, topicName -> { |
| 72 | + if (isFinishing() || isDestroyed()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + Intent launcher = new Intent(this, MessageActivity.class); |
| 76 | + Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 77 | + if (uri != null) { |
| 78 | + launcher.setDataAndType(uri, intent.getType()); |
| 79 | + launcher.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 80 | + } |
| 81 | + launcher.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); |
| 82 | + launcher.putExtra("topic", topicName); |
| 83 | + startActivity(launcher); |
| 84 | + finish(); |
| 85 | + }, Topic::isWriter); |
| 86 | + rv.setAdapter(mAdapter); |
53 | 87 | }
|
54 | 88 |
|
55 |
| - public boolean shouldRequestReadContactsPermission() { |
56 |
| - return !mReadContactsPermissionsAlreadyRequested; |
| 89 | + @Override |
| 90 | + public void onResume() { |
| 91 | + super.onResume(); |
| 92 | + mAdapter.resetContent(this); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 97 | + final SearchManager searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE); |
| 98 | + if (searchManager == null) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + MenuInflater inflater = getMenuInflater(); |
| 103 | + menu.clear(); |
| 104 | + inflater.inflate(R.menu.menu_search, menu); |
| 105 | + |
| 106 | + // Setting up SearchView |
| 107 | + |
| 108 | + // Locate the search item |
| 109 | + MenuItem searchItem = menu.findItem(R.id.action_search); |
| 110 | + |
| 111 | + // Retrieves the SearchView from the search menu item |
| 112 | + final SearchView searchView = (SearchView) searchItem.getActionView(); |
| 113 | + if (searchView == null) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + searchView.setQueryHint(getResources().getString(R.string.hint_search_tags)); |
| 117 | + // Assign searchable info to SearchView |
| 118 | + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); |
| 119 | + searchView.setFocusable(true); |
| 120 | + searchView.setFocusableInTouchMode(true); |
| 121 | + |
| 122 | + // Set listeners for SearchView |
| 123 | + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { |
| 124 | + private Handler mHandler; |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean onQueryTextSubmit(String queryText) { |
| 128 | + if (mHandler != null) { |
| 129 | + mHandler.removeCallbacksAndMessages(null); |
| 130 | + } |
| 131 | + |
| 132 | + mSearchTerm = doSearch(queryText); |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean onQueryTextChange(final String queryText) { |
| 139 | + |
| 140 | + if (mHandler == null) { |
| 141 | + mHandler = new Handler(); |
| 142 | + } else { |
| 143 | + mHandler.removeCallbacksAndMessages(null); |
| 144 | + } |
| 145 | + |
| 146 | + // Delay search in case of more input |
| 147 | + mHandler.postDelayed(() -> mSearchTerm = doSearch(queryText), SEARCH_REQUEST_DELAY); |
| 148 | + return true; |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { |
| 153 | + @Override |
| 154 | + public boolean onMenuItemActionExpand(@NonNull MenuItem menuItem) { |
| 155 | + searchView.setIconified(false); |
| 156 | + searchView.requestFocus(); |
| 157 | + searchView.requestFocusFromTouch(); |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public boolean onMenuItemActionCollapse(@NonNull MenuItem menuItem) { |
| 163 | + searchView.clearFocus(); |
| 164 | + mSearchTerm = null; |
| 165 | + return true; |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + |
| 170 | + if (mSearchTerm != null) { |
| 171 | + // If search term is already set here then this fragment is |
| 172 | + // being restored from a saved state and the search menu item |
| 173 | + // needs to be expanded and populated again. |
| 174 | + |
| 175 | + // Stores the search term (as it will be wiped out by |
| 176 | + // onQueryTextChange() when the menu item is expanded). |
| 177 | + final String savedSearchTerm = mSearchTerm; |
| 178 | + |
| 179 | + // Expands the search menu item |
| 180 | + searchItem.expandActionView(); |
| 181 | + |
| 182 | + // Sets the SearchView to the previous search string |
| 183 | + searchView.setQuery(savedSearchTerm, false); |
| 184 | + } |
| 185 | + return true; |
57 | 186 | }
|
58 | 187 |
|
59 |
| - public void setReadContactsPermissionRequested() { |
60 |
| - mReadContactsPermissionsAlreadyRequested = true; |
| 188 | + private String doSearch(String query) { |
| 189 | + query = query.trim().toLowerCase(Locale.getDefault()); |
| 190 | + query = !TextUtils.isEmpty(query) ? query : null; |
| 191 | + |
| 192 | + // No change. |
| 193 | + if (mSearchTerm == null && query == null) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + |
| 197 | + // Don't do anything if the new filter is the same as the current filter |
| 198 | + if (mSearchTerm != null && mSearchTerm.equals(query)) { |
| 199 | + return mSearchTerm; |
| 200 | + } |
| 201 | + |
| 202 | + mAdapter.setTextFilter(query); |
| 203 | + mAdapter.resetContent(this); |
| 204 | + |
| 205 | + return query; |
61 | 206 | }
|
62 | 207 | }
|
0 commit comments