Initial work on browser incognito mode history feature.

Change-Id: I5d1575a9b74704abc313b322ae08ca7caaccdc69
diff --git a/res/layout/tab_view_add_incognito_tab.xml b/res/layout/tab_view_add_incognito_tab.xml
new file mode 100644
index 0000000..d07b4f5
--- /dev/null
+++ b/res/layout/tab_view_add_incognito_tab.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:layout_width="match_parent"
+    android:orientation="horizontal"
+    android:gravity="center_vertical"
+    >
+    <ImageView
+        android:layout_width="40dip"
+        android:layout_height="40dip"
+        android:layout_marginLeft="8dip"
+        android:layout_marginRight="8dip"
+        android:src="@drawable/ic_list_new_window"/>
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:maxLines="1"
+        android:singleLine="true"
+        android:ellipsize="end"
+        android:text="@string/new_incognito_tab"
+        />
+</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0dcfa9e..af0bbe9 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -20,6 +20,8 @@
     <string name="choose_upload">Choose file for upload</string>
     <!-- Name of menu item of a new tab.  Also used in the title bar when displaying a new tab -->
     <string name="new_tab">New window</string>
+    <!-- Name of menu item of a new incognito tab.  Also used in the title bar when displaying a new tab -->
+    <string name="new_incognito_tab">New incognito window</string>
     <!-- Name of menu item which brings up a list of the currently active tabs -->
     <string name="active_tabs">Windows</string>
     <!-- Name of tab containing bookmarks -->
diff --git a/src/com/android/browser/ActiveTabsPage.java b/src/com/android/browser/ActiveTabsPage.java
index 52828b3..e450a99 100644
--- a/src/com/android/browser/ActiveTabsPage.java
+++ b/src/com/android/browser/ActiveTabsPage.java
@@ -53,12 +53,15 @@
                 public void onItemClick(AdapterView<?> parent, View view,
                         int position, long id) {
                     if (mControl.canCreateNewTab()) {
-                        position--;
+                        position -= 2;
                     }
                     boolean needToAttach = false;
-                    if (position == -1) {
+                    if (position == -2) {
                         // Create a new tab
                         mBrowserActivity.openTabToHomePage();
+                    } else if (position == -1) {
+                        // Create a new incognito tab
+                        mBrowserActivity.openIncognitoTab();
                     } else {
                         // Open the corresponding tab
                         // If the tab is the current one, switchToTab will
@@ -100,7 +103,7 @@
         public int getCount() {
             int count = mControl.getTabCount();
             if (mControl.canCreateNewTab()) {
-                count++;
+                count += 2;
             }
             // XXX: This is a workaround to be more like a real adapter. Most
             // adapters call notifyDataSetChanged() whenever the internal data
@@ -130,23 +133,28 @@
         }
         public int getItemViewType(int position) {
             if (mControl.canCreateNewTab()) {
-                position--;
+                position -= 2;
             }
             // Do not recycle the "add new tab" item.
-            return position == -1 ? IGNORE_ITEM_VIEW_TYPE : 1;
+            return position < 0 ? IGNORE_ITEM_VIEW_TYPE : 1;
         }
         public View getView(int position, View convertView, ViewGroup parent) {
             final int tabCount = mControl.getTabCount();
             if (mControl.canCreateNewTab()) {
-                position--;
+                position -= 2;
             }
 
             if (convertView == null) {
-                convertView = mFactory.inflate(position == -1 ?
-                        R.layout.tab_view_add_tab : R.layout.tab_view, 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);
+                } else {
+                    convertView = mFactory.inflate(R.layout.tab_view, null);
+                }
             }
 
-            if (position != -1) {
+            if (position >= 0) {
                 TextView title =
                         (TextView) convertView.findViewById(R.id.title);
                 TextView url = (TextView) convertView.findViewById(R.id.url);
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 69e0ee5..b9f5f01 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -348,7 +348,8 @@
                     intent.getData() != null)
                     || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
                     .equals(action),
-                    intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
+                    intent.getStringExtra(Browser.EXTRA_APPLICATION_ID),
+                    urlData.mUrl, false);
             mTabControl.setCurrentTab(t);
             attachTabToContentView(t);
             WebView webView = t.getWebView();
@@ -632,14 +633,16 @@
 
         final ContentResolver cr = mResolver;
         final String newUrl = url;
-        new AsyncTask<Void, Void, Void>() {
-            @Override
-            protected Void doInBackground(Void... unused) {
-                Browser.updateVisitedHistory(cr, newUrl, false);
-                Browser.addSearchUrl(cr, newUrl);
-                return null;
-            }
-        }.execute();
+        if (!mTabControl.getCurrentWebView().isPrivateBrowsingEnabled()) {
+            new AsyncTask<Void, Void, Void>() {
+                @Override
+                protected Void doInBackground(Void... unused) {
+                        Browser.updateVisitedHistory(cr, newUrl, false);
+                        Browser.addSearchUrl(cr, newUrl);
+                    return null;
+                }
+            }.execute();
+        }
 
         Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
         intent.addCategory(Intent.CATEGORY_DEFAULT);
@@ -1864,7 +1867,7 @@
         final Tab currentTab = mTabControl.getCurrentTab();
         if (mTabControl.canCreateNewTab()) {
             final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
-                    urlData.mUrl);
+                    urlData.mUrl, false);
             WebView webview = tab.getWebView();
             // If the last tab was removed from the active tabs page, currentTab
             // will be null.
@@ -1903,6 +1906,20 @@
         }
     }
 
