blob: 932568852308c72a8257313441363586f253ef97 [file] [log] [blame]
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -07001/*
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 */
30package com.android.browser;
31
32import android.app.Activity;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070033import android.content.Context;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070034import android.content.Intent;
35import android.content.pm.ActivityInfo;
36import android.content.pm.ResolveInfo;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070037import android.graphics.Bitmap;
Danesh M22398a72015-10-07 13:08:21 -070038import android.net.Uri;
39import android.os.AsyncTask;
Martin Brabham1a8e7472016-01-21 12:47:43 -050040import android.os.Environment;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070041
42import java.util.List;
43import java.util.Collections;
44
45import android.util.Log;
46
Danesh M22398a72015-10-07 13:08:21 -070047import org.chromium.base.ContentUriUtils;
Danesh M22398a72015-10-07 13:08:21 -070048
49import java.io.File;
50import java.io.FileOutputStream;
51import java.io.IOException;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070052
jrizzoli608c7a92016-04-26 15:16:59 +020053public class ShareDialog {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070054 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. Ajmera34d3f142014-06-30 20:05:36 -070059 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
60 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
Danesh M22398a72015-10-07 13:08:21 -070061 private static final String SCREENSHOT_DIRECTORY_NAME = "screenshot_share";
62 private static final int MAX_SCREENSHOT_COUNT = 10;
Martin Brabham1a8e7472016-01-21 12:47:43 -050063 public static final String EXTERNAL_IMAGE_FILE_PATH = "browser-images";
64 public static final String IMAGE_FILE_PATH = "images";
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070065
66 public ShareDialog (Activity activity, String title, String url, Bitmap favicon, Bitmap screenshot) {
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070067 this.activity = activity;
Axesh R. Ajmera34d3f142014-06-30 20:05:36 -070068 this.title = title;
69 this.url = url;
70 this.favicon = favicon;
71 this.screenshot = screenshot;
Danesh M22398a72015-10-07 13:08:21 -070072
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 Brabham1a8e7472016-01-21 12:47:43 -050090 File baseDir = getDirectoryForImageCapture(activity);
Danesh M22398a72015-10-07 13:08:21 -070091 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. Ajmera34d3f142014-06-30 20:05:36 -0700116 }
117
Danesh M22398a72015-10-07 13:08:21 -0700118 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 Brabham1a8e7472016-01-21 12:47:43 -0500131 uri = getUriForImageCaptureFile(activity, saveFile);
Danesh M22398a72015-10-07 13:08:21 -0700132 }
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 Brabham1a8e7472016-01-21 12:47:43 -0500145
146 public static Uri getUriForImageCaptureFile(Context context, File file) {
jrizzoli608c7a92016-04-26 15:16:59 +0200147 return ContentUriUtils.getContentUriFromFile(context, file);
Martin Brabham1a8e7472016-01-21 12:47:43 -0500148 }
149
150 public static File getDirectoryForImageCapture(Context context) throws IOException {
jrizzoli608c7a92016-04-26 15:16:59 +0200151 File path = new File(context.getFilesDir(), IMAGE_FILE_PATH);
152 if (!path.exists() && !path.mkdir()) {
153 throw new IOException("Folder cannot be created.");
Martin Brabham1a8e7472016-01-21 12:47:43 -0500154 }
155 return path;
156 }
157
jrizzoli608c7a92016-04-26 15:16:59 +0200158 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 Nainani82ca1bd2015-07-16 14:31:17 -0700174}