blob: 507eb06ef67828cd60de61d6a10843ba6dc22ac0 [file] [log] [blame]
Scott Su0af8f5c2009-04-23 20:47:48 -07001/*
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
17package android.provider.cts;
18
19import android.content.Context;
20
Brian Muramatsucb964682011-02-02 13:11:14 -080021import java.io.File;
22import java.io.FileOutputStream;
Scott Su0af8f5c2009-04-23 20:47:48 -070023import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.util.ArrayList;
27
28/**
29 * The Class 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 */
36public 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
Brian Muramatsuad704042012-01-10 15:22:20 -080060 * @throws IOException
Scott Su0af8f5c2009-04-23 20:47:48 -070061 */
Brian Muramatsuad704042012-01-10 15:22:20 -080062 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 }
Scott Su0af8f5c2009-04-23 20:47:48 -070069
Brian Muramatsuad704042012-01-10 15:22:20 -080070 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 {
Scott Su0af8f5c2009-04-23 20:47:48 -070077 try {
Scott Su0af8f5c2009-04-23 20:47:48 -070078 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 }
Scott Su0af8f5c2009-04-23 20:47:48 -070082 } finally {
Brian Muramatsuad704042012-01-10 15:22:20 -080083 if (source != null) {
84 source.close();
85 }
86 if (target != null) {
87 target.close();
Scott Su0af8f5c2009-04-23 20:47:48 -070088 }
89 }
Scott Su0af8f5c2009-04-23 20:47:48 -070090 }
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 }
Scott Su0af8f5c2009-04-23 20:47:48 -0700100}