package mprog.nl.mars_weather_explorer;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by Nadeche
 */
public class PhotoManager {

    public static Bitmap downloadPhoto(Context context, String url){
        Bitmap photoBitMap = null;
        try {
            InputStream inputStream = new java.net.URL(url).openStream();
            photoBitMap = BitmapFactory.decodeStream(inputStream);
            photoBitMap = PhotoManager.resizePhoto(context, photoBitMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return photoBitMap;
    }

    public static Bitmap resizePhoto(Context context, Bitmap photoBitmap) {

        // get the with of the device screen
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();

        int screenWidth = metrics.widthPixels;

        int photoBitmapWidth = photoBitmap.getWidth();
        int photoBitmapHeight = photoBitmap.getHeight();

        if (photoBitmapWidth > screenWidth){

            float scaleRatio = ((float) screenWidth) / photoBitmapWidth;

            // Create a matrix for the manipulation
            Matrix matrix = new Matrix();

            // Resize the bit map
            matrix.postScale(scaleRatio, scaleRatio);

            return Bitmap.createBitmap(photoBitmap, 0, 0, photoBitmapWidth, photoBitmapHeight, matrix, false);
        }
        else {
            return photoBitmap;
        }
    }

    public static void saveToInternalStorage(Activity activity, Bitmap bitmapImage){

        ContextWrapper contextWrapper = new ContextWrapper(activity);
        // path to /data/user/0/mprog.nl.mars_weather_explorer/app_roverImageDir
        File directory = contextWrapper.getDir("roverImageDir", Context.MODE_PRIVATE);
        // Create image file
        File imgFile = new File(directory, "roverImage.jpg");

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(imgFile);
            // Use the compress method on the BitMap object to write the image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SharedPreferencesManager.getInstance(activity).setImageFilePath(imgFile.getAbsolutePath());
    }
}

@@ -34,16 +34,79 @@
 lorer;%0A%0A
+/**%0A * PhotoManager.java%0A *%0A * Created by Nadeche Studer%0A * */%0A
 import a
@@ -452,67 +452,243 @@
 m;%0A%0A
-%0A
 /**%0A * 
-Created by Nadeche%0A */%0Apublic class PhotoManager %7B%0A
+This class provides methods to handle photo related operations:%0A * download, resize and save.%0A */%0Apublic class PhotoManager %7B%0A%0A    /** This method downloads a photo from the given url and resizes it before returning the photo */
 %0A   
@@ -1120,32 +1120,124 @@
 oBitMap;%0A    %7D%0A%0A
+    /** This method resizes a photo according to the width of the screen from the device */%0A
     public stati
@@ -1318,16 +1318,17 @@
 t the wi
+d
 th of th
@@ -1416,25 +1416,24 @@
 yMetrics();%0A
-%0A
         int 
@@ -1468,16 +1468,67 @@
 ixels;%0A%0A
+        // get the current dimensions of the photo%0A
         
@@ -1674,24 +1674,87 @@
 eenWidth)%7B%0A%0A
+            // calculate the scale ratio to reduce the size by%0A
             
@@ -1912,43 +1912,8 @@
 x();
-%0A%0A            // Resize the bit map
 %0A   
@@ -1960,24 +1960,96 @@
 aleRatio);%0A%0A
+            // Resize the bit map with height and width ratio conserved%0A
             
@@ -2217,24 +2217,202 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * This method saves a photo to internal storage not accessible for the user%0A     * but to retrieve internally by the widget and the app to save data usage%0A     * */%0A
     public s
@@ -2555,16 +2555,17 @@
 ivity);%0A
+%0A
         
@@ -2566,16 +2566,24 @@
       //
+ get the
  path to
@@ -2743,23 +2743,28 @@
      // 
-C
+ c
 reate 
+the 
 image fi
@@ -2765,16 +2765,42 @@
 age file
+ or overwrite if it exists
 %0A       
@@ -2977,24 +2977,25 @@
 m(imgFile);%0A
+%0A
             
@@ -3000,16 +3000,8 @@
   //
- Use the
  com
@@ -3010,38 +3010,21 @@
 ess 
-method on the BitMap object to
+the image and
  wri
@@ -3446,32 +3446,114 @@
     %7D%0A        %7D%0A
+        // save the filepath to where the image is saved in the sharedPreferences%0A
         SharedPr

Refactor and comments added to PhotoManager.