// Path: app/src/main/java/com/zulip/android/util/PhotoHelper.java
// public class PhotoHelper {
// 
//     /**
//      * Function to delete the file at {@param photoPath} and store {@param bitmap}
//      * at {@param photoPath}.
//      *
//      * @param photoPath file path
//      * @param bitmap    to be saved as file
//      */
//     public static String saveBitmapAsFile(String photoPath, Bitmap bitmap) {
//         if (bitmap == null) {
//             return photoPath;
//         }
// 
//         // delete old bitmap
//         File file = new File(photoPath);
//         file.delete();
// 
//         // store new bitmap at mPhotoPath file path
//         FileOutputStream out = null;
//         try {
//             // change file name to avoid catching issues with Glide
//             int position = photoPath.lastIndexOf(".");
//             photoPath = photoPath.substring(0, position) + Math.round(Math.random() * 10)
//                     + photoPath.substring(position);
// 
//             out = new FileOutputStream(photoPath);
// 
//             // bmp is your Bitmap instance
//             // PNG is a lossless format, the compression factor (100) is ignored
//             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
//         } catch (Exception e) {
//             ZLog.logException(e);
//         } finally {
//             try {
//                 if (out != null) {
//                     out.close();
//                 }
//             } catch (IOException e) {
//                 ZLog.logException(e);
//             }
//         }
// 
//         return photoPath;
//     }
// 
//     /**
//      * From http://stackoverflow.com/a/26930938/5334314
//      * Returns the actual bitmap position in an imageView.
//      *
//      * @param imageView source ImageView
//      * @return 0: left, 1: top, 2: width, 3: height
//      */
//     public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
//         int[] ret = new int[4];
// 
//         if (imageView == null || imageView.getDrawable() == null) {
//             return ret;
//         }
// 
//         // Get image dimensions
//         // Get image matrix values and place them in an array
//         float[] f = new float[9];
//         imageView.getImageMatrix().getValues(f);
// 
//         // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
//         final float scaleX = f[Matrix.MSCALE_X];
//         final float scaleY = f[Matrix.MSCALE_Y];
// 
//         // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
//         final Drawable d = imageView.getDrawable();
//         final int origW = d.getIntrinsicWidth();
//         final int origH = d.getIntrinsicHeight();
// 
//         // Calculate the actual dimensions
//         final int actW = Math.round(origW * scaleX);
//         final int actH = Math.round(origH * scaleY);
// 
//         ret[2] = actW;
//         ret[3] = actH;
// 
//         // Get image position
//         // We assume that the image is centered into ImageView
//         int imgViewW = imageView.getWidth();
//         int imgViewH = imageView.getHeight();
// 
//         int top = (int) (imgViewH - actH) / 2;
//         int left = (int) (imgViewW - actW) / 2;
// 
//         ret[0] = left;
//         ret[1] = top;
// 
//         return ret;
//     }
// }


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.theartofdev.edmodo.cropper.CropImageView;
import com.zulip.android.R;
import com.zulip.android.util.ActivityTransitionAnim;
import com.zulip.android.util.PhotoHelper;
import java.io.File;
                    // tint the crop button white when cropping is finished
                    mCropBtn.setColorFilter(Color.WHITE);
                    mIsCropFinished = false;
                }
            }
        });

        ImageView sendPhoto = (ImageView) findViewById(R.id.send_photo);
        sendPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap;
                Drawable drawable = mImageView.getDrawable();
                if (drawable instanceof GlideBitmapDrawable) {
                    // if imageview has drawable of type GlideBitmapDrawable
                    bitmap = ((GlideBitmapDrawable) mImageView.getDrawable().getCurrent())
                            .getBitmap();

                } else {
                    // if imageView stores cropped image drawable which is of type drawable
                    bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
                }

                /* Most phone cameras are landscape, meaning if you take the photo in portrait,
                   the resulting photos will be rotated 90 degrees. Hence used the displayed image
                   with correct orientation and saved that in the current photo path.
                */

                // delete old file and store new bitmap on that location
                // used the displayed image and saved it in the mPhotoPath
