2012-06-07 21:47:22 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.mozilla.gecko.sync.setup.SyncAccounts;
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
import android.content.Context;
|
2012-10-31 22:14:16 -07:00
|
|
|
import android.content.res.Resources;
|
2012-07-23 13:08:48 -07:00
|
|
|
import android.content.res.TypedArray;
|
2012-10-31 22:14:16 -07:00
|
|
|
import android.graphics.Color;
|
2012-10-12 16:23:20 -07:00
|
|
|
import android.graphics.Rect;
|
2012-10-31 22:14:16 -07:00
|
|
|
import android.graphics.drawable.ColorDrawable;
|
2012-10-31 22:12:02 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2012-10-31 22:14:16 -07:00
|
|
|
import android.graphics.drawable.LayerDrawable;
|
2012-06-07 21:47:22 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageButton;
|
2012-06-19 14:10:21 -07:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.RelativeLayout;
|
2012-06-07 21:47:22 -07:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-10-31 22:14:16 -07:00
|
|
|
public class TabsPanel extends LinearLayout
|
|
|
|
implements LightweightTheme.OnChangeListener {
|
2012-06-07 21:47:22 -07:00
|
|
|
private static final String LOGTAG = "GeckoTabsPanel";
|
|
|
|
|
|
|
|
public static enum Panel {
|
|
|
|
LOCAL_TABS,
|
|
|
|
REMOTE_TABS
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public static interface PanelView {
|
2012-06-07 21:47:22 -07:00
|
|
|
public ViewGroup getLayout();
|
2012-07-27 23:31:40 -07:00
|
|
|
public void setTabsPanel(TabsPanel panel);
|
2012-06-07 21:47:22 -07:00
|
|
|
public void show();
|
|
|
|
public void hide();
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public static interface TabsLayoutChangeListener {
|
2012-06-07 21:48:29 -07:00
|
|
|
public void onTabsLayoutChange(int width, int height);
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
private Context mContext;
|
2012-07-27 23:31:40 -07:00
|
|
|
private GeckoApp mActivity;
|
2012-06-07 21:47:22 -07:00
|
|
|
private PanelView mPanel;
|
2012-06-19 14:10:21 -07:00
|
|
|
private TabsPanelToolbar mToolbar;
|
|
|
|
private TabsListContainer mListContainer;
|
2012-06-07 21:48:29 -07:00
|
|
|
private TabsLayoutChangeListener mLayoutChangeListener;
|
2012-06-07 21:47:22 -07:00
|
|
|
|
|
|
|
private static ImageButton mRemoteTabs;
|
|
|
|
private TextView mTitle;
|
|
|
|
|
|
|
|
private Panel mCurrentPanel;
|
2012-07-23 13:08:48 -07:00
|
|
|
private boolean mIsSideBar;
|
2012-06-07 21:47:22 -07:00
|
|
|
private boolean mVisible;
|
|
|
|
|
|
|
|
private static final int REMOTE_TABS_HIDDEN = 1;
|
|
|
|
private static final int REMOTE_TABS_SHOWN = 2;
|
|
|
|
|
|
|
|
public TabsPanel(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
mContext = context;
|
2012-07-27 23:31:40 -07:00
|
|
|
mActivity = (GeckoApp) context;
|
2012-06-07 21:47:22 -07:00
|
|
|
|
|
|
|
setOrientation(LinearLayout.VERTICAL);
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.tabs_panel, this);
|
|
|
|
|
|
|
|
mCurrentPanel = Panel.LOCAL_TABS;
|
|
|
|
mVisible = false;
|
|
|
|
|
2012-07-23 13:08:48 -07:00
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabsPanel);
|
|
|
|
mIsSideBar = a.getBoolean(R.styleable.TabsPanel_sidebar, false);
|
|
|
|
a.recycle();
|
|
|
|
|
2012-06-19 14:10:21 -07:00
|
|
|
mToolbar = (TabsPanelToolbar) findViewById(R.id.toolbar);
|
|
|
|
mListContainer = (TabsListContainer) findViewById(R.id.list_container);
|
|
|
|
|
|
|
|
initToolbar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initToolbar() {
|
|
|
|
mTitle = (TextView) mToolbar.findViewById(R.id.title);
|
|
|
|
ImageButton addTab = (ImageButton) mToolbar.findViewById(R.id.add_tab);
|
2012-06-07 21:47:22 -07:00
|
|
|
addTab.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
2012-07-27 23:31:40 -07:00
|
|
|
mActivity.addTab();
|
|
|
|
mActivity.autoHideTabs();
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-06-19 14:10:21 -07:00
|
|
|
mRemoteTabs = (ImageButton) mToolbar.findViewById(R.id.remote_tabs);
|
2012-06-07 21:47:22 -07:00
|
|
|
mRemoteTabs.setOnClickListener(new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (mRemoteTabs.getDrawable().getLevel() == REMOTE_TABS_SHOWN)
|
2012-07-27 23:31:40 -07:00
|
|
|
mActivity.showLocalTabs();
|
2012-06-07 21:47:22 -07:00
|
|
|
else
|
2012-07-27 23:31:40 -07:00
|
|
|
mActivity.showRemoteTabs();
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
|
|
|
});
|
2012-06-12 22:29:23 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 16:23:20 -07:00
|
|
|
private static int getTabContainerHeight(View view) {
|
|
|
|
Context context = view.getContext();
|
|
|
|
|
|
|
|
int actionBarHeight = context.getResources().getDimensionPixelSize(R.dimen.browser_toolbar_height);
|
|
|
|
int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
|
|
|
|
|
|
|
|
Rect windowRect = new Rect();
|
|
|
|
view.getWindowVisibleDisplayFrame(windowRect);
|
|
|
|
int windowHeight = windowRect.bottom - windowRect.top;
|
|
|
|
|
|
|
|
// The web content area should have at least 1.5x the height of the action bar.
|
|
|
|
// The tabs panel shouldn't take less than 50% of the screen height and can take
|
|
|
|
// up to 80% of the window height.
|
|
|
|
return (int) Math.max(screenHeight * 0.5f,
|
|
|
|
Math.min(windowHeight - 2.5f * actionBarHeight, windowHeight * 0.8f) - actionBarHeight);
|
|
|
|
}
|
|
|
|
|
2012-10-31 22:14:16 -07:00
|
|
|
@Override
|
|
|
|
public void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
mActivity.getLightweightTheme().addListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
mActivity.getLightweightTheme().removeListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeChanged() {
|
|
|
|
Drawable drawable = mActivity.getLightweightTheme().getDrawableWithAlpha(this, 255, 0);
|
|
|
|
if (drawable == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
drawable.setAlpha(30);
|
|
|
|
|
|
|
|
LayerDrawable layers = new LayerDrawable(new Drawable[] { mContext.getResources().getDrawable(R.drawable.tabs_tray_bg_repeat), drawable });
|
|
|
|
setBackgroundDrawable(layers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeReset() {
|
|
|
|
setBackgroundResource(R.drawable.tabs_tray_bg_repeat);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
|
|
onLightweightThemeChanged();
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:29:23 -07:00
|
|
|
// Tabs List Container holds the ListView
|
|
|
|
public static class TabsListContainer extends LinearLayout {
|
2012-07-27 23:31:40 -07:00
|
|
|
private Context mContext;
|
|
|
|
|
2012-06-12 22:29:23 -07:00
|
|
|
public TabsListContainer(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2012-07-27 23:31:40 -07:00
|
|
|
mContext = context;
|
2012-06-12 22:29:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
2012-07-23 13:08:48 -07:00
|
|
|
if (!GeckoApp.mAppContext.hasTabsSideBar()) {
|
2012-10-12 16:23:20 -07:00
|
|
|
int heightSpec = MeasureSpec.makeMeasureSpec(getTabContainerHeight(this), MeasureSpec.EXACTLY);
|
2012-06-12 22:29:23 -07:00
|
|
|
super.onMeasure(widthMeasureSpec, heightSpec);
|
|
|
|
} else {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
}
|
|
|
|
}
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 14:10:21 -07:00
|
|
|
// Tabs Panel Toolbar contains the Buttons
|
2012-10-31 22:12:02 -07:00
|
|
|
public static class TabsPanelToolbar extends RelativeLayout
|
|
|
|
implements LightweightTheme.OnChangeListener {
|
|
|
|
private BrowserApp mActivity;
|
|
|
|
|
2012-06-19 14:10:21 -07:00
|
|
|
public TabsPanelToolbar(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
|
|
|
|
(int) context.getResources().getDimension(R.dimen.browser_toolbar_height)));
|
|
|
|
|
2012-07-12 15:03:09 -07:00
|
|
|
int panelToolbarRes;
|
|
|
|
|
2012-07-23 13:08:48 -07:00
|
|
|
if (!GeckoApp.mAppContext.hasPermanentMenuKey())
|
2012-07-12 15:03:09 -07:00
|
|
|
panelToolbarRes = R.layout.tabs_panel_toolbar_menu;
|
|
|
|
else
|
|
|
|
panelToolbarRes = R.layout.tabs_panel_toolbar;
|
|
|
|
|
|
|
|
LayoutInflater.from(context).inflate(panelToolbarRes, this);
|
2012-10-31 22:12:02 -07:00
|
|
|
|
|
|
|
mActivity = (BrowserApp) context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
mActivity.getLightweightTheme().addListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
mActivity.getLightweightTheme().removeListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeChanged() {
|
|
|
|
Drawable drawable = mActivity.getLightweightTheme().getDrawableWithAlpha(this, 34);
|
|
|
|
if (drawable == null)
|
|
|
|
return;
|
|
|
|
|
2012-10-31 22:14:16 -07:00
|
|
|
Resources resources = this.getContext().getResources();
|
|
|
|
LayerDrawable layers = new LayerDrawable(new Drawable[] { resources.getDrawable(R.drawable.tabs_tray_bg_repeat), drawable });
|
|
|
|
setBackgroundDrawable(layers);
|
2012-10-31 22:12:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLightweightThemeReset() {
|
2012-10-31 22:14:16 -07:00
|
|
|
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
2012-10-31 22:12:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
|
|
onLightweightThemeChanged();
|
2012-06-19 14:10:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
public void show(Panel panel) {
|
|
|
|
if (mPanel != null) {
|
2012-06-07 21:48:29 -07:00
|
|
|
// Remove the old panel.
|
2012-06-07 21:47:22 -07:00
|
|
|
mPanel.hide();
|
2012-06-12 22:29:23 -07:00
|
|
|
mListContainer.removeAllViews();
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:37:00 -07:00
|
|
|
final boolean showAnimation = !mVisible;
|
2012-06-07 21:47:22 -07:00
|
|
|
mVisible = true;
|
|
|
|
mCurrentPanel = panel;
|
|
|
|
|
|
|
|
if (panel == Panel.LOCAL_TABS) {
|
|
|
|
mPanel = new TabsTray(mContext, null);
|
|
|
|
mTitle.setText("");
|
|
|
|
mRemoteTabs.setImageLevel(REMOTE_TABS_HIDDEN);
|
|
|
|
} else {
|
|
|
|
mPanel = new RemoteTabs(mContext, null);
|
|
|
|
mTitle.setText(R.string.remote_tabs);
|
|
|
|
mRemoteTabs.setVisibility(View.VISIBLE);
|
|
|
|
mRemoteTabs.setImageLevel(REMOTE_TABS_SHOWN);
|
|
|
|
}
|
|
|
|
|
2012-07-27 23:31:40 -07:00
|
|
|
mPanel.setTabsPanel(this);
|
2012-06-07 21:47:22 -07:00
|
|
|
mPanel.show();
|
2012-06-12 22:29:23 -07:00
|
|
|
mListContainer.addView(mPanel.getLayout());
|
2012-06-07 21:48:29 -07:00
|
|
|
|
2012-07-27 23:31:40 -07:00
|
|
|
if (isSideBar()) {
|
2012-08-15 14:37:00 -07:00
|
|
|
if (showAnimation)
|
|
|
|
dispatchLayoutChange(getWidth(), getHeight());
|
2012-06-12 22:29:23 -07:00
|
|
|
} else {
|
2012-10-12 16:23:20 -07:00
|
|
|
int actionBarHeight = mContext.getResources().getDimensionPixelSize(R.dimen.browser_toolbar_height);
|
|
|
|
int height = actionBarHeight + getTabContainerHeight(mListContainer);
|
2012-08-31 11:14:43 -07:00
|
|
|
dispatchLayoutChange(getWidth(), height);
|
2012-06-12 22:29:23 -07:00
|
|
|
}
|
2012-06-07 21:47:22 -07:00
|
|
|
|
|
|
|
// If Sync is set up, query the database for remote clients.
|
|
|
|
final Context context = mContext;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void hide() {
|
2012-06-12 22:47:52 -07:00
|
|
|
if (mVisible) {
|
|
|
|
mVisible = false;
|
|
|
|
dispatchLayoutChange(0, 0);
|
|
|
|
}
|
2012-06-07 21:48:29 -07:00
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
public void refresh() {
|
2012-06-19 14:10:21 -07:00
|
|
|
mListContainer.forceLayout();
|
|
|
|
|
|
|
|
int index = indexOfChild(mToolbar);
|
|
|
|
removeViewAt(index);
|
|
|
|
|
|
|
|
mToolbar = new TabsPanelToolbar(mContext, null);
|
|
|
|
addView(mToolbar, index);
|
|
|
|
initToolbar();
|
|
|
|
|
|
|
|
if (mVisible)
|
2012-06-07 21:47:22 -07:00
|
|
|
show(mCurrentPanel);
|
|
|
|
}
|
|
|
|
|
2012-07-27 23:31:40 -07:00
|
|
|
public void autoHidePanel() {
|
|
|
|
mActivity.autoHideTabs();
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
@Override
|
|
|
|
public boolean isShown() {
|
|
|
|
return mVisible;
|
|
|
|
}
|
2012-06-07 21:48:29 -07:00
|
|
|
|
2012-07-23 13:08:48 -07:00
|
|
|
public boolean isSideBar() {
|
|
|
|
return mIsSideBar;
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:48:29 -07:00
|
|
|
public void setTabsLayoutChangeListener(TabsLayoutChangeListener listener) {
|
|
|
|
mLayoutChangeListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dispatchLayoutChange(int width, int height) {
|
|
|
|
if (mLayoutChangeListener != null)
|
|
|
|
mLayoutChangeListener.onTabsLayoutChange(width, height);
|
|
|
|
}
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|