NavigationBarTablet.java revision 30adae6ebf17a724d24066a200432e6b22a4eeff
1/*
2 * Copyright (C) 2011 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 */
16package com.android.browser;
17
18import android.animation.Animator;
19import android.animation.AnimatorListenerAdapter;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
22import android.content.Context;
23import android.content.res.Configuration;
24import android.content.res.Resources;
25import android.graphics.drawable.Drawable;
26import android.text.TextUtils;
27import android.util.AttributeSet;
28import android.view.View;
29import android.widget.ImageButton;
30import android.widget.ImageView;
31
32import java.util.List;
33
34public class NavigationBarTablet extends NavigationBarBase {
35
36    private Drawable mStopDrawable;
37    private Drawable mReloadDrawable;
38    private String mStopDescription;
39    private String mRefreshDescription;
40
41    private View mUrlContainer;
42    private ImageButton mBackButton;
43    private ImageButton mForwardButton;
44    private ImageView mStar;
45    private ImageView mUrlIcon;
46    private ImageView mSearchButton;
47    private View mGoButton;
48    private ImageView mStopButton;
49    private View mAllButton;
50    private View mClearButton;
51    private ImageView mVoiceSearch;
52    private View mNavButtons;
53    private Drawable mFocusDrawable;
54    private Drawable mUnfocusDrawable;
55    private boolean mHideNavButtons;
56
57    public NavigationBarTablet(Context context) {
58        super(context);
59        init(context);
60    }
61
62    public NavigationBarTablet(Context context, AttributeSet attrs) {
63        super(context, attrs);
64        init(context);
65    }
66
67    public NavigationBarTablet(Context context, AttributeSet attrs, int defStyle) {
68        super(context, attrs, defStyle);
69        init(context);
70    }
71
72    private void init(Context context) {
73        Resources resources = context.getResources();
74        mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
75        mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
76        mStopDescription = resources.getString(R.string.accessibility_button_stop);
77        mRefreshDescription = resources.getString(R.string.accessibility_button_refresh);
78        mFocusDrawable = resources.getDrawable(
79                R.drawable.textfield_active_holo_dark);
80        mUnfocusDrawable = resources.getDrawable(
81                R.drawable.textfield_default_holo_dark);
82        mHideNavButtons = resources.getBoolean(R.bool.hide_nav_buttons);
83    }
84
85    @Override
86    protected void onFinishInflate() {
87        super.onFinishInflate();
88        mAllButton = findViewById(R.id.all_btn);
89        // TODO: Change enabled states based on whether you can go
90        // back/forward.  Probably should be done inside onPageStarted.
91        mNavButtons = findViewById(R.id.navbuttons);
92        mBackButton = (ImageButton) findViewById(R.id.back);
93        mForwardButton = (ImageButton) findViewById(R.id.forward);
94        mUrlIcon = (ImageView) findViewById(R.id.url_icon);
95        mStar = (ImageView) findViewById(R.id.star);
96        mStopButton = (ImageView) findViewById(R.id.stop);
97        mSearchButton = (ImageView) findViewById(R.id.search);
98        mGoButton = findViewById(R.id.go);
99        mClearButton = findViewById(R.id.clear);
100        mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
101        mUrlContainer = findViewById(R.id.urlbar_focused);
102        mBackButton.setOnClickListener(this);
103        mForwardButton.setOnClickListener(this);
104        mStar.setOnClickListener(this);
105        mAllButton.setOnClickListener(this);
106        mStopButton.setOnClickListener(this);
107        mSearchButton.setOnClickListener(this);
108        mGoButton.setOnClickListener(this);
109        mClearButton.setOnClickListener(this);
110        mVoiceSearch.setOnClickListener(this);
111        setUaSwitcher(mUrlIcon);
112        mUrlInput.setContainer(mUrlContainer);
113    }
114
115    public void onConfigurationChanged(Configuration config) {
116        super.onConfigurationChanged(config);
117        Resources res = mContext.getResources();
118        mHideNavButtons = res.getBoolean(R.bool.hide_nav_buttons);
119        if (mUrlInput.hasFocus()) {
120            if (mHideNavButtons && (mNavButtons.getVisibility() == View.VISIBLE)) {
121                int aw = mNavButtons.getMeasuredWidth();
122                mNavButtons.setVisibility(View.GONE);
123                mNavButtons.setAlpha(0f);
124                mNavButtons.setTranslationX(-aw);
125            } else if (!mHideNavButtons && (mNavButtons.getVisibility() == View.GONE)) {
126                mNavButtons.setVisibility(View.VISIBLE);
127                mNavButtons.setAlpha(1f);
128                mNavButtons.setTranslationX(0);
129            }
130        }
131    }
132
133    @Override
134    public void setTitleBar(TitleBar titleBar) {
135        super.setTitleBar(titleBar);
136        setFocusState(false);
137    }
138
139    void updateNavigationState(Tab tab) {
140        if (tab != null) {
141            mBackButton.setImageResource(tab.canGoBack()
142                    ? R.drawable.ic_back_holo_dark
143                    : R.drawable.ic_back_disabled_holo_dark);
144            mForwardButton.setImageResource(tab.canGoForward()
145                    ? R.drawable.ic_forward_holo_dark
146                    : R.drawable.ic_forward_disabled_holo_dark);
147        }
148        updateUrlIcon();
149    }
150
151    @Override
152    public void setCurrentUrlIsBookmark(boolean isBookmark) {
153        mStar.setActivated(isBookmark);
154    }
155
156    @Override
157    public void onClick(View v) {
158        if (mBackButton == v) {
159            mUiController.getCurrentTab().goBack();
160        } else if (mForwardButton == v) {
161            mUiController.getCurrentTab().goForward();
162        } else if (mStar == v) {
163            getContext().startActivity(mUiController.createBookmarkCurrentPageIntent(true));
164        } else if (mAllButton == v) {
165            mUiController.bookmarksOrHistoryPicker(false);
166        } else if (mSearchButton == v) {
167            mBaseUi.editUrl(true);
168        } else if (mStopButton == v) {
169            stopOrRefresh();
170        } else if (mGoButton == v) {
171            if (!TextUtils.isEmpty(mUrlInput.getText())) {
172                onAction(mUrlInput.getText().toString(), null,
173                        UrlInputView.TYPED);
174            }
175        } else if (mClearButton == v) {
176            clearOrClose();
177        } else if (mVoiceSearch == v) {
178            mUiController.startVoiceSearch();
179        } else {
180            super.onClick(v);
181        }
182    }
183
184    private void clearOrClose() {
185        if (TextUtils.isEmpty(mUrlInput.getUserText())) {
186            // close
187            mUrlInput.clearFocus();
188        } else {
189            // clear
190            mUrlInput.setText("");
191        }
192    }
193
194    void updateUrlIcon() {
195        mUrlIcon.setImageResource(mInVoiceMode ?
196                R.drawable.ic_search_holo_dark
197                : R.drawable.ic_web_holo_dark);
198    }
199
200    @Override
201    protected void setFocusState(boolean focus) {
202        super.setFocusState(focus);
203        if (focus) {
204            if (mHideNavButtons) {
205                hideNavButtons();
206            }
207            mSearchButton.setVisibility(View.GONE);
208            mStar.setVisibility(View.GONE);
209            mClearButton.setVisibility(View.VISIBLE);
210            mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
211            updateSearchMode(false);
212        } else {
213            if (mHideNavButtons) {
214                showNavButtons();
215            }
216            mGoButton.setVisibility(View.GONE);
217            mVoiceSearch.setVisibility(View.GONE);
218            mStar.setVisibility(View.VISIBLE);
219            mClearButton.setVisibility(View.GONE);
220            if (mTitleBar.useQuickControls()) {
221                mSearchButton.setVisibility(View.GONE);
222            } else {
223                mSearchButton.setVisibility(View.VISIBLE);
224            }
225            updateUrlIcon();
226        }
227        mUrlContainer.setBackgroundDrawable(focus
228                ? mFocusDrawable : mUnfocusDrawable);
229    }
230
231    private void stopOrRefresh() {
232        if (mTitleBar.isInLoad()) {
233            mUiController.stopLoading();
234        } else {
235            mUiController.getCurrentTopWebView().reload();
236        }
237    }
238
239    @Override
240    public void onProgressStarted() {
241        mStopButton.setImageDrawable(mStopDrawable);
242        mStopButton.setContentDescription(mStopDescription);
243    }
244
245    @Override
246    public void onProgressStopped() {
247        mStopButton.setImageDrawable(mReloadDrawable);
248        mStopButton.setContentDescription(mRefreshDescription);
249    }
250
251    protected void updateSearchMode(boolean userEdited) {
252        setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
253    }
254
255    @Override
256    protected void setSearchMode(boolean voiceSearchEnabled) {
257        boolean showvoicebutton = voiceSearchEnabled &&
258                mUiController.supportsVoiceSearch();
259        mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
260                View.GONE);
261        mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
262                View.VISIBLE);
263    }
264
265    @Override
266    public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
267        super.setInVoiceMode(voicemode, voiceResults);
268        if (voicemode) {
269            mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
270        }
271    }
272
273    private void hideNavButtons() {
274        int awidth = mNavButtons.getMeasuredWidth();
275        Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, 0, - awidth);
276        Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", mUrlContainer.getLeft(),
277                mUrlContainer.getPaddingLeft());
278        Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 1f, 0f);
279        AnimatorSet combo = new AnimatorSet();
280        combo.playTogether(anim1, anim2, anim3);
281        combo.addListener(new AnimatorListenerAdapter() {
282            @Override
283            public void onAnimationEnd(Animator animation) {
284                mNavButtons.setVisibility(View.GONE);
285            }
286        });
287        combo.setDuration(150);
288        combo.start();
289    }
290
291    private void showNavButtons() {
292        int awidth = mNavButtons.getMeasuredWidth();
293        Animator anim1 = ObjectAnimator.ofFloat(mNavButtons, View.TRANSLATION_X, -awidth, 0);
294        Animator anim2 = ObjectAnimator.ofInt(mUrlContainer, "left", 0, awidth);
295        Animator anim3 = ObjectAnimator.ofFloat(mNavButtons, View.ALPHA, 0f, 1f);
296        AnimatorSet combo = new AnimatorSet();
297        combo.playTogether(anim1, anim2, anim3);
298        mNavButtons.setVisibility(View.VISIBLE);
299        combo.setDuration(150);
300        combo.start();
301    }
302
303}
304