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 */ 16 17 package com.android.browser; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.res.Configuration; 22 import android.view.LayoutInflater; 23 import android.view.Menu; 24 import android.view.MenuItem; 25 import android.view.View; 26 import android.view.View.OnClickListener; 27 import android.view.ViewConfiguration; 28 import android.view.ViewGroup; 29 import android.widget.BaseAdapter; 30 import android.widget.FrameLayout; 31 import android.widget.ImageButton; 32 import android.widget.ImageView; 33 import android.widget.LinearLayout; 34 import android.widget.PopupMenu; 35 import android.widget.PopupMenu.OnMenuItemClickListener; 36 import android.widget.RelativeLayout; 37 import android.widget.TextView; 38 39 import com.android.browser.NavTabScroller.OnLayoutListener; 40 import com.android.browser.NavTabScroller.OnRemoveListener; 41 import com.android.browser.TabControl.OnThumbnailUpdatedListener; 42 import com.android.browser.UI.ComboViews; 43 44 import java.util.HashMap; 45 46 public class NavScreen extends RelativeLayout 47 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener { 48 49 50 UiController mUiController; 51 PhoneUi mUi; 52 Tab mTab; 53 Activity mActivity; 54 55 ImageButton mRefresh; 56 ImageButton mForward; 57 ImageButton mBookmarks; 58 ImageButton mMore; 59 ImageButton mNewTab; 60 FrameLayout mHolder; 61 62 TextView mTitle; 63 ImageView mFavicon; 64 ImageButton mCloseTab; 65 66 NavTabScroller mScroller; 67 TabAdapter mAdapter; 68 int mOrientation; 69 boolean mNeedsMenu; 70 HashMap<Tab, View> mTabViews; 71 72 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { 73 super(activity); 74 mActivity = activity; 75 mUiController = ctl; 76 mUi = ui; 77 mOrientation = activity.getResources().getConfiguration().orientation; 78 init(); 79 } 80 81 protected void showMenu() { 82 PopupMenu popup = new PopupMenu(mContext, mMore); 83 Menu menu = popup.getMenu(); 84 popup.getMenuInflater().inflate(R.menu.browser, menu); 85 mUiController.updateMenuState(mUiController.getCurrentTab(), menu); 86 popup.setOnMenuItemClickListener(this); 87 popup.show(); 88 } 89 90 @Override 91 public boolean onMenuItemClick(MenuItem item) { 92 return mUiController.onOptionsItemSelected(item); 93 } 94 95 protected float getToolbarHeight() { 96 return mActivity.getResources().getDimension(R.dimen.toolbar_height); 97 } 98 99 @Override 100 protected void onConfigurationChanged(Configuration newconfig) { 101 if (newconfig.orientation != mOrientation) { 102 int sv = mScroller.getScrollValue(); 103 removeAllViews(); 104 mOrientation = newconfig.orientation; 105 init(); 106 mScroller.setScrollValue(sv); 107 mAdapter.notifyDataSetChanged(); 108 } 109 } 110 111 public void refreshAdapter() { 112 mScroller.handleDataChanged( 113 mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); 114 } 115 116 private void init() { 117 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this); 118 setContentDescription(mContext.getResources().getString( 119 R.string.accessibility_transition_navscreen)); 120 mBookmarks = (ImageButton) findViewById(R.id.bookmarks); 121 mNewTab = (ImageButton) findViewById(R.id.newtab); 122 mMore = (ImageButton) findViewById(R.id.more); 123 mBookmarks.setOnClickListener(this); 124 mNewTab.setOnClickListener(this); 125 mMore.setOnClickListener(this); 126 mScroller = (NavTabScroller) findViewById(R.id.scroller); 127 TabControl tc = mUiController.getTabControl(); 128 mTabViews = new HashMap<Tab, View>(tc.getTabCount()); 129 mAdapter = new TabAdapter(mContext, tc); 130 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE 131 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); 132 // update state for active tab 133 mScroller.setAdapter(mAdapter, 134 mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); 135 mScroller.setOnRemoveListener(new OnRemoveListener() { 136 public void onRemovePosition(int pos) { 137 Tab tab = mAdapter.getItem(pos); 138 onCloseTab(tab); 139 } 140 }); 141 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); 142 if (!mNeedsMenu) { 143 mMore.setVisibility(View.GONE); 144 } 145 } 146 147 @Override 148 public void onClick(View v) { 149 if (mBookmarks == v) { 150 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks); 151 } else if (mNewTab == v) { 152 openNewTab(); 153 } else if (mMore == v) { 154 showMenu(); 155 } 156 } 157 158 private void onCloseTab(Tab tab) { 159 if (tab != null) { 160 if (tab == mUiController.getCurrentTab()) { 161 mUiController.closeCurrentTab(); 162 } else { 163 mUiController.closeTab(tab); 164 } 165 } 166 } 167 168 private void openNewTab() { 169 // need to call openTab explicitely with setactive false 170 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), 171 false, false, false); 172 if (tab != null) { 173 mUiController.setBlockEvents(true); 174 final int tix = mUi.mTabControl.getTabPosition(tab); 175 mScroller.setOnLayoutListener(new OnLayoutListener() { 176 177 @Override 178 public void onLayout(int l, int t, int r, int b) { 179 mUi.hideNavScreen(tix, true); 180 switchToTab(tab); 181 } 182 }); 183 mScroller.handleDataChanged(tix); 184 mUiController.setBlockEvents(false); 185 } 186 } 187 188 private void switchToTab(Tab tab) { 189 if (tab != mUi.getActiveTab()) { 190 mUiController.setActiveTab(tab); 191 } 192 } 193 194 protected void close(int position) { 195 close(position, true); 196 } 197 198 protected void close(int position, boolean animate) { 199 mUi.hideNavScreen(position, animate); 200 } 201 202 protected NavTabView getTabView(int pos) { 203 return mScroller.getTabView(pos); 204 } 205 206 class TabAdapter extends BaseAdapter { 207 208 Context context; 209 TabControl tabControl; 210 211 public TabAdapter(Context ctx, TabControl tc) { 212 context = ctx; 213 tabControl = tc; 214 } 215 216 @Override 217 public int getCount() { 218 return tabControl.getTabCount(); 219 } 220 221 @Override 222 public Tab getItem(int position) { 223 return tabControl.getTab(position); 224 } 225 226 public long getItemId(int position) { 227 return position; 228 } 229 230 @Override 231 public View getView(final int position, View convertView, ViewGroup parent) { 232 final NavTabView tabview = new NavTabView(mActivity); 233 final Tab tab = getItem(position); 234 tabview.setWebView(tab); 235 mTabViews.put(tab, tabview.mImage); 236 tabview.setOnClickListener(new OnClickListener() { 237 @Override 238 public void onClick(View v) { 239 if (tabview.isClose(v)) { 240 mScroller.animateOut(tabview); 241 } else if (tabview.isTitle(v)) { 242 switchToTab(tab); 243 mUi.getTitleBar().setSkipTitleBarAnimations(true); 244 close(position, false); 245 mUi.editUrl(false, true); 246 mUi.getTitleBar().setSkipTitleBarAnimations(false); 247 } else if (tabview.isWebView(v)) { 248 close(position); 249 } 250 } 251 }); 252 return tabview; 253 } 254 255 } 256 257 @Override 258 public void onThumbnailUpdated(Tab t) { 259 View v = mTabViews.get(t); 260 if (v != null) { 261 v.invalidate(); 262 } 263 } 264 265 } 266