Dbrant has submitted this change and it was merged. Change subject: Share a fact ......................................................................
Share a fact - Refactored the GalleryActivity's image sharing code to be reusable. - Reused it for Share a fact - Currently it uses the lead image as the image to be share, plus the mainly static teaser text. Change-Id: Ib78821dcb772c9b5811f1c8f2bbdefb77a826844 --- M wikipedia/res/values-qq/strings.xml M wikipedia/res/values/strings.xml M wikipedia/src/main/java/org/wikipedia/page/PageActivity.java M wikipedia/src/main/java/org/wikipedia/page/PageViewFragmentInternal.java M wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java M wikipedia/src/main/java/org/wikipedia/page/leadimages/LeadImagesHandler.java A wikipedia/src/main/java/org/wikipedia/util/ShareUtils.java 7 files changed, 131 insertions(+), 50 deletions(-) Approvals: Dbrant: Looks good to me, approved diff --git a/wikipedia/res/values-qq/strings.xml b/wikipedia/res/values-qq/strings.xml index a2ea87d..0d4adc6 100644 --- a/wikipedia/res/values-qq/strings.xml +++ b/wikipedia/res/values-qq/strings.xml @@ -379,4 +379,8 @@ <string name="gallery_menu_save">Label for menu item to save the current image to the Media collection on the local device.</string> <string name="gallery_save_error">Error message shown when there was a problem with saving the image to the local device. The \"%s\" symbol is replaced with the actual error message from the system.</string> <string name="gallery_save_success">Toast message shown when the current image was saved successfully to the local device.</string> + <string name="snippet_share_intro">Intro text included as part of the shared text snippet, aka Tweet-a-fact. This message has two parameters. Please keep the parameters exactly as in the original English message. + * The first parameter is the title of the page the text snippet is from. + * The second parameter is an HTML link to the page. + </string> </resources> diff --git a/wikipedia/res/values/strings.xml b/wikipedia/res/values/strings.xml index 608fca5..9f285e7 100644 --- a/wikipedia/res/values/strings.xml +++ b/wikipedia/res/values/strings.xml @@ -288,4 +288,5 @@ <string name="gallery_menu_save">Save to device</string> <string name="gallery_save_error">Could not save image: %s</string> <string name="gallery_save_success">Image saved successfully.</string> + <string name="snippet_share_intro">Interesting fact on @Wikipedia about %1$s: %2$s</string> </resources> diff --git a/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java b/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java index 2a9083f..dd07945 100644 --- a/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java +++ b/wikipedia/src/main/java/org/wikipedia/page/PageActivity.java @@ -21,6 +21,7 @@ import org.wikipedia.settings.PrefKeys; import org.wikipedia.staticdata.MainPageNameData; import org.wikipedia.theme.ThemeChooserDialog; +import org.wikipedia.util.ShareUtils; import org.wikipedia.zero.ZeroMessage; import com.squareup.otto.Bus; @@ -36,6 +37,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.graphics.Bitmap; import android.net.Uri; import android.os.BadParcelableException; import android.os.Build; @@ -58,6 +60,7 @@ import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; public class PageActivity extends ThemedActionBarActivity { public static final String ACTION_PAGE_FOR_TITLE = "org.wikipedia.page_for_title"; @@ -790,15 +793,38 @@ if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClip().getItemCount() > 0) { + CharSequence selectedText = clipboard.getPrimaryClip().getItemAt(0) + .coerceToText(PageActivity.this); + Log.d("Share", ">>> Clipboard text: " + selectedText); - // TODO: Pass the clipboard text to the Share handler! - - - Log.d("Share", ">>> Clipboard text: " + clipboard.getPrimaryClip() - .getItemAt(0).coerceToText(PageActivity.this)); - + // Pass the clipboard text to the Share handler! + shareSnippet(selectedText); } clipboard.removePrimaryClipChangedListener(clipListener); + } + + private void shareSnippet(CharSequence selectedText) { + // TODO: create sharable image with selected text + // currently only using lead image + + PageTitle title = getCurPageFragment().getTitle(); + String text = getString(R.string.snippet_share_intro, + title.getDisplayText(), + title.getCanonicalUri()); + + Bitmap leadImageBitmap = getCurPageFragment().getLeadImageBitmap(); + + if (leadImageBitmap != null) { + ShareUtils.shareImage(PageActivity.this, leadImageBitmap, "*/*", + title.getDisplayText(), text); + } else { + // TODO: remove if check once we build our own image + // For now, as we're using the lead image we may not always have one, + // esp. on the main page. + Toast.makeText(PageActivity.this, + "For now: only works on pages that have a lead image loaded", + Toast.LENGTH_SHORT).show(); + } } }; } @@ -841,5 +867,4 @@ webViewActionMode = null; super.onSupportActionModeFinished(mode); } - } diff --git a/wikipedia/src/main/java/org/wikipedia/page/PageViewFragmentInternal.java b/wikipedia/src/main/java/org/wikipedia/page/PageViewFragmentInternal.java index 05d65c4..b4f5fa8 100644 --- a/wikipedia/src/main/java/org/wikipedia/page/PageViewFragmentInternal.java +++ b/wikipedia/src/main/java/org/wikipedia/page/PageViewFragmentInternal.java @@ -39,6 +39,7 @@ import android.app.AlertDialog; import android.content.Intent; import android.content.res.Resources; +import android.graphics.Bitmap; import android.os.Build; import android.os.Bundle; import android.support.v4.view.MenuItemCompat; @@ -446,6 +447,10 @@ && !webView.canGoBack(); } + public Bitmap getLeadImageBitmap() { + return leadImagesHandler.getLeadImageBitmap(); + } + /** * Update the WebView's base font size, based on the specified font size from the app preferences. */ diff --git a/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java b/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java index fae17b3..f4ccf6c 100644 --- a/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java +++ b/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java @@ -11,12 +11,12 @@ import org.wikipedia.history.HistoryEntry; import org.wikipedia.page.LinkMovementMethodExt; import org.wikipedia.page.PageActivity; +import org.wikipedia.util.ShareUtils; + import android.content.Intent; -import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; -import android.os.Environment; import android.provider.MediaStore; import android.support.v7.widget.Toolbar; import android.text.Html; @@ -36,9 +36,7 @@ import de.keyboardsurfer.android.widget.crouton.Crouton; import de.keyboardsurfer.android.widget.crouton.Style; import uk.co.senab.photoview.PhotoViewAttacher; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; + import java.util.Map; public class GalleryActivity extends ThemedActionBarActivity { @@ -198,49 +196,22 @@ } /** - * Share the current image using an activity chooser, so that the user can choose the - * app with which to share the content. - * This is done by saving the image to a temporary file in external storage, then specifying - * that file in the share intent. The name of the temporary file is kept constant, so that - * it's overwritten every time an image is shared from the app, so that it takes up a - * constant amount of space. + * Share the current image using an activity chooser. + * + * @see org.wikipedia.util.ShareUtils#shareImage */ private void shareImage() { if (currentGalleryItem == null) { return; } - final int jpegQuality = 85; - final String tempShareFileName = getPackageName() + "_tempShareImage.jpg"; - new SaneAsyncTask<String>(SaneAsyncTask.SINGLE_THREAD) { - @Override - public String performTask() throws Throwable { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - Bitmap bmp = ((BitmapDrawable) mainImage.getDrawable()).getBitmap(); - bmp.compress(Bitmap.CompressFormat.JPEG, jpegQuality, bytes); - File f = new File(Environment.getExternalStorageDirectory() + File.separator - + tempShareFileName); - FileOutputStream fo = new FileOutputStream(f); - fo.write(bytes.toByteArray()); - fo.close(); - return f.getAbsolutePath(); - } - @Override - public void onFinish(String result) { - Intent shareIntent = new Intent(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_SUBJECT, pageTitle.getDisplayText()); - shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + result)); - shareIntent.setType(currentGalleryItem.getMimeType()); - Intent chooser = Intent.createChooser(shareIntent, getResources() - .getString(R.string.share_via)); - startActivity(chooser); - } - @Override - public void onCatch(Throwable caught) { - Toast.makeText(GalleryActivity.this, - String.format(getString(R.string.gallery_share_error), - caught.getLocalizedMessage()), Toast.LENGTH_SHORT).show(); - } - }.execute(); + ShareUtils.shareImage(GalleryActivity.this, + ((BitmapDrawable) mainImage.getDrawable()).getBitmap(), + // shouldn't this always be + "image/jpeg", + // instead of: +// currentGalleryItem.getMimeType(), + pageTitle.getDisplayText(), + ""); } /** diff --git a/wikipedia/src/main/java/org/wikipedia/page/leadimages/LeadImagesHandler.java b/wikipedia/src/main/java/org/wikipedia/page/leadimages/LeadImagesHandler.java index 8226948..3cd41f3 100644 --- a/wikipedia/src/main/java/org/wikipedia/page/leadimages/LeadImagesHandler.java +++ b/wikipedia/src/main/java/org/wikipedia/page/leadimages/LeadImagesHandler.java @@ -100,6 +100,7 @@ private int displayHeight; private int imageBaseYOffset = 0; private float displayDensity; + private Bitmap leadImageBitmap; public interface OnLeadImageLayoutListener { void onLayoutComplete(); @@ -184,8 +185,13 @@ imageContainer.setVisibility(View.INVISIBLE); } + public Bitmap getLeadImageBitmap() { + return leadImageBitmap; + } + @Override public void onImageLoaded(Bitmap bitmap, final PointF faceLocation) { + leadImageBitmap = bitmap; final int bmpHeight = bitmap.getHeight(); final float aspect = (float)bitmap.getHeight() / (float)bitmap.getWidth(); imageContainer.post(new Runnable() { diff --git a/wikipedia/src/main/java/org/wikipedia/util/ShareUtils.java b/wikipedia/src/main/java/org/wikipedia/util/ShareUtils.java new file mode 100644 index 0000000..45008bf --- /dev/null +++ b/wikipedia/src/main/java/org/wikipedia/util/ShareUtils.java @@ -0,0 +1,69 @@ +package org.wikipedia.util; + +import android.app.Activity; +import android.content.Intent; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Environment; +import android.widget.Toast; + +import org.wikipedia.R; +import org.wikipedia.concurrency.SaneAsyncTask; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; + +/** + * + */ +public final class ShareUtils { + /** Private constructor, so nobody can construct ShareUtils. */ + private ShareUtils() { } + + /** + * Share a bitmap image using an activity chooser, so that the user can choose the + * app with which to share the content. + * This is done by saving the image to a temporary file in external storage, then specifying + * that file in the share intent. The name of the temporary file is kept constant, so that + * it's overwritten every time an image is shared from the app, so that it takes up a + * constant amount of space. + */ + public static void shareImage(final Activity activity, final Bitmap bmp, final String mimeType, + final String subject, final String text) { + final int jpegQuality = 85; + final String tempShareFileName = activity.getPackageName() + "_tempShareImage.jpg"; + new SaneAsyncTask<String>(SaneAsyncTask.SINGLE_THREAD) { + @Override + public String performTask() throws Throwable { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + bmp.compress(Bitmap.CompressFormat.JPEG, jpegQuality, bytes); + File f = new File(Environment.getExternalStorageDirectory() + File.separator + + tempShareFileName); + FileOutputStream fo = new FileOutputStream(f); + fo.write(bytes.toByteArray()); + fo.close(); + return f.getAbsolutePath(); + } + + @Override + public void onFinish(String result) { + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject); + shareIntent.putExtra(Intent.EXTRA_TEXT, text); + shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + result)); + shareIntent.setType(mimeType); + Intent chooser = Intent.createChooser(shareIntent, + activity.getResources().getString(R.string.share_via)); + activity.startActivity(chooser); + } + + @Override + public void onCatch(Throwable caught) { + Toast.makeText(activity, + String.format(activity.getString(R.string.gallery_share_error), + caught.getLocalizedMessage()), Toast.LENGTH_SHORT).show(); + } + }.execute(); + } +} -- To view, visit https://gerrit.wikimedia.org/r/181223 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ib78821dcb772c9b5811f1c8f2bbdefb77a826844 Gerrit-PatchSet: 9 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: BearND <bsitzm...@wikimedia.org> Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org> Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits