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