Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.compatibility.common.util;
     18 
     19 import android.content.Context;
     20 
     21 import java.io.File;
     22 import java.io.FileOutputStream;
     23 import java.io.IOException;
     24 import java.io.InputStream;
     25 import java.io.OutputStream;
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * FileCopyHelper is used to copy files from resources to the
     30  * application directory and responsible for deleting the files.
     31  *
     32  * @see MediaStore_VideoTest
     33  * @see MediaStore_Images_MediaTest
     34  * @see MediaStore_Images_ThumbnailsTest
     35  */
     36 public class FileCopyHelper {
     37     /** The context. */
     38     private Context mContext;
     39 
     40     /** The files added. */
     41     private ArrayList<String> mFilesList;
     42 
     43     /**
     44      * Instantiates a new file copy helper.
     45      *
     46      * @param context the context
     47      */
     48     public FileCopyHelper(Context context) {
     49         mContext = context;
     50         mFilesList = new ArrayList<String>();
     51     }
     52 
     53     /**
     54      * Copy the file from the resources with a filename.
     55      *
     56      * @param resId the res id
     57      * @param fileName the file name
     58      *
     59      * @return the absolute path of the destination file
     60      * @throws IOException
     61      */
     62     public String copy(int resId, String fileName) throws IOException {
     63         InputStream source = mContext.getResources().openRawResource(resId);
     64         OutputStream target = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
     65         copyFile(source, target);
     66         mFilesList.add(fileName);
     67         return mContext.getFileStreamPath(fileName).getAbsolutePath();
     68     }
     69 
     70     public void copyToExternalStorage(int resId, File path) throws IOException {
     71         InputStream source = mContext.getResources().openRawResource(resId);
     72         OutputStream target = new FileOutputStream(path);
     73         copyFile(source, target);
     74     }
     75 
     76     private void copyFile(InputStream source, OutputStream target) throws IOException {
     77         try {
     78             byte[] buffer = new byte[1024];
     79             for (int len = source.read(buffer); len > 0; len = source.read(buffer)) {
     80                 target.write(buffer, 0, len);
     81             }
     82         } finally {
     83             if (source != null) {
     84                 source.close();
     85             }
     86             if (target != null) {
     87                 target.close();
     88             }
     89         }
     90     }
     91 
     92     /**
     93      * Delete all the files copied by the helper.
     94      */
     95     public void clear(){
     96         for (String path : mFilesList) {
     97             mContext.deleteFile(path);
     98         }
     99     }
    100 }
    101