// Path: app/src/main/java/io/gresse/hugo/anecdote/Configuration.java
// public class Configuration {
// 
//     public static final boolean DEBUG                     = BuildConfig.DEBUG;
//     public static final String  API_VERSION               = "2";
//     public static final String  API_URL                   = "https://anecdote-api.firebaseio.com/v" + API_VERSION + "/websites.json";
//     public static final String  DOWNLOAD_FOLDER           = "Anecdote";
//     public static final int     IMAGE_SAVE_TOAST_DURATION = 5000;
//     public static final String  DONATION_LINK             = "https://paypal.me/HugoGresse/5";
// 
// }
// 
// Path: app/src/main/java/io/gresse/hugo/anecdote/util/ImageSaver.java
// public class ImageSaver {
// 
//     public static final String TAG = ImageSaver.class.getSimpleName();
//     private String mDirectoryName = "images";
//     private String mFileName      = "image.png";
//     private Context mContext;
//     private boolean mExternal;
// 
//     public ImageSaver(Context context) {
//         this.mContext = context;
//     }
// 
//     public ImageSaver setFileName(String fileName) {
//         this.mFileName = fileName;
//         return this;
//     }
// 
//     public ImageSaver setExternal(boolean external) {
//         this.mExternal = external;
//         return this;
//     }
// 
//     public ImageSaver setDirectoryName(String directoryName) {
//         this.mDirectoryName = directoryName;
//         return this;
//     }
// 
//     @Nullable
//     public File save(Bitmap bitmapImage) {
//         FileOutputStream fileOutputStream = null;
//         File file = null;
//         try {
//             file = createFile();
//             if(!file.createNewFile()){
//                 Log.w(TAG, "Fail to create image file to " + file.getAbsolutePath());
//                 return null;
//             }
//             fileOutputStream = new FileOutputStream(file);
//             Log.d(TAG, "Saving image to " + file.getAbsolutePath());
//             bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
// 
//             if(mExternal){
//                 ContentValues values = new ContentValues();
//                 values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
//                 values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
//                 mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//             }
//         } catch (Exception e) {
//             Log.w(TAG, "Fail to save image " + ((file != null) ? file.getAbsolutePath() : ""));
//             e.printStackTrace();
//         } finally {
//             mContext = null;
//             try {
//                 if (fileOutputStream != null) {
//                     fileOutputStream.close();
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return file;
//     }
// 
//     @NonNull
//     private File createFile() {
//         File directory;
//         if (mExternal) {
//             directory = new File(
//                     Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
//                     mDirectoryName);
//             if(!directory.exists()) {
//                 if(!directory.mkdirs()){
//                     Log.w(TAG, "Unable to make Anecdote picture folder " + directory.getAbsolutePath());
//                 }
//             }
//         } else {
//             directory = mContext.getDir(mDirectoryName, Context.MODE_PRIVATE);
//         }
// 
//         return new File(directory, mFileName);
//     }
// 
//     public Bitmap load() {
//         FileInputStream inputStream = null;
//         try {
//             inputStream = new FileInputStream(createFile());
//             return BitmapFactory.decodeStream(inputStream);
//         } catch (Exception e) {
//             e.printStackTrace();
//         } finally {
//             try {
//                 if (inputStream != null) {
//                     inputStream.close();
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return null;
//     }
// }


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import java.io.File;
import io.gresse.hugo.anecdote.Configuration;
import io.gresse.hugo.anecdote.R;
import io.gresse.hugo.anecdote.util.ImageSaver;
package io.gresse.hugo.anecdote.view;





/**
 * Custom imageview that add some functionalities to it
 * Created by Hugo Gresse on 18/03/2017.
 */

