Browser: add wap2estore feature

Add this for carrier. If click a hyperlink starting
with "estore:", will go to estore app when installed or go
to download client webpage when uninstalled. If input url
directly starting with "estore:", will be the same behavior
as above.

CRs-Fixed: 516742

Change-Id: I17988bac9e40dbdd120693bfbaacd02e3383d411
diff --git a/src/com/android/browser/NavigationBarBase.java b/src/com/android/browser/NavigationBarBase.java
index 7f100ff..1963564 100644
--- a/src/com/android/browser/NavigationBarBase.java
+++ b/src/com/android/browser/NavigationBarBase.java
@@ -16,14 +16,18 @@
 package com.android.browser;
 
 import android.app.SearchManager;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
+import android.net.Uri;
 import android.os.Bundle;
+import android.os.SystemProperties;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -31,13 +35,18 @@
 import android.webkit.WebView;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.Toast;
 
 import com.android.browser.UrlInputView.UrlInputListener;
 
+import java.io.UnsupportedEncodingException;
+
 public class NavigationBarBase extends LinearLayout implements
         OnClickListener, UrlInputListener, OnFocusChangeListener,
         TextWatcher {
 
+    private final static String TAG = "NavigationBarBase";
+
     protected BaseUi mBaseUi;
     protected TitleBar mTitleBar;
     protected UiController mUiController;
@@ -158,7 +167,15 @@
     public void onAction(String text, String extra, String source) {
         stopEditingUrl();
         if (UrlInputView.TYPED.equals(source)) {
-            String url = UrlUtils.smartUrlFilter(text, false);
+            String url = null;
+            boolean wap2estore = SystemProperties.getBoolean(
+                    "persist.env.browser.wap2estore", false);
+            if (wap2estore && isEstoreTypeUrl(text)) {
+                url = text;
+            } else {
+                url = UrlUtils.smartUrlFilter(text, false);
+            }
+
             Tab t = mBaseUi.getActiveTab();
             // Only shortcut javascript URIs for now, as there is special
             // logic in UrlHandler for other schemas
@@ -167,6 +184,13 @@
                 setDisplayTitle(text);
                 return;
             }
+
+            // add for carrier wap2estore feature
+            if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
+                handleEstoreTypeUrl(url);
+                setDisplayTitle(text);
+                return;
+            }
         }
         Intent i = new Intent();
         String action = Intent.ACTION_SEARCH;
@@ -184,6 +208,46 @@
         setDisplayTitle(text);
     }
 
+    private boolean isEstoreTypeUrl(String url) {
+        String utf8Url = null;
+        try {
+            utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            Log.e(TAG, "err " + e);
+        }
+        if (utf8Url != null && utf8Url.startsWith("estore:")) {
+            return true;
+        }
+        return false;
+    }
+
+    private void handleEstoreTypeUrl(String url) {
+        String utf8Url = null, finalUrl = null;
+        try {
+            utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            Log.e(TAG, "err " + e);
+        }
+        if (utf8Url != null) {
+            finalUrl = utf8Url;
+        } else {
+            finalUrl = url;
+        }
+        if (finalUrl.replaceFirst("estore:", "").length() > 256) {
+            Toast.makeText(mContext, R.string.estore_url_warning, Toast.LENGTH_LONG).show();
+            return;
+        }
+        Intent intent = new Intent(Intent.ACTION_VIEW);
+        intent.setData(Uri.parse(finalUrl));
+        try {
+            mContext.startActivity(intent);
+        } catch (ActivityNotFoundException ex) {
+            String downloadUrl = mContext.getResources().getString(R.string.estore_homepage);
+            mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
+            Toast.makeText(mContext, R.string.download_estore_app, Toast.LENGTH_LONG).show();
+        }
+    }
+
     @Override
     public void onDismiss() {
         final Tab currentTab = mBaseUi.getActiveTab();
diff --git a/src/com/android/browser/UrlHandler.java b/src/com/android/browser/UrlHandler.java
index 167d410..37e70f8 100644
--- a/src/com/android/browser/UrlHandler.java
+++ b/src/com/android/browser/UrlHandler.java
@@ -25,10 +25,13 @@
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.AsyncTask;
+import android.os.SystemProperties;
 import android.provider.Browser;
 import android.util.Log;
 import android.webkit.WebView;
+import android.widget.Toast;
 
+import java.io.UnsupportedEncodingException;
 import java.net.URISyntaxException;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -38,6 +41,7 @@
  */
 public class UrlHandler {
 
+    private final static String TAG = "UrlHandler";
     static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider";
     static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
 
@@ -115,6 +119,14 @@
             }
         }
 
+        // add for carrier wap2estore feature
+        boolean wap2estore = SystemProperties.getBoolean(
+                "persist.env.browser.wap2estore", false);
+        if (wap2estore && isEstoreTypeUrl(url)) {
+            handleEstoreTypeUrl(url);
+            return true;
+        }
+
         if (startActivityForUrl(tab, url)) {
             return true;
         }
@@ -126,6 +138,46 @@
         return false;
     }
 
+    private boolean isEstoreTypeUrl(String url) {
+        String utf8Url = null;
+        try {
+            utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            Log.e(TAG, "err " + e);
+        }
+        if (utf8Url != null && utf8Url.startsWith("estore:")) {
+            return true;
+        }
+        return false;
+    }
+
+    private void handleEstoreTypeUrl(String url) {
+        String utf8Url = null, finalUrl = null;
+        try {
+            utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            Log.e(TAG, "err " + e);
+        }
+        if (utf8Url != null) {
+            finalUrl = utf8Url;
+        } else {
+            finalUrl = url;
+        }
+        if (finalUrl.replaceFirst("estore:", "").length() > 256) {
+            Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show();
+            return;
+        }
+        Intent intent = new Intent(Intent.ACTION_VIEW);
+        intent.setData(Uri.parse(finalUrl));
+        try {
+            mActivity.startActivity(intent);
+        } catch (ActivityNotFoundException ex) {
+            String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage);
+            mController.loadUrl(mController.getCurrentTab(), downloadUrl);
+            Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show();
+        }
+    }
+
     boolean startActivityForUrl(Tab tab, String url) {
       Intent intent;
       // perform generic parsing of the URI to turn it into an Intent.