Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | */ |
| 30 | package com.android.browser; |
| 31 | |
| 32 | import android.app.Activity; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 33 | import android.content.Context; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 34 | import android.content.Intent; |
| 35 | import android.content.pm.ActivityInfo; |
| 36 | import android.content.pm.ResolveInfo; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 37 | import android.graphics.Bitmap; |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
| 39 | import android.os.AsyncTask; |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 40 | import android.os.Environment; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.List; |
| 43 | import java.util.Collections; |
| 44 | |
| 45 | import android.util.Log; |
| 46 | |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 47 | import org.chromium.base.ContentUriUtils; |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 48 | |
| 49 | import java.io.File; |
| 50 | import java.io.FileOutputStream; |
| 51 | import java.io.IOException; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 52 | |
jrizzoli | 608c7a9 | 2016-04-26 15:16:59 +0200 | [diff] [blame] | 53 | public class ShareDialog { |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 54 | private Activity activity = null; |
| 55 | public String title = null; |
| 56 | public String url = null; |
| 57 | public Bitmap favicon = null; |
| 58 | public Bitmap screenshot = null; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 59 | public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot"; |
| 60 | public final static String EXTRA_SHARE_FAVICON = "share_favicon"; |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 61 | private static final String SCREENSHOT_DIRECTORY_NAME = "screenshot_share"; |
| 62 | private static final int MAX_SCREENSHOT_COUNT = 10; |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 63 | public static final String EXTERNAL_IMAGE_FILE_PATH = "browser-images"; |
| 64 | public static final String IMAGE_FILE_PATH = "images"; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 65 | |
| 66 | public ShareDialog (Activity activity, String title, String url, Bitmap favicon, Bitmap screenshot) { |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 67 | this.activity = activity; |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 68 | this.title = title; |
| 69 | this.url = url; |
| 70 | this.favicon = favicon; |
| 71 | this.screenshot = screenshot; |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 72 | |
| 73 | ContentUriUtils.setFileProviderUtil(new FileProviderHelper()); |
| 74 | trimScreenshots(); |
| 75 | } |
| 76 | |
| 77 | private void trimScreenshots() { |
| 78 | try { |
| 79 | File directory = getScreenshotDir(); |
| 80 | if (directory.list() != null && directory.list().length >= MAX_SCREENSHOT_COUNT) { |
| 81 | clearSharedScreenshots(); |
| 82 | } |
| 83 | } catch (IOException e) { |
| 84 | e.printStackTrace(); |
| 85 | clearSharedScreenshots(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private File getScreenshotDir() throws IOException { |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 90 | File baseDir = getDirectoryForImageCapture(activity); |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 91 | return new File(baseDir, SCREENSHOT_DIRECTORY_NAME); |
| 92 | } |
| 93 | |
| 94 | private void deleteScreenshotFiles(File file) { |
| 95 | if (!file.exists()) return; |
| 96 | if (file.isDirectory()) { |
| 97 | for (File f : file.listFiles()) deleteScreenshotFiles(f); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Clears all shared screenshot files. |
| 103 | */ |
| 104 | private void clearSharedScreenshots() { |
| 105 | AsyncTask.execute(new Runnable() { |
| 106 | @Override |
| 107 | public void run() { |
| 108 | try { |
| 109 | File dir = getScreenshotDir(); |
| 110 | deleteScreenshotFiles(dir); |
| 111 | } catch (IOException ie) { |
| 112 | // Ignore exception. |
| 113 | } |
| 114 | } |
| 115 | }); |
Axesh R. Ajmera | 34d3f14 | 2014-06-30 20:05:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 118 | public Uri getShareBitmapUri(Bitmap screenshot) { |
| 119 | Uri uri = null; |
| 120 | if (screenshot != null) { |
| 121 | FileOutputStream fOut = null; |
| 122 | try { |
| 123 | File path = getScreenshotDir(); |
| 124 | if (path.exists() || path.mkdir()) { |
| 125 | File saveFile = File.createTempFile( |
| 126 | String.valueOf(System.currentTimeMillis()), ".jpg", path); |
| 127 | fOut = new FileOutputStream(saveFile); |
| 128 | screenshot.compress(Bitmap.CompressFormat.JPEG, 90, fOut); |
| 129 | fOut.flush(); |
| 130 | fOut.close(); |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 131 | uri = getUriForImageCaptureFile(activity, saveFile); |
Danesh M | 22398a7 | 2015-10-07 13:08:21 -0700 | [diff] [blame] | 132 | } |
| 133 | } catch (IOException ie) { |
| 134 | if (fOut != null) { |
| 135 | try { |
| 136 | fOut.close(); |
| 137 | } catch (IOException e) { |
| 138 | // Ignore exception. |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return uri; |
| 144 | } |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 145 | |
| 146 | public static Uri getUriForImageCaptureFile(Context context, File file) { |
jrizzoli | 608c7a9 | 2016-04-26 15:16:59 +0200 | [diff] [blame] | 147 | return ContentUriUtils.getContentUriFromFile(context, file); |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | public static File getDirectoryForImageCapture(Context context) throws IOException { |
jrizzoli | 608c7a9 | 2016-04-26 15:16:59 +0200 | [diff] [blame] | 151 | File path = new File(context.getFilesDir(), IMAGE_FILE_PATH); |
| 152 | if (!path.exists() && !path.mkdir()) { |
| 153 | throw new IOException("Folder cannot be created."); |
Martin Brabham | 1a8e747 | 2016-01-21 12:47:43 -0500 | [diff] [blame] | 154 | } |
| 155 | return path; |
| 156 | } |
| 157 | |
jrizzoli | 608c7a9 | 2016-04-26 15:16:59 +0200 | [diff] [blame] | 158 | public void sharePage() { |
| 159 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 160 | intent.setType("text/plain"); |
| 161 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | |
| 162 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP | |
| 163 | Intent.FLAG_ACTIVITY_NEW_DOCUMENT | |
| 164 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 165 | intent.putExtra(Intent.EXTRA_TEXT, url); |
| 166 | intent.putExtra(Intent.EXTRA_SUBJECT, title); |
| 167 | intent.putExtra(EXTRA_SHARE_FAVICON, favicon); |
| 168 | intent.putExtra(Intent.EXTRA_STREAM, getShareBitmapUri(screenshot)); |
| 169 | |
| 170 | activity.startActivity(Intent.createChooser(intent, |
| 171 | activity.getString(R.string.choosertitle_sharevia))); |
| 172 | } |
| 173 | |
Tarun Nainani | 82ca1bd | 2015-07-16 14:31:17 -0700 | [diff] [blame] | 174 | } |