public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Nullable
    public File saveImage(){

        Drawable drawable = getDrawable();
        Bitmap bitmap = null;

        if(drawable instanceof BitmapDrawable){
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof GlideDrawable){
            bitmap = ((GlideBitmapDrawable)drawable.getCurrent()).getBitmap();
        }

        File output = new ImageSaver(getContext())
                .setExternal(true)
// Path: app/src/main/java/io/gresse/hugo/anecdote/Configuration.java
// public class Configuration {
// 
//     public static final boolean DEBUG                     = BuildConfig.DEBUG;
//     public static final String  API_VERSION               = "2";
//     public static final String  API_URL                   = "https://anecdote-api.firebaseio.com/v" + API_VERSION + "/websites.json";
//     public static final String  DOWNLOAD_FOLDER           = "Anecdote";
//     public static final int     IMAGE_SAVE_TOAST_DURATION = 5000;
//     public static final String  DONATION_LINK             = "https://paypal.me/HugoGresse/5";
// 
// }
// 
// Path: app/src/main/java/io/gresse/hugo/anecdote/util/ImageSaver.java
// public class ImageSaver {
// 
//     public static final String TAG = ImageSaver.class.getSimpleName();
//     private String mDirectoryName = "images";
//     private String mFileName      = "image.png";
//     private Context mContext;
//     private boolean mExternal;
// 
//     public ImageSaver(Context context) {
//         this.mContext = context;
//     }
// 
//     public ImageSaver setFileName(String fileName) {
//         this.mFileName = fileName;
//         return this;
//     }
// 
//     public ImageSaver setExternal(boolean external) {
//         this.mExternal = external;
//         return this;
//     }
// 
//     public ImageSaver setDirectoryName(String directoryName) {
//         this.mDirectoryName = directoryName;
//         return this;
//     }
// 
//     @Nullable
//     public File save(Bitmap bitmapImage) {
//         FileOutputStream fileOutputStream = null;
//         File file = null;
//         try {
//             file = createFile();
//             if(!file.createNewFile()){
//                 Log.w(TAG, "Fail to create image file to " + file.getAbsolutePath());
//                 return null;
//             }
//             fileOutputStream = new FileOutputStream(file);
//             Log.d(TAG, "Saving image to " + file.getAbsolutePath());
//             bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
// 
//             if(mExternal){
//                 ContentValues values = new ContentValues();
//                 values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
//                 values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
//                 mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//             }
//         } catch (Exception e) {
//             Log.w(TAG, "Fail to save image " + ((file != null) ? file.getAbsolutePath() : ""));
//             e.printStackTrace();
//         } finally {
//             mContext = null;
//             try {
//                 if (fileOutputStream != null) {
//                     fileOutputStream.close();
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return file;
//     }
// 
//     @NonNull
//     private File createFile() {
//         File directory;
//         if (mExternal) {
//             directory = new File(
//                     Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
//                     mDirectoryName);
//             if(!directory.exists()) {
//                 if(!directory.mkdirs()){
//                     Log.w(TAG, "Unable to make Anecdote picture folder " + directory.getAbsolutePath());
//                 }
//             }
//         } else {
//             directory = mContext.getDir(mDirectoryName, Context.MODE_PRIVATE);
//         }
// 
//         return new File(directory, mFileName);
//     }
// 
//     public Bitmap load() {
//         FileInputStream inputStream = null;
//         try {
//             inputStream = new FileInputStream(createFile());
//             return BitmapFactory.decodeStream(inputStream);
//         } catch (Exception e) {
//             e.printStackTrace();
//         } finally {
//             try {
//                 if (inputStream != null) {
//                     inputStream.close();
//                 }
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return null;
//     }
// }


// Path: app/src/main/java/io/gresse/hugo/anecdote/view/CustomImageView.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import java.io.File;
import io.gresse.hugo.anecdote.Configuration;
import io.gresse.hugo.anecdote.R;
import io.gresse.hugo.anecdote.util.ImageSaver;

package io.gresse.hugo.anecdote.view;





/**
 * Custom imageview that add some functionalities to it
 * Created by Hugo Gresse on 18/03/2017.
 */

public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Nullable
    public File saveImage(){

        Drawable drawable = getDrawable();
        Bitmap bitmap = null;

        if(drawable instanceof BitmapDrawable){
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else if (drawable instanceof GlideDrawable){
            bitmap = ((GlideBitmapDrawable)drawable.getCurrent()).getBitmap();
        }

        File output = new ImageSaver(getContext())
                .setExternal(true)
                .setDirectoryName(Configuration.DOWNLOAD_FOLDER)