Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| index 9e195bcd6030e0ed6282d4740642200a147ccbf3..5316c38d9fd27b42547b6638b21f2c596685845d 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtils.java |
| @@ -4,17 +4,25 @@ |
| package org.chromium.chrome.browser.offlinepages; |
| +import android.app.Activity; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentFilter; |
| +import android.graphics.Bitmap; |
| +import android.net.Uri; |
| +import android.os.AsyncTask; |
| import android.os.BatteryManager; |
| import android.os.Environment; |
| +import android.webkit.MimeTypeMap; |
| +import org.chromium.base.Callback; |
| +import org.chromium.base.ContentUriUtils; |
| import org.chromium.base.Log; |
| import org.chromium.base.metrics.RecordHistogram; |
| import org.chromium.base.metrics.RecordUserAction; |
| import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.ChromeActivity; |
| +import org.chromium.chrome.browser.share.ShareHelper; |
| import org.chromium.chrome.browser.snackbar.Snackbar; |
| import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| @@ -27,6 +35,11 @@ import org.chromium.net.ConnectionType; |
| import org.chromium.net.NetworkChangeNotifier; |
| import org.chromium.ui.base.PageTransition; |
| +import java.io.File; |
| +import java.io.FileInputStream; |
| +import java.io.FileOutputStream; |
| +import java.io.IOException; |
| +import java.nio.channels.FileChannel; |
| import java.util.concurrent.TimeUnit; |
| /** |
| @@ -238,6 +251,104 @@ public class OfflinePageUtils { |
| TimeUnit.MILLISECONDS); |
| } |
| + /** |
| + * Share saved offline page. |
| + */ |
| + public static void shareOfflinePage(final boolean shareDirectly, final Activity mainActivity, |
| + final String onlineUrl, final Bitmap bitmap, final Context mContext, |
| + final Tab currentTab) { |
| + final String offlineUrl = currentTab.getUrl(); |
| + final String title = currentTab.getTitle(); |
| + OfflinePageBridge offlinePageBridge = |
| + OfflinePageBridge.getForProfile(currentTab.getProfile()); |
|
dewittj
2016/06/28 16:45:25
This needs a null check, because for certain tabs
Vivian
2016/07/27 23:44:10
Done.
|
| + offlinePageBridge.getPageByOfflineUrl(offlineUrl, new Callback<OfflinePageItem>() { |
| + @Override |
| + public void onResult(OfflinePageItem item) { |
| + if (item != null) { |
| + String offlineFilePath = item.getFilePath(); |
| + // ShareHelper.shareOfflinePage(shareDirectly, |
|
dewittj
2016/06/28 16:45:25
nit: remove commented out code.
Vivian
2016/07/27 23:44:10
Done.
|
| + // mainActivity, title, |
| + // offlineFilePath, mContext); |
| + preparingForShare(shareDirectly, mainActivity, title, onlineUrl, bitmap, |
| + offlineFilePath, mContext); |
| + } |
| + } |
| + }); |
| + } |
| + |
| + public static void preparingForShare(final boolean shareDirectly, final Activity activity, |
| + final String title, final String onlineUrl, final Bitmap bitmap, |
| + final String offlineUrl, final Context context) { |
| + new AsyncTask<Void, Void, File>() { |
| + @Override |
| + protected File doInBackground(Void... params) { |
| + File offlinePageOriginal = new File(offlineUrl); |
| + File shareableDir = new File(context.getFilesDir(), "offline-pages"); |
| + Log.d(TAG, "shareableDir: " + shareableDir.getAbsolutePath()); |
| + boolean success = true; |
| + if (!shareableDir.exists()) { |
| + success = shareableDir.mkdir(); |
| + } |
| + if (success) { |
| + File offlinePageShareable = |
| + new File(shareableDir, offlinePageOriginal.getName()); |
| + |
| + if (!offlinePageShareable.exists()) { |
| + try { |
| + copyToExternal(offlinePageOriginal, offlinePageShareable); |
|
dewittj
2016/06/28 16:45:25
When does this new file get cleaned up?
Vivian
2016/07/27 23:44:10
Added code for file cleaning up. Files are copied
|
| + } catch (IOException e) { |
| + Log.e(TAG, "failed to copy the file: " + offlinePageOriginal.getName()); |
| + } |
| + } |
| + return offlinePageShareable; |
| + } else { |
| + Log.e(TAG, "Unable to create subdirectory in shareable directory"); |
| + } |
| + return null; |
| + } |
| + |
| + @Override |
| + protected void onPostExecute(File offlinePageShareable) { |
| + if (offlinePageShareable == null) return; |
|
dewittj
2016/06/28 16:45:25
If this has an error, how does the user know what
Vivian
2016/07/27 23:44:10
Seems like the current sharing features (sharing s
|
| + try { |
| + Uri offlineUri = |
| + ContentUriUtils.getContentUriFromFile(context, offlinePageShareable); |
| + Log.d(TAG, "Uri path: " + offlineUri.getEncodedPath()); |
| + Log.d(TAG, "Uri authority: " + offlineUri.getEncodedAuthority()); |
| + Log.d(TAG, "Uri string: " + offlineUri.toString()); |
| + Log.d(TAG, "file mime type: " |
| + + getMimeType(offlinePageShareable.getAbsolutePath())); |
| + ShareHelper.share( |
| + shareDirectly, activity, title, onlineUrl, offlineUri, bitmap); |
| + } catch (IllegalArgumentException e) { |
| + Log.e(TAG, |
| + "The selected file can't be shared: " + offlinePageShareable.getName()); |
| + } |
| + } |
| + }.execute(); |
| + } |
| + |
| + public static String getMimeType(String url) { |
| + String type = null; |
| + String extension = ".mhtml"; |
| + if (extension != null) { |
| + type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); |
| + } |
| + return type; |
| + } |
| + |
| + public static void copyToExternal(File src, File dst) throws IOException { |
|
dewittj
2016/06/28 16:45:25
This should be renamed, since we're not copying ex
Vivian
2016/07/27 23:44:10
I didn't find many useful discussion on symbolic l
|
| + 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; |
| + } |
| + |
| private static boolean isPowerConnected(Intent batteryStatus) { |
| int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); |
| boolean isConnected = (status == BatteryManager.BATTERY_STATUS_CHARGING |