switch to alsa.omap3 module
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / util / CompressManager.java
CommitLineData
811a5a4a 1package org.openintents.filemanager.util;\r
2\r
3import android.app.ProgressDialog;\r
4import android.content.Intent;\r
5import android.os.AsyncTask;\r
6import android.util.Log;\r
7import android.widget.Toast;\r
8import org.openintents.filemanager.FileManagerActivity;\r
9import org.openintents.filemanager.R;\r
10import org.openintents.intents.FileManagerIntents;\r
11\r
12import java.io.*;\r
13import java.util.ArrayList;\r
14import java.util.List;\r
15import java.util.zip.ZipEntry;\r
16import java.util.zip.ZipOutputStream;\r
17\r
18public class CompressManager {\r
19 /**\r
20 * TAG for log messages.\r
21 */\r
22 static final String TAG = "CompressManager";\r
23\r
24 private static final int BUFFER_SIZE = 1024;\r
25 private FileManagerActivity activity;\r
26 private ProgressDialog progressDialog;\r
27 private int fileCount;\r
28 private String fileOut;\r
29\r
30 public CompressManager(FileManagerActivity activity) {\r
31 this.activity = activity;\r
32 }\r
33\r
34 public void compress(File f, String out) {\r
35 List <File>list = new ArrayList<File>();\r
36 list.add(f);\r
37 compress(list, out);\r
38 } \r
39\r
40 public void compress(List<File> list, String out) {\r
41 if (list.isEmpty()){\r
42 Log.v(TAG, "couldn't compress empty file list");\r
43 return;\r
44 }\r
45 this.fileOut = list.get(0).getParent()+File.separator+out;\r
46 fileCount=0;\r
47 for (File f: list){\r
48 fileCount += FileUtils.getFileCount(f);\r
49 }\r
50 new CompressTask().execute(list);\r
51 }\r
52\r
53 private class CompressTask extends AsyncTask<Object, Void, Integer> {\r
54 private static final int success = 0;\r
55 private static final int error = 1;\r
56 private ZipOutputStream zos;\r
57\r
58 /**\r
59 * count of compressed file to update the progress bar\r
60 */\r
61 private int isCompressed = 0;\r
62\r
63 /**\r
64 * Recursively compress file or directory\r
65 * @returns 0 if successful, error value otherwise.\r
66 */\r
67 private void compressFile(File file, String path) throws IOException {\r
68 if (!file.isDirectory()){\r
69 byte[] buf = new byte[BUFFER_SIZE];\r
70 int len;\r
71 FileInputStream in = new FileInputStream(file);\r
72 zos.putNextEntry(new ZipEntry(path + "/" + file.getName()));\r
73 while ((len = in.read(buf)) > 0) {\r
74 zos.write(buf, 0, len);\r
75 }\r
76 in.close();\r
77 return;\r
78 }\r
79 if (file.list() == null){\r
80 return;\r
81 }\r
82 for (String fileName: file.list()){\r
83 File f = new File(file.getAbsolutePath()+File.separator+fileName);\r
84 compressFile(f, path + File.separator + file.getName());\r
85 isCompressed++;\r
86 progressDialog.setProgress((isCompressed * 100)/ fileCount);\r
87 }\r
88 }\r
89\r
90 @Override\r
91 protected void onPreExecute() {\r
92 FileOutputStream out = null;\r
93 progressDialog = new ProgressDialog(activity);\r
94 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\r
95 progressDialog.setMessage(activity.getResources().getString(R.string.compressing));\r
96 progressDialog.show();\r
97 progressDialog.setProgress(0);\r
98 try {\r
99 out = new FileOutputStream(new File(fileOut));\r
100 zos = new ZipOutputStream(new BufferedOutputStream(out));\r
101 } catch (FileNotFoundException e) {\r
102 Log.e(TAG, "error while creating ZipOutputStream");\r
103 }\r
104 }\r
105\r
106 @Override\r
107 protected Integer doInBackground(Object... params) {\r
108 if (zos == null){\r
109 return error;\r
110 }\r
111 List<File> list = (List<File>) params[0]; \r
112 for (File file:list){\r
113 try {\r
114 compressFile(file, "");\r
115 } catch (IOException e) {\r
116 Log.e(TAG, "Error while compressing", e);\r
117 return error;\r
118 }\r
119 }\r
120 return success;\r
121 }\r
122\r
123 @Override\r
124 protected void onPostExecute(Integer result) {\r
125 try {\r
126 zos.flush();\r
127 zos.close();\r
128 } catch (IOException e) {\r
129 Log.e(TAG, "error while closing zos", e);\r
130 }\r
131 progressDialog.cancel();\r
132 if (result == error){\r
133 Toast.makeText(activity, R.string.compressing_error, Toast.LENGTH_SHORT).show();\r
134 } else if (result == success){\r
135 Toast.makeText(activity, R.string.compressing_success, Toast.LENGTH_SHORT).show();\r
136 }\r
137\r
138 if (activity.getIntent().getAction().equals(FileManagerIntents.ACTION_MULTI_SELECT)){\r
139 Intent intent = activity.getIntent();\r
140 activity.setResult(activity.RESULT_OK, intent);\r
141 activity.finish();\r
142 } else {\r
143 activity.refreshList();\r
144 }\r
145 }\r
146 }\r
147}\r