Initial commit
[wl-mobile.git] / src / pl / org / nowoczesnapolska / wlmobi / AssetCopy.java
1 package pl.org.nowoczesnapolska.wlmobi;
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                         File fout = new File(targetPath);
49
50                         if(overwrite.equals("false") && fout.exists()) {
51                                 Log.d("AssetCopy", "File already exists");
52                                 return new PluginResult(PluginResult.Status.OK, "exist");
53                         }
54
55                         FileOutputStream fos = new FileOutputStream(fout);
56
57                         AssetManager assetManager = this.ctx.getResources().getAssets();
58                         InputStream is = assetManager.open(assetPath);
59
60                         byte[] buffer = new byte[1024];
61                         int len1 = 0;
62
63                         while ( (len1 = is.read(buffer)) > 0 ) {
64                                 fos.write(buffer,0, len1);
65                         }
66
67                         fos.close();
68
69                         Log.d("AssetCopy", "Copied to " + targetPath);
70                 } catch (IOException e) {
71                         Log.d("AssetCopy", "Error: " + e);
72                         return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
73                 }
74                 return new PluginResult(PluginResult.Status.OK, targetPath);
75         }
76 }