Enable file upload, WLAN alert and estore URI

 - Implement file upload feature.
     1. Allow selecting a file to upload from device
     2. Port DRM check's for carrier.

 - Bring in wap2estore changes to browser from 3.7.1
      This feature contains two points:
        1. When user input text starting with "estore:" in url bar,
           will go to estore app when installed or go to download
           client webpage when uninstalled.
        2. When user click one hyperlink whose url starts with "estore"
           in webpage, will be the same behavior as above case.

 - Alert user when WLAN is not available and request using mobile data

Change-Id: I2b93d82c19ea13f36889c3171ef695187d77a12b
diff --git a/src/com/android/browser/UploadHandler.java b/src/com/android/browser/UploadHandler.java
index 349f67f..51ca7f9 100644
--- a/src/com/android/browser/UploadHandler.java
+++ b/src/com/android/browser/UploadHandler.java
@@ -18,12 +18,14 @@
 
 import android.app.Activity;
 import android.content.ActivityNotFoundException;
+import android.content.ContentResolver;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Environment;
 import android.provider.MediaStore;
 import android.webkit.ValueCallback;
+import android.util.Log;
 import android.widget.Toast;
 
 import com.android.browser.R;
@@ -37,10 +39,12 @@
  */
 public class UploadHandler {
 
+    private static final String TAG = "UploadHandler";
     /*
      * The Object used to inform the WebView of the file to upload.
      */
     private ValueCallback<Uri> mUploadMessage;
+    private ValueCallback<String[]> mUploadFilePaths;
     private String mCameraFilePath;
 
     private boolean mHandled;
@@ -60,38 +64,6 @@
         return mHandled;
     }
 
-    private boolean isDrmFileUpload(Uri uri) {
-        if (uri == null) return false;
-
-        String path = null;
-        String scheme = uri.getScheme();
-        if ("content".equals(scheme)) {
-            String[] proj = null;
-            if (uri.toString().contains("/images/")) {
-                proj = new String[]{MediaStore.Images.Media.DATA};
-            } else if (uri.toString().contains("/audio/")) {
-                proj = new String[]{MediaStore.Audio.Media.DATA};
-            } else if (uri.toString().contains("/video/")) {
-                proj = new String[]{MediaStore.Video.Media.DATA};
-            }
-            Cursor cursor = mController.getActivity().managedQuery(uri, proj, null, null, null);
-            if (cursor != null && cursor.moveToFirst() && proj != null) {
-                path = cursor.getString(0);
-            }
-        } else if ("file".equals(scheme)) {
-            path = uri.getPath();
-        }
-        if (path != null) {
-            if (path.endsWith(".fl") || path.endsWith(".dm")
-                    || path.endsWith(".dcf") || path.endsWith(".dr") || path.endsWith(".dd")) {
-                Toast.makeText(mController.getContext(), R.string.drm_file_unsupported,
-                        Toast.LENGTH_LONG).show();
-                return true;
-            }
-        }
-        return false;
-    }
-
     void onResult(int resultCode, Intent intent) {
 
         if (resultCode == Activity.RESULT_CANCELED && mCaughtActivityNotFoundException) {
@@ -121,16 +93,59 @@
             }
         }
 
