Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
| index 9f0590a64eaaa24ec6345cea8b28e4da7fa34e56..e1bdefa49b6d7a5ead1c9233d1f9db2156913c21 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/share/ShareHelper.java |
| @@ -24,6 +24,8 @@ import android.graphics.drawable.Drawable; |
| import android.net.Uri; |
| import android.os.AsyncTask; |
| import android.os.Build; |
| +import android.os.Environment; |
| +import android.support.v4.content.FileProvider; |
| import android.support.v7.app.AlertDialog; |
| import android.util.Pair; |
| import android.view.MenuItem; |
| @@ -44,8 +46,10 @@ import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
| import org.chromium.ui.UiUtils; |
| import java.io.File; |
| +import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| +import java.nio.channels.FileChannel; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.concurrent.ExecutionException; |
| @@ -190,6 +194,89 @@ public class ShareHelper { |
| } |
| } |
| + /* |
| + * This is a tryout function that only called when user shares offline pages |
| + * */ |
| + public static void shareOfflinePage(final boolean shareDirectly, final Activity activity, |
|
dewittj
2016/06/23 01:26:49
There seems to be a lot of code here that is very
|
| + final String title, final String offlineUrl, final Context context) { |
| + // try copy file to external directory |
| + new AsyncTask<Void, Void, File>() { |
| + @Override |
| + protected File doInBackground(Void... params) { |
| + File offlinePageInternal = new File(offlineUrl); |
| + File externalDataDir = |
| + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); |
| + File externalOfflinePageDir = |
| + new File(externalDataDir.getAbsolutePath() + "/OfflinePages"); |
| + |
| + boolean success = true; |
| + if (!externalOfflinePageDir.exists()) { |
| + success = externalOfflinePageDir.mkdir(); |
| + } |
| + if (success) { |
| + File offlinePageExternal = new File(externalOfflinePageDir.getAbsolutePath() |
| + + File.separator + offlinePageInternal.getName()); |
| + |
| + if (!offlinePageExternal.exists()) { |
| + try { |
| + copyToExternal(offlinePageInternal, offlinePageExternal); |
|
dewittj
2016/06/23 01:26:49
I would prefer if we copied to internal if possibl
|
| + } catch (IOException e) { |
| + Log.e(TAG, "failed to copy the file: " + offlinePageInternal.getName()); |
| + } |
| + } |
| + return offlinePageExternal; |
| + |
| + } else { |
| + Log.e(TAG, "Unable to create subdirectory in external storage"); |
| + } |
| + return null; |
| + } |
| + |
| + @Override |
| + protected void onPostExecute(File offlinePageExternal) { |
| + if (offlinePageExternal == null) return; |
| + // sharing from external file |
| + try { |
| + Uri offlinePageUri = FileProvider.getUriForFile( |
| + context, "org.chromium.chrome.FileProvider", offlinePageExternal); |
| + Intent chooserIntent = |
| + Intent.createChooser(getShareOfflinePageIntent(offlinePageUri), |
| + activity.getString(R.string.share_link_chooser_title)); |
| + activity.startActivity(chooserIntent); |
| + } catch (IllegalArgumentException e) { |
| + Log.e(TAG, |
| + "The selected file can't be shared: " + offlinePageExternal.getName()); |
| + } |
| + } |
| + }.execute(); |
| + |
| + return; |
| + } |
| + |
| + private static Intent getShareOfflinePageIntent(Uri offlinePageUri) { |
| + Intent intent = new Intent(Intent.ACTION_SEND); |
| + intent.addFlags(ApiCompatibilityUtils.getActivityNewDocumentFlag()); |
| + intent.setType("multipart/related"); |
| + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| + intent.putExtra(Intent.EXTRA_STREAM, offlinePageUri); |
| + return intent; |
| + } |
| + |
| + /* |
| + * This is a helper function that copies an offline page file from internal storage to external |
| + * */ |
| + public static void copyToExternal(File src, File dst) throws IOException { |
|
dewittj
2016/06/23 01:26:48
I'm surprised that this is the first time in Chrom
|
| + FileChannel inChannel = new FileInputStream(src).getChannel(); |
| + FileChannel outChannel = new FileOutputStream(dst).getChannel(); |
| + try { |
| + inChannel.transferTo(0, inChannel.size(), outChannel); |
| + } finally { |
| + if (inChannel != null) inChannel.close(); |
| + if (outChannel != null) outChannel.close(); |
| + } |
| + return; |
| + } |
| + |
| /** |
| * Trigger the share action for the given image data. |
| * @param activity The activity used to trigger the share action. |