Bug 990642 - Reinsert code to share images instead of just urls. r=mfinkle

This commit is contained in:
Wes Johnston 2014-05-12 10:13:44 -07:00
Родитель 85a337a3c1
Коммит 6e08f524ef
3 изменённых файлов: 73 добавлений и 2 удалений

Просмотреть файл

@ -9,11 +9,15 @@ import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Proxy;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
@ -106,6 +110,7 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.MimeTypeMap;
import android.widget.AbsoluteLayout;
import android.widget.Toast;
public class GeckoAppShell
{
@ -2639,4 +2644,61 @@ public class GeckoAppShell
return "DIRECT";
}
/* Downloads the uri pointed to by a share intent, and alters the intent to point to the locally stored file.
*/
public static void downloadImageForIntent(final Intent intent) {
final String src = intent.getStringExtra(Intent.EXTRA_TEXT);
final File dir = GeckoApp.getTempDirectory();
if (dir == null) {
showImageShareFailureToast();
return;
}
GeckoApp.deleteTempFiles();
OutputStream os = null;
try {
// Create a temporary file for the image
final String type = intent.getType().replace("image/", "");
final File imageFile = File.createTempFile("image", "." + type, dir);
os = new FileOutputStream(imageFile);
if (src.startsWith("data:")) {
int dataStart = src.indexOf(",");
byte[] buf = Base64.decode(src.substring(dataStart + 1), Base64.DEFAULT);
os.write(buf);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} else {
InputStream is = null;
try {
final byte[] buf = new byte[2048];
final URL url = new URL(src);
is = url.openStream();
int length;
while ((length = is.read(buf)) != -1) {
os.write(buf, 0, length);
}
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} finally {
safeStreamClose(is);
}
}
} catch(IOException ex) {
} finally {
safeStreamClose(os);
}
}
// Don't fail silently, tell the user that we weren't able to share the image
private static final void showImageShareFailureToast() {
Toast toast = Toast.makeText(getContext(),
getContext().getResources().getString(R.string.share_image_failed),
Toast.LENGTH_SHORT);
toast.show();
}
}

Просмотреть файл

@ -8,6 +8,7 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
@ -31,6 +32,7 @@ public class PromptListItem {
public Drawable mIcon;
PromptListItem(JSONObject aObject) {
Context context = GeckoAppShell.getContext();
label = aObject.isNull("label") ? "" : aObject.optString("label");
isGroup = aObject.optBoolean("isGroup");
inGroup = aObject.optBoolean("inGroup");
@ -44,7 +46,8 @@ public class PromptListItem {
String uri = obj.isNull("uri") ? "" : obj.optString("uri");
String type = obj.isNull("type") ? GeckoActionProvider.DEFAULT_MIME_TYPE :
obj.optString("type", GeckoActionProvider.DEFAULT_MIME_TYPE);
mIntent = GeckoAppShell.getShareIntent(GeckoAppShell.getContext(), uri, type, "");
mIntent = GeckoAppShell.getShareIntent(context, uri, type, "");
isParent = true;
} else {
mIntent = null;
@ -55,7 +58,7 @@ public class PromptListItem {
final String iconStr = aObject.optString("icon");
if (iconStr != null) {
BitmapUtils.getDrawable(GeckoAppShell.getContext(), iconStr, new BitmapUtils.BitmapLoader() {
BitmapUtils.getDrawable(context, iconStr, new BitmapUtils.BitmapLoader() {
@Override
public void onBitmapFound(Drawable d) {
mIcon = d;

Просмотреть файл

@ -184,6 +184,12 @@ public class GeckoActionProvider {
ActivityChooserModel dataModel = ActivityChooserModel.get(mContext, mHistoryFileName);
Intent launchIntent = dataModel.chooseActivity(index);
if (launchIntent != null) {
// Share image syncrhonously downloads the image before sharing it.
String type = launchIntent.getType();
if (Intent.ACTION_SEND.equals(launchIntent.getAction()) && type != null && type.startsWith("image/")) {
GeckoAppShell.downloadImageForIntent(launchIntent);
}
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
mContext.startActivity(launchIntent);
}