Dbrant has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/180302

Change subject: [WIP] Share images.
......................................................................

[WIP] Share images.

- Added toolbar item for sharing images.
- The function automatically saves the image to the user's Gallery, then
  shares the image from there.
- Requires new WRITE_EXTERNAL_STORAGE permission.

Change-Id: I577cc5f4607aba9f0f1be2d9e3e6eb53868f78f4
---
M wikipedia/AndroidManifest.xml
M wikipedia/res/menu/menu_gallery.xml
M wikipedia/res/values-qq/strings.xml
M wikipedia/res/values/strings.xml
M wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
5 files changed, 110 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/02/180302/1

diff --git a/wikipedia/AndroidManifest.xml b/wikipedia/AndroidManifest.xml
index 185a55b..86607bd 100644
--- a/wikipedia/AndroidManifest.xml
+++ b/wikipedia/AndroidManifest.xml
@@ -12,6 +12,7 @@
 
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
/>
 
     <!-- For Nearby feature -->
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
diff --git a/wikipedia/res/menu/menu_gallery.xml 
b/wikipedia/res/menu/menu_gallery.xml
index be038c5..db07d0d 100644
--- a/wikipedia/res/menu/menu_gallery.xml
+++ b/wikipedia/res/menu/menu_gallery.xml
@@ -3,9 +3,16 @@
 <menu xmlns:android="http://schemas.android.com/apk/res/android";
       xmlns:app="http://schemas.android.com/apk/res-auto";
         >
+    <item android:id="@+id/menu_gallery_share"
+        android:title="@string/gallery_menu_share"
+        android:icon="@android:drawable/ic_menu_share"
+        app:showAsAction="ifRoom"/>
     <item android:id="@+id/menu_gallery_more_info"
           android:title="@string/gallery_menu_more_info"
           app:showAsAction="never"/>
+    <item android:id="@+id/menu_gallery_save"
+        android:title="@string/gallery_menu_save"
+        app:showAsAction="never"/>
     <item android:id="@+id/menu_gallery_visit_page"
         android:title="@string/menu_gallery_visit_page"
         app:showAsAction="never"/>
diff --git a/wikipedia/res/values-qq/strings.xml 
b/wikipedia/res/values-qq/strings.xml
index 66fb2fe..bf805e6 100644
--- a/wikipedia/res/values-qq/strings.xml
+++ b/wikipedia/res/values-qq/strings.xml
@@ -371,4 +371,9 @@
   <string name="gallery_credit_text">Label that specifies the person or entity 
to whom the image should be credited. The \"%s\" symbol will be replaced by the 
author of the image.
 {{Identical|Credit}}</string>
   <string name="gallery_image_info">Label for a dialog that shows detailed 
information about the current image shown in the gallery.</string>
+  <string name="gallery_menu_share">Label for menu item to share the current 
image.</string>
+  <string name="gallery_share_error">Error message shown when there was a 
problem with sharing the image. The \"%s\" symbol is replaced with the actual 
error message from the system.</string>
+  <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>
 </resources>
diff --git a/wikipedia/res/values/strings.xml b/wikipedia/res/values/strings.xml
index 00664b6..1aa19ca 100644
--- a/wikipedia/res/values/strings.xml
+++ b/wikipedia/res/values/strings.xml
@@ -284,4 +284,9 @@
     <string name="gallery_license_text">License – %s</string>
     <string name="gallery_credit_text">Author – %s</string>
     <string name="gallery_image_info">Image information</string>
+    <string name="gallery_menu_share">Share</string>
+    <string name="gallery_share_error">Could not share image: %s</string>
+    <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>
 </resources>
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 b8a2c7c..4d96ee0 100644
--- a/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
+++ b/wikipedia/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
@@ -7,12 +7,17 @@
 import org.wikipedia.Utils;
 import org.wikipedia.ViewAnimations;
 import org.wikipedia.WikipediaApp;
+import org.wikipedia.concurrency.SaneAsyncTask;
 import org.wikipedia.history.HistoryEntry;
 import org.wikipedia.page.LinkMovementMethodExt;
 import org.wikipedia.page.PageActivity;
 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;
 import android.text.TextUtils;
@@ -24,11 +29,15 @@
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
+import android.widget.Toast;
 import com.squareup.picasso.Callback;
 import com.squareup.picasso.Picasso;
 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 {
@@ -147,6 +156,8 @@
     public boolean onPrepareOptionsMenu(Menu menu) {
         
menu.findItem(R.id.menu_gallery_more_info).setEnabled(currentGalleryItem != 
null);
         
menu.findItem(R.id.menu_gallery_visit_page).setEnabled(currentImageTitle != 
null);
+        menu.findItem(R.id.menu_gallery_share).setEnabled(
+                currentGalleryItem != null && 
currentGalleryItem.getMimeType().contains("jpeg"));
         return true;
     }
 
@@ -167,6 +178,12 @@
                     finishWithPageResult(currentImageTitle);
                 }
                 return true;
+            case R.id.menu_gallery_save:
+                saveImageToMediaStore();
+                return true;
+            case R.id.menu_gallery_share:
+                shareImage();
+                return true;
             default:
                 break;
         }
@@ -174,6 +191,81 @@
     }
 
     /**
+     * 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.
+     */
+    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();
+    }
+
+    /**
+     * Save the current image to the MediaStore of the local device ("Photos" 
/ "Gallery" / etc).
+     */
+    private void saveImageToMediaStore() {
+        if (currentGalleryItem == null) {
+            return;
+        }
+        new SaneAsyncTask<Void>(SaneAsyncTask.SINGLE_THREAD) {
+            @Override
+            public Void performTask() throws Throwable {
+                MediaStore.Images.Media.insertImage(getContentResolver(),
+                            ((BitmapDrawable) 
mainImage.getDrawable()).getBitmap(),
+                            pageTitle.getDisplayText(), 
currentGalleryItem.getName());
+                return null;
+            }
+            @Override
+            public void onFinish(Void result) {
+                Toast.makeText(GalleryActivity.this, 
getString(R.string.gallery_save_success),
+                               Toast.LENGTH_SHORT).show();
+            }
+            @Override
+            public void onCatch(Throwable caught) {
+                Toast.makeText(GalleryActivity.this,
+                               
String.format(getString(R.string.gallery_save_error),
+                               caught.getLocalizedMessage()), 
Toast.LENGTH_SHORT).show();
+            }
+        }.execute();
+    }
+
+    /**
      * Show or hide all the UI controls in this activity (slide them out or 
in).
      * @param showing Whether to show or hide the controls.
      */

-- 
To view, visit https://gerrit.wikimedia.org/r/180302
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I577cc5f4607aba9f0f1be2d9e3e6eb53868f78f4
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to