Add reader mode option in popup menu

Reader mode use the DOM distiller to load the distilled version of a webpage
Reader mode is disabled by default and will be available only with the use
of the command line flag "reader-mode"

Change-Id: Ide36057cae4e71b94e8118cc767f600cd610b82e
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 5563b62..5c6a9b8 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1952,6 +1952,11 @@
         } else {
             bookmark_icon.setChecked(false);
         }
+
+        // update reader mode checkbox
+        MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
+        readerSwitcher.setVisible(false);
+        readerSwitcher.setChecked(false);
     }
 
     @Override
@@ -1964,6 +1969,9 @@
         boolean isLiveScheme = false;
         boolean isPageFinished = false;
         boolean isSavable = false;
+
+        boolean isDistillable = false;
+        boolean isDistilled = false;
         resetMenuItems(menu);
 
         if (tab != null) {
@@ -1973,6 +1981,9 @@
             isLiveScheme = UrlUtils.isLiveScheme(tab.getWebView().getUrl());
             isPageFinished = (tab.getPageFinishedStatus() || !tab.inPageLoad());
             isSavable = tab.getWebView().isSavable();
+
+            isDistillable = tab.isDistillable();
+            isDistilled = tab.isDistilled();
         }
 
         final MenuItem forward = menu.findItem(R.id.forward_menu_id);
@@ -2001,11 +2012,19 @@
         setMenuItemVisibility(menu, R.id.add_to_homescreen,
                 isLive && isLiveScheme && isPageFinished);
         setMenuItemVisibility(menu, R.id.save_snapshot_menu_id,
-                isLive && isLiveScheme && isPageFinished && isSavable);
+                isLive && ( isLiveScheme || isDistilled ) && isPageFinished && isSavable);
         // history and snapshots item are the members of COMBO menu group,
         // so if show history item, only make snapshots item invisible.
         menu.findItem(R.id.snapshots_menu_id).setVisible(false);
 
+
+        // update reader mode checkbox
+        final MenuItem readerSwitcher = menu.findItem(R.id.reader_mode_menu_id);
+        // The reader mode checkbox is hidden only
+        // when the current page is neither distillable nor distilled
+        readerSwitcher.setVisible(isDistillable || isDistilled);
+        readerSwitcher.setChecked(isDistilled);
+
         mUi.updateMenuState(tab, menu);
     }
 
@@ -2154,6 +2173,10 @@
                 toggleUserAgent();
                 break;
 
+            case R.id.reader_mode_menu_id:
+                toggleReaderMode();
+                break;
+
             case R.id.window_one_menu_id:
             case R.id.window_two_menu_id:
             case R.id.window_three_menu_id:
@@ -2294,6 +2317,17 @@
         mSettings.toggleDesktopUseragent(web);
     }
 
+    // This function calls the  method in the webview to enable/disable
+    // the reader mode through the DOM distiller
+    public void toggleReaderMode() {
+        Tab t = mTabControl.getCurrentTab();
+        if (t.isDistilled()) {
+            closeTab(t);
+        } else if (t.isDistillable()) {
+            openTab(t.getDistilledUrl(), false, true, false, t);
+        }
+    }
+
     @Override
     public void findOnPage() {
         getCurrentTopWebView().showFindDialog(null, true);
diff --git a/src/com/android/browser/NavTabView.java b/src/com/android/browser/NavTabView.java
index 0fc8dfd..c79dafc 100644
--- a/src/com/android/browser/NavTabView.java
+++ b/src/com/android/browser/NavTabView.java
@@ -90,6 +90,8 @@
         }
         if (mTab.isSnapshot()) {
             setTitleIcon(R.drawable.ic_suggest_history_normal);
+        } else  if (mTab.isDistilled()) {
+            setTitleIcon(R.drawable.ic_deco_reader_mode_normal);
         } else if (mTab.isPrivateBrowsingEnabled()) {
             mContent.setBackgroundResource(R.drawable.nav_tab_title_incognito);
             mTitle.setTextColor(getResources().getColor(R.color.white));
diff --git a/src/com/android/browser/SnapshotBar.java b/src/com/android/browser/SnapshotBar.java
index d17a3ac..29f2a90 100644
--- a/src/com/android/browser/SnapshotBar.java
+++ b/src/com/android/browser/SnapshotBar.java
@@ -47,6 +47,8 @@
     private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY;
 
     private ImageView mFavicon;
+    private ImageView mSnapshoticon;
+    private ImageView mReadericon;
     private TextView mDate;
     private TextView mTitle;
     private View mBookmarks;
@@ -107,6 +109,8 @@
     protected void onFinishInflate() {
         super.onFinishInflate();
         mFavicon = (ImageView) findViewById(R.id.favicon);
+        mSnapshoticon = (ImageView) findViewById(R.id.snapshot_icon);
+        mReadericon = (ImageView) findViewById(R.id.reader_icon);
         mDate = (TextView) findViewById(R.id.date);
         mTitle = (TextView) findViewById(R.id.title);
         mBookmarks = findViewById(R.id.all_btn);
@@ -234,4 +238,30 @@
         return mIsAnimating;
     }
 
+    public void setTitle(String title) {
+        mTitle.setText(title);
+    }
+
+    public void setDate(String date) {
+        mDate.setText(date);
+    }
+
+    public void setSnapshoticonVisibility(int visibility) {
+        if (mSnapshoticon.getVisibility() != visibility) {
+            mSnapshoticon.setVisibility(visibility);
+        }
+    }
+
+    public void setReadericonVisibility(int visibility) {
+        if (mReadericon.getVisibility() != visibility) {
+            mReadericon.setVisibility(visibility);
+        }
+    }
+
+    public void setFaviconVisibility(int visibility) {
+        if (mFavicon.getVisibility() != visibility) {
+            mFavicon.setVisibility(visibility);
+        }
+    }
+
 }
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index ef4c380..4b361a1 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -76,6 +76,7 @@
 import org.codeaurora.swe.WebView.CreateWindowParams;
 import org.codeaurora.swe.WebViewClient;
 import org.codeaurora.swe.util.Observable;
+import org.codeaurora.swe.DomDistillerUtils;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -206,6 +207,8 @@
         return mTabHistoryUpdateObservable;
     }
 
+    // dertermines if the tab contains a disllable page
+    private boolean mIsDistillable = false;
 
     private static synchronized Bitmap getDefaultFavicon(Context context) {
         if (sDefaultFavicon == null) {
@@ -315,6 +318,7 @@
 
         @Override
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
+            setIsDistillable(false);
             mInPageLoad = true;
             mPageFinished = false;
             mFirstVisualPixelPainted = false;
@@ -1716,6 +1720,7 @@
 
     protected void onPageFinished() {
         mPageFinished = true;
+        isDistillable();
     }
 
     public boolean getPageFinishedStatus() {
@@ -2127,4 +2132,67 @@
             setSecurityState(SecurityState.SECURITY_STATE_MIXED);
         }
     }
+
+    // dertermines if the tab contains a dislled page
+    public boolean isDistilled() {
+        if (!BrowserCommandLine.hasSwitch("reader-mode")) {
+            return false;
+        }
+        try {
+            return DomDistillerUtils.isUrlDistilled(getUrl());
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    //determines if the tab contains a distillable page
+    public boolean isDistillable() {
+        if (!BrowserCommandLine.hasSwitch("reader-mode")) {
+            mIsDistillable = false;
+            return mIsDistillable;
+        }
+        final ValueCallback<String> onIsDistillable =  new ValueCallback<String>() {
+            @Override
+            public void onReceiveValue(String str) {
+                mIsDistillable = Boolean.parseBoolean(str);
+            }
+        };
+
+        if (isDistilled()) {
+            mIsDistillable = true;
+            return mIsDistillable;
+        }
+
+        try {
+            DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
+        } catch (Exception e) {
+            mIsDistillable = false;
+        }
+
+        return mIsDistillable;
+    }
+
+    // Function that sets the mIsDistillable variable
+    public void setIsDistillable(boolean value) {
+        if (!BrowserCommandLine.hasSwitch("reader-mode")) {
+            mIsDistillable = false;
+        }
+        mIsDistillable = value;
+    }
+
+    // Function that returns the distilled url of the current url
+    public String getDistilledUrl() {
+        if (getUrl() != null) {
+            return DomDistillerUtils.getDistilledUrl(getUrl());
+        }
+        return new String();
+    }
+
+    // function that returns the non-distilled version of the current url
+    public String getNonDistilledUrl() {
+        if (getUrl() != null) {
+            return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
+        }
+        return new String();
+    }
 }
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 357da3c..3c2b5f1 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -387,10 +387,21 @@
             mSnapshotBar.onTabDataChanged(tab);
         }
 
-        if (tab.isSnapshot()) {
+        if (tab.isSnapshot() || tab.isDistilled()) {
             inflateSnapshotBar();
             mSnapshotBar.setVisibility(VISIBLE);
             mNavBar.setVisibility(GONE);
+            if  (tab.isDistilled()) {
+                mSnapshotBar.setTitle(tab.getWebView().getTitle());
+                mSnapshotBar.setDate(tab.getNonDistilledUrl());
+                mSnapshotBar.setSnapshoticonVisibility(View.GONE);
+                mSnapshotBar.setFaviconVisibility(View.GONE);
+                mSnapshotBar.setReadericonVisibility(View.VISIBLE);
+            } else {
+                mSnapshotBar.setSnapshoticonVisibility(View.VISIBLE);
+                mSnapshotBar.setFaviconVisibility(View.VISIBLE);
+                mSnapshotBar.setReadericonVisibility(View.GONE);
+            }
         } else {
             if (mSnapshotBar != null) {
                 mSnapshotBar.setVisibility(GONE);