1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
package ml.docilealligator.infinityforreddit.activities;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.inputmethod.EditorInfoCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.thing.SelectThingReturnKey;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.adapters.SearchActivityRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr;
import ml.docilealligator.infinityforreddit.databinding.ActivitySearchBinding;
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.multireddit.MultiReddit;
import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery;
import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQueryViewModel;
import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData;
import ml.docilealligator.infinityforreddit.subreddit.SubredditData;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class SearchActivity extends BaseActivity {
public static final String EXTRA_QUERY = "EQ";
public static final String EXTRA_SEARCH_IN_SUBREDDIT_OR_USER_NAME = "ESISOUN";
public static final String EXTRA_SEARCH_IN_MULTIREDDIT = "ESIM";
public static final String EXTRA_SEARCH_IN_THING_TYPE = "ESITY";
public static final String EXTRA_SEARCH_ONLY_SUBREDDITS = "ESOS";
public static final String EXTRA_SEARCH_ONLY_USERS = "ESOU";
public static final String EXTRA_SEARCH_SUBREDDITS_AND_USERS = "ESSAU";
public static final String RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES = "RESSN";
public static final String RETURN_EXTRA_SELECTED_USERNAMES = "RESU";
public static final String EXTRA_IS_MULTI_SELECTION = "EIMS";
public static final int SUICIDE_PREVENTION_ACTIVITY_REQUEST_CODE = 101;
private static final String SEARCH_IN_SUBREDDIT_OR_NAME_STATE = "SNS";
private static final String SEARCH_IN_THING_TYPE_STATE = "SITTS";
private static final String SEARCH_IN_MULTIREDDIT_STATE = "SIMS";
private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1;
private static final int USER_SEARCH_REQUEST_CODE = 2;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
@Named("nsfw_and_spoiler")
SharedPreferences mNsfwAndSpoilerSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor executor;
private String query;
private String searchInSubredditOrUserName;
private MultiReddit searchInMultiReddit;
@SelectThingReturnKey.THING_TYPE
private int searchInThingType;
private boolean searchOnlySubreddits;
private boolean searchOnlyUsers;
private boolean searchSubredditsAndUsers;
private SearchActivityRecyclerViewAdapter adapter;
private SubredditAutocompleteRecyclerViewAdapter subredditAutocompleteRecyclerViewAdapter;
private Handler handler;
private Runnable autoCompleteRunnable;
private Call<String> subredditAutocompleteCall;
RecentSearchQueryViewModel mRecentSearchQueryViewModel;
private ActivityResultLauncher<Intent> requestThingSelectionForCurrentActivityLauncher;
private ActivityResultLauncher<Intent> requestThingSelectionForAnotherActivityLauncher;
private ActivitySearchBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this);
setImmersiveModeNotApplicable();
super.onCreate(savedInstanceState);
binding = ActivitySearchBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
EventBus.getDefault().register(this);
applyCustomTheme();
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) {
Slidr.attach(this);
}
setSupportActionBar(binding.toolbar);
binding.clearSearchEditViewSearchActivity.setVisibility(View.GONE);
binding.deleteAllRecentSearchesButtonSearchActivity.setVisibility(View.GONE);
searchOnlySubreddits = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_SUBREDDITS, false);
searchOnlyUsers = getIntent().getBooleanExtra(EXTRA_SEARCH_ONLY_USERS, false);
searchSubredditsAndUsers = getIntent().getBooleanExtra(EXTRA_SEARCH_SUBREDDITS_AND_USERS, false);
if (searchOnlySubreddits) {
binding.searchEditTextSearchActivity.setHint(R.string.search_only_subreddits_hint);
} else if (searchOnlyUsers) {
binding.searchEditTextSearchActivity.setHint(R.string.search_only_users_hint);
} else if (searchSubredditsAndUsers) {
binding.searchEditTextSearchActivity.setHint(R.string.search_subreddits_and_users_hint);
}
boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName.equals(Account.ANONYMOUS_ACCOUNT) ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false);
subredditAutocompleteRecyclerViewAdapter = new SubredditAutocompleteRecyclerViewAdapter(this,
mCustomThemeWrapper, subredditData -> {
if (searchOnlySubreddits || searchSubredditsAndUsers) {
Intent returnIntent = new Intent();
if (getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)) {
ArrayList<String> subredditNameList = new ArrayList<>();
subredditNameList.add(subredditData.getName());
returnIntent.putStringArrayListExtra(RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES, subredditNameList);
} else {
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME, subredditData.getName());
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_ICON, subredditData.getIconUrl());
}
setResult(Activity.RESULT_OK, returnIntent);
} else {
Intent intent = new Intent(SearchActivity.this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName());
startActivity(intent);
}
finish();
});
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
binding.searchEditTextSearchActivity.setImeOptions(binding.searchEditTextSearchActivity.getImeOptions() | EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING);
}
handler = new Handler();
binding.searchEditTextSearchActivity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) {
subredditAutocompleteCall.cancel();
}
if (autoCompleteRunnable != null) {
handler.removeCallbacks(autoCompleteRunnable);
}
}
@Override
public void afterTextChanged(Editable s) {
String currentQuery = s.toString().trim();
if (!currentQuery.isEmpty()) {
binding.clearSearchEditViewSearchActivity.setVisibility(View.VISIBLE);
autoCompleteRunnable = () -> {
subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken),
currentQuery, nsfw);
subredditAutocompleteCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
subredditAutocompleteCall = null;
if (response.isSuccessful() && !call.isCanceled()) {
ParseSubredditData.parseSubredditListingData(executor, handler,
response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() {
@Override
public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.GONE);
binding.subredditAutocompleteRecyclerViewSearchActivity.setVisibility(View.VISIBLE);
subredditAutocompleteRecyclerViewAdapter.setSubreddits(subredditData);
}
@Override
public void onParseSubredditListingDataFail() {
}
});
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
subredditAutocompleteCall = null;
}
});
};
handler.postDelayed(autoCompleteRunnable, 500);
} else {
binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.VISIBLE);
binding.subredditAutocompleteRecyclerViewSearchActivity.setVisibility(View.GONE);
binding.clearSearchEditViewSearchActivity.setVisibility(View.GONE);
}
}
});
binding.searchEditTextSearchActivity.setOnEditorActionListener((v, actionId, event) -> {
if ((actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_SEARCH) || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)) {
if (!binding.searchEditTextSearchActivity.getText().toString().isEmpty()) {
search(binding.searchEditTextSearchActivity.getText().toString());
return true;
}
}
return false;
});
binding.clearSearchEditViewSearchActivity.setOnClickListener(view -> {
binding.searchEditTextSearchActivity.getText().clear();
});
binding.linkHandlerImageViewSearchActivity.setOnClickListener(view -> {
if (!binding.searchEditTextSearchActivity.getText().toString().equals("")) {
Intent intent = new Intent(this, LinkResolverActivity.class);
intent.setData(Uri.parse(binding.searchEditTextSearchActivity.getText().toString()));
startActivity(intent);
finish();
}
});
binding.deleteAllRecentSearchesButtonSearchActivity.setOnClickListener(view -> {
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.confirm)
.setMessage(R.string.confirm_delete_all_recent_searches)
.setPositiveButton(R.string.yes, (dialogInterface, i) -> {
Utils.hideKeyboard(SearchActivity.this);
executor.execute(() -> {
List<RecentSearchQuery> deletedQueries = mRedditDataRoomDatabase.recentSearchQueryDao().getAllRecentSearchQueries(accountName);
mRedditDataRoomDatabase.recentSearchQueryDao().deleteAllRecentSearchQueries(accountName);
handler.post(() -> Snackbar.make(view, R.string.all_recent_searches_deleted, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, v -> executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().insertAll(deletedQueries)))
.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
Utils.showKeyboard(SearchActivity.this, handler, binding.searchEditTextSearchActivity);
}
})
.show());
});
})
.setNegativeButton(R.string.no, null)
.show();
});
if (savedInstanceState != null) {
searchInSubredditOrUserName = savedInstanceState.getString(SEARCH_IN_SUBREDDIT_OR_NAME_STATE);
searchInThingType = savedInstanceState.getInt(SEARCH_IN_THING_TYPE_STATE);
searchInMultiReddit = savedInstanceState.getParcelable(SEARCH_IN_MULTIREDDIT_STATE);
setSearchInThingText();
} else {
query = getIntent().getStringExtra(EXTRA_QUERY);
searchInSubredditOrUserName = getIntent().getStringExtra(EXTRA_SEARCH_IN_SUBREDDIT_OR_USER_NAME);
searchInMultiReddit = getIntent().getParcelableExtra(EXTRA_SEARCH_IN_MULTIREDDIT);
searchInThingType = getIntent().getIntExtra(EXTRA_SEARCH_IN_THING_TYPE, SelectThingReturnKey.THING_TYPE.SUBREDDIT);
if (searchInSubredditOrUserName != null) {
binding.subredditNameTextViewSearchActivity.setText(searchInSubredditOrUserName);
} else if (searchInMultiReddit != null) {
binding.subredditNameTextViewSearchActivity.setText(searchInMultiReddit.getDisplayName());
}
}
bindView();
requestThingSelectionForCurrentActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Intent returnIntent = result.getData();
if (returnIntent == null || returnIntent.getExtras() == null) {
return;
}
searchInSubredditOrUserName = returnIntent.getStringExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME);
searchInMultiReddit = returnIntent.getParcelableExtra(SelectThingReturnKey.RETRUN_EXTRA_MULTIREDDIT);
searchInThingType = returnIntent.getIntExtra(SelectThingReturnKey.RETURN_EXTRA_THING_TYPE, SelectThingReturnKey.THING_TYPE.SUBREDDIT);
setSearchInThingText();
});
requestThingSelectionForAnotherActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
setResult(RESULT_OK, result.getData());
finish();
});
if (searchOnlySubreddits || searchOnlyUsers || searchSubredditsAndUsers) {
binding.subredditNameRelativeLayoutSearchActivity.setVisibility(View.GONE);
binding.dividerSearchActivity.setVisibility(View.GONE);
} else {
binding.subredditNameRelativeLayoutSearchActivity.setOnClickListener(view -> {
Intent intent = new Intent(this, SubscribedThingListingActivity.class);
intent.putExtra(SubscribedThingListingActivity.EXTRA_THING_SELECTION_MODE, true);
intent.putExtra(SubscribedThingListingActivity.EXTRA_EXTRA_CLEAR_SELECTION, true);
requestThingSelectionForCurrentActivityLauncher.launch(intent);
});
}
}
private void bindView() {
if (!accountName.equals(Account.ANONYMOUS_ACCOUNT)) {
adapter = new SearchActivityRecyclerViewAdapter(this, mCustomThemeWrapper, new SearchActivityRecyclerViewAdapter.ItemOnClickListener() {
@Override
public void onClick(RecentSearchQuery recentSearchQuery) {
searchInSubredditOrUserName = recentSearchQuery.getSearchInSubredditOrUserName();
searchInMultiReddit = MultiReddit.getDummyMultiReddit(recentSearchQuery.getMultiRedditPath());
if (searchInMultiReddit != null && recentSearchQuery.getMultiRedditDisplayName() != null) {
searchInMultiReddit.setDisplayName(recentSearchQuery.getMultiRedditDisplayName());
}
searchInThingType = recentSearchQuery.getSearchInThingType();
search(recentSearchQuery.getSearchQuery());
}
@Override
public void onDelete(RecentSearchQuery recentSearchQuery) {
Utils.hideKeyboard(SearchActivity.this);
executor.execute(() -> {
mRedditDataRoomDatabase.recentSearchQueryDao().deleteRecentSearchQueries(recentSearchQuery);
Snackbar.make(binding.getRoot(), R.string.recent_search_deleted, Snackbar.LENGTH_SHORT)
.setAction(R.string.undo, v -> executor.execute(() -> mRedditDataRoomDatabase.recentSearchQueryDao().insert(recentSearchQuery)))
.addCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
Utils.showKeyboard(SearchActivity.this, handler, binding.searchEditTextSearchActivity);
}
})
.show();
});
}
});
binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.VISIBLE);
binding.recentSearchQueryRecyclerViewSearchActivity.setNestedScrollingEnabled(false);
binding.recentSearchQueryRecyclerViewSearchActivity.setAdapter(adapter);
binding.recentSearchQueryRecyclerViewSearchActivity.addItemDecoration(new RecyclerView.ItemDecoration() {
final int spacing = (int) Utils.convertDpToPixel(16, SearchActivity.this);
final int halfSpacing = spacing / 2;
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int column = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
boolean toTheLeft = column == 0;
if (toTheLeft) {
outRect.left = spacing;
outRect.right = halfSpacing;
} else {
outRect.left = halfSpacing;
outRect.right = spacing;
}
outRect.top = spacing;
}
});
binding.subredditAutocompleteRecyclerViewSearchActivity.setAdapter(subredditAutocompleteRecyclerViewAdapter);
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SEARCH_HISTORY, true)) {
mRecentSearchQueryViewModel = new ViewModelProvider(this,
new RecentSearchQueryViewModel.Factory(mRedditDataRoomDatabase, accountName))
.get(RecentSearchQueryViewModel.class);
mRecentSearchQueryViewModel.getAllRecentSearchQueries().observe(this, recentSearchQueries -> {
if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) {
binding.dividerSearchActivity.setVisibility(View.VISIBLE);
binding.deleteAllRecentSearchesButtonSearchActivity.setVisibility(View.VISIBLE);
} else {
binding.dividerSearchActivity.setVisibility(View.GONE);
binding.deleteAllRecentSearchesButtonSearchActivity.setVisibility(View.GONE);
}
adapter.setRecentSearchQueries(recentSearchQueries);
});
}
}
}
private void search(String query) {
if (query.equalsIgnoreCase("suicide") && mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_SUICIDE_PREVENTION_ACTIVITY, true)) {
Intent intent = new Intent(this, SuicidePreventionActivity.class);
intent.putExtra(SuicidePreventionActivity.EXTRA_QUERY, query);
startActivityForResult(intent, SUICIDE_PREVENTION_ACTIVITY_REQUEST_CODE);
} else {
openSearchResult(query);
}
}
private void openSearchResult(String query) {
if (searchOnlySubreddits) {
Intent intent = new Intent(SearchActivity.this, SearchSubredditsResultActivity.class);
intent.putExtra(SearchSubredditsResultActivity.EXTRA_QUERY, query);
intent.putExtra(SearchSubredditsResultActivity.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false));
startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE);
} else if (searchOnlyUsers) {
Intent intent = new Intent(this, SearchUsersResultActivity.class);
intent.putExtra(SearchUsersResultActivity.EXTRA_QUERY, query);
intent.putExtra(SearchUsersResultActivity.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false));
startActivityForResult(intent, USER_SEARCH_REQUEST_CODE);
} else if (searchSubredditsAndUsers) {
Intent intent = new Intent(this, SearchResultActivity.class);
intent.putExtra(SearchResultActivity.EXTRA_QUERY, query);
intent.putExtra(SearchResultActivity.EXTRA_SHOULD_RETURN_SUBREDDIT_AND_USER_NAME, true);
requestThingSelectionForAnotherActivityLauncher.launch(intent);
} else {
Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
intent.putExtra(SearchResultActivity.EXTRA_QUERY, query);
intent.putExtra(SearchResultActivity.EXTRA_SEARCH_IN_SUBREDDIT_OR_USER_NAME, searchInSubredditOrUserName);
intent.putExtra(SearchResultActivity.EXTRA_SEARCH_IN_MULTIREDDIT, searchInMultiReddit);
intent.putExtra(SearchResultActivity.EXTRA_SEARCH_IN_THING_TYPE, searchInThingType);
startActivity(intent);
finish();
}
}
private void setSearchInThingText() {
switch (searchInThingType) {
case SelectThingReturnKey.THING_TYPE.SUBREDDIT:
case SelectThingReturnKey.THING_TYPE.USER:
if (searchInSubredditOrUserName == null) {
binding.subredditNameTextViewSearchActivity.setText(R.string.all_subreddits);
} else {
binding.subredditNameTextViewSearchActivity.setText(searchInSubredditOrUserName);
}
break;
case SelectThingReturnKey.THING_TYPE.MULTIREDDIT:
binding.subredditNameTextViewSearchActivity.setText(searchInMultiReddit.getDisplayName());
}
}
@Override
public SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences;
}
@Override
public SharedPreferences getCurrentAccountSharedPreferences() {
return mCurrentAccountSharedPreferences;
}
@Override
public CustomThemeWrapper getCustomThemeWrapper() {
return mCustomThemeWrapper;
}
@Override
protected void applyCustomTheme() {
binding.getRoot().setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutSearchActivity, null, binding.toolbar);
int toolbarPrimaryTextAndIconColorColor = mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor();
binding.searchEditTextSearchActivity.setTextColor(toolbarPrimaryTextAndIconColorColor);
binding.searchEditTextSearchActivity.setHintTextColor(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor());
binding.clearSearchEditViewSearchActivity.setColorFilter(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor(), android.graphics.PorterDuff.Mode.SRC_IN);
binding.linkHandlerImageViewSearchActivity.setColorFilter(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor(), android.graphics.PorterDuff.Mode.SRC_IN);
int colorAccent = mCustomThemeWrapper.getColorAccent();
binding.searchInTextViewSearchActivity.setTextColor(colorAccent);
binding.subredditNameTextViewSearchActivity.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
binding.deleteAllRecentSearchesButtonSearchActivity.setIconTint(ColorStateList.valueOf(mCustomThemeWrapper.getPrimaryIconColor()));
binding.dividerSearchActivity.setBackgroundColor(mCustomThemeWrapper.getDividerColor());
if (typeface != null) {
Utils.setFontToAllTextViews(binding.getRoot(), typeface);
}
}
@Override
protected void onStart() {
super.onStart();
binding.searchEditTextSearchActivity.requestFocus();
if (query != null) {
binding.searchEditTextSearchActivity.setText(query);
binding.searchEditTextSearchActivity.setSelection(query.length());
query = null;
}
Utils.showKeyboard(this, new Handler(), binding.searchEditTextSearchActivity);
}
@Override
protected void onPause() {
super.onPause();
Utils.hideKeyboard(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_OK && data != null) {
if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE) {
Intent returnIntent = new Intent();
if (getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)) {
returnIntent.putStringArrayListExtra(RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES, data.getStringArrayListExtra(SearchSubredditsResultActivity.RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES));
} else {
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME, data.getStringExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME));
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_ICON, data.getStringExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_ICON));
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_THING_TYPE, SelectThingReturnKey.THING_TYPE.SUBREDDIT);
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else if (requestCode == USER_SEARCH_REQUEST_CODE) {
Intent returnIntent = new Intent();
if (getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)) {
returnIntent.putStringArrayListExtra(RETURN_EXTRA_SELECTED_USERNAMES, data.getStringArrayListExtra(SearchUsersResultActivity.RETURN_EXTRA_SELECTED_USERNAMES));
} else {
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME, data.getStringExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_NAME));
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_ICON, data.getStringExtra(SelectThingReturnKey.RETURN_EXTRA_SUBREDDIT_OR_USER_ICON));
returnIntent.putExtra(SelectThingReturnKey.RETURN_EXTRA_THING_TYPE, SelectThingReturnKey.THING_TYPE.USER);
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else if (requestCode == SUICIDE_PREVENTION_ACTIVITY_REQUEST_CODE) {
openSearchResult(data.getStringExtra(SuicidePreventionActivity.EXTRA_RETURN_QUERY));
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return false;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SEARCH_IN_SUBREDDIT_OR_NAME_STATE, searchInSubredditOrUserName);
outState.putInt(SEARCH_IN_THING_TYPE_STATE, searchInThingType);
outState.putParcelable(SEARCH_IN_MULTIREDDIT_STATE, searchInMultiReddit);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
}
|