diff options
author | John Reck <jreck@google.com> | 2011-03-21 11:46:09 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2011-03-21 14:58:39 -0700 |
commit | 18e93f72fdfdd57ecaebe80ecccc9fecfbb16c8f (patch) | |
tree | 11cd560b335f4842bfde8619b7b948acf5cb6191 /src/com/android | |
parent | bd9eaf60f38ce56f7aa25ba59bc60a6051eb37ce (diff) | |
download | packages_apps_Browser-18e93f72fdfdd57ecaebe80ecccc9fecfbb16c8f.zip packages_apps_Browser-18e93f72fdfdd57ecaebe80ecccc9fecfbb16c8f.tar.gz packages_apps_Browser-18e93f72fdfdd57ecaebe80ecccc9fecfbb16c8f.tar.bz2 |
Tab switcher cleanup
Make it holo and clean up the code
Change-Id: I6462cb72f104a2c076fd2ce2da876c74de8e783a
Diffstat (limited to 'src/com/android')
-rw-r--r-- | src/com/android/browser/ActiveTabsPage.java | 264 | ||||
-rw-r--r-- | src/com/android/browser/BrowserBookmarksAdapter.java | 8 |
2 files changed, 122 insertions, 150 deletions
diff --git a/src/com/android/browser/ActiveTabsPage.java b/src/com/android/browser/ActiveTabsPage.java index 664fd68..5e27eab 100644 --- a/src/com/android/browser/ActiveTabsPage.java +++ b/src/com/android/browser/ActiveTabsPage.java @@ -18,62 +18,86 @@ package com.android.browser; import android.content.Context; import android.graphics.Bitmap; -import android.os.Handler; import android.util.AttributeSet; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.widget.AbsListView; import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; +import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; -import android.widget.ListView; import android.widget.TextView; -public class ActiveTabsPage extends LinearLayout { +interface OnCloseTab { + void onCloseTab(int position); +} - private static final String LOGTAG = "TabPicker"; +public class ActiveTabsPage extends LinearLayout implements OnClickListener, + OnItemClickListener, OnCloseTab { - private final LayoutInflater mFactory; - private final UiController mUiController; - private final TabControl mControl; - private final TabsListAdapter mAdapter; - private final ListView mListView; + private Context mContext; + private UiController mController; + private TabControl mTabControl; + private View mNewTab, mNewIncognitoTab; + private TabAdapter mAdapter; + private AbsListView mTabsList; - public ActiveTabsPage(Context context, UiController control) { + public ActiveTabsPage(Context context, UiController controller) { super(context); - mUiController = control; - mControl = control.getTabControl(); - mFactory = LayoutInflater.from(context); - mFactory.inflate(R.layout.active_tabs, this); - mListView = (ListView) findViewById(R.id.list); - mAdapter = new TabsListAdapter(); - mListView.setAdapter(mAdapter); - mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(AdapterView<?> parent, View view, - int position, long id) { - if (mControl.canCreateNewTab()) { - position -= 2; - } - boolean needToAttach = false; - if (position == -2) { - // Create a new tab - mUiController.openTabToHomePage(); - } else if (position == -1) { - // Create a new incognito tab - mUiController.openIncognitoTab(); - } else { - // Open the corresponding tab - // If the tab is the current one, switchToTab will - // do nothing and return, so we need to make sure - // it gets attached back to its mContentView in - // removeActiveTabPage - needToAttach = !mUiController.switchToTab(position); - } - mUiController.removeActiveTabsPage(needToAttach); - } - }); + mContext = context; + mController = controller; + mTabControl = mController.getTabControl(); + setOrientation(VERTICAL); + setBackgroundResource(R.drawable.bg_browser); + LayoutInflater inflate = LayoutInflater.from(mContext); + inflate.inflate(R.layout.active_tabs, this, true); + mNewTab = findViewById(R.id.new_tab); + mNewIncognitoTab = findViewById(R.id.new_incognito_tab); + mNewTab.setOnClickListener(this); + mNewIncognitoTab.setOnClickListener(this); + int visibility = mTabControl.canCreateNewTab() ? View.VISIBLE : View.GONE; + mNewTab.setVisibility(visibility); + mNewIncognitoTab.setVisibility(visibility); + mTabsList = (AbsListView) findViewById(android.R.id.list); + mAdapter = new TabAdapter(mContext, mTabControl); + mAdapter.setOnCloseListener(this); + mTabsList.setAdapter(mAdapter); + mTabsList.setOnItemClickListener(this); + } + + @Override + public void onClick(View v) { + if (v == mNewTab) { + mController.openTabToHomePage(); + } else if (v == mNewIncognitoTab) { + mController.openIncognitoTab(); + } + mController.removeActiveTabsPage(false); + } + + @Override + public void onItemClick( + AdapterView<?> parent, View view, int position, long id) { + boolean needToAttach = !mController.switchToTab(position); + mController.removeActiveTabsPage(needToAttach); + } + + @Override + public void onCloseTab(int position) { + Tab tab = mTabControl.getTab(position); + if (tab != null) { + mController.closeTab(tab); + if (mTabControl.getTabCount() == 0) { + mController.openTabToHomePage(); + mController.removeActiveTabsPage(false); + } else { + mAdapter.notifyDataSetChanged(); + } + } } /** @@ -81,7 +105,7 @@ public class ActiveTabsPage extends LinearLayout { * the parent to be pressed without being pressed itself. This way the line * of a tab can be pressed, but the close button itself is not. */ - private static class CloseHolder extends ImageView { + public static class CloseHolder extends ImageView { public CloseHolder(Context context, AttributeSet attrs) { super(context, attrs); } @@ -96,118 +120,72 @@ public class ActiveTabsPage extends LinearLayout { } } - private class TabsListAdapter extends BaseAdapter { - private boolean mNotified = true; - private int mReturnedCount; - private Handler mHandler = new Handler(); + static class TabAdapter extends BaseAdapter implements OnClickListener { - public int getCount() { - int count = mControl.getTabCount(); - if (mControl.canCreateNewTab()) { - count += 2; - } - // XXX: This is a workaround to be more like a real adapter. Most - // adapters call notifyDataSetChanged() whenever the internal data - // has changed. Since TabControl is our internal data, we don't - // know when that changes. - // - // Keep track of the last count we returned and whether we called - // notifyDataSetChanged(). If we did not initiate a data set - // change, and the count is different, send the notify and return - // the old count. - if (!mNotified && count != mReturnedCount) { - notifyChange(); - return mReturnedCount; - } - mReturnedCount = count; - mNotified = false; - return count; - } - public Object getItem(int position) { - return null; - } - public long getItemId(int position) { - return position; - } - public int getViewTypeCount() { - return 2; + LayoutInflater mInflater; + OnCloseTab mCloseListener; + TabControl mTabControl; + + TabAdapter(Context context, TabControl tabs) { + mInflater = LayoutInflater.from(context); + mTabControl = tabs; } - public int getItemViewType(int position) { - if (mControl.canCreateNewTab()) { - position -= 2; - } - // Do not recycle the "add new tab" item. - return position < 0 ? IGNORE_ITEM_VIEW_TYPE : 1; + + void setOnCloseListener(OnCloseTab listener) { + mCloseListener = listener; } - public View getView(int position, View convertView, ViewGroup parent) { - final int tabCount = mControl.getTabCount(); - if (mControl.canCreateNewTab()) { - position -= 2; + + @Override + public View getView(int position, View view, ViewGroup parent) { + if (view == null) { + view = mInflater.inflate(R.layout.tab_view, parent, false); } + ImageView favicon = (ImageView) view.findViewById(R.id.favicon); + TextView title = (TextView) view.findViewById(R.id.title); + TextView url = (TextView) view.findViewById(R.id.url); + Tab tab = getItem(position); - if (convertView == null) { - if (position == -2) { - convertView = mFactory.inflate(R.layout.tab_view_add_tab, null); - } else if (position == -1) { - convertView = mFactory.inflate(R.layout.tab_view_add_incognito_tab, null); + title.setText(tab.getTitle()); + url.setText(tab.getUrl()); + Bitmap faviconBitmap = tab.getFavicon(); + if (tab.isPrivateBrowsingEnabled()) { + favicon.setImageResource(R.drawable.ic_incognito_holo_dark); + favicon.setBackgroundDrawable(null); + } else { + if (faviconBitmap == null) { + favicon.setImageResource(R.drawable.app_web_browser_sm); } else { - convertView = mFactory.inflate(R.layout.tab_view, null); + favicon.setImageBitmap(faviconBitmap); } + favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); } + View close = view.findViewById(R.id.close); + close.setTag(position); + close.setOnClickListener(this); + return view; + } - if (position >= 0) { - TextView title = - (TextView) convertView.findViewById(R.id.title); - TextView url = (TextView) convertView.findViewById(R.id.url); - ImageView favicon = - (ImageView) convertView.findViewById(R.id.favicon); - View close = convertView.findViewById(R.id.close); - Tab tab = mControl.getTab(position); - if (tab.getWebView() == null) { - // This means that populatePickerData will have to use the - // saved state. - Log.w(LOGTAG, "Tab " + position + " has a null WebView and " - + (tab.getSavedState() == null ? "null" : "non-null") - + " saved state "); - } - if (tab.getTitle() == null || tab.getTitle().length() == 0) { - Log.w(LOGTAG, "Tab " + position + " has no title. " - + "Check above in the Logs to see whether it has a " - + "null WebView or null WebHistoryItem"); - } - title.setText(tab.getTitle()); - url.setText(tab.getUrl()); - Bitmap icon = tab.getFavicon(); - if (icon != null) { - favicon.setImageBitmap(icon); - } else { - favicon.setImageResource(R.drawable.app_web_browser_sm); - } - final int closePosition = position; - close.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - mUiController.closeTab( - mControl.getTab(closePosition)); - if (tabCount == 1) { - mUiController.openTabToHomePage(); - mUiController.removeActiveTabsPage(false); - } else { - mNotified = true; - notifyDataSetChanged(); - } - } - }); + @Override + public void onClick(View v) { + int position = (Integer) v.getTag(); + if (mCloseListener != null) { + mCloseListener.onCloseTab(position); } - return convertView; } - void notifyChange() { - mHandler.post(new Runnable() { - public void run() { - mNotified = true; - notifyDataSetChanged(); - } - }); + @Override + public int getCount() { + return mTabControl.getTabCount(); + } + + @Override + public Tab getItem(int position) { + return mTabControl.getTab(position); + } + + @Override + public long getItemId(int position) { + return position; } } } diff --git a/src/com/android/browser/BrowserBookmarksAdapter.java b/src/com/android/browser/BrowserBookmarksAdapter.java index 9e71077..201079c 100644 --- a/src/com/android/browser/BrowserBookmarksAdapter.java +++ b/src/com/android/browser/BrowserBookmarksAdapter.java @@ -20,7 +20,6 @@ import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -31,7 +30,6 @@ import android.widget.TextView; class BrowserBookmarksAdapter extends CursorAdapter { LayoutInflater mInflater; int mCurrentView; - Drawable mFaviconBackground; /** * Create a new BrowserBookmarksAdapter. @@ -42,8 +40,6 @@ class BrowserBookmarksAdapter extends CursorAdapter { super(context, null, 0); mInflater = LayoutInflater.from(context); selectView(defaultView); - float density = context.getResources().getDisplayMetrics().density; - mFaviconBackground = BookmarkUtils.createListFaviconBackground(context); } @Override @@ -101,9 +97,7 @@ class BrowserBookmarksAdapter extends CursorAdapter { } else { favicon.setImageBitmap(faviconBitmap); } - //favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); - // TODO: Switch to above instead of below once b/3353813 is fixed - favicon.setBackgroundDrawable(mFaviconBackground); + favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); } } |