/WebVox/src/com/marvin/webvox/ActiveTabsPage.java

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