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.content.Context; 20 import android.support.annotation.NonNull; 21 import android.support.annotation.StringRes; 22 import android.support.v7.app.AppCompatActivity; 23 import android.support.v7.widget.Toolbar; 24 import android.util.AttributeSet; 25 import android.view.MenuItem; 26 import android.view.View; 27 import android.view.animation.AccelerateDecelerateInterpolator; 28 import android.widget.ImageButton; 29 import android.widget.PopupMenu; 30 import com.android.dialer.common.Assert; 31 import com.android.dialer.common.LogUtil; 32 import com.android.dialer.util.ViewUtil; 33 import com.google.common.base.Optional; 34 35 /** Toolbar for {@link com.android.dialer.main.impl.MainActivity}. */ 36 public final class MainToolbar extends Toolbar implements PopupMenu.OnMenuItemClickListener { 37 38 private static final int SLIDE_DURATION = 300; 39 private static final AccelerateDecelerateInterpolator SLIDE_INTERPOLATOR = 40 new AccelerateDecelerateInterpolator(); 41 42 private SearchBarView searchBar; 43 private SearchBarListener listener; 44 private MainToolbarMenu overflowMenu; 45 private boolean hasGlobalLayoutListener; 46 MainToolbar(Context context, AttributeSet attrs)47 public MainToolbar(Context context, AttributeSet attrs) { 48 super(context, attrs); 49 } 50 51 @Override onFinishInflate()52 protected void onFinishInflate() { 53 super.onFinishInflate(); 54 ImageButton optionsMenuButton = findViewById(R.id.main_options_menu_button); 55 overflowMenu = new MainToolbarMenu(getContext(), optionsMenuButton); 56 overflowMenu.inflate(R.menu.main_menu); 57 overflowMenu.setOnMenuItemClickListener(this); 58 optionsMenuButton.setOnClickListener(v -> overflowMenu.show()); 59 optionsMenuButton.setOnTouchListener(overflowMenu.getDragToOpenListener()); 60 61 searchBar = findViewById(R.id.search_view_container); 62 } 63 64 @Override onMenuItemClick(MenuItem menuItem)65 public boolean onMenuItemClick(MenuItem menuItem) { 66 return listener.onMenuItemClicked(menuItem); 67 } 68 setSearchBarListener(@onNull SearchBarListener listener)69 public void setSearchBarListener(@NonNull SearchBarListener listener) { 70 this.listener = Assert.isNotNull(listener); 71 searchBar.setSearchBarListener(listener); 72 } 73 74 /** Slides the toolbar up and off the screen. */ slideUp(boolean animate, View container)75 public void slideUp(boolean animate, View container) { 76 if (hasGlobalLayoutListener) { 77 // Return early since we've already scheduled the toolbar to slide up 78 return; 79 } 80 81 if (getHeight() == 0) { 82 hasGlobalLayoutListener = true; 83 ViewUtil.doOnGlobalLayout( 84 this, 85 view -> { 86 hasGlobalLayoutListener = false; 87 slideUp(animate, container); 88 }); 89 return; 90 } 91 92 if (isSlideUp()) { 93 LogUtil.e("MainToolbar.slideDown", "Already slide up."); 94 return; 95 } 96 97 animate() 98 .translationY(-getHeight()) 99 .setDuration(animate ? SLIDE_DURATION : 0) 100 .setInterpolator(SLIDE_INTERPOLATOR) 101 .start(); 102 container 103 .animate() 104 .translationY(-getHeight()) 105 .setDuration(animate ? SLIDE_DURATION : 0) 106 .setInterpolator(SLIDE_INTERPOLATOR) 107 .start(); 108 } 109 110 /** Slides the toolbar down and back onto the screen. */ slideDown(boolean animate, View container)111 public void slideDown(boolean animate, View container) { 112 if (getTranslationY() == 0) { 113 LogUtil.e("MainToolbar.slideDown", "Already slide down."); 114 return; 115 } 116 animate() 117 .translationY(0) 118 .setDuration(animate ? SLIDE_DURATION : 0) 119 .setInterpolator(SLIDE_INTERPOLATOR) 120 .start(); 121 container 122 .animate() 123 .translationY(0) 124 .setDuration(animate ? SLIDE_DURATION : 0) 125 .setInterpolator(SLIDE_INTERPOLATOR) 126 .start(); 127 } 128 129 /** @see SearchBarView#collapse(boolean) */ collapse(boolean animate)130 public void collapse(boolean animate) { 131 searchBar.collapse(animate); 132 } 133 134 /** @see SearchBarView#expand(boolean, Optional, boolean) */ expand(boolean animate, Optional<String> text, boolean requestFocus)135 public void expand(boolean animate, Optional<String> text, boolean requestFocus) { 136 searchBar.expand(animate, text, requestFocus); 137 } 138 isSlideUp()139 public boolean isSlideUp() { 140 return getHeight() != 0 && getTranslationY() == -getHeight(); 141 } 142 isExpanded()143 public boolean isExpanded() { 144 return searchBar.isExpanded(); 145 } 146 getQuery()147 public String getQuery() { 148 return searchBar.getQuery(); 149 } 150 transferQueryFromDialpad(String query)151 public void transferQueryFromDialpad(String query) { 152 searchBar.setQueryWithoutUpdate(query); 153 } 154 hideKeyboard()155 public void hideKeyboard() { 156 searchBar.hideKeyboard(); 157 } 158 showKeyboard()159 public void showKeyboard() { 160 searchBar.showKeyboard(); 161 } 162 getOverflowMenu()163 public MainToolbarMenu getOverflowMenu() { 164 return overflowMenu; 165 } 166 setHint(@tringRes int hint)167 public void setHint(@StringRes int hint) { 168 searchBar.setHint(hint); 169 } 170 showClearFrequents(boolean show)171 public void showClearFrequents(boolean show) { 172 overflowMenu.showClearFrequents(show); 173 } 174 maybeShowSimulator(AppCompatActivity appCompatActivity)175 public void maybeShowSimulator(AppCompatActivity appCompatActivity) { 176 overflowMenu.maybeShowSimulator(appCompatActivity); 177 } 178 } 179