fix navscreen orientation

    handle configuration change in navscreen
    capture actual screen for tabs
    size tabs correctly

Change-Id: I20ff72bdcab13678cdba4c55849bd5ff16cb6568
diff --git a/res/layout/custom_screen.xml b/res/layout/custom_screen.xml
index 525f30c..2105501 100644
--- a/res/layout/custom_screen.xml
+++ b/res/layout/custom_screen.xml
@@ -37,3 +37,4 @@
         />
     </LinearLayout>
 </FrameLayout>
+
diff --git a/res/layout/nav_tab_view.xml b/res/layout/nav_tab_view.xml
index 439eb89..f657f4f 100644
--- a/res/layout/nav_tab_view.xml
+++ b/res/layout/nav_tab_view.xml
@@ -18,7 +18,7 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tab_view"
     android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
+    android:layout_height="match_parent"
     android:orientation="vertical"
     android:padding="4dip"
     android:focusable="false"
diff --git a/res/values/dimensions.xml b/res/values/dimensions.xml
index 5b962d8..28b9e8b 100644
--- a/res/values/dimensions.xml
+++ b/res/values/dimensions.xml
@@ -67,4 +67,5 @@
     <dimen name="nav_tab_spacing">8dp</dimen>
     <dimen name="menu_width">240dip</dimen>
     <dimen name="toolbar_height">52dip</dimen>
+    <dimen name="tab_capture_size">480dp</dimen>
 </resources>
diff --git a/src/com/android/browser/BrowserWebView.java b/src/com/android/browser/BrowserWebView.java
index 9be9ad9..a1d8c2d 100644
--- a/src/com/android/browser/BrowserWebView.java
+++ b/src/com/android/browser/BrowserWebView.java
@@ -36,6 +36,7 @@
     private boolean mBackgroundRemoved = false;
     private boolean mUserInitiated = false;
     private TitleBarBase mTitleBar;
+    private int mCaptureSize;
     private Bitmap mCapture;
 
     /**
@@ -57,6 +58,7 @@
     public BrowserWebView(
             Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
         super(context, attrs, defStyle, privateBrowsing);
+        init();
     }
 
     /**
@@ -65,6 +67,7 @@
      */
     public BrowserWebView(Context context, AttributeSet attrs) {
         super(context, attrs);
+        init();
     }
 
     /**
@@ -72,12 +75,13 @@
      */
     public BrowserWebView(Context context) {
         super(context);
+        init();
     }
 
-    @Override
-    protected void onSizeChanged(int w, int h, int ow, int oh) {
-        super.onSizeChanged(w, h, ow, oh);
-        mCapture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+    private void init() {
+        mCaptureSize = mContext.getResources().getDimensionPixelSize(R.dimen.tab_capture_size);
+        mCapture = Bitmap.createBitmap(mCaptureSize, mCaptureSize,
+                Bitmap.Config.RGB_565);
     }
 
     @Override
@@ -150,7 +154,11 @@
     protected Bitmap capture() {
         if (mCapture == null) return null;
         Canvas c = new Canvas(mCapture);
-        c.translate(-getScrollX(), -(getScrollY() + getVisibleTitleHeight()));
+        final int left = getScrollX();
+        final int top = getScrollY() + getVisibleTitleHeight();
+        c.translate(-left, -top);
+        float scale = mCaptureSize / (float) Math.max(getWidth(), getHeight());
+        c.scale(scale, scale, left, top);
         onDraw(c);
         return mCapture;
     }
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 5498d6d..d1e1b0a 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -97,6 +97,7 @@
     private static final String LOGTAG = "Controller";
     private static final String SEND_APP_ID_EXTRA =
         "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
+    private static final String INCOGNITO_URI = "browser:incognito";
 
 
     // public message ids
@@ -2222,7 +2223,6 @@
         }
     }
 
-
     // This method does a ton of stuff. It will attempt to create a new tab
     // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
     // url isn't null, it will load the given url.
@@ -2261,7 +2261,21 @@
                     null, true);
             addTab(tab);
             setActiveTab(tab);
