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 | |
| 19 | import android.view.KeyEvent; |
| 20 | import android.view.LayoutInflater; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | import android.widget.AdapterView; |
| 24 | import android.widget.BaseAdapter; |
| 25 | import android.widget.LinearLayout; |
| 26 | import android.widget.ListView; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | public class ActiveTabsPage extends LinearLayout { |
| 30 | private final BrowserActivity mBrowserActivity; |
| 31 | private final LayoutInflater mFactory; |
| 32 | private final TabControl mControl; |
| 33 | private final TabsListAdapter mAdapter; |
| 34 | private final ListView mListView; |
| 35 | |
| 36 | public ActiveTabsPage(BrowserActivity context, TabControl control) { |
| 37 | super(context); |
| 38 | mBrowserActivity = context; |
| 39 | mControl = control; |
| 40 | mFactory = LayoutInflater.from(context); |
| 41 | mFactory.inflate(R.layout.active_tabs, this); |
| 42 | mListView = (ListView) findViewById(R.id.list); |
| 43 | mAdapter = new TabsListAdapter(); |
| 44 | mListView.setAdapter(mAdapter); |
| 45 | mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 46 | public void onItemClick(AdapterView<?> parent, View view, |
| 47 | int position, long id) { |
| 48 | if (mControl.getTabCount() < TabControl.MAX_TABS) { |
| 49 | position--; |
| 50 | } |
| 51 | boolean needToAttach = false; |
| 52 | if (position == -1) { |
| 53 | // Create a new tab |
| 54 | mBrowserActivity.openTabToHomePage(); |
| 55 | } else { |
| 56 | // Open the corresponding tab |
| 57 | // If the tab is the current one, switchToTab will |
| 58 | // do nothing and return, so we need to make sure |
| 59 | // it gets attached back to its mContentView in |
| 60 | // removeActiveTabPage |
| 61 | needToAttach = !mBrowserActivity.switchToTab(position); |
| 62 | } |
| 63 | mBrowserActivity.removeActiveTabPage(needToAttach); |
| 64 | } |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 69 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 70 | if (event.isDown()) return true; |
| 71 | mBrowserActivity.removeActiveTabPage(true); |
| 72 | return true; |
| 73 | } |
| 74 | return super.dispatchKeyEvent(event); |
| 75 | } |
| 76 | |
| 77 | private class TabsListAdapter extends BaseAdapter { |
| 78 | public int getCount() { |
| 79 | int count = mControl.getTabCount(); |
| 80 | if (count < TabControl.MAX_TABS) { |
| 81 | count++; |
| 82 | } |
| 83 | return count; |
| 84 | } |
| 85 | public Object getItem(int position) { |
| 86 | return null; |
| 87 | } |
| 88 | public long getItemId(int position) { |
| 89 | return position; |
| 90 | } |
| 91 | public View getView(int position, View convertView, ViewGroup parent) { |
| 92 | if (convertView == null) { |
| 93 | convertView = mFactory.inflate(R.layout.tab_view, null); |
| 94 | } |
| 95 | TextView title = (TextView) convertView.findViewById(R.id.title); |
| 96 | TextView url = (TextView) convertView.findViewById(R.id.url); |
| 97 | FakeWebView webView |
| 98 | = (FakeWebView) convertView.findViewById(R.id.screen_shot); |
| 99 | View close = convertView.findViewById(R.id.close); |
| 100 | |
| 101 | final int tabCount = mControl.getTabCount(); |
| 102 | if (tabCount < TabControl.MAX_TABS) { |
| 103 | position--; |
| 104 | } |
| 105 | if (position == -1) { |
| 106 | title.setText(R.string.new_tab); |
| 107 | url.setText(R.string.http); |
| 108 | webView.setImageResource(R.drawable.ic_add_tab); |
| 109 | close.setVisibility(View.GONE); |
| 110 | } else { |
| 111 | TabControl.Tab tab = mControl.getTab(position); |
| 112 | mControl.populatePickerData(tab); |
| 113 | title.setText(tab.getTitle()); |
| 114 | url.setText(tab.getUrl()); |
| 115 | webView.setTab(tab); |
| 116 | close.setVisibility(View.VISIBLE); |
| 117 | final int closePosition = position; |
| 118 | close.setOnClickListener(new View.OnClickListener() { |
| 119 | public void onClick(View v) { |
| 120 | mBrowserActivity.closeTab( |
| 121 | mControl.getTab(closePosition)); |
| 122 | if (tabCount == 1) { |
| 123 | mBrowserActivity.openTabToHomePage(); |
| 124 | mBrowserActivity.removeActiveTabPage(false); |
| 125 | } else { |
| 126 | mListView.setAdapter(mAdapter); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | return convertView; |
| 132 | } |
| 133 | } |
| 134 | } |