1 package pl.org.nowoczesnapolska.wloffline;
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 Log.d("AssetCopy", "1");
49 File fout = new File(targetPath);
50 Log.d("AssetCopy", "2");
52 if(overwrite.equals("false") && fout.exists()) {
53 Log.d("AssetCopy", "File already exists");
54 return new PluginResult(PluginResult.Status.OK, "exist");
56 Log.d("AssetCopy", "3");
58 FileOutputStream fos = new FileOutputStream(fout);
59 Log.d("AssetCopy", "4");
61 AssetManager assetManager = this.ctx.getResources().getAssets();
62 InputStream is = assetManager.open(assetPath);
64 byte[] buffer = new byte[1024];
67 while ( (len1 = is.read(buffer)) > 0 ) {
68 fos.write(buffer,0, len1);
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);
78 return new PluginResult(PluginResult.Status.OK, targetPath);