• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.dialer.main.impl.toolbar;
18 
19 import android.animation.ValueAnimator;
20 import android.animation.ValueAnimator.AnimatorUpdateListener;
21 import android.content.Context;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.StringRes;
24 import android.support.v7.app.AppCompatActivity;
25 import android.support.v7.widget.Toolbar;
26 import android.util.AttributeSet;
27 import android.view.MenuItem;
28 import android.view.animation.AccelerateDecelerateInterpolator;
29 import android.widget.ImageButton;
30 import android.widget.PopupMenu;
31 import android.widget.RelativeLayout;
32 import com.android.dialer.common.Assert;
33 import com.android.dialer.util.ViewUtil;
34 import com.google.common.base.Optional;
35 
36 /** Toolbar for {@link com.android.dialer.main.impl.MainActivity}. */
37 public final class MainToolbar extends Toolbar implements PopupMenu.OnMenuItemClickListener {
38 
39   private static final int SLIDE_DURATION = 300;
40   private static final AccelerateDecelerateInterpolator SLIDE_INTERPOLATOR =
41       new AccelerateDecelerateInterpolator();
42 
43   private SearchBarView searchBar;
44   private SearchBarListener listener;
45   private MainToolbarMenu overflowMenu;
46   private boolean isSlideUp;
47 
MainToolbar(Context context, AttributeSet attrs)48   public MainToolbar(Context context, AttributeSet attrs) {
49     super(context, attrs);
50   }
51 
52   @Override
onFinishInflate()53   protected void onFinishInflate() {
54     super.onFinishInflate();
55     ImageButton optionsMenuButton = findViewById(R.id.main_options_menu_button);
56     overflowMenu = new MainToolbarMenu(getContext(), optionsMenuButton);
57     overflowMenu.inflate(R.menu.main_menu);
58     overflowMenu.setOnMenuItemClickListener(this);
59     optionsMenuButton.setOnClickListener(v -> overflowMenu.show());
60     optionsMenuButton.setOnTouchListener(overflowMenu.getDragToOpenListener());
61 
62     searchBar = findViewById(R.id.search_view_container);
63   }
64 
65   @Override
onMenuItemClick(MenuItem menuItem)66   public boolean onMenuItemClick(MenuItem menuItem) {
67     return listener.onMenuItemClicked(menuItem);
68   }
69 
setSearchBarListener(@onNull SearchBarListener listener)70   public void setSearchBarListener(@NonNull SearchBarListener listener) {
71     this.listener = Assert.isNotNull(listener);
72     searchBar.setSearchBarListener(listener);
73   }
74 
75   /** Slides the toolbar up and off the screen. */
slideUp(boolean animate)76   public void slideUp(boolean animate) {
77     Assert.checkArgument(!isSlideUp);
78     if (getHeight() == 0) {
79       ViewUtil.doOnGlobalLayout(this, view -> slideUp(animate));
80       return;
81     }
82     isSlideUp = true;
83     ValueAnimator animator = ValueAnimator.ofFloat(0, -getHeight());
84     animator.setDuration(animate ? SLIDE_DURATION : 0);
85     animator.setInterpolator(SLIDE_INTERPOLATOR);
86     animator.addUpdateListener(
87         new AnimatorUpdateListener() {
88           @Override
89           public void onAnimationUpdate(ValueAnimator animation) {
90             int val = ((Float) animation.getAnimatedValue()).intValue();
91             RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
92             params.topMargin = val;
93             requestLayout();
94           }
95         });
96     animator.start();
97   }
98 
99   /** Slides the toolbar down and back onto the screen. */
slideDown(boolean animate)100   public void slideDown(boolean animate) {
101     Assert.checkArgument(isSlideUp);
102     isSlideUp = false;
103     ValueAnimator animator = ValueAnimator.ofFloat(-getHeight(), 0);
104     animator.setDuration(animate ? SLIDE_DURATION : 0);
105     animator.setInterpolator(SLIDE_INTERPOLATOR);
106     animator.addUpdateListener(
107         new AnimatorUpdateListener() {
108           @Override
109           public void onAnimationUpdate(ValueAnimator animation) {
110             int val = ((Float) animation.getAnimatedValue()).intValue();
111             RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
112             params.topMargin = val;
113             requestLayout();
114           }
115         });
116     animator.start();
117   }
118 
119   /** @see SearchBarView#collapse(boolean) */
collapse(boolean animate)120   public void collapse(boolean animate) {
121     searchBar.collapse(animate);
122   }
123 
124   /** @see SearchBarView#collapse(boolean) */
expand(boolean animate, Optional<String> text)125   public void expand(boolean animate, Optional<String> text) {
126     searchBar.expand(animate, text);
127   }
128 
isSlideUp()129   public boolean isSlideUp() {
130     return isSlideUp;
131   }
132 
isExpanded()133   public boolean isExpanded() {
134     return searchBar.isExpanded();
135   }
136 
getQuery()137   public String getQuery() {
138     return searchBar.getQuery();
139   }
140 
transferQueryFromDialpad(String query)141   public void transferQueryFromDialpad(String query) {
142     searchBar.setQueryWithoutUpdate(query);
143   }
144 
hideKeyboard()145   public void hideKeyboard() {
146     searchBar.hideKeyboard();
147   }
148 
showKeyboard()149   public void showKeyboard() {
150     searchBar.showKeyboard();
151   }
152 
getOverflowMenu()153   public MainToolbarMenu getOverflowMenu() {
154     return overflowMenu;
155   }
156 
setHint(@tringRes int hint)157   public void setHint(@StringRes int hint) {
158     searchBar.setHint(hint);
159   }
160 
showClearFrequents(boolean show)161   public void showClearFrequents(boolean show) {
162     overflowMenu.showClearFrequents(show);
163   }
164 
maybeShowSimulator(AppCompatActivity appCompatActivity)165   public void maybeShowSimulator(AppCompatActivity appCompatActivity) {
166     overflowMenu.maybeShowSimulator(appCompatActivity);
167   }
168 }
169