From 7ad41b17540e81bdbaa44a46f2b446b4869cc3af Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 28 Feb 2019 18:42:07 +0100 Subject: [PATCH] Can save some uploaded images --- app/src/main/AndroidManifest.xml | 4 + .../client/data/helpers/ImageLoadHelper.java | 16 +++ .../data/runnables/ImageLoadRunnable.java | 13 +-- .../client/data/utils/ImageLoadUtils.java | 22 +++- .../client/ui/activities/MainActivity.java | 7 ++ .../client/ui/adapters/CommentsAdapter.java | 1 + .../adapters/ConversationMessageAdapter.java | 1 + .../client/ui/adapters/PostsAdapter.java | 1 + .../client/ui/utils/PermissionsUtils.java | 44 ++++++++ .../ui/views/EnlargeableWebImageView.java | 104 +++++++++++++++++- app/src/main/res/menu/image_menu.xml | 10 ++ app/src/main/res/values-fr/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 13 files changed, 218 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/utils/PermissionsUtils.java create mode 100644 app/src/main/res/menu/image_menu.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ea1b827..298ed7c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,10 @@ + + + + { //Update comment image (if any) EnlargeableWebImageView commentImage = view.findViewById(R.id.comment_image); + commentImage.setCanSaveImageToGallery(true); if(comment.getImage_url().length() < 5) commentImage.setVisibility(View.GONE); else { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java index 3d2e58f..5f3a87e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java @@ -136,6 +136,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { mMessage.setOnLongClickListener(this); mImage.setOnLongClickListener(this); + mImage.setCanSaveImageToGallery(true); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index 629aa77..259fb5c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -415,6 +415,7 @@ public class PostsAdapter extends BaseRecyclerViewAdapter { super(itemView); mPostImage = new EnlargeableWebImageView(getContext()); + mPostImage.setCanSaveImageToGallery(true); getAdditionalViewsLayout().addView(mPostImage, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UiUtils.GetPixel(getContext(), 200))); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/PermissionsUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/PermissionsUtils.java new file mode 100644 index 0000000..8c58816 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/PermissionsUtils.java @@ -0,0 +1,44 @@ +package org.communiquons.android.comunic.client.ui.utils; + +import android.app.Activity; +import android.content.pm.PackageManager; +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; + +import java.util.ArrayList; + +/** + * Permissions utilities + * + * @author Pierre HUBERT + */ +public class PermissionsUtils { + + /** + * Request some permissions from the user + * + * @param activity Activity that perform the request + * @param permissions Requested permissions + */ + public static void RequestPermissions(@NonNull Activity activity, String[] permissions, int c){ + + ArrayList permissionsToRequest = new ArrayList<>(); + + for(String permission : permissions){ + if(ContextCompat.checkSelfPermission(activity, permission) + != PackageManager.PERMISSION_GRANTED) + permissionsToRequest.add(permission); + } + + + //Ask for permissions if required + if(permissionsToRequest.size() > 0){ + String[] permissionsArray = permissionsToRequest.toArray(new String[0]); + assert permissionsArray != null; + ActivityCompat.requestPermissions(activity, permissionsArray, c); + } + + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/EnlargeableWebImageView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/EnlargeableWebImageView.java index dcd1746..34477be 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/EnlargeableWebImageView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/EnlargeableWebImageView.java @@ -2,11 +2,22 @@ package org.communiquons.android.comunic.client.ui.views; import android.app.AlertDialog; import android.content.Context; +import android.graphics.Bitmap; +import android.provider.MediaStore; import android.util.AttributeSet; +import android.util.Log; import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; +import android.widget.PopupMenu; +import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.helpers.ImageLoadHelper; +import org.communiquons.android.comunic.client.data.utils.ImageLoadUtils; +import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; + +import java.io.File; /** * Enlargeable images view. @@ -17,13 +28,21 @@ import org.communiquons.android.comunic.client.R; * Created by pierre on 4/28/18. */ -public class EnlargeableWebImageView extends WebImageView { +public class EnlargeableWebImageView extends WebImageView implements PopupMenu.OnMenuItemClickListener { + + + /** + * Debug tag + */ + private static final String TAG = EnlargeableWebImageView.class.getSimpleName(); /** * Optional additional OnClick Listener */ private OnClickListener mOnClickListener; + private boolean mCanSaveImageToGallery; + public EnlargeableWebImageView(Context context) { super(context); init(); @@ -68,6 +87,14 @@ public class EnlargeableWebImageView extends WebImageView { return mOnClickListener == null; } + public boolean isCanSaveImageToGallery() { + return mCanSaveImageToGallery; + } + + public void setCanSaveImageToGallery(boolean mCanSaveImageToGallery) { + this.mCanSaveImageToGallery = mCanSaveImageToGallery; + } + /** * Open the image in large dimensions */ @@ -86,5 +113,80 @@ public class EnlargeableWebImageView extends WebImageView { .show(); + //Offer the user to save if image if possible + if(isCanSaveImageToGallery()){ + image.setOnLongClickListener(this::openContextMenu); + } + } + + + /** + * Open context menu for the image + */ + private boolean openContextMenu(View v){ + + //Show popup menu + PopupMenu popupMenu = new PopupMenu(getContext(), v); + popupMenu.inflate(R.menu.image_menu); + popupMenu.setOnMenuItemClickListener(this); + + if(ImageLoadHelper.IsLoading(this)) + popupMenu.getMenu().findItem(R.id.action_save_image_in_gallery).setEnabled(false); + + + popupMenu.show(); + + return true; + } + + @Override + public boolean onMenuItemClick(MenuItem item) { + + + if(item.getItemId() == R.id.action_save_image_in_gallery){ + + if(!saveImageToGallery()) + Toast.makeText(getContext(), R.string.err_save_image_in_gallery, Toast.LENGTH_SHORT).show(); + else + Toast.makeText(getContext(), R.string.success_save_image_in_gallery, Toast.LENGTH_SHORT).show(); + + return true; + } + + return false; + } + + /** + * Save the current image in this {@link WebImageView} into the gallery + * + * @return TRUE in case of success / FALSE else + */ + private boolean saveImageToGallery(){ + + try { + if (ImageLoadHelper.IsLoading(this)) + return false; + + File file = ImageLoadUtils.getFileForImage(getContext(), getCurrURL()); + + if (!file.isFile()) { + Log.e(TAG, "Image is not a file!"); + return false; + } + + Bitmap bm = BitmapUtils.openResized(file, 1500, 1500); + + //Save the image into the gallery + return MediaStore.Images.Media.insertImage( + getContext().getContentResolver(), + bm, + "Comunic image", + getCurrURL() + ) != null; + + } catch (Exception e){ + e.printStackTrace(); + return false; + } } } diff --git a/app/src/main/res/menu/image_menu.xml b/app/src/main/res/menu/image_menu.xml new file mode 100644 index 0000000..906cbb6 --- /dev/null +++ b/app/src/main/res/menu/image_menu.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 0ad1596..60b9889 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -335,4 +335,7 @@ Raccrocher Changer de caméra Arrêter la caméra + Enregistrer dans la gallerie + Une erreur a survenue lors de l\'enregistrement de l\'image dans la gallerie ! + L\'image a bien été enregistrée dans la gallerie ! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e414624..8225afc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -334,4 +334,7 @@ Hang up Switch camera Stop camera + Save in gallery + Could not save the image in the gallery! + Successfully saved the image into the gallery!