2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Sriram Ramasubramanian <sriram@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Bundle;
|
2011-12-15 11:53:04 -08:00
|
|
|
import android.os.Build;
|
2012-01-27 12:18:02 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.DisplayMetrics;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
public class TabsTray extends Activity implements GeckoApp.OnTabsChangedListener {
|
|
|
|
|
2012-01-27 12:18:02 -08:00
|
|
|
private static int sPreferredHeight;
|
|
|
|
private static int sMaxHeight;
|
|
|
|
private static int sListItemHeight;
|
2012-02-07 13:56:34 -08:00
|
|
|
private static int sAddTabHeight;
|
2012-01-27 12:18:02 -08:00
|
|
|
private static ListView mList;
|
2012-02-07 13:56:34 -08:00
|
|
|
private static TabsListContainer mListContainer;
|
2011-11-18 10:28:17 -08:00
|
|
|
private TabsAdapter mTabsAdapter;
|
2011-11-21 12:21:10 -08:00
|
|
|
private boolean mWaitingForClose;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-02-07 13:56:34 -08:00
|
|
|
// 100 for item + 2 for divider
|
|
|
|
private static final int TABS_LIST_ITEM_HEIGHT = 102;
|
|
|
|
private static final int TABS_ADD_TAB_HEIGHT = 50;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.tabs_tray);
|
|
|
|
|
2011-12-15 11:53:04 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
GeckoActionBar.hide(this);
|
|
|
|
}
|
|
|
|
|
2011-11-21 12:21:10 -08:00
|
|
|
mWaitingForClose = false;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
mList = (ListView) findViewById(R.id.list);
|
2012-02-07 13:56:34 -08:00
|
|
|
mListContainer = (TabsListContainer) findViewById(R.id.list_container);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
LinearLayout addTab = (LinearLayout) findViewById(R.id.add_tab);
|
|
|
|
addTab.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
GeckoApp.mAppContext.addTab();
|
|
|
|
finishActivity();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
LinearLayout container = (LinearLayout) findViewById(R.id.container);
|
|
|
|
container.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
finishActivity();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-27 12:18:02 -08:00
|
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
|
|
|
getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
2012-02-07 13:56:34 -08:00
|
|
|
|
|
|
|
sListItemHeight = (int) (TABS_LIST_ITEM_HEIGHT * metrics.density);
|
|
|
|
sAddTabHeight = (int) (TABS_ADD_TAB_HEIGHT * metrics.density);
|
2012-01-27 12:18:02 -08:00
|
|
|
sPreferredHeight = (int) (0.67 * metrics.heightPixels);
|
|
|
|
sMaxHeight = (int) (sPreferredHeight + (0.33 * sListItemHeight));
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
GeckoApp.registerOnTabsChangedListener(this);
|
2012-01-25 15:36:03 -08:00
|
|
|
Tabs.getInstance().refreshThumbnails();
|
2011-11-18 10:28:17 -08:00
|
|
|
onTabsChanged(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
GeckoApp.unregisterOnTabsChangedListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
|
|
// This function is called after the initial list is populated
|
|
|
|
// Scrolling to the selected tab can happen here
|
|
|
|
if (hasFocus) {
|
|
|
|
int position = mTabsAdapter.getPositionForTab(Tabs.getInstance().getSelectedTab());
|
2012-02-01 16:05:06 -08:00
|
|
|
if (position == -1)
|
|
|
|
return;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
|
2011-11-18 10:28:17 -08:00
|
|
|
mList.smoothScrollToPosition(position);
|
2012-02-01 16:05:06 -08:00
|
|
|
} else {
|
|
|
|
/* To Do: Find a way to scroll with Eclair's APIs */
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTabsChanged(Tab tab) {
|
|
|
|
if (Tabs.getInstance().getCount() == 1)
|
|
|
|
finishActivity();
|
|
|
|
|
|
|
|
if (mTabsAdapter == null) {
|
|
|
|
mTabsAdapter = new TabsAdapter(this, Tabs.getInstance().getTabsInOrder());
|
|
|
|
mList.setAdapter(mTabsAdapter);
|
2012-02-07 13:56:34 -08:00
|
|
|
mListContainer.requestLayout();
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int position = mTabsAdapter.getPositionForTab(tab);
|
|
|
|
if (position == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tabs.getInstance().getIndexOf(tab) == -1) {
|
2011-11-21 12:21:10 -08:00
|
|
|
mWaitingForClose = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
mTabsAdapter = new TabsAdapter(this, Tabs.getInstance().getTabsInOrder());
|
|
|
|
mList.setAdapter(mTabsAdapter);
|
2012-02-07 13:56:34 -08:00
|
|
|
mListContainer.requestLayout();
|
2011-11-18 10:28:17 -08:00
|
|
|
} else {
|
|
|
|
View view = mList.getChildAt(position - mList.getFirstVisiblePosition());
|
|
|
|
mTabsAdapter.assignValues(view, tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void finishActivity() {
|
|
|
|
finish();
|
|
|
|
overridePendingTransition(0, R.anim.shrink_fade_out);
|
2012-01-25 23:46:16 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(new GeckoEvent("Tab:Screenshot:Cancel",""));
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-27 12:18:02 -08:00
|
|
|
// Tabs List Container holds the ListView and the New Tab button
|
|
|
|
public static class TabsListContainer extends LinearLayout {
|
|
|
|
public TabsListContainer(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-02-07 13:56:34 -08:00
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
if (mList.getAdapter() == null) {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-27 12:18:02 -08:00
|
|
|
|
2012-02-07 13:56:34 -08:00
|
|
|
int restrictedHeightSpec;
|
|
|
|
int childrenHeight = (mList.getAdapter().getCount() * sListItemHeight) + sAddTabHeight;
|
2012-01-27 12:18:02 -08:00
|
|
|
|
2012-02-07 13:56:34 -08:00
|
|
|
if (childrenHeight <= sPreferredHeight) {
|
|
|
|
restrictedHeightSpec = MeasureSpec.makeMeasureSpec(childrenHeight, MeasureSpec.EXACTLY);
|
|
|
|
} else {
|
|
|
|
if ((childrenHeight - sAddTabHeight) % sListItemHeight == 0)
|
|
|
|
restrictedHeightSpec = MeasureSpec.makeMeasureSpec(sMaxHeight, MeasureSpec.EXACTLY);
|
|
|
|
else
|
|
|
|
restrictedHeightSpec = MeasureSpec.makeMeasureSpec(sPreferredHeight, MeasureSpec.EXACTLY);
|
2012-01-27 12:18:02 -08:00
|
|
|
}
|
2012-02-07 13:56:34 -08:00
|
|
|
|
|
|
|
super.onMeasure(widthMeasureSpec, restrictedHeightSpec);
|
2012-01-27 12:18:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
// Adapter to bind tabs into a list
|
|
|
|
private class TabsAdapter extends BaseAdapter {
|
2011-11-22 12:32:45 -08:00
|
|
|
public TabsAdapter(Context context, ArrayList<Tab> tabs) {
|
2011-11-18 10:28:17 -08:00
|
|
|
mContext = context;
|
2011-11-21 12:21:10 -08:00
|
|
|
mInflater = LayoutInflater.from(mContext);
|
2011-11-18 10:28:17 -08:00
|
|
|
mTabs = new ArrayList<Tab>();
|
2011-11-21 12:21:10 -08:00
|
|
|
|
|
|
|
if (tabs == null)
|
|
|
|
return;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
mTabs.add(tabs.get(i));
|
|
|
|
}
|
2011-11-22 12:32:45 -08:00
|
|
|
|
|
|
|
mOnInfoClickListener = new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
2012-01-24 09:15:52 -08:00
|
|
|
Tabs.getInstance().selectTab(Integer.parseInt((String) v.getTag()));
|
2011-11-22 12:32:45 -08:00
|
|
|
finishActivity();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mOnCloseClickListener = new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (mWaitingForClose)
|
|
|
|
return;
|
2011-12-19 10:44:52 -08:00
|
|
|
|
2011-11-22 12:32:45 -08:00
|
|
|
mWaitingForClose = true;
|
2011-12-19 10:44:52 -08:00
|
|
|
|
2011-12-06 14:51:08 -08:00
|
|
|
String tabId = v.getTag().toString();
|
2011-11-22 12:32:45 -08:00
|
|
|
Tabs tabs = Tabs.getInstance();
|
2011-12-06 14:51:08 -08:00
|
|
|
Tab tab = tabs.getTab(Integer.parseInt(tabId));
|
2011-12-19 10:44:52 -08:00
|
|
|
tabs.closeTab(tab);
|
2011-11-22 12:32:45 -08:00
|
|
|
}
|
|
|
|
};
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount() {
|
|
|
|
return mTabs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tab getItem(int position) {
|
|
|
|
return mTabs.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPositionForTab(Tab tab) {
|
|
|
|
if (mTabs == null || tab == null)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return mTabs.indexOf(tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void assignValues(View view, Tab tab) {
|
|
|
|
if (view == null || tab == null)
|
|
|
|
return;
|
|
|
|
|
2011-12-14 14:31:39 -08:00
|
|
|
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-14 14:31:39 -08:00
|
|
|
Drawable thumbnailImage = tab.getThumbnail();
|
|
|
|
if (thumbnailImage != null)
|
|
|
|
thumbnail.setImageDrawable(thumbnailImage);
|
2011-11-18 10:28:17 -08:00
|
|
|
else
|
2011-12-14 14:31:39 -08:00
|
|
|
thumbnail.setImageResource(R.drawable.tab_thumbnail_default);
|
|
|
|
|
|
|
|
if (Tabs.getInstance().isSelectedTab(tab))
|
|
|
|
((ImageView) view.findViewById(R.id.selected_indicator)).setVisibility(View.VISIBLE);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
TextView title = (TextView) view.findViewById(R.id.title);
|
|
|
|
title.setText(tab.getDisplayTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
convertView = mInflater.inflate(R.layout.tabs_row, null);
|
2011-11-22 12:32:45 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
Tab tab = mTabs.get(position);
|
|
|
|
|
|
|
|
RelativeLayout info = (RelativeLayout) convertView.findViewById(R.id.info);
|
2011-11-22 12:32:45 -08:00
|
|
|
info.setTag(String.valueOf(tab.getId()));
|
|
|
|
info.setOnClickListener(mOnInfoClickListener);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
assignValues(convertView, tab);
|
|
|
|
|
|
|
|
ImageButton close = (ImageButton) convertView.findViewById(R.id.close);
|
|
|
|
if (mTabs.size() > 1) {
|
2011-11-22 12:32:45 -08:00
|
|
|
close.setTag(String.valueOf(tab.getId()));
|
|
|
|
close.setOnClickListener(mOnCloseClickListener);
|
2011-11-18 10:28:17 -08:00
|
|
|
} else {
|
|
|
|
close.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void notifyDataSetChanged() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void notifyDataSetInvalidated() {
|
|
|
|
}
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private ArrayList<Tab> mTabs;
|
|
|
|
private LayoutInflater mInflater;
|
2011-11-22 12:32:45 -08:00
|
|
|
private View.OnClickListener mOnInfoClickListener;
|
|
|
|
private Button.OnClickListener mOnCloseClickListener;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|