BearND has uploaded a new change for review. https://gerrit.wikimedia.org/r/181223
Change subject: Share a fact ...................................................................... Share a fact - Refactored the GalleryActivity's image sharing code to be reusable. - Reused it for Tweet a fact - Currently it uses the lead image as the image to be share, plus the mainly static teaser text. Change-Id: Ib78821dcb772c9b5811f1c8f2bbdefb77a826844 (cherry picked from commit 5f9e08b69572108e9669a55032903f8cbe052ea4) --- 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, 124 insertions(+), 45 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/23/181223/1 diff --git a/wikipedia/res/values-qq/strings.xml b/wikipedia/res/values-qq/strings.xml index 14b98d4..4bdedcf 100644 --- a/wikipedia/res/values-qq/strings.xml +++ b/wikipedia/res/values-qq/strings.xml @@ -379,4 +379,5 @@ <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.</string> </resources> diff --git a/wikipedia/res/values/strings.xml b/wikipedia/res/values/strings.xml index 1aa19ca..0a89938 100644 --- a/wikipedia/res/values/strings.xml +++ b/wikipedia/res/values/strings.xml @@ -289,4 +289,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 f3d824f..00a5daa 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; @@ -33,6 +34,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; @@ -55,6 +57,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"; @@ -748,12 +751,36 @@ @Override public boolean onMenuItemClick(MenuItem item) { - // TODO: implement sharing of image with quote! + shareSnippet(); if (webViewActionMode != null) { webViewActionMode.finish(); } return true; + } + + private void shareSnippet() { + // 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(); + } } }); } @@ -765,5 +792,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 9f7fd44..c20072e 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; @@ -448,6 +449,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 81fdd8c..fcadb73 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..90bf090 --- /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 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: newchange Gerrit-Change-Id: Ib78821dcb772c9b5811f1c8f2bbdefb77a826844 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: BearND <bsitzm...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits