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/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.