// Path: app/src/main/java/com/zulip/android/util/PhotoHelper.java
// public class PhotoHelper {
// 
//     /**
//      * Function to delete the file at {@param photoPath} and store {@param bitmap}
//      * at {@param photoPath}.
//      *
//      * @param photoPath file path
//      * @param bitmap    to be saved as file
//      */
//     public static String saveBitmapAsFile(String photoPath, Bitmap bitmap) {
//         if (bitmap == null) {
//             return photoPath;
//         }
// 
//         // delete old bitmap
//         File file = new File(photoPath);
//         file.delete();
// 
//         // store new bitmap at mPhotoPath file path
//         FileOutputStream out = null;
//         try {
//             // change file name to avoid catching issues with Glide
//             int position = photoPath.lastIndexOf(".");
//             photoPath = photoPath.substring(0, position) + Math.round(Math.random() * 10)
//                     + photoPath.substring(position);
// 
//             out = new FileOutputStream(photoPath);
// 
//             // bmp is your Bitmap instance
//             // PNG is a lossless format, the compression factor (100) is ignored
//             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
//         } catch (Exception e) {
//             ZLog.logException(e);
//         } finally {
//             try {
//                 if (out != null) {
//                     out.close();
//                 }
//             } catch (IOException e) {
//                 ZLog.logException(e);
//             }
//         }
// 
//         return photoPath;
//     }
// 
//     /**
//      * From http://stackoverflow.com/a/26930938/5334314
//      * Returns the actual bitmap position in an imageView.
//      *
//      * @param imageView source ImageView
//      * @return 0: left, 1: top, 2: width, 3: height
//      */
//     public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
//         int[] ret = new int[4];
// 
//         if (imageView == null || imageView.getDrawable() == null) {
//             return ret;
//         }
// 
//         // Get image dimensions
//         // Get image matrix values and place them in an array
//         float[] f = new float[9];
//         imageView.getImageMatrix().getValues(f);
// 
//         // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
//         final float scaleX = f[Matrix.MSCALE_X];
//         final float scaleY = f[Matrix.MSCALE_Y];
// 
//         // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
//         final Drawable d = imageView.getDrawable();
//         final int origW = d.getIntrinsicWidth();
//         final int origH = d.getIntrinsicHeight();
// 
//         // Calculate the actual dimensions
//         final int actW = Math.round(origW * scaleX);
//         final int actH = Math.round(origH * scaleY);
// 
//         ret[2] = actW;
//         ret[3] = actH;
// 
//         // Get image position
//         // We assume that the image is centered into ImageView
//         int imgViewW = imageView.getWidth();
//         int imgViewH = imageView.getHeight();
// 
//         int top = (int) (imgViewH - actH) / 2;
//         int left = (int) (imgViewW - actW) / 2;
// 
//         ret[0] = left;
//         ret[1] = top;
// 
//         return ret;
//     }
// }


// Path: app/src/main/java/com/zulip/android/activities/PhotoSendActivity.java
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.theartofdev.edmodo.cropper.CropImageView;
import com.zulip.android.R;
import com.zulip.android.util.ActivityTransitionAnim;
import com.zulip.android.util.PhotoHelper;
import java.io.File;

                    // tint the crop button white when cropping is finished
                    mCropBtn.setColorFilter(Color.WHITE);
                    mIsCropFinished = false;
                }
            }
        });

        ImageView sendPhoto = (ImageView) findViewById(R.id.send_photo);
        sendPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap;
                Drawable drawable = mImageView.getDrawable();
                if (drawable instanceof GlideBitmapDrawable) {
                    // if imageview has drawable of type GlideBitmapDrawable
                    bitmap = ((GlideBitmapDrawable) mImageView.getDrawable().getCurrent())
                            .getBitmap();

                } else {
                    // if imageView stores cropped image drawable which is of type drawable
                    bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
                }

                /* Most phone cameras are landscape, meaning if you take the photo in portrait,
                   the resulting photos will be rotated 90 degrees. Hence used the displayed image
                   with correct orientation and saved that in the current photo path.
                */

                // delete old file and store new bitmap on that location
                // used the displayed image and saved it in the mPhotoPath
                mPhotoPath = PhotoHelper.saveBitmapAsFile(mPhotoPath, bitmap);