offline version
[wl-mobile.git] / src / pl / org / nowoczesnapolska / wloffline / AssetCopy.java
1 package pl.org.nowoczesnapolska.wloffline;
2
3 /*
4  @author Radek Czajka
5  */
6
7 import org.json.JSONArray;
8 import org.json.JSONException;
9
10 import android.util.Log;
11 import android.content.res.AssetManager;
12
13 import com.phonegap.api.Plugin;
14 import com.phonegap.api.PluginResult;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.InputStream;
19 import java.io.IOException;
20
21 public class AssetCopy extends Plugin{
22
23         @Override
24         public PluginResult execute(String action, JSONArray args, String callbackId) {
25                 if (action.equals("copy")) {
26                         try {
27                                 return this.copy(args.getString(0), args.getString(1), args.getString(2));
28                         } catch (JSONException e) {
29                                 return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
30                         }
31                 }
32                 else {
33                         return new PluginResult(PluginResult.Status.INVALID_ACTION);
34                 }
35         }
36
37         private PluginResult copy(String assetPath, String targetPath, String overwrite) {
38                 int index = targetPath.lastIndexOf('/');
39                 String targetDir = targetPath.substring(0, index);
40
41                 try {
42                         File dir = new File(targetDir);
43                         if(!dir.exists()) {
44                                 Log.d("AssetCopy", "directory " + targetDir + " created");
45                                 dir.mkdirs();
46                         }
47
48                         Log.d("AssetCopy", "1");
49                         File fout = new File(targetPath);
50                         Log.d("AssetCopy", "2");
51
52                         if(overwrite.equals("false") && fout.exists()) {
53                                 Log.d("AssetCopy", "File already exists");
54                                 return new PluginResult(PluginResult.Status.OK, "exist");
55                         }
56                         Log.d("AssetCopy", "3");
57
58                         FileOutputStream fos = new FileOutputStream(fout);
59                         Log.d("AssetCopy", "4");
60
61                         AssetManager assetManager = this.ctx.getResources().getAssets();
62                         InputStream is = assetManager.open(assetPath);
63
64                         byte[] buffer = new byte[1024];
65                         int len1 = 0;
66
67                         while ( (len1 = is.read(buffer)) > 0 ) {
68                                 fos.write(buffer,0, len1);
69                         }
70
71                         fos.close();
72
73                         Log.d("AssetCopy", "Copied to " + targetPath);
74                 } catch (IOException e) {
75                         Log.d("AssetCopy", "Error: " + e);
76                         return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
77                 }
78                 return new PluginResult(PluginResult.Status.OK, targetPath);
79         }
80 }