-            loadUrlDataIn(tab, new UrlData("browser:incognito"));
+            loadUrlDataIn(tab, new UrlData(INCOGNITO_URI));
+            return tab;
+        } else {
+            mUi.showMaxTabsWarning();
+            return null;
+        }
+    }
+
+    @Override
+    public Tab createNewTab(String url, boolean incognito) {
+        if (mTabControl.canCreateNewTab()) {
+            Tab tab = mTabControl.createNewTab(false, null, null, incognito);
+            WebView w = tab.getWebView();
+            addTab(tab);
+            loadUrl(w, (incognito ? INCOGNITO_URI : url));
             return tab;
         } else {
             mUi.showMaxTabsWarning();
diff --git a/src/com/android/browser/NavScreen.java b/src/com/android/browser/NavScreen.java
index 1a25671..848b7b1 100644
--- a/src/com/android/browser/NavScreen.java
+++ b/src/com/android/browser/NavScreen.java
@@ -18,7 +18,9 @@
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.graphics.Bitmap;
+import android.graphics.Matrix;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -31,9 +33,11 @@
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.BaseAdapter;
 import android.widget.FrameLayout;
+import android.widget.FrameLayout.LayoutParams;
 import android.widget.Gallery;
 import android.widget.ImageButton;
 import android.widget.ImageView;
+import android.widget.ImageView.ScaleType;
 import android.widget.LinearLayout;
 import android.widget.ListPopupWindow;
 import android.widget.TextView;
@@ -60,29 +64,36 @@
     FrameLayout mHolder;
 
     Gallery mFlipper;
+    float mTabAspect = 0.66f;
     int mTabWidth;
     int mTabHeight;
     TabAdapter mAdapter;
     ListPopupWindow mPopup;
+    int mOrientation;
 
     public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
         super(activity);
         mActivity = activity;
         mUiController = ctl;
         mUi = ui;
+        mOrientation = activity.getResources().getConfiguration().orientation;
         init();
     }
 
+    @Override
+    public void onMeasure(int wspec, int hspec) {
+        super.onMeasure(wspec, hspec);
+        mTabHeight = mFlipper.getMeasuredHeight();
+        mTabWidth = (int) (mTabHeight * mTabAspect);
+        if (mAdapter != null) {
+            mAdapter.notifyDataSetChanged();
+        }
+    }
+
     protected Tab getSelectedTab() {
         return (Tab) mFlipper.getSelectedItem();
     }
 
-    protected void setTabDimensions(int w, int h) {
-        mTabWidth = w;
-        mTabHeight = h;
-        requestLayout();
-    }
-
     protected void showMenu() {
         Menu menu = mUi.getMenu();
         menu.setGroupVisible(R.id.NAV_MENU, false);
@@ -110,6 +121,18 @@
         }
     }
 
+    // for configuration changes
+    @Override
+    protected void onConfigurationChanged(Configuration newconfig) {
+        if (newconfig.orientation != mOrientation) {
+            int selIx = mFlipper.getSelectedItemPosition();
+            removeAllViews();
+            init();
+            mFlipper.setSelection(selIx);
+            mOrientation = newconfig.orientation;
+        }
+    }
+
     private void init() {
         LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
         LinearLayout content = (LinearLayout) findViewById(R.id.nav_screen);
@@ -136,6 +159,7 @@
         mFlipper = new TabGallery(mContext);
         mFlipper.setSpacing((int)(mContext.getResources()
                 .getDimension(R.dimen.nav_tab_spacing)));
+        mFlipper.setUnselectedAlpha(0.8f);
         mFlipper.setLayoutParams(lp);
         mHolder.addView(mFlipper, 0);
         mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
@@ -192,24 +216,25 @@
         if (web != null) {
             if (mBack == v) {
                 mUi.hideNavScreen(true);
+                switchToSelected();
                 web.goBack();
             } else if (mForward == v) {
                 mUi.hideNavScreen(true);
+                switchToSelected();
                 web.goForward();
             } else if (mRefresh == v) {
                 mUi.hideNavScreen(true);
+                switchToSelected();
                 web.reload();
             }
         }
         if (mBookmarks == v) {
             mUi.hideNavScreen(false);
+            switchToSelected();
             mUiController.bookmarksOrHistoryPicker(false);
         } else if (mTabs == v) {
-            mUi.hideNavScreen(false);
-            mUi.showActiveTabsPage();
         } else if (mNewTab == v) {
-            mUi.hideNavScreen(true);
-            mUiController.openTabToHomePage();
+            openNewTab();
         } else if (mMore == v) {
             showMenu();
         } else if (mNewIncognito == v) {
@@ -218,16 +243,50 @@
         }
     }
 
