2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-03-13 08:15:43 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.accounts.AccountManager;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
2012-03-09 10:42:43 -08:00
|
|
|
import android.content.Intent;
|
2011-11-18 10:28:17 -08:00
|
|
|
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;
|
|
|
|
|
2012-03-13 08:15:43 -07:00
|
|
|
import org.mozilla.gecko.sync.setup.SyncAccounts;
|
|
|
|
|
2012-02-29 09:09:43 -08:00
|
|
|
public class TabsTray extends Activity implements Tabs.OnTabsChangedListener {
|
2011-11-18 10:28:17 -08:00
|
|
|
|
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;
|
2012-03-09 10:42:43 -08:00
|
|
|
private static LinkTextView mRemoteTabs;
|
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-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
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
ImageButton addTab = (ImageButton) findViewById(R.id.add_tab);
|
2011-11-18 10:28:17 -08:00
|
|
|
addTab.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
GeckoApp.mAppContext.addTab();
|
|
|
|
finishActivity();
|
|
|
|
}
|
|
|
|
});
|
2012-03-09 10:42:43 -08:00
|
|
|
|
|
|
|
mRemoteTabs = (LinkTextView) findViewById(R.id.remote_tabs);
|
|
|
|
mRemoteTabs.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
showRemoteTabs();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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);
|
2012-03-09 10:42:43 -08:00
|
|
|
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));
|
|
|
|
|
2012-02-29 09:09:43 -08:00
|
|
|
Tabs tabs = Tabs.getInstance();
|
|
|
|
tabs.registerOnTabsChangedListener(this);
|
|
|
|
tabs.refreshThumbnails();
|
|
|
|
onTabChanged(null, null);
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-03-13 08:15:43 -07:00
|
|
|
// If Sync is set up, query the database for remote clients.
|
|
|
|
final Context context = getApplicationContext();
|
|
|
|
new SyncAccounts.AccountsExistTask() {
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean result) {
|
|
|
|
if (!result.booleanValue()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TabsAccessor.areClientsAvailable(context, new TabsAccessor.OnClientsAvailableListener() {
|
|
|
|
@Override
|
|
|
|
public void areAvailable(boolean available) {
|
|
|
|
final int visibility = available ? View.VISIBLE : View.GONE;
|
|
|
|
mRemoteTabs.setVisibility(visibility);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.execute(context);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2012-02-29 09:09:43 -08:00
|
|
|
Tabs.getInstance().unregisterOnTabsChangedListener(this);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-02-29 09:09:43 -08:00
|
|
|
public void onTabChanged(Tab tab, Tabs.TabEvents msg) {
|
2011-11-18 10:28:17 -08:00
|
|
|
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();
|
2012-02-14 16:27:51 -08:00
|
|
|
|
|
|
|
int selected = mTabsAdapter.getPositionForTab(Tabs.getInstance().getSelectedTab());
|
|
|
|
if (selected == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mList.setSelection(selected);
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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;
|
2012-02-13 16:50:09 -08:00
|
|
|
mTabsAdapter.removeTab(tab);
|
|
|
|
mList.invalidateViews();
|
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-02-08 23:18:27 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Screenshot:Cancel",""));
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
void showRemoteTabs() {
|
2012-03-12 12:48:55 -07:00
|
|
|
Intent intent = new Intent(this, RemoteTabs.class);
|
|
|
|
intent.putExtra("exit-to-tabs-tray", true);
|
|
|
|
startActivity(intent);
|
|
|
|
overridePendingTransition(R.anim.grow_fade_in, R.anim.shrink_fade_out);
|
2012-03-09 10:42:43 -08:00
|
|
|
finishActivity();
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-03-12 12:30:52 -07:00
|
|
|
if (((childrenHeight - sAddTabHeight) % sListItemHeight == 0) && (childrenHeight >= sMaxHeight))
|
2012-02-07 13:56:34 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
// Adapter to bind tabs into a list
|
2011-11-18 10:28:17 -08:00
|
|
|
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));
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
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();
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:50:09 -08:00
|
|
|
public void removeTab(Tab tab) {
|
|
|
|
mTabs.remove(tab);
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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);
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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() {
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|