-        // add unsupport uploading drm file feature for carrier.
-        Object[] params  = {new String("persist.env.browser.drmupload"),
-                            Boolean.valueOf(false)};
-        Class[] type = new Class[] {String.class, boolean.class};
-        Boolean drmUpload = (Boolean) ReflectHelper.invokeMethod(
-                      "android.os.SystemProperties", "getBoolean", type, params);
-        if (drmUpload && isDrmFileUpload(result)) {
-            mUploadMessage.onReceiveValue(null);
-        } else {
-            mUploadMessage.onReceiveValue(result);
+        // try to get local file path from uri
+        boolean hasGoodFilePath = false;
+        String filePath = null;
+        if (result != null) {
+            String scheme = result.getScheme();
+            if ("file".equals(scheme)) {
+                filePath = result.getPath();
+                hasGoodFilePath = filePath != null && !filePath.isEmpty();
+            } else if ("content".equals(scheme)) {
+                ContentResolver cr = mController.getActivity().getContentResolver();
+                String[] projection = {"_data"};
+                Cursor c = cr.query(result, projection, null, null, null);
+                try {
+                    if (c != null && c.moveToFirst()) {
+                        filePath = c.getString(0);
+                        hasGoodFilePath = filePath != null && !filePath.isEmpty();
+                    }
+                } finally {
+                    if (c != null) {
+                        c.close();
+                    }
+                }
+            }
+        }
+
+        // Add for carrier feature - prevent uploading DRM type files.
+        String browserRes = mController.getActivity().getResources().
+                getString(R.string.config_carrier_resource);
+        boolean isDRMFileType = false;
+        if ("ct".equals(browserRes) && filePath != null
+                && (filePath.endsWith(".fl") || filePath.endsWith(".dm")
+                || filePath.endsWith(".dcf") || filePath.endsWith(".dr")
+                || filePath.endsWith(".dd"))) {
+            isDRMFileType = true;
+            Toast.makeText(mController.getContext(), R.string.drm_file_unsupported,
+                    Toast.LENGTH_LONG).show();
+        }
+
+        if (mUploadMessage != null) {
+            if (!isDRMFileType) {
+                mUploadMessage.onReceiveValue(result);
+            } else {
+                mUploadMessage.onReceiveValue(null);
+            }
+        }
+
+        if (mUploadFilePaths != null) {
+            if (hasGoodFilePath && !isDRMFileType) {
+                Log.d(TAG, "upload file path:" + filePath);
+                mUploadFilePaths.onReceiveValue(new String[]{filePath});
+            } else {
+                mUploadFilePaths.onReceiveValue(null);
+            }
         }
 
         mHandled = true;
@@ -238,6 +253,80 @@
         startActivity(createDefaultOpenableIntent());
     }
 
+    void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
+            boolean capture) {
+
+        final String imageMimeType = "image/*";
+        final String videoMimeType = "video/*";
+        final String audioMimeType = "audio/*";
+
+        if (mUploadFilePaths != null) {
+            // Already a file picker operation in progress.
+            return;
+        }
+
+        mUploadFilePaths = uploadFilePaths;
+
+        // Parse the accept type.
+        String params[] = acceptTypes.split(";");
+        String mimeType = params[0];
+
+        // Ensure it is not still set from a previous upload.
+        mCameraFilePath = null;
+
+        if (mimeType.equals(imageMimeType)) {
+            if (capture) {
+                // Specified 'image/*' and capture=true, so go ahead and launch the
+                // camera directly.
+                startActivity(createCameraIntent());
+                return;
+            } else {
+                // Specified just 'image/*', capture=false, or no capture value.
+                // In all these cases we show a traditional picker filetered on accept type
+                // so launch an intent for both the Camera and image/* OPENABLE.
+                Intent chooser = createChooserIntent(createCameraIntent());
+                chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(imageMimeType));
+                startActivity(chooser);
+                return;
+            }
+        } else if (mimeType.equals(videoMimeType)) {
+            if (capture) {
+                // Specified 'video/*' and capture=true, so go ahead and launch the
+                // camcorder directly.
+                startActivity(createCamcorderIntent());
+                return;
+           } else {
+                // Specified just 'video/*', capture=false, or no capture value.
+                // In all these cases we show an intent for the traditional file picker, filtered
+                // on accept type so launch an intent for both camcorder and video/* OPENABLE.
+                Intent chooser = createChooserIntent(createCamcorderIntent());
+                chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(videoMimeType));
+                startActivity(chooser);
+                return;
+            }
+        } else if (mimeType.equals(audioMimeType)) {
+            if (capture) {
+                // Specified 'audio/*' and capture=true, so go ahead and launch the sound
+                // recorder.
+                startActivity(createSoundRecorderIntent());
+                return;
+            } else {
+                // Specified just 'audio/*',  capture=false, or no capture value.
+                // In all these cases so go ahead and launch an intent for both the sound
+                // recorder and audio/* OPENABLE.
+                Intent chooser = createChooserIntent(createSoundRecorderIntent());
+                chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(audioMimeType));
+                startActivity(chooser);
+                return;
+            }
+        }
+
+        // No special handling based on the accept type was necessary, so trigger the default
+        // file upload chooser.
+        startActivity(createDefaultOpenableIntent());
+    }
+
+
     private void startActivity(Intent intent) {
         try {
             mController.getActivity().startActivityForResult(intent, Controller.FILE_SELECTED);