+    /* package */ Tab openIncognitoTab() {
+        if (mTabControl.canCreateNewTab()) {
+            Tab currentTab = mTabControl.getCurrentTab();
+            Tab tab = mTabControl.createNewTab(false, null, null, true);
+            if (currentTab != null) {
+                removeTabFromContentView(currentTab);
+            }
+            mTabControl.setCurrentTab(tab);
+            attachTabToContentView(tab);
+            return tab;
+        }
+        return null;
+    }
+
     private class Copy implements OnMenuItemClickListener {
         private CharSequence mText;
 
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 0fbdd7a..d850b1e 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -154,13 +154,14 @@
      * @return The newly createTab or null if we have reached the maximum
      *         number of open tabs.
      */
-    Tab createNewTab(boolean closeOnExit, String appId, String url) {
+    Tab createNewTab(boolean closeOnExit, String appId, String url,
+            boolean privateBrowsing) {
         int size = mTabs.size();
         // Return false if we have maxed out on tabs
         if (MAX_TABS == size) {
             return null;
         }
-        final WebView w = createNewWebView();
+        final WebView w = createNewWebView(privateBrowsing);
 
         // Create a new tab and add it to the tab list
         Tab t = new Tab(mActivity, w, closeOnExit, appId, url);
@@ -175,10 +176,10 @@
 
     /**
      * Create a new tab with default values for closeOnExit(false),
-     * appId(null), and url(null).
+     * appId(null), url(null), and privateBrowsing(false).
      */
     Tab createNewTab() {
-        return createNewTab(false, null, null);
+        return createNewTab(false, null, null, false);
     }
 
     /**
@@ -535,8 +536,18 @@
      * Creates a new WebView and registers it with the global settings.
      */
     private WebView createNewWebView() {
+        return createNewWebView(false);
+    }
+
+    /**
+     * Creates a new WebView and registers it with the global settings.
+     * @param privateBrowsing When true, enables private browsing in the new
+     *        WebView.
+     */
+    private WebView createNewWebView(boolean privateBrowsing) {
         // Create a new WebView
-        WebView w = new WebView(mActivity);
+        WebView w = new WebView(mActivity, null,
+                com.android.internal.R.attr.webViewStyle, privateBrowsing);
         w.setScrollbarFadingEnabled(true);
         w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
         w.setMapTrackballToArrowKeys(false); // use trackball directly