/WebVox/src/com/marvin/webvox/ActiveTabsPage.java
Java | 163 lines | 125 code | 10 blank | 28 comment | 21 complexity | 152296001a51f876dbca7e33100218ae MD5 | raw file
1/* 2 * Copyright (C) 2009 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 17package com.marvin.webvox; 18 19import com.marvin.webvox.R; 20 21import android.content.Context; 22import android.graphics.Bitmap; 23import android.util.AttributeSet; 24import android.view.KeyEvent; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28import android.widget.AdapterView; 29import android.widget.BaseAdapter; 30import android.widget.ImageView; 31import android.widget.LinearLayout; 32import android.widget.ListView; 33import android.widget.TextView; 34 35public class ActiveTabsPage extends LinearLayout { 36 private final BrowserActivity mBrowserActivity; 37 private final LayoutInflater mFactory; 38 private final TabControl mControl; 39 private final TabsListAdapter mAdapter; 40 private final ListView mListView; 41 42 public ActiveTabsPage(BrowserActivity context, TabControl control) { 43 super(context); 44 mBrowserActivity = context; 45 mControl = control; 46 mFactory = LayoutInflater.from(context); 47 mFactory.inflate(R.layout.active_tabs, this); 48 mListView = (ListView) findViewById(R.id.list); 49 mAdapter = new TabsListAdapter(); 50 mListView.setAdapter(mAdapter); 51 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 52 public void onItemClick(AdapterView<?> parent, View view, 53 int position, long id) { 54 if (mControl.getTabCount() < TabControl.MAX_TABS) { 55 position--; 56 } 57 boolean needToAttach = false; 58 if (position == -1) { 59 // Create a new tab 60 mBrowserActivity.openTabToHomePage(); 61 } else { 62 // Open the corresponding tab 63 // If the tab is the current one, switchToTab will 64 // do nothing and return, so we need to make sure 65 // it gets attached back to its mContentView in 66 // removeActiveTabPage 67 needToAttach = !mBrowserActivity.switchToTab(position); 68 } 69 mBrowserActivity.removeActiveTabPage(needToAttach); 70 } 71 }); 72 } 73 74 /** 75 * Special class to hold the close drawable. Its sole purpose is to allow 76 * the parent to be pressed without being pressed itself. This way the line 77 * of a tab can be pressed, but the close button itself is not. 78 */ 79 private static class CloseHolder extends ImageView { 80 public CloseHolder(Context context, AttributeSet attrs) { 81 super(context, attrs); 82 } 83 84 @Override 85 public void setPressed(boolean pressed) { 86 // If the parent is pressed, do not set to pressed. 87 if (pressed && ((View) getParent()).isPressed()) { 88 return; 89 } 90 super.setPressed(pressed); 91 } 92 } 93 94 private class TabsListAdapter extends BaseAdapter { 95 public int getCount() { 96 int count = mControl.getTabCount(); 97 if (count < TabControl.MAX_TABS) { 98 count++; 99 } 100 return count; 101 } 102 public Object getItem(int position) { 103 return null; 104 } 105 public long getItemId(int position) { 106 return position; 107 } 108 public int getViewTypeCount() { 109 return 2; 110 } 111 public int getItemViewType(int position) { 112 if (mControl.getTabCount() < TabControl.MAX_TABS) { 113 position--; 114 } 115 // Do not recycle the "add new tab" item. 116 return position == -1 ? IGNORE_ITEM_VIEW_TYPE : 1; 117 } 118 public View getView(int position, View convertView, ViewGroup parent) { 119 final int tabCount = mControl.getTabCount(); 120 if (tabCount < TabControl.MAX_TABS) { 121 position--; 122 } 123 124 if (convertView == null) { 125 convertView = mFactory.inflate(position == -1 ? 126 R.layout.tab_view_add_tab : R.layout.tab_view, null); 127 } 128 129 if (position != -1) { 130 TextView title = 131 (TextView) convertView.findViewById(R.id.title); 132 TextView url = (TextView) convertView.findViewById(R.id.url); 133 ImageView favicon = 134 (ImageView) convertView.findViewById(R.id.favicon); 135 View close = convertView.findViewById(R.id.close); 136 TabControl.Tab tab = mControl.getTab(position); 137 mControl.populatePickerData(tab); 138 title.setText(tab.getTitle()); 139 url.setText(tab.getUrl()); 140 Bitmap icon = tab.getFavicon(); 141 if (icon != null) { 142 favicon.setImageBitmap(icon); 143 } else { 144 favicon.setImageResource(R.drawable.app_web_browser_sm); 145 } 146 final int closePosition = position; 147 close.setOnClickListener(new View.OnClickListener() { 148 public void onClick(View v) { 149 mBrowserActivity.closeTab( 150 mControl.getTab(closePosition)); 151 if (tabCount == 1) { 152 mBrowserActivity.openTabToHomePage(); 153 mBrowserActivity.removeActiveTabPage(false); 154 } else { 155 mListView.setAdapter(mAdapter); 156 } 157 } 158 }); 159 } 160 return convertView; 161 } 162 } 163}