1 package pl.org.nowoczesnapolska.wlmobi;
7 import org.json.JSONArray;
8 import org.json.JSONException;
10 import android.util.Log;
11 import android.content.res.AssetManager;
13 import com.phonegap.api.Plugin;
14 import com.phonegap.api.PluginResult;
17 import java.io.FileOutputStream;
18 import java.io.InputStream;
19 import java.io.IOException;
21 public class AssetCopy extends Plugin{
24 public PluginResult execute(String action, JSONArray args, String callbackId) {
25 if (action.equals("copy")) {
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");
33 return new PluginResult(PluginResult.Status.INVALID_ACTION);
37 private PluginResult copy(String assetPath, String targetPath, String overwrite) {
38 int index = targetPath.lastIndexOf('/');
39 String targetDir = targetPath.substring(0, index);
42 File dir = new File(targetDir);
44 Log.d("AssetCopy", "directory " + targetDir + " created");
48 File fout = new File(targetPath);
50 if(overwrite.equals("false") && fout.exists()) {
51 Log.d("AssetCopy", "File already exists");
52 return new PluginResult(PluginResult.Status.OK, "exist");
55 FileOutputStream fos = new FileOutputStream(fout);
57 AssetManager assetManager = this.ctx.getResources().getAssets();
58 InputStream is = assetManager.open(assetPath);
60 byte[] buffer = new byte[1024];
63 while ( (len1 = is.read(buffer)) > 0 ) {
64 fos.write(buffer,0, len1);
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);
74 return new PluginResult(PluginResult.Status.OK, targetPath);