+    private void openNewTab() {
+        Tab tab = mUiController.createNewTab(
+                BrowserSettings.getInstance().getHomePage(),
+                false);
+        mAdapter.notifyDataSetChanged();
+
+        if (tab != null) {
+            // set tab as the selected in flipper, then hide
+            final int tix = mUi.mTabControl.getTabIndex(tab);
+            post(new Runnable() {
+                public void run() {
+                    if (tix != -1) {
+                        for (int i = mFlipper.getSelectedItemPosition();
+                                i <= tix; i++) {
+                            mFlipper.setSelection(i, true);
+                            mFlipper.invalidate();
+                            try {
+                                Thread.sleep(100);
+                            } catch (InterruptedException e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                    mUi.hideNavScreen(true);
+                    switchToSelected();
+                }
+            });
+        }
+    }
+
+    private void switchToSelected() {
+        Tab tab = (Tab) mFlipper.getSelectedItem();
+        if (tab != mUi.getActiveTab()) {
+            mUiController.setActiveTab(tab);
+        }
+    }
+
     protected void close() {
         close(true);
     }
 
     protected void close(boolean animate) {
         mUi.hideNavScreen(animate);
-        Tab tab = (Tab) mFlipper.getSelectedItem();
-        if (tab != mUi.getActiveTab()) {
-            mUiController.setActiveTab(tab);
-        }
+        switchToSelected();
     }
 
     class TabGallery extends Gallery {
@@ -296,6 +355,7 @@
                 content.setLayoutParams(new LayoutParams(mTabWidth, mTabHeight));
             } else {
                 content = (ImageView) convertView.findViewById(R.id.content);
+                content.setLayoutParams(new LayoutParams(mTabWidth, mTabHeight));
             }
             View tbar = convertView.findViewById(R.id.titlebar);
             TextView title = (TextView) convertView.findViewById(R.id.title);
@@ -304,8 +364,20 @@
             final Tab tab = getItem(position);
             icon.setImageDrawable(mUi.getFaviconDrawable(tab.getFavicon()));
             title.setText(tab.getUrl());
+            content.setScaleType(ScaleType.MATRIX);
+            Matrix matrix = new Matrix();
             Bitmap screen = tab.getScreenshot();
-            content.setImageBitmap(screen);
+            if (screen != null) {
+                float scale = 1.0f;
+                if (mTabWidth > mTabHeight) {
+                    scale = mTabWidth / (float) screen.getWidth();
+                } else {
+                    scale = mTabHeight / (float) screen.getHeight();
+                }
+                matrix.setScale(scale, scale);
+                content.setImageMatrix(matrix);
+                content.setImageBitmap(screen);
+            }
             close.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index 13a5257..e838cd4 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -285,12 +285,14 @@
 
     @Override
     protected void captureTab(final Tab tab) {
+        if (tab == null) return;
         if (mUseQuickControls) {
             super.captureTab(tab);
         } else {
-            captureTab(tab,
-                    mActivity.getWindowManager().getDefaultDisplay().getWidth(),
-                    mActivity.getWindowManager().getDefaultDisplay().getHeight());
+            BrowserWebView web = (BrowserWebView) tab.getWebView();
+            if (web != null) {
+                tab.setScreenshot(web.capture());
+            }
         }
     }
 
@@ -301,11 +303,8 @@
         float yoffset = 0;
         WebView web = getWebView();
         if (web != null) {
-            int w = web.getWidth();
-            int h = web.getHeight();
-            yoffset = mNavScreen.getToolbarHeight() -  web.getVisibleTitleHeight();
-            mNavScreen.setTabDimensions((int) (w * NAV_TAB_SCALE),
-                    (int) (h * NAV_TAB_SCALE));
+            yoffset = mNavScreen.getToolbarHeight() -
+                    web.getVisibleTitleHeight();
         }
         // Add the custom view to its container.
         mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_GRAVITY_CENTER);
@@ -366,7 +365,8 @@
             float yoffset = 0;
             WebView web = mNavScreen.getSelectedTab().getWebView();
             if (web != null) {
-                yoffset = mNavScreen.getToolbarHeight() -  web.getVisibleTitleHeight();
+                yoffset = mNavScreen.getToolbarHeight() -
+                        web.getVisibleTitleHeight();
             }
             ObjectAnimator animx = ObjectAnimator.ofFloat(mNavScreen, "scaleX",
                     1.0f, 1 / NAV_TAB_SCALE);
diff --git a/src/com/android/browser/UiController.java b/src/com/android/browser/UiController.java
index 2b3ee69..9b75aca 100644
--- a/src/com/android/browser/UiController.java
+++ b/src/com/android/browser/UiController.java
@@ -44,6 +44,8 @@
 
     Tab openIncognitoTab();
 
+    Tab createNewTab(String url, boolean incognito);
+
     void setActiveTab(Tab tab);
 
     boolean switchToTab(int tabIndex);