/*
 * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.amazonaws.devicefarm.android.referenceapp.Util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

/**
 * Screenshot taker for Espresso tests within Device Farm
 *
 * Assigns a random file name to the image and saves it into a specific device farm directory
 */
public class ScreenShot {
    private static final String TAG = "SCREENSHOT";
    private static final String DEVICE_FARM_ESPRESSO_SCREEN_DIRECTORY = "/test-screenshots/";
    private static final int SCREEN_SHOT_IMAGE_QUALITY = 100;

    public static void take(Activity activity){

        final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + DEVICE_FARM_ESPRESSO_SCREEN_DIRECTORY + UUID.randomUUID().toString();;
        Log.i(TAG, "Saving to path: " +  path);

        View phoneView = activity.getWindow().getDecorView().getRootView();
        phoneView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(phoneView.getDrawingCache());
        phoneView.setDrawingCacheEnabled(false);

        OutputStream out = null;

        File imageFile = new File(path);

        try {
            out = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, SCREEN_SHOT_IMAGE_QUALITY, out);
            out.flush();
        }

        catch (FileNotFoundException e) {
            Log.e(TAG, e.toString());
        }
        catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            }
        }
    }
}

@@ -1006,16 +1006,20 @@
  within 
+AWS 
 Device F
@@ -1032,51 +1032,84 @@
 %0A * 
-Assigns a random file name to the image
+Saves the image with the specified name (or a randomly generated one)
  and
+%0A *
  sav
@@ -1125,38 +1125,68 @@
 o a 
-specific d
+directory D
 evice 
-f
+F
 arm 
-directory
+will pull from when generating reports
 %0A */
@@ -1470,40 +1470,156 @@
 vity
-)%7B%0A%0A        final String path = 
+, String fileName) %7B%0A        // Create the directory path%0A        final StringBuilder pathBuilder = new StringBuilder();%0A        pathBuilder.append(
 Envi
@@ -1673,19 +1673,46 @@
 tePath()
- + 
+);%0A        pathBuilder.append(
 DEVICE_F
@@ -1744,28 +1744,445 @@
 TORY
- + UUID.randomUUID()
+);%0A%0A        // Verify that the directory exists and create it if not%0A        File directoryPath = new File(pathBuilder.toString());%0A        if (!directoryPath.isDirectory()) %7B%0A            Log.i(TAG, %22Creating directory: %22 + directoryPath);%0A            directoryPath.mkdirs();%0A        %7D%0A%0A        // Build the full file path%0A        pathBuilder.append(fileName);%0A        pathBuilder.append(%22.png%22);%0A%0A        final String path = pathBuilder
 .toS
@@ -2189,17 +2189,17 @@
 tring();
-;
+%0A
 %0A       
@@ -2230,17 +2230,16 @@
 ath: %22 +
- 
  path);%0A
@@ -3154,11 +3154,121 @@
  %7D%0A    %7D
+%0A%0A    public static void take(Activity activity) %7B%0A        take(activity, UUID.randomUUID().toString());%0A    %7D
 %0A%7D%0A

Update screenshot utility