Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.graphics.drawable.Animatable; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.DisplayMetrics; |
| 24 | import android.util.TypedValue; |
| 25 | import android.view.ContextMenu; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.MenuInflater; |
| 28 | import android.view.View; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.ProgressBar; |
| 31 | import android.widget.TextView; |
| 32 | |
| 33 | import com.android.common.speech.LoggingEvents; |
| 34 | |
| 35 | /** |
| 36 | * This class represents a title bar for a particular "tab" or "window" in the |
| 37 | * browser. |
| 38 | */ |
| 39 | public class TitleBarXLarge extends TitleBarBase { |
| 40 | private Drawable mCircularProgress; |
| 41 | private ProgressBar mHorizontalProgress; |
| 42 | private Drawable mStopDrawable; |
| 43 | private Drawable mReloadDrawable; |
| 44 | private boolean mInLoad; |
| 45 | private BrowserActivity mBrowserActivity; |
| 46 | |
| 47 | private final View mBackButton; |
| 48 | private final View mForwardButton; |
| 49 | private final View mStar; |
| 50 | private final View mMenu; |
| 51 | private final ImageView mStopButton; |
| 52 | private final TextView mTitle; |
| 53 | private final View mAllButton; |
| 54 | |
| 55 | public TitleBarXLarge(BrowserActivity context) { |
| 56 | super(context); |
| 57 | Resources resources = context.getResources(); |
| 58 | LayoutInflater factory = LayoutInflater.from(context); |
| 59 | factory.inflate(R.layout.title_bar_xlarge, this); |
| 60 | mBrowserActivity = context; |
| 61 | |
| 62 | mTitle = (TextView) findViewById(R.id.title); |
| 63 | mTitle.setCompoundDrawablePadding(5); |
| 64 | mTitle.setLongClickable(true); |
| 65 | |
| 66 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 67 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 68 | mStopButton = (ImageView) findViewById(R.id.stop); |
| 69 | mStopDrawable = mStopButton.getDrawable(); |
| 70 | mReloadDrawable = resources.getDrawable(R.drawable.ic_reload); |
| 71 | |
| 72 | mAllButton = (ImageView) findViewById(R.id.all_btn); |
| 73 | mCircularProgress = (Drawable) resources.getDrawable( |
| 74 | com.android.internal.R.drawable.search_spinner); |
| 75 | DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 76 | int iconDimension = (int) TypedValue.applyDimension( |
| 77 | TypedValue.COMPLEX_UNIT_DIP, 20f, metrics); |
| 78 | mCircularProgress.setBounds(0, 0, iconDimension, iconDimension); |
| 79 | mHorizontalProgress = (ProgressBar) findViewById( |
| 80 | R.id.progress_horizontal); |
Leon Scroggins | 897ba06 | 2010-06-16 14:35:33 -0400 | [diff] [blame^] | 81 | mHorizontalProgress.setProgressDrawable( |
| 82 | resources.getDrawable(R.drawable.progress)); |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 83 | |
| 84 | // FIXME: Change enabled states based on whether you can go |
| 85 | // back/forward. Probably should be done inside onPageStarted. |
| 86 | mBackButton = findViewById(R.id.back); |
| 87 | mForwardButton = findViewById(R.id.forward); |
| 88 | mStar = findViewById(R.id.star); |
| 89 | mMenu = findViewById(R.id.menu); |
| 90 | View.OnClickListener listener = new View.OnClickListener() { |
| 91 | public void onClick(View v) { |
| 92 | if (mBackButton == v) { |
| 93 | mBrowserActivity.getTopWindow().goBack(); |
| 94 | } else if (mForwardButton == v) { |
| 95 | mBrowserActivity.getTopWindow().goForward(); |
| 96 | } else if (mStar == v) { |
| 97 | // FIXME: Show a menu with option to bookmark or |
| 98 | // save to home page |
| 99 | mBrowserActivity.bookmarkCurrentPage(); |
| 100 | } else if (mMenu == v) { |
| 101 | mBrowserActivity.openOptionsMenu(); |
| 102 | } else if (mStopButton == v) { |
| 103 | if (mInLoad) { |
| 104 | mBrowserActivity.stopLoading(); |
| 105 | } else { |
| 106 | mBrowserActivity.getTopWindow().reload(); |
| 107 | } |
| 108 | } else if (mTitle == v) { |
| 109 | mBrowserActivity.editUrl(); |
| 110 | } else if (mAllButton == v) { |
| 111 | // FIXME: Show the new bookmarks/windows view. |
| 112 | mBrowserActivity.bookmarksOrHistoryPicker(false); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | mBackButton.setOnClickListener(listener); |
| 117 | mForwardButton.setOnClickListener(listener); |
| 118 | mStar.setOnClickListener(listener); |
| 119 | mStopButton.setOnClickListener(listener); |
| 120 | mTitle.setOnClickListener(listener); |
| 121 | mAllButton.setOnClickListener(listener); |
| 122 | mMenu.setOnClickListener(listener); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void createContextMenu(ContextMenu menu) { |
| 127 | MenuInflater inflater = mBrowserActivity.getMenuInflater(); |
| 128 | inflater.inflate(R.menu.title_context, menu); |
| 129 | mBrowserActivity.onCreateContextMenu(menu, this, null); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Update the progress, from 0 to 100. |
| 134 | */ |
| 135 | /* package */ void setProgress(int newProgress) { |
| 136 | if (newProgress >= mHorizontalProgress.getMax()) { |
| 137 | mTitle.setCompoundDrawables(null, null, null, null); |
| 138 | ((Animatable) mCircularProgress).stop(); |
| 139 | mHorizontalProgress.setVisibility(View.GONE); |
| 140 | mInLoad = false; |
| 141 | mStopButton.setImageDrawable(mReloadDrawable); |
| 142 | } else { |
| 143 | mHorizontalProgress.setProgress(newProgress); |
| 144 | if (!mInLoad && getWindowToken() != null) { |
| 145 | // checking the window token lets us be sure that we |
| 146 | // are attached to a window before starting the animation, |
| 147 | // preventing a potential race condition |
| 148 | // (fix for bug http://b/2115736) |
| 149 | mTitle.setCompoundDrawables(null, null, mCircularProgress, |
| 150 | null); |
| 151 | ((Animatable) mCircularProgress).start(); |
| 152 | mHorizontalProgress.setVisibility(View.VISIBLE); |
| 153 | mInLoad = true; |
| 154 | mStopButton.setImageDrawable(mStopDrawable); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Update the text displayed in the title bar. |
| 161 | * @param title String to display. If null, the loading string will be |
| 162 | * shown. |
| 163 | */ |
| 164 | /* package */ void setDisplayTitle(String title) { |
| 165 | if (title == null) { |
| 166 | mTitle.setText(R.string.title_bar_loading); |
| 167 | } else { |
| 168 | mTitle.setText(title); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |