This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
ActionBarAdapter.java
602 lines (533 loc) · 23.4 KB
/
ActionBarAdapter.java
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
594
595
596
597
598
599
600
601
602
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.contacts.activities;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SearchView.OnCloseListener;
import android.widget.TextView;
import com.android.contacts.R;
import com.android.contacts.activities.ActionBarAdapter.Listener.Action;
import com.android.contacts.activities.PeopleActivity;
import com.android.contacts.compat.CompatUtils;
import com.android.contacts.list.ContactsRequest;
import com.android.contacts.util.MaterialColorMapUtils;
import java.util.ArrayList;
/**
* Adapter for the action bar at the top of the Contacts activity.
*/
public class ActionBarAdapter implements OnCloseListener {
public interface Listener {
public abstract class Action {
public static final int CHANGE_SEARCH_QUERY = 0;
public static final int START_SEARCH_MODE = 1;
public static final int START_SELECTION_MODE = 2;
public static final int STOP_SEARCH_AND_SELECTION_MODE = 3;
public static final int BEGIN_STOPPING_SEARCH_AND_SELECTION_MODE = 4;
}
void onAction(int action);
void onUpButtonPressed();
}
private static final String EXTRA_KEY_SEARCH_MODE = "navBar.searchMode";
private static final String EXTRA_KEY_QUERY = "navBar.query";
private static final String EXTRA_KEY_SELECTED_MODE = "navBar.selectionMode";
private boolean mSelectionMode;
private boolean mSearchMode;
private String mQueryString;
private EditText mSearchView;
private View mClearSearchView;
private View mSearchContainer;
private View mSelectionContainer;
private int mMaxToolbarContentInsetStart;
private int mActionBarAnimationDuration;
private final Activity mActivity;
private Listener mListener;
private final ActionBar mActionBar;
private final Toolbar mToolbar;
/**
* Frame that contains the toolbar and draws the toolbar's background color. This is useful
* for placing things behind the toolbar.
*/
private final FrameLayout mToolBarFrame;
private boolean mShowHomeIcon;
private boolean mShowHomeAsUp;
private int mSearchHintResId;
private ValueAnimator mStatusBarAnimator;
public ActionBarAdapter(Activity activity, Listener listener, ActionBar actionBar,
Toolbar toolbar) {
this(activity, listener, actionBar, toolbar, R.string.hint_findContacts);
}
public ActionBarAdapter(Activity activity, Listener listener, ActionBar actionBar,
Toolbar toolbar, int searchHintResId) {
mActivity = activity;
mListener = listener;
mActionBar = actionBar;
mToolbar = toolbar;
mToolBarFrame = (FrameLayout) mToolbar.getParent();
mMaxToolbarContentInsetStart = mToolbar.getContentInsetStart();
mSearchHintResId = searchHintResId;
mActionBarAnimationDuration =
mActivity.getResources().getInteger(R.integer.action_bar_animation_duration);
setupSearchAndSelectionViews();
}
public void setShowHomeIcon(boolean showHomeIcon) {
mShowHomeIcon = showHomeIcon;
}
public void setShowHomeAsUp(boolean showHomeAsUp) {
mShowHomeAsUp = showHomeAsUp;
}
public View getSelectionContainer() {
return mSelectionContainer;
}
private void setupSearchAndSelectionViews() {
final LayoutInflater inflater = (LayoutInflater) mToolbar.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// Setup search bar
mSearchContainer = inflater.inflate(R.layout.search_bar_expanded, mToolbar,
/* attachToRoot = */ false);
mSearchContainer.setVisibility(View.VISIBLE);
mToolbar.addView(mSearchContainer);
mSearchContainer.setBackgroundColor(mActivity.getResources().getColor(
R.color.searchbox_background_color));
mSearchView = (EditText) mSearchContainer.findViewById(R.id.search_view);
mSearchView.setHint(mActivity.getString(mSearchHintResId));
mSearchView.addTextChangedListener(new SearchTextWatcher());
final ImageButton searchBackButton = (ImageButton) mSearchContainer
.findViewById(R.id.search_back_button);
searchBackButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onUpButtonPressed();
}
}
});
searchBackButton.getDrawable().setAutoMirrored(true);
mClearSearchView = mSearchContainer.findViewById(R.id.search_close_button);
mClearSearchView.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
setQueryString(null);
}
});
// Setup selection bar
mSelectionContainer = inflater.inflate(R.layout.selection_bar, mToolbar,
/* attachToRoot = */ false);
// Insert the selection container into mToolBarFrame behind the Toolbar, so that
// the Toolbar's MenuItems can appear on top of the selection container.
mToolBarFrame.addView(mSelectionContainer, 0);
mSelectionContainer.findViewById(R.id.selection_close).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onUpButtonPressed();
}
}
});
}
public void initialize(Bundle savedState, ContactsRequest request) {
if (savedState == null) {
mSearchMode = request.isSearchMode();
mQueryString = request.getQueryString();
mSelectionMode = false;
} else {
mSearchMode = savedState.getBoolean(EXTRA_KEY_SEARCH_MODE);
mSelectionMode = savedState.getBoolean(EXTRA_KEY_SELECTED_MODE);
mQueryString = savedState.getString(EXTRA_KEY_QUERY);
}
// Show tabs or the expanded {@link SearchView}, depending on whether or not we are in
// search mode.
update(true /* skipAnimation */);
// Expanding the {@link SearchView} clears the query, so set the query from the
// {@link ContactsRequest} after it has been expanded, if applicable.
if (mSearchMode && !TextUtils.isEmpty(mQueryString)) {
setQueryString(mQueryString);
}
}
public void setListener(Listener listener) {
mListener = listener;
}
private class SearchTextWatcher implements TextWatcher {
@Override
public void onTextChanged(CharSequence queryString, int start, int before, int count) {
if (queryString.equals(mQueryString)) {
return;
}
mQueryString = queryString.toString();
if (!mSearchMode) {
if (!TextUtils.isEmpty(queryString)) {
setSearchMode(true);
}
} else if (mListener != null) {
mListener.onAction(Action.CHANGE_SEARCH_QUERY);
}
mClearSearchView.setVisibility(
TextUtils.isEmpty(queryString) ? View.GONE : View.VISIBLE);
}
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}
/**
* @return Whether in search mode, i.e. if the search view is visible/expanded.
*
* Note even if the action bar is in search mode, if the query is empty, the search fragment
* will not be in search mode.
*/
public boolean isSearchMode() {
return mSearchMode;
}
/**
* @return Whether in selection mode, i.e. if the selection view is visible/expanded.
*/
public boolean isSelectionMode() {
return mSelectionMode;
}
public void setSearchMode(boolean flag) {
if (mSearchMode != flag) {
mSearchMode = flag;
update(false /* skipAnimation */);
if (mSearchView == null) {
return;
}
if (mSearchMode) {
mSearchView.setEnabled(true);
setFocusOnSearchView();
} else {
// Disable search view, so that it doesn't keep the IME visible.
mSearchView.setEnabled(false);
}
setQueryString(null);
} else if (flag) {
// Everything is already set up. Still make sure the keyboard is up
if (mSearchView != null) setFocusOnSearchView();
}
}
public void setSelectionMode(boolean flag) {
if (mSelectionMode != flag) {
mSelectionMode = flag;
update(false /* skipAnimation */);
}
}
public String getQueryString() {
return mSearchMode ? mQueryString : null;
}
public void setQueryString(String query) {
mQueryString = query;
if (mSearchView != null) {
mSearchView.setText(query);
// When programmatically entering text into the search view, the most reasonable
// place for the cursor is after all the text.
mSearchView.setSelection(mSearchView.getText() == null ?
0 : mSearchView.getText().length());
}
}
/** @return true if the "UP" icon is showing. */
public boolean isUpShowing() {
return mSearchMode; // Only shown on the search mode.
}
private void updateDisplayOptionsInner() {
// All the flags we may change in this method.
final int MASK = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP;
// The current flags set to the action bar. (only the ones that we may change here)
final int current = mActionBar.getDisplayOptions() & MASK;
final boolean isSearchOrSelectionMode = mSearchMode || mSelectionMode;
// Build the new flags...
int newFlags = 0;
if (mShowHomeIcon && !isSearchOrSelectionMode) {
newFlags |= ActionBar.DISPLAY_SHOW_HOME;
if (mShowHomeAsUp) {
newFlags |= ActionBar.DISPLAY_HOME_AS_UP;
}
}
if (mSearchMode && !mSelectionMode) {
// The search container is placed inside the toolbar. So we need to disable the
// Toolbar's content inset in order to allow the search container to be the width of
// the window.
mToolbar.setContentInsetsRelative(0, mToolbar.getContentInsetEnd());
}
if (!isSearchOrSelectionMode) {
newFlags |= ActionBar.DISPLAY_SHOW_TITLE;
mToolbar.setContentInsetsRelative(mMaxToolbarContentInsetStart,
mToolbar.getContentInsetEnd());
mToolbar.setNavigationIcon(R.drawable.quantum_ic_menu_vd_theme_24);
} else {
mToolbar.setNavigationIcon(null);
}
if (mSelectionMode) {
// Minimize the horizontal width of the Toolbar since the selection container is placed
// behind the toolbar and its left hand side needs to be clickable.
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolbar.getLayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.END;
mToolbar.setLayoutParams(params);
} else {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolbar.getLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.END;
mToolbar.setLayoutParams(params);
}
if (current != newFlags) {
// Pass the mask here to preserve other flags that we're not interested here.
mActionBar.setDisplayOptions(newFlags, MASK);
}
}
private void update(boolean skipAnimation) {
updateOverflowButtonColor();
final boolean isSelectionModeChanging
= (mSelectionContainer.getParent() == null) == mSelectionMode;
final boolean isSwitchingFromSearchToSelection =
mSearchMode && isSelectionModeChanging || mSearchMode && mSelectionMode;
final boolean isSearchModeChanging
= (mSearchContainer.getParent() == null) == mSearchMode;
final boolean isTabHeightChanging = isSearchModeChanging || isSelectionModeChanging;
// Update toolbar and status bar color.
mToolBarFrame.setBackgroundColor(MaterialColorMapUtils.getToolBarColor(mActivity));
updateStatusBarColor(isSelectionModeChanging && !isSearchModeChanging);
// When skipAnimation=true, it is possible that we will switch from search mode
// to selection mode directly. So we need to remove the undesired container in addition
// to adding the desired container.
if (skipAnimation || isSwitchingFromSearchToSelection) {
if (isTabHeightChanging || isSwitchingFromSearchToSelection) {
mToolbar.removeView(mSearchContainer);
mToolBarFrame.removeView(mSelectionContainer);
if (mSelectionMode) {
addSelectionContainer();
} else if (mSearchMode) {
addSearchContainer();
}
updateDisplayOptions(isSearchModeChanging);
}
return;
}
// Handle a switch to/from selection mode, due to UI interaction.
if (isSelectionModeChanging) {
if (mSelectionMode) {
addSelectionContainer();
mSelectionContainer.setAlpha(0);
mSelectionContainer.animate().alpha(1).setDuration(mActionBarAnimationDuration);
updateDisplayOptions(isSearchModeChanging);
} else {
if (mListener != null) {
mListener.onAction(Action.BEGIN_STOPPING_SEARCH_AND_SELECTION_MODE);
}
mSelectionContainer.setAlpha(1);
mSelectionContainer.animate().alpha(0).setDuration(mActionBarAnimationDuration)
.withEndAction(new Runnable() {
@Override
public void run() {
updateDisplayOptions(isSearchModeChanging);
mToolBarFrame.removeView(mSelectionContainer);
}
});
}
}
// Handle a switch to/from search mode, due to UI interaction.
if (isSearchModeChanging) {
if (mSearchMode) {
addSearchContainer();
mSearchContainer.setAlpha(0);
mSearchContainer.animate().alpha(1).setDuration(mActionBarAnimationDuration);
updateDisplayOptions(isSearchModeChanging);
} else {
mSearchContainer.setAlpha(1);
mSearchContainer.animate().alpha(0).setDuration(mActionBarAnimationDuration)
.withEndAction(new Runnable() {
@Override
public void run() {
updateDisplayOptions(isSearchModeChanging);
mToolbar.removeView(mSearchContainer);
}
});
}
}
}
/**
* Find overflow menu ImageView by its content description and update its color.
*/
public void updateOverflowButtonColor() {
final String overflowDescription = mActivity.getResources().getString(
R.string.abc_action_menu_overflow_description);
final ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Find the overflow ImageView.
final ArrayList<View> outViews = new ArrayList<>();
decorView.findViewsWithText(outViews, overflowDescription,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
for (View view : outViews) {
if (!(view instanceof ImageView)) {
continue;
}
final ImageView overflow = (ImageView) view;
// Update the overflow image color.
final int iconColor;
if (mSelectionMode) {
iconColor = mActivity.getResources().getColor(
R.color.actionbar_color_grey_solid);
} else {
iconColor = mActivity.getResources().getColor(
R.color.actionbar_text_color);
}
overflow.setImageTintList(ColorStateList.valueOf(iconColor));
}
// We're done, remove the listener.
decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
public void setSelectionCount(int selectionCount) {
TextView textView = (TextView) mSelectionContainer.findViewById(R.id.selection_count_text);
if (selectionCount == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setVisibility(View.VISIBLE);
}
textView.setText(String.valueOf(selectionCount));
}
public void setActionBarTitle(String title) {
final TextView textView =
(TextView) mSelectionContainer.findViewById(R.id.selection_count_text);
textView.setVisibility(View.VISIBLE);
textView.setText(title);
}
private void updateStatusBarColor(boolean shouldAnimate) {
if (!CompatUtils.isLollipopCompatible()) {
return; // we can't change the status bar color prior to Lollipop
}
if (mSelectionMode) {
final int cabStatusBarColor = ContextCompat.getColor(
mActivity, R.color.contextual_selection_bar_status_bar_color);
runStatusBarAnimation(/* colorTo */ cabStatusBarColor);
} else {
if (shouldAnimate) {
runStatusBarAnimation(/* colorTo */
MaterialColorMapUtils.getStatusBarColor(mActivity));
} else if (mActivity instanceof PeopleActivity) {
((PeopleActivity) mActivity).updateStatusBarBackground();
}
}
}
private void runStatusBarAnimation(int colorTo) {
final Window window = mActivity.getWindow();
if (window.getStatusBarColor() != colorTo) {
// Cancel running animation.
if (mStatusBarAnimator != null && mStatusBarAnimator.isRunning()) {
mStatusBarAnimator.cancel();
}
final int from = window.getStatusBarColor();
// Set up mStatusBarAnimator and run animation.
mStatusBarAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), from, colorTo);
mStatusBarAnimator.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
window.setStatusBarColor((Integer) animator.getAnimatedValue());
}
});
mStatusBarAnimator.setDuration(mActionBarAnimationDuration);
mStatusBarAnimator.setStartDelay(0);
mStatusBarAnimator.start();
}
}
private void addSearchContainer() {
mToolbar.removeView(mSearchContainer);
mToolbar.addView(mSearchContainer);
mSearchContainer.setAlpha(1);
}
private void addSelectionContainer() {
mToolBarFrame.removeView(mSelectionContainer);
mToolBarFrame.addView(mSelectionContainer, 0);
mSelectionContainer.setAlpha(1);
}
private void updateDisplayOptions(boolean isSearchModeChanging) {
if (mSearchMode && !mSelectionMode) {
setFocusOnSearchView();
// Since we have the {@link SearchView} in a custom action bar, we must manually handle
// expanding the {@link SearchView} when a search is initiated. Note that a side effect
// of this method is that the {@link SearchView} query text is set to empty string.
if (isSearchModeChanging) {
final CharSequence queryText = mSearchView.getText();
if (!TextUtils.isEmpty(queryText)) {
mSearchView.setText(queryText);
}
}
}
if (mListener != null) {
if (mSearchMode) {
mListener.onAction(Action.START_SEARCH_MODE);
}
if (mSelectionMode) {
mListener.onAction(Action.START_SELECTION_MODE);
}
if (!mSearchMode && !mSelectionMode) {
mListener.onAction(Action.STOP_SEARCH_AND_SELECTION_MODE);
}
}
updateDisplayOptionsInner();
}
@Override
public boolean onClose() {
setSearchMode(false);
return false;
}
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
outState.putBoolean(EXTRA_KEY_SELECTED_MODE, mSelectionMode);
outState.putString(EXTRA_KEY_QUERY, mQueryString);
}
public void setFocusOnSearchView() {
mSearchView.requestFocus();
showInputMethod(mSearchView); // Workaround for the "IME not popping up" issue.
}
private void showInputMethod(View view) {
final InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, 0);
}
}
}