package com.google.android.libraries.motionphotoreader;

import android.content.Context;
import android.util.Log;

import com.google.common.io.ByteStreams;

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

/**
 * A suite of methods to fetch resource files, asset files, and raw files.
 */
class ResourceFetcher {
    private static final String TAG = "ResourceFetcher";

    /**
     * Gets a resource from the res/raw folder and creates a temporary file holding the resource.
     * Used to fetch motion photo files.
     * @param context The context containing the raw resource.
     * @param id The id of the file.
     * @param prefix The name of the temporary file to create from the raw resource.
     * @param suffix The extension of the temporary file to create from the raw resource.
     * @return a File object containing the raw resource file.
     */
    public static File fetchRawFile(Context context,
                                    int id, String prefix,
                                    String suffix) throws IOException {
        try (InputStream input = context.getResources().openRawResource(id)) {
            File file = null;
            try {
                file = File.createTempFile(prefix, suffix);
            } catch (IOException e) {
                Log.e(TAG, "Error fetching raw file", e);
            }
            writeBytesToFile(input, file);
            return file;
        }
    }

    /**
     * Gets a resource from the res/raw folder and creates a temporary file holding the resource.
     * Used to fetch motion photo files.
     * @param classLoader The class loader for the resource.
     * @param filename The name of the resource file.
     * @return a File object containing the raw resource file.
     */
    public static File fetchResourceFile(ClassLoader classLoader, String filename) {
        URL resource = classLoader.getResource(filename);
        return new File(resource.getPath());
    }

    /**
     * Gets a resource from the assets folder of a given context and creates a temporary file
     * holding the resource. Used in instrumentation tests to fetch motion photo files.
     * @param context The context containing the asset file.
     * @param filename The name of the asset file.
     * @return a File object containing the raw resource file.
     * @throws IOException if an error occurs while creating the new file
     */
    public static File fetchAssetFile(Context context,
                                      String filename,
                                      String prefix,
                                      String suffix) throws IOException {
        try (InputStream input = context.getResources().getAssets().open(filename)) {
            // Write file to temporary folder for instrumentation test access
            File f = File.createTempFile(prefix, suffix);
            writeBytesToFile(input, f);
            return f;
        }
    }

    private static void writeBytesToFile(InputStream input, File file) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            ByteStreams.copy(input, fos);
        }
    }
}

@@ -451,1067 +451,8 @@
 %22;%0A%0A
-    /**%0A     * Gets a resource from the res/raw folder and creates a temporary file holding the resource.%0A     * Used to fetch motion photo files.%0A     * @param context The context containing the raw resource.%0A     * @param id The id of the file.%0A     * @param prefix The name of the temporary file to create from the raw resource.%0A     * @param suffix The extension of the temporary file to create from the raw resource.%0A     * @return a File object containing the raw resource file.%0A     */%0A    public static File fetchRawFile(Context context,%0A                                    int id, String prefix,%0A                                    String suffix) throws IOException %7B%0A        try (InputStream input = context.getResources().openRawResource(id)) %7B%0A            File file = null;%0A            try %7B%0A                file = File.createTempFile(prefix, suffix);%0A            %7D catch (IOException e) %7B%0A                Log.e(TAG, %22Error fetching raw file%22, e);%0A            %7D%0A            writeBytesToFile(input, file);%0A            return file;%0A        %7D%0A    %7D%0A%0A
     

Delete all raw files and corresponding method to fetch them.