From 97e2e8d79a3e620bfb049d02bfd18aa8f2be65ef Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Tue, 21 Apr 2015 14:46:30 -0400 Subject: [PATCH] Fix file uploading on Lollipop, clean up the code --- .../browser/lightning/BrowserActivity.java | 116 ++++++++++++++---- .../browser/lightning/BrowserController.java | 7 +- src/acr/browser/lightning/LightningView.java | 23 ++-- src/acr/browser/lightning/Utils.java | 41 ++++++- 4 files changed, 142 insertions(+), 45 deletions(-) diff --git a/src/acr/browser/lightning/BrowserActivity.java b/src/acr/browser/lightning/BrowserActivity.java index e59fd41..33c0766 100644 --- a/src/acr/browser/lightning/BrowserActivity.java +++ b/src/acr/browser/lightning/BrowserActivity.java @@ -31,6 +31,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Browser; +import android.provider.MediaStore; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewCompat; import android.support.v4.widget.DrawerLayout; @@ -104,6 +105,7 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll private ClickHandler mClickHandler; private CustomViewCallback mCustomViewCallback; private ValueCallback mUploadMessage; + private ValueCallback mFilePathCallback; // Context private Activity mActivity; @@ -112,7 +114,7 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll private boolean mSystemBrowser = false, mIsNewIntent = false, mFullScreen, mColorMode, mDarkTheme; private int mOriginalOrientation, mBackgroundColor, mIdGenerator; - private String mSearchText, mUntitledTitle, mHomepage; + private String mSearchText, mUntitledTitle, mHomepage, mCameraPhotoPath; // Storage private HistoryDatabase mHistoryDatabase; @@ -144,13 +146,13 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); + mActionBar = getSupportActionBar(); mPreferences = PreferenceManager.getInstance(); mDarkTheme = mPreferences.getUseDarkTheme() || isIncognito(); mActivity = this; mWebViews.clear(); - mActivity = this; mClickHandler = new ClickHandler(this); mBrowserFrame = (FrameLayout) findViewById(R.id.content_frame); mToolbarLayout = (LinearLayout) findViewById(R.id.toolbar_layout); @@ -161,9 +163,8 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll mProgressBar = (AnimatedProgressBar) findViewById(R.id.progress_view); mNewTab = (RelativeLayout) findViewById(R.id.new_tab_button); mDrawerLeft = (LinearLayout) findViewById(R.id.left_drawer); - mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null); // Drawer - // stutters - // otherwise + // Drawer stutters otherwise + mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerListLeft = (ListView) findViewById(R.id.left_drawer_list); mDrawerRight = (LinearLayout) findViewById(R.id.right_drawer); @@ -173,13 +174,7 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll setNavigationDrawerWidth(); mDrawerLayout.setDrawerListener(new DrawerLocker()); - if (mDarkTheme) { - mWebpageBitmap = BitmapFactory.decodeResource(getResources(), - R.drawable.ic_webpage_dark); - } else { - mWebpageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_webpage); - } - mActionBar = getSupportActionBar(); + mWebpageBitmap = Utils.getWebpageBitmap(getResources(), mDarkTheme); mHomepage = mPreferences.getHomepage(); @@ -195,11 +190,7 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll // set display options of the ActionBar mActionBar.setDisplayShowTitleEnabled(false); - mActionBar.setHomeButtonEnabled(false); mActionBar.setDisplayShowHomeEnabled(false); - mActionBar.setDisplayHomeAsUpEnabled(false); - mActionBar.setDisplayShowTitleEnabled(false); - mActionBar.setDisplayUseLogoEnabled(false); mActionBar.setDisplayShowCustomEnabled(true); mActionBar.setCustomView(R.layout.toolbar_content); @@ -282,10 +273,8 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll }); - // mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END); mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START); - initializePreferences(); initializeTabs(); if (API <= Build.VERSION_CODES.JELLY_BEAN_MR2) { @@ -622,6 +611,9 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll } public void initializePreferences() { + if (mPreferences == null) { + mPreferences = PreferenceManager.getInstance(); + } mFullScreen = mPreferences.getFullScreenEnabled(); mColorMode = mPreferences.getColorModeEnabled(); mColorMode &= !mDarkTheme; @@ -731,7 +723,6 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll if (mDrawerLayout.isDrawerOpen(mDrawerRight)) { mDrawerLayout.closeDrawer(mDrawerRight); } - // mDrawerToggle.syncState(); return true; case R.id.action_back: if (mCurrentView != null) { @@ -1968,15 +1959,90 @@ public class BrowserActivity extends ThemableActivity implements BrowserControll * used to allow uploading into the browser */ protected void onActivityResult(int requestCode, int resultCode, Intent intent) { - if (requestCode == 1) { - if (null == mUploadMessage) { - return; + if (API < Build.VERSION_CODES.LOLLIPOP) { + if (requestCode == 1) { + if (null == mUploadMessage) { + return; + } + Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); + mUploadMessage.onReceiveValue(result); + mUploadMessage = null; + } - Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); - mUploadMessage.onReceiveValue(result); - mUploadMessage = null; + } + + if (requestCode != 1 || mFilePathCallback == null) { + super.onActivityResult(requestCode, resultCode, intent); + return; + } + + Uri[] results = null; + // Check that the response is a good one + if (resultCode == Activity.RESULT_OK) { + if (intent == null) { + // If there is not data, then we may have taken a photo + if (mCameraPhotoPath != null) { + results = new Uri[] { Uri.parse(mCameraPhotoPath) }; + } + } else { + String dataString = intent.getDataString(); + if (dataString != null) { + results = new Uri[] { Uri.parse(dataString) }; + } + } } + + mFilePathCallback.onReceiveValue(results); + mFilePathCallback = null; + return; + } + + @Override + public void showFileChooser(ValueCallback filePathCallback) { + if (mFilePathCallback != null) { + mFilePathCallback.onReceiveValue(null); + } + mFilePathCallback = filePathCallback; + + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { + // Create the File where the photo should go + File photoFile = null; + try { + photoFile = Utils.createImageFile(); + takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); + } catch (IOException ex) { + // Error occurred while creating the File + Log.e(Constants.TAG, "Unable to create Image File", ex); + } + + // Continue only if the File was successfully created + if (photoFile != null) { + mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); + } else { + takePictureIntent = null; + } + } + + Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); + contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); + contentSelectionIntent.setType("image/*"); + + Intent[] intentArray; + if (takePictureIntent != null) { + intentArray = new Intent[] { takePictureIntent }; + } else { + intentArray = new Intent[0]; + } + + Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); + chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); + chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); + + mActivity.startActivityForResult(chooserIntent, 1); } @Override diff --git a/src/acr/browser/lightning/BrowserController.java b/src/acr/browser/lightning/BrowserController.java index 85eb544..e320757 100644 --- a/src/acr/browser/lightning/BrowserController.java +++ b/src/acr/browser/lightning/BrowserController.java @@ -23,7 +23,7 @@ public interface BrowserController { public void openFileChooser(ValueCallback uploadMsg); public void update(); - + public void onLongPress(); public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback); @@ -41,17 +41,18 @@ public interface BrowserController { public void hideActionBar(); public void showActionBar(); - + public void toggleActionBar(); public void longClickPage(String url); public void openBookmarkPage(WebView view); + public void showFileChooser(ValueCallback filePathCallback); public void closeEmptyTab(); public boolean isIncognito(); - + public int getMenu(); } diff --git a/src/acr/browser/lightning/LightningView.java b/src/acr/browser/lightning/LightningView.java index 51fba7e..14bfadb 100644 --- a/src/acr/browser/lightning/LightningView.java +++ b/src/acr/browser/lightning/LightningView.java @@ -20,7 +20,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; @@ -87,13 +86,7 @@ public class LightningView { mTitle = new Title(activity, darkTheme); mAdBlock = AdBlock.getInstance(activity.getApplicationContext()); - if (darkTheme) { - mWebpageBitmap = BitmapFactory.decodeResource(activity.getResources(), - R.drawable.ic_webpage_dark); - } else { - mWebpageBitmap = BitmapFactory.decodeResource(activity.getResources(), - R.drawable.ic_webpage); - } + mWebpageBitmap = Utils.getWebpageBitmap(activity.getResources(), darkTheme); try { mBrowserController = (BrowserController) activity; @@ -933,6 +926,12 @@ public class LightningView { mBrowserController.openFileChooser(uploadMsg); } + public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, + WebChromeClient.FileChooserParams fileChooserParams) { + mBrowserController.showFileChooser(filePathCallback); + return true; + } + @Override public Bitmap getDefaultVideoPoster() { return mBrowserController.getDefaultVideoPoster(); @@ -1002,13 +1001,7 @@ public class LightningView { private Bitmap mDefaultIcon; public Title(Context context, boolean darkTheme) { - if (darkTheme) { - mDefaultIcon = BitmapFactory.decodeResource(context.getResources(), - R.drawable.ic_webpage_dark); - } else { - mDefaultIcon = BitmapFactory.decodeResource(context.getResources(), - R.drawable.ic_webpage); - } + mDefaultIcon = Utils.getWebpageBitmap(context.getResources(), darkTheme); mFavicon = mDefaultIcon; mTitle = mActivity.getString(R.string.action_new_tab); } diff --git a/src/acr/browser/lightning/Utils.java b/src/acr/browser/lightning/Utils.java index 11ef64a..a3caa7c 100644 --- a/src/acr/browser/lightning/Utils.java +++ b/src/acr/browser/lightning/Utils.java @@ -3,6 +3,7 @@ */ package acr.browser.lightning; +import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; @@ -10,8 +11,10 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; +import android.os.Environment; import android.util.DisplayMetrics; import android.util.Log; import android.webkit.URLUtil; @@ -20,7 +23,9 @@ import android.widget.Toast; import java.io.*; import java.net.URI; import java.net.URISyntaxException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; public final class Utils { @@ -71,7 +76,7 @@ public final class Utils { */ public static int convertDpToPixels(int dp) { DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); - return (int) (dp * metrics.density + 0.5f); + return (int) (dp * metrics.density + 0.5f); } public static String getDomainName(String url) { @@ -98,7 +103,7 @@ public final class Utils { else return domain.startsWith("www.") ? domain.substring(4) : domain; } - + public static String getProtocol(String url) { int index = url.indexOf('/'); return url.substring(0, index + 2); @@ -174,4 +179,36 @@ public final class Utils { return paddedBitmap; } + + @SuppressLint("SimpleDateFormat") + public static File createImageFile() throws IOException { + // Create an image file name + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); + String imageFileName = "JPEG_" + timeStamp + "_"; + File storageDir = Environment + .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + File imageFile = File.createTempFile(imageFileName, /* prefix */ + ".jpg", /* suffix */ + storageDir /* directory */ + ); + return imageFile; + } + + public static Bitmap getWebpageBitmap(Resources resources, boolean dark) { + if (dark) { + if (mWebIconDark == null) { + mWebIconDark = BitmapFactory.decodeResource(resources, R.drawable.ic_webpage_dark); + } + return mWebIconDark; + } else { + if (mWebIconLight == null) { + mWebIconLight = BitmapFactory.decodeResource(resources, R.drawable.ic_webpage); + } + return mWebIconLight; + } + } + + private static Bitmap mWebIconLight; + private static Bitmap mWebIconDark; + }