mobile_template 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +28 -0
- data/Rakefile +3 -0
- data/bin/mobile_template +71 -0
- data/lib/mobile_template/version.rb +4 -0
- data/lib/mobile_template.rb +5 -0
- data/mobile_template.gemspec +21 -0
- data/templates/assets/Gemfile +4 -0
- data/templates/assets/config.rb +96 -0
- data/templates/assets/config.ru +4 -0
- data/templates/assets/source/images/vendor/ajax-loader.gif +0 -0
- data/templates/assets/source/images/vendor/ajax-loader.png +0 -0
- data/templates/assets/source/images/vendor/icons-18-black.png +0 -0
- data/templates/assets/source/images/vendor/icons-18-white.png +0 -0
- data/templates/assets/source/images/vendor/icons-36-black.png +0 -0
- data/templates/assets/source/images/vendor/icons-36-white.png +0 -0
- data/templates/assets/source/index.html.erb +2 -0
- data/templates/assets/source/javascripts/app/index.js.coffee +2 -0
- data/templates/assets/source/javascripts/application.js.coffee +5 -0
- data/templates/assets/source/javascripts/vendor/cordova.js +4841 -0
- data/templates/assets/source/javascripts/vendor/jquery.js +9267 -0
- data/templates/assets/source/javascripts/vendor/jquery.mobile.js +7410 -0
- data/templates/assets/source/layout.erb +24 -0
- data/templates/assets/source/stylesheets/application.css.scss +2 -0
- data/templates/assets/source/stylesheets/vendor/jquery.mobile.css.scss +1872 -0
- data/templates/cordova_android/.gitignore +18 -0
- data/templates/cordova_android/LICENSE +202 -0
- data/templates/cordova_android/NOTICE +5 -0
- data/templates/cordova_android/README.md +95 -0
- data/templates/cordova_android/VERSION +1 -0
- data/templates/cordova_android/bin/BOOM +4 -0
- data/templates/cordova_android/bin/autotest +2 -0
- data/templates/cordova_android/bin/bench +29 -0
- data/templates/cordova_android/bin/create +46 -0
- data/templates/cordova_android/bin/create.bat +1 -0
- data/templates/cordova_android/bin/create.js +88 -0
- data/templates/cordova_android/bin/create.xml +79 -0
- data/templates/cordova_android/bin/node_modules/.bin/cake +7 -0
- data/templates/cordova_android/bin/node_modules/.bin/coffee +7 -0
- data/templates/cordova_android/bin/node_modules/.bin/nodeunit +120 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/.npmignore +11 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/LICENSE +22 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/README +48 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/Rakefile +78 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/bin/cake +7 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/bin/coffee +7 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/extras/jsl.conf +44 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/browser.js +75 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/cake.js +76 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/coffee-script.js +135 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/command.js +301 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/grammar.js +591 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/helpers.js +66 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/index.js +8 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/lexer.js +656 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/nodes.js +2289 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/optparse.js +111 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/parser.js +676 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/repl.js +123 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/rewriter.js +363 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/lib/scope.js +120 -0
- data/templates/cordova_android/bin/node_modules/coffee-script/package.json +27 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/.gitignore +5 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/.npmignore +3 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/CONTRIBUTORS.md +60 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/LICENSE +19 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/Makefile +126 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/README.md +432 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit +120 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit.json +10 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/deps/async.js +623 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/deps/console.log.js +55 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/deps/ejs.js +125 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/deps/json2.js +483 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/nodeunit.js +1757 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite1.js +12 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite2.js +13 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/test.html +16 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/img/example_fail.png +0 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/img/example_pass.png +0 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/index.js +3 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/assert.js +316 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/core.js +260 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/nodeunit.js +82 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/browser.js +119 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/default.js +123 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/html.js +107 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/index.js +9 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/junit.js +183 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/minimal.js +112 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/skip_passed.js +105 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/track.js +48 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/types.js +187 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/lib/utils.js +209 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/man1/nodeunit.1 +95 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/nodelint.cfg +4 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/package.json +56 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/share/junit.xml.ejs +19 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/share/license.js +11 -0
- data/templates/cordova_android/bin/node_modules/nodeunit/share/nodeunit.css +70 -0
- data/templates/cordova_android/bin/templates/project/.cordova/android/readme.md +1 -0
- data/templates/cordova_android/bin/templates/project/.cordova/readme.md +3 -0
- data/templates/cordova_android/bin/templates/project/cordova/create +36 -0
- data/templates/cordova_android/bin/templates/project/cordova/debug +9 -0
- data/templates/cordova_android/bin/templates/project/cordova/emulate +12 -0
- data/templates/cordova_android/bin/templates/project/cordova/log +3 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/Activity.java +16 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/AndroidManifest.xml +50 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/index.html +42 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/main.js +146 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/master.css +96 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/drawable/icon.png +0 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/cordova.xml +5 -0
- data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/plugins.xml +19 -0
- data/templates/cordova_android/bin/test +26 -0
- data/templates/cordova_android/bin/tests/autotest.coffee +4 -0
- data/templates/cordova_android/bin/tests/create.coffee +21 -0
- data/templates/cordova_android/bin/tests/debug.coffee +0 -0
- data/templates/cordova_android/bin/tests/test.coffee +0 -0
- data/templates/cordova_android/framework/.classpath +8 -0
- data/templates/cordova_android/framework/.project +33 -0
- data/templates/cordova_android/framework/AndroidManifest.xml +68 -0
- data/templates/cordova_android/framework/ant.properties +34 -0
- data/templates/cordova_android/framework/assets/js/accelerometer.js +137 -0
- data/templates/cordova_android/framework/assets/js/app.js +89 -0
- data/templates/cordova_android/framework/assets/js/battery.js +134 -0
- data/templates/cordova_android/framework/assets/js/camera.js +168 -0
- data/templates/cordova_android/framework/assets/js/capture.js +203 -0
- data/templates/cordova_android/framework/assets/js/compass.js +168 -0
- data/templates/cordova_android/framework/assets/js/contact.js +310 -0
- data/templates/cordova_android/framework/assets/js/cordova.android.js +4841 -0
- data/templates/cordova_android/framework/assets/js/cordova.js.base +924 -0
- data/templates/cordova_android/framework/assets/js/crypto.js +54 -0
- data/templates/cordova_android/framework/assets/js/device.js +83 -0
- data/templates/cordova_android/framework/assets/js/file.js +1082 -0
- data/templates/cordova_android/framework/assets/js/filetransfer.js +125 -0
- data/templates/cordova_android/framework/assets/js/geolocation.js +209 -0
- data/templates/cordova_android/framework/assets/js/header.txt +19 -0
- data/templates/cordova_android/framework/assets/js/media.js +233 -0
- data/templates/cordova_android/framework/assets/js/network.js +100 -0
- data/templates/cordova_android/framework/assets/js/notification.js +133 -0
- data/templates/cordova_android/framework/assets/js/position.js +100 -0
- data/templates/cordova_android/framework/assets/js/storage.js +439 -0
- data/templates/cordova_android/framework/assets/www/index.html +27 -0
- data/templates/cordova_android/framework/build.xml +216 -0
- data/templates/cordova_android/framework/libs/commons-codec-1.3.jar +0 -0
- data/templates/cordova_android/framework/proguard-project.txt +20 -0
- data/templates/cordova_android/framework/project.properties +14 -0
- data/templates/cordova_android/framework/res/drawable/icon.png +0 -0
- data/templates/cordova_android/framework/res/drawable/splash.png +0 -0
- data/templates/cordova_android/framework/res/layout/main.xml +29 -0
- data/templates/cordova_android/framework/res/values/strings.xml +23 -0
- data/templates/cordova_android/framework/res/xml/cordova.xml +37 -0
- data/templates/cordova_android/framework/res/xml/plugins.xml +37 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/IPlugin.java +27 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/LOG.java +28 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/PhonegapActivity.java +28 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/Plugin.java +27 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/PluginManager.java +35 -0
- data/templates/cordova_android/framework/src/com/phonegap/api/PluginResult.java +53 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/AccelListener.java +311 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/App.java +198 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/AudioHandler.java +364 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/AudioPlayer.java +450 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/AuthenticationToken.java +69 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/BatteryListener.java +156 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/CallbackServer.java +431 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/CameraLauncher.java +500 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/Capture.java +400 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/CompassListener.java +308 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessor.java +198 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessorSdk5.java +1934 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/ContactManager.java +113 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/CordovaChromeClient.java +314 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/CordovaWebViewClient.java +306 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/Device.java +219 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/DirectoryManager.java +161 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/DroidGap.java +1417 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/ExifHelper.java +165 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/FileTransfer.java +458 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/FileUploadResult.java +63 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/FileUtils.java +1048 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/GeoBroker.java +165 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/GeoListener.java +133 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/GpsListener.java +163 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/HttpHandler.java +80 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/LinearLayoutSoftKeyboardDetect.java +104 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/NetworkListener.java +153 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/NetworkManager.java +248 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/Notification.java +366 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceNode.java +34 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceSet.java +62 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/StandAlone.java +35 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/Storage.java +239 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/TempListener.java +112 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/CordovaInterface.java +145 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/IPlugin.java +116 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/LOG.java +234 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/Plugin.java +210 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginManager.java +359 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginResult.java +119 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/file/EncodingException.java +28 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/file/FileExistsException.java +28 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/file/InvalidModificationException.java +29 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/file/NoModificationAllowedException.java +28 -0
- data/templates/cordova_android/framework/src/org/apache/cordova/file/TypeMismatchException.java +29 -0
- data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceNodeTest.java +53 -0
- data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceSetTest.java +73 -0
- metadata +279 -0
@@ -0,0 +1,1048 @@
|
|
1
|
+
/*
|
2
|
+
Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
or more contributor license agreements. See the NOTICE file
|
4
|
+
distributed with this work for additional information
|
5
|
+
regarding copyright ownership. The ASF licenses this file
|
6
|
+
to you under the Apache License, Version 2.0 (the
|
7
|
+
"License"); you may not use this file except in compliance
|
8
|
+
with the License. You may obtain a copy of the License at
|
9
|
+
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
|
12
|
+
Unless required by applicable law or agreed to in writing,
|
13
|
+
software distributed under the License is distributed on an
|
14
|
+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
KIND, either express or implied. See the License for the
|
16
|
+
specific language governing permissions and limitations
|
17
|
+
under the License.
|
18
|
+
*/
|
19
|
+
package org.apache.cordova;
|
20
|
+
|
21
|
+
import java.io.*;
|
22
|
+
import java.net.MalformedURLException;
|
23
|
+
import java.net.URL;
|
24
|
+
import java.net.URLDecoder;
|
25
|
+
import java.nio.channels.FileChannel;
|
26
|
+
|
27
|
+
import org.apache.commons.codec.binary.Base64;
|
28
|
+
import org.apache.cordova.api.CordovaInterface;
|
29
|
+
import org.apache.cordova.api.Plugin;
|
30
|
+
import org.apache.cordova.api.PluginResult;
|
31
|
+
import org.apache.cordova.file.EncodingException;
|
32
|
+
import org.apache.cordova.file.FileExistsException;
|
33
|
+
import org.apache.cordova.file.InvalidModificationException;
|
34
|
+
import org.apache.cordova.file.NoModificationAllowedException;
|
35
|
+
import org.apache.cordova.file.TypeMismatchException;
|
36
|
+
import org.json.JSONArray;
|
37
|
+
import org.json.JSONException;
|
38
|
+
import org.json.JSONObject;
|
39
|
+
|
40
|
+
import android.database.Cursor;
|
41
|
+
import android.net.Uri;
|
42
|
+
import android.os.Environment;
|
43
|
+
import android.provider.MediaStore;
|
44
|
+
import android.util.Log;
|
45
|
+
import android.webkit.MimeTypeMap;
|
46
|
+
|
47
|
+
|
48
|
+
/**
|
49
|
+
* This class provides SD card file and directory services to JavaScript.
|
50
|
+
* Only files on the SD card can be accessed.
|
51
|
+
*/
|
52
|
+
public class FileUtils extends Plugin {
|
53
|
+
private static final String LOG_TAG = "FileUtils";
|
54
|
+
private static final String _DATA = "_data"; // The column name where the file path is stored
|
55
|
+
|
56
|
+
public static int NOT_FOUND_ERR = 1;
|
57
|
+
public static int SECURITY_ERR = 2;
|
58
|
+
public static int ABORT_ERR = 3;
|
59
|
+
|
60
|
+
public static int NOT_READABLE_ERR = 4;
|
61
|
+
public static int ENCODING_ERR = 5;
|
62
|
+
public static int NO_MODIFICATION_ALLOWED_ERR = 6;
|
63
|
+
public static int INVALID_STATE_ERR = 7;
|
64
|
+
public static int SYNTAX_ERR = 8;
|
65
|
+
public static int INVALID_MODIFICATION_ERR = 9;
|
66
|
+
public static int QUOTA_EXCEEDED_ERR = 10;
|
67
|
+
public static int TYPE_MISMATCH_ERR = 11;
|
68
|
+
public static int PATH_EXISTS_ERR = 12;
|
69
|
+
|
70
|
+
public static int TEMPORARY = 0;
|
71
|
+
public static int PERSISTENT = 1;
|
72
|
+
public static int RESOURCE = 2;
|
73
|
+
public static int APPLICATION = 3;
|
74
|
+
|
75
|
+
FileReader f_in;
|
76
|
+
FileWriter f_out;
|
77
|
+
|
78
|
+
/**
|
79
|
+
* Constructor.
|
80
|
+
*/
|
81
|
+
public FileUtils() {
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Executes the request and returns PluginResult.
|
86
|
+
*
|
87
|
+
* @param action The action to execute.
|
88
|
+
* @param args JSONArry of arguments for the plugin.
|
89
|
+
* @param callbackId The callback id used when calling back into JavaScript.
|
90
|
+
* @return A PluginResult object with a status and message.
|
91
|
+
*/
|
92
|
+
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
93
|
+
PluginResult.Status status = PluginResult.Status.OK;
|
94
|
+
String result = "";
|
95
|
+
//System.out.println("FileUtils.execute("+action+")");
|
96
|
+
|
97
|
+
try {
|
98
|
+
if (action.equals("testSaveLocationExists")) {
|
99
|
+
boolean b = DirectoryManager.testSaveLocationExists();
|
100
|
+
return new PluginResult(status, b);
|
101
|
+
}
|
102
|
+
else if (action.equals("getFreeDiskSpace")) {
|
103
|
+
long l = DirectoryManager.getFreeDiskSpace(false);
|
104
|
+
return new PluginResult(status, l);
|
105
|
+
}
|
106
|
+
else if (action.equals("testFileExists")) {
|
107
|
+
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
108
|
+
return new PluginResult(status, b);
|
109
|
+
}
|
110
|
+
else if (action.equals("testDirectoryExists")) {
|
111
|
+
boolean b = DirectoryManager.testFileExists(args.getString(0));
|
112
|
+
return new PluginResult(status, b);
|
113
|
+
}
|
114
|
+
else if (action.equals("readAsText")) {
|
115
|
+
String s = this.readAsText(args.getString(0), args.getString(1));
|
116
|
+
return new PluginResult(status, s);
|
117
|
+
}
|
118
|
+
else if (action.equals("readAsDataURL")) {
|
119
|
+
String s = this.readAsDataURL(args.getString(0));
|
120
|
+
return new PluginResult(status, s);
|
121
|
+
}
|
122
|
+
else if (action.equals("write")) {
|
123
|
+
long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2));
|
124
|
+
return new PluginResult(status, fileSize);
|
125
|
+
}
|
126
|
+
else if (action.equals("truncate")) {
|
127
|
+
long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
|
128
|
+
return new PluginResult(status, fileSize);
|
129
|
+
}
|
130
|
+
else if (action.equals("requestFileSystem")) {
|
131
|
+
long size = args.optLong(1);
|
132
|
+
if (size != 0) {
|
133
|
+
if (size > (DirectoryManager.getFreeDiskSpace(true)*1024)) {
|
134
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
JSONObject obj = requestFileSystem(args.getInt(0));
|
138
|
+
return new PluginResult(status, obj);
|
139
|
+
}
|
140
|
+
else if (action.equals("resolveLocalFileSystemURI")) {
|
141
|
+
JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
|
142
|
+
return new PluginResult(status, obj);
|
143
|
+
}
|
144
|
+
else if (action.equals("getMetadata")) {
|
145
|
+
JSONObject obj = getMetadata(args.getString(0));
|
146
|
+
return new PluginResult(status, obj);
|
147
|
+
}
|
148
|
+
else if (action.equals("getFileMetadata")) {
|
149
|
+
JSONObject obj = getFileMetadata(args.getString(0));
|
150
|
+
return new PluginResult(status, obj);
|
151
|
+
}
|
152
|
+
else if (action.equals("getParent")) {
|
153
|
+
JSONObject obj = getParent(args.getString(0));
|
154
|
+
return new PluginResult(status, obj);
|
155
|
+
}
|
156
|
+
else if (action.equals("getDirectory")) {
|
157
|
+
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
|
158
|
+
return new PluginResult(status, obj);
|
159
|
+
}
|
160
|
+
else if (action.equals("getFile")) {
|
161
|
+
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
|
162
|
+
return new PluginResult(status, obj);
|
163
|
+
}
|
164
|
+
else if (action.equals("remove")) {
|
165
|
+
boolean success;
|
166
|
+
|
167
|
+
success = remove(args.getString(0));
|
168
|
+
|
169
|
+
if (success) {
|
170
|
+
notifyDelete(args.getString(0));
|
171
|
+
return new PluginResult(status);
|
172
|
+
} else {
|
173
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
174
|
+
}
|
175
|
+
}
|
176
|
+
else if (action.equals("removeRecursively")) {
|
177
|
+
boolean success = removeRecursively(args.getString(0));
|
178
|
+
if (success) {
|
179
|
+
return new PluginResult(status);
|
180
|
+
} else {
|
181
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
else if (action.equals("moveTo")) {
|
185
|
+
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
|
186
|
+
return new PluginResult(status, entry);
|
187
|
+
}
|
188
|
+
else if (action.equals("copyTo")) {
|
189
|
+
JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
|
190
|
+
return new PluginResult(status, entry);
|
191
|
+
}
|
192
|
+
else if (action.equals("readEntries")) {
|
193
|
+
JSONArray entries = readEntries(args.getString(0));
|
194
|
+
return new PluginResult(status, entries);
|
195
|
+
}
|
196
|
+
return new PluginResult(status, result);
|
197
|
+
} catch (FileNotFoundException e) {
|
198
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR);
|
199
|
+
} catch (FileExistsException e) {
|
200
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.PATH_EXISTS_ERR);
|
201
|
+
} catch (NoModificationAllowedException e) {
|
202
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
203
|
+
} catch (JSONException e) {
|
204
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.NO_MODIFICATION_ALLOWED_ERR);
|
205
|
+
} catch (InvalidModificationException e) {
|
206
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
|
207
|
+
} catch (MalformedURLException e) {
|
208
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
|
209
|
+
} catch (IOException e) {
|
210
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.INVALID_MODIFICATION_ERR);
|
211
|
+
} catch (EncodingException e) {
|
212
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.ENCODING_ERR);
|
213
|
+
} catch (TypeMismatchException e) {
|
214
|
+
return new PluginResult(PluginResult.Status.ERROR, FileUtils.TYPE_MISMATCH_ERR);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
/**
|
219
|
+
* Need to check to see if we need to clean up the content store
|
220
|
+
*
|
221
|
+
* @param filePath the path to check
|
222
|
+
*/
|
223
|
+
private void notifyDelete(String filePath) {
|
224
|
+
int result = this.ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
225
|
+
MediaStore.Images.Media.DATA + " = ?",
|
226
|
+
new String[] {filePath});
|
227
|
+
}
|
228
|
+
|
229
|
+
/**
|
230
|
+
* Allows the user to look up the Entry for a file or directory referred to by a local URI.
|
231
|
+
*
|
232
|
+
* @param url of the file/directory to look up
|
233
|
+
* @return a JSONObject representing a Entry from the filesystem
|
234
|
+
* @throws MalformedURLException if the url is not valid
|
235
|
+
* @throws FileNotFoundException if the file does not exist
|
236
|
+
* @throws IOException if the user can't read the file
|
237
|
+
* @throws JSONException
|
238
|
+
*/
|
239
|
+
private JSONObject resolveLocalFileSystemURI(String url) throws IOException, JSONException {
|
240
|
+
String decoded = URLDecoder.decode(url, "UTF-8");
|
241
|
+
|
242
|
+
File fp = null;
|
243
|
+
|
244
|
+
// Handle the special case where you get an Android content:// uri.
|
245
|
+
if (decoded.startsWith("content:")) {
|
246
|
+
Cursor cursor = this.ctx.managedQuery(Uri.parse(decoded), new String[] { MediaStore.Images.Media.DATA }, null, null, null);
|
247
|
+
// Note: MediaStore.Images/Audio/Video.Media.DATA is always "_data"
|
248
|
+
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
249
|
+
cursor.moveToFirst();
|
250
|
+
fp = new File(cursor.getString(column_index));
|
251
|
+
} else {
|
252
|
+
// Test to see if this is a valid URL first
|
253
|
+
@SuppressWarnings("unused")
|
254
|
+
URL testUrl = new URL(decoded);
|
255
|
+
|
256
|
+
if (decoded.startsWith("file://")) {
|
257
|
+
fp = new File(decoded.substring(7, decoded.length()));
|
258
|
+
} else {
|
259
|
+
fp = new File(decoded);
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
if (!fp.exists()) {
|
264
|
+
throw new FileNotFoundException();
|
265
|
+
}
|
266
|
+
if (!fp.canRead()) {
|
267
|
+
throw new IOException();
|
268
|
+
}
|
269
|
+
return getEntry(fp);
|
270
|
+
}
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Read the list of files from this directory.
|
274
|
+
*
|
275
|
+
* @param fileName the directory to read from
|
276
|
+
* @return a JSONArray containing JSONObjects that represent Entry objects.
|
277
|
+
* @throws FileNotFoundException if the directory is not found.
|
278
|
+
* @throws JSONException
|
279
|
+
*/
|
280
|
+
private JSONArray readEntries(String fileName) throws FileNotFoundException, JSONException {
|
281
|
+
File fp = createFileObject(fileName);
|
282
|
+
|
283
|
+
if (!fp.exists()) {
|
284
|
+
// The directory we are listing doesn't exist so we should fail.
|
285
|
+
throw new FileNotFoundException();
|
286
|
+
}
|
287
|
+
|
288
|
+
JSONArray entries = new JSONArray();
|
289
|
+
|
290
|
+
if (fp.isDirectory()) {
|
291
|
+
File[] files = fp.listFiles();
|
292
|
+
for (int i=0; i<files.length; i++) {
|
293
|
+
entries.put(getEntry(files[i]));
|
294
|
+
}
|
295
|
+
}
|
296
|
+
|
297
|
+
return entries;
|
298
|
+
}
|
299
|
+
|
300
|
+
/**
|
301
|
+
* A setup method that handles the move/copy of files/directories
|
302
|
+
*
|
303
|
+
* @param fileName to be copied/moved
|
304
|
+
* @param newParent is the location where the file will be copied/moved to
|
305
|
+
* @param newName for the file directory to be called, if null use existing file name
|
306
|
+
* @param move if false do a copy, if true do a move
|
307
|
+
* @return a Entry object
|
308
|
+
* @throws NoModificationAllowedException
|
309
|
+
* @throws IOException
|
310
|
+
* @throws InvalidModificationException
|
311
|
+
* @throws EncodingException
|
312
|
+
* @throws JSONException
|
313
|
+
*/
|
314
|
+
private JSONObject transferTo(String fileName, String newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException {
|
315
|
+
fileName = stripFileProtocol(fileName);
|
316
|
+
newParent = stripFileProtocol(newParent);
|
317
|
+
|
318
|
+
|
319
|
+
// Check for invalid file name
|
320
|
+
if (newName != null && newName.contains(":")) {
|
321
|
+
throw new EncodingException("Bad file name");
|
322
|
+
}
|
323
|
+
|
324
|
+
File source = new File(fileName);
|
325
|
+
|
326
|
+
if (!source.exists()) {
|
327
|
+
// The file/directory we are copying doesn't exist so we should fail.
|
328
|
+
throw new FileNotFoundException("The source does not exist");
|
329
|
+
}
|
330
|
+
|
331
|
+
File destinationDir = new File(newParent);
|
332
|
+
if (!destinationDir.exists()) {
|
333
|
+
// The destination does not exist so we should fail.
|
334
|
+
throw new FileNotFoundException("The source does not exist");
|
335
|
+
}
|
336
|
+
|
337
|
+
// Figure out where we should be copying to
|
338
|
+
File destination = createDestination(newName, source, destinationDir);
|
339
|
+
|
340
|
+
//Log.d(LOG_TAG, "Source: " + source.getAbsolutePath());
|
341
|
+
//Log.d(LOG_TAG, "Destin: " + destination.getAbsolutePath());
|
342
|
+
|
343
|
+
// Check to see if source and destination are the same file
|
344
|
+
if (source.getAbsolutePath().equals(destination.getAbsolutePath())) {
|
345
|
+
throw new InvalidModificationException("Can't copy a file onto itself");
|
346
|
+
}
|
347
|
+
|
348
|
+
if (source.isDirectory()) {
|
349
|
+
if (move) {
|
350
|
+
return moveDirectory(source, destination);
|
351
|
+
} else {
|
352
|
+
return copyDirectory(source, destination);
|
353
|
+
}
|
354
|
+
} else {
|
355
|
+
if (move) {
|
356
|
+
return moveFile(source, destination);
|
357
|
+
} else {
|
358
|
+
return copyFile(source, destination);
|
359
|
+
}
|
360
|
+
}
|
361
|
+
}
|
362
|
+
|
363
|
+
/**
|
364
|
+
* Creates the destination File object based on name passed in
|
365
|
+
*
|
366
|
+
* @param newName for the file directory to be called, if null use existing file name
|
367
|
+
* @param fp represents the source file
|
368
|
+
* @param destination represents the destination file
|
369
|
+
* @return a File object that represents the destination
|
370
|
+
*/
|
371
|
+
private File createDestination(String newName, File fp, File destination) {
|
372
|
+
File destFile = null;
|
373
|
+
|
374
|
+
// I know this looks weird but it is to work around a JSON bug.
|
375
|
+
if ("null".equals(newName) || "".equals(newName) ) {
|
376
|
+
newName = null;
|
377
|
+
}
|
378
|
+
|
379
|
+
if (newName != null) {
|
380
|
+
destFile = new File(destination.getAbsolutePath() + File.separator + newName);
|
381
|
+
} else {
|
382
|
+
destFile = new File(destination.getAbsolutePath() + File.separator + fp.getName());
|
383
|
+
}
|
384
|
+
return destFile;
|
385
|
+
}
|
386
|
+
|
387
|
+
/**
|
388
|
+
* Copy a file
|
389
|
+
*
|
390
|
+
* @param srcFile file to be copied
|
391
|
+
* @param destFile destination to be copied to
|
392
|
+
* @return a FileEntry object
|
393
|
+
* @throws IOException
|
394
|
+
* @throws InvalidModificationException
|
395
|
+
* @throws JSONException
|
396
|
+
*/
|
397
|
+
private JSONObject copyFile(File srcFile, File destFile) throws IOException, InvalidModificationException, JSONException {
|
398
|
+
// Renaming a file to an existing directory should fail
|
399
|
+
if (destFile.exists() && destFile.isDirectory()) {
|
400
|
+
throw new InvalidModificationException("Can't rename a file to a directory");
|
401
|
+
}
|
402
|
+
|
403
|
+
FileChannel input = new FileInputStream(srcFile).getChannel();
|
404
|
+
FileChannel output = new FileOutputStream(destFile).getChannel();
|
405
|
+
|
406
|
+
input.transferTo(0, input.size(), output);
|
407
|
+
|
408
|
+
input.close();
|
409
|
+
output.close();
|
410
|
+
|
411
|
+
/*
|
412
|
+
if (srcFile.length() != destFile.length()) {
|
413
|
+
return false;
|
414
|
+
}
|
415
|
+
*/
|
416
|
+
|
417
|
+
return getEntry(destFile);
|
418
|
+
}
|
419
|
+
|
420
|
+
/**
|
421
|
+
* Copy a directory
|
422
|
+
*
|
423
|
+
* @param srcDir directory to be copied
|
424
|
+
* @param destinationDir destination to be copied to
|
425
|
+
* @return a DirectoryEntry object
|
426
|
+
* @throws JSONException
|
427
|
+
* @throws IOException
|
428
|
+
* @throws NoModificationAllowedException
|
429
|
+
* @throws InvalidModificationException
|
430
|
+
*/
|
431
|
+
private JSONObject copyDirectory(File srcDir, File destinationDir) throws JSONException, IOException, NoModificationAllowedException, InvalidModificationException {
|
432
|
+
// Renaming a file to an existing directory should fail
|
433
|
+
if (destinationDir.exists() && destinationDir.isFile()) {
|
434
|
+
throw new InvalidModificationException("Can't rename a file to a directory");
|
435
|
+
}
|
436
|
+
|
437
|
+
// Check to make sure we are not copying the directory into itself
|
438
|
+
if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
|
439
|
+
throw new InvalidModificationException("Can't copy itself into itself");
|
440
|
+
}
|
441
|
+
|
442
|
+
// See if the destination directory exists. If not create it.
|
443
|
+
if (!destinationDir.exists()) {
|
444
|
+
if (!destinationDir.mkdir()) {
|
445
|
+
// If we can't create the directory then fail
|
446
|
+
throw new NoModificationAllowedException("Couldn't create the destination direcotry");
|
447
|
+
}
|
448
|
+
}
|
449
|
+
|
450
|
+
for (File file : srcDir.listFiles()) {
|
451
|
+
if (file.isDirectory()) {
|
452
|
+
copyDirectory(file, destinationDir);
|
453
|
+
} else {
|
454
|
+
File destination = new File(destinationDir.getAbsoluteFile() + File.separator + file.getName());
|
455
|
+
copyFile(file, destination);
|
456
|
+
}
|
457
|
+
}
|
458
|
+
|
459
|
+
return getEntry(destinationDir);
|
460
|
+
}
|
461
|
+
|
462
|
+
/**
|
463
|
+
* Check to see if the user attempted to copy an entry into its parent without changing its name,
|
464
|
+
* or attempted to copy a directory into a directory that it contains directly or indirectly.
|
465
|
+
*
|
466
|
+
* @param srcDir
|
467
|
+
* @param destinationDir
|
468
|
+
* @return
|
469
|
+
*/
|
470
|
+
private boolean isCopyOnItself(String src, String dest) {
|
471
|
+
|
472
|
+
// This weird test is to determine if we are copying or moving a directory into itself.
|
473
|
+
// Copy /sdcard/myDir to /sdcard/myDir-backup is okay but
|
474
|
+
// Copy /sdcard/myDir to /sdcard/myDir/backup should thow an INVALID_MODIFICATION_ERR
|
475
|
+
if (dest.startsWith(src) && dest.indexOf(File.separator, src.length()-1) != -1) {
|
476
|
+
return true;
|
477
|
+
}
|
478
|
+
|
479
|
+
return false;
|
480
|
+
}
|
481
|
+
|
482
|
+
/**
|
483
|
+
* Move a file
|
484
|
+
*
|
485
|
+
* @param srcFile file to be copied
|
486
|
+
* @param destFile destination to be copied to
|
487
|
+
* @return a FileEntry object
|
488
|
+
* @throws IOException
|
489
|
+
* @throws InvalidModificationException
|
490
|
+
* @throws JSONException
|
491
|
+
*/
|
492
|
+
private JSONObject moveFile(File srcFile, File destFile) throws JSONException, InvalidModificationException {
|
493
|
+
// Renaming a file to an existing directory should fail
|
494
|
+
if (destFile.exists() && destFile.isDirectory()) {
|
495
|
+
throw new InvalidModificationException("Can't rename a file to a directory");
|
496
|
+
}
|
497
|
+
|
498
|
+
// Try to rename the file
|
499
|
+
if (!srcFile.renameTo(destFile)) {
|
500
|
+
// Trying to rename the file failed. Possibly because we moved across file system on the device.
|
501
|
+
// Now we have to do things the hard way
|
502
|
+
// 1) Copy all the old file
|
503
|
+
// 2) delete the src file
|
504
|
+
}
|
505
|
+
|
506
|
+
return getEntry(destFile);
|
507
|
+
}
|
508
|
+
|
509
|
+
/**
|
510
|
+
* Move a directory
|
511
|
+
*
|
512
|
+
* @param srcDir directory to be copied
|
513
|
+
* @param destinationDir destination to be copied to
|
514
|
+
* @return a DirectoryEntry object
|
515
|
+
* @throws JSONException
|
516
|
+
* @throws IOException
|
517
|
+
* @throws InvalidModificationException
|
518
|
+
*/
|
519
|
+
private JSONObject moveDirectory(File srcDir, File destinationDir) throws JSONException, InvalidModificationException {
|
520
|
+
// Renaming a file to an existing directory should fail
|
521
|
+
if (destinationDir.exists() && destinationDir.isFile()) {
|
522
|
+
throw new InvalidModificationException("Can't rename a file to a directory");
|
523
|
+
}
|
524
|
+
|
525
|
+
// Check to make sure we are not copying the directory into itself
|
526
|
+
if (isCopyOnItself(srcDir.getAbsolutePath(), destinationDir.getAbsolutePath())) {
|
527
|
+
throw new InvalidModificationException("Can't move itself into itself");
|
528
|
+
}
|
529
|
+
|
530
|
+
// If the destination directory already exists and is empty then delete it. This is according to spec.
|
531
|
+
if (destinationDir.exists()) {
|
532
|
+
if (destinationDir.list().length > 0) {
|
533
|
+
throw new InvalidModificationException("directory is not empty");
|
534
|
+
}
|
535
|
+
}
|
536
|
+
|
537
|
+
// Try to rename the directory
|
538
|
+
if (!srcDir.renameTo(destinationDir)) {
|
539
|
+
// Trying to rename the directory failed. Possibly because we moved across file system on the device.
|
540
|
+
// Now we have to do things the hard way
|
541
|
+
// 1) Copy all the old files
|
542
|
+
// 2) delete the src directory
|
543
|
+
}
|
544
|
+
|
545
|
+
return getEntry(destinationDir);
|
546
|
+
}
|
547
|
+
|
548
|
+
/**
|
549
|
+
* Deletes a directory and all of its contents, if any. In the event of an error
|
550
|
+
* [e.g. trying to delete a directory that contains a file that cannot be removed],
|
551
|
+
* some of the contents of the directory may be deleted.
|
552
|
+
* It is an error to attempt to delete the root directory of a filesystem.
|
553
|
+
*
|
554
|
+
* @param filePath the directory to be removed
|
555
|
+
* @return a boolean representing success of failure
|
556
|
+
* @throws FileExistsException
|
557
|
+
*/
|
558
|
+
private boolean removeRecursively(String filePath) throws FileExistsException {
|
559
|
+
File fp = createFileObject(filePath);
|
560
|
+
|
561
|
+
// You can't delete the root directory.
|
562
|
+
if (atRootDirectory(filePath)) {
|
563
|
+
return false;
|
564
|
+
}
|
565
|
+
|
566
|
+
return removeDirRecursively(fp);
|
567
|
+
}
|
568
|
+
|
569
|
+
/**
|
570
|
+
* Loops through a directory deleting all the files.
|
571
|
+
*
|
572
|
+
* @param directory to be removed
|
573
|
+
* @return a boolean representing success of failure
|
574
|
+
* @throws FileExistsException
|
575
|
+
*/
|
576
|
+
private boolean removeDirRecursively(File directory) throws FileExistsException {
|
577
|
+
if (directory.isDirectory()) {
|
578
|
+
for (File file : directory.listFiles()) {
|
579
|
+
removeDirRecursively(file);
|
580
|
+
}
|
581
|
+
}
|
582
|
+
|
583
|
+
if (!directory.delete()) {
|
584
|
+
throw new FileExistsException("could not delete: " + directory.getName());
|
585
|
+
} else {
|
586
|
+
return true;
|
587
|
+
}
|
588
|
+
}
|
589
|
+
|
590
|
+
/**
|
591
|
+
* Deletes a file or directory. It is an error to attempt to delete a directory that is not empty.
|
592
|
+
* It is an error to attempt to delete the root directory of a filesystem.
|
593
|
+
*
|
594
|
+
* @param filePath file or directory to be removed
|
595
|
+
* @return a boolean representing success of failure
|
596
|
+
* @throws NoModificationAllowedException
|
597
|
+
* @throws InvalidModificationException
|
598
|
+
*/
|
599
|
+
private boolean remove(String filePath) throws NoModificationAllowedException, InvalidModificationException {
|
600
|
+
File fp = createFileObject(filePath);
|
601
|
+
|
602
|
+
// You can't delete the root directory.
|
603
|
+
if (atRootDirectory(filePath)) {
|
604
|
+
throw new NoModificationAllowedException("You can't delete the root directory");
|
605
|
+
}
|
606
|
+
|
607
|
+
// You can't delete a directory that is not empty
|
608
|
+
if (fp.isDirectory() && fp.list().length > 0) {
|
609
|
+
throw new InvalidModificationException("You can't delete a directory that is not empty.");
|
610
|
+
}
|
611
|
+
|
612
|
+
return fp.delete();
|
613
|
+
}
|
614
|
+
|
615
|
+
/**
|
616
|
+
* Creates or looks up a file.
|
617
|
+
*
|
618
|
+
* @param dirPath base directory
|
619
|
+
* @param fileName file/directory to lookup or create
|
620
|
+
* @param options specify whether to create or not
|
621
|
+
* @param directory if true look up directory, if false look up file
|
622
|
+
* @return a Entry object
|
623
|
+
* @throws FileExistsException
|
624
|
+
* @throws IOException
|
625
|
+
* @throws TypeMismatchException
|
626
|
+
* @throws EncodingException
|
627
|
+
* @throws JSONException
|
628
|
+
*/
|
629
|
+
private JSONObject getFile(String dirPath, String fileName, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException {
|
630
|
+
boolean create = false;
|
631
|
+
boolean exclusive = false;
|
632
|
+
if (options != null) {
|
633
|
+
create = options.optBoolean("create");
|
634
|
+
if (create) {
|
635
|
+
exclusive = options.optBoolean("exclusive");
|
636
|
+
}
|
637
|
+
}
|
638
|
+
|
639
|
+
// Check for a ":" character in the file to line up with BB and iOS
|
640
|
+
if (fileName.contains(":")) {
|
641
|
+
throw new EncodingException("This file has a : in it's name");
|
642
|
+
}
|
643
|
+
|
644
|
+
File fp = createFileObject(dirPath, fileName);
|
645
|
+
|
646
|
+
if (create) {
|
647
|
+
if (exclusive && fp.exists()) {
|
648
|
+
throw new FileExistsException("create/exclusive fails");
|
649
|
+
}
|
650
|
+
if (directory) {
|
651
|
+
fp.mkdir();
|
652
|
+
} else {
|
653
|
+
fp.createNewFile();
|
654
|
+
}
|
655
|
+
if (!fp.exists()) {
|
656
|
+
throw new FileExistsException("create fails");
|
657
|
+
}
|
658
|
+
}
|
659
|
+
else {
|
660
|
+
if (!fp.exists()) {
|
661
|
+
throw new FileNotFoundException("path does not exist");
|
662
|
+
}
|
663
|
+
if (directory) {
|
664
|
+
if (fp.isFile()) {
|
665
|
+
throw new TypeMismatchException("path doesn't exist or is file");
|
666
|
+
}
|
667
|
+
} else {
|
668
|
+
if (fp.isDirectory()) {
|
669
|
+
throw new TypeMismatchException("path doesn't exist or is directory");
|
670
|
+
}
|
671
|
+
}
|
672
|
+
}
|
673
|
+
|
674
|
+
// Return the directory
|
675
|
+
return getEntry(fp);
|
676
|
+
}
|
677
|
+
|
678
|
+
/**
|
679
|
+
* If the path starts with a '/' just return that file object. If not construct the file
|
680
|
+
* object from the path passed in and the file name.
|
681
|
+
*
|
682
|
+
* @param dirPath root directory
|
683
|
+
* @param fileName new file name
|
684
|
+
* @return
|
685
|
+
*/
|
686
|
+
private File createFileObject(String dirPath, String fileName) {
|
687
|
+
File fp = null;
|
688
|
+
if (fileName.startsWith("/")) {
|
689
|
+
fp = new File(fileName);
|
690
|
+
} else {
|
691
|
+
dirPath = stripFileProtocol(dirPath);
|
692
|
+
fp = new File(dirPath + File.separator + fileName);
|
693
|
+
}
|
694
|
+
return fp;
|
695
|
+
}
|
696
|
+
|
697
|
+
/**
|
698
|
+
* Look up the parent DirectoryEntry containing this Entry.
|
699
|
+
* If this Entry is the root of its filesystem, its parent is itself.
|
700
|
+
*
|
701
|
+
* @param filePath
|
702
|
+
* @return
|
703
|
+
* @throws JSONException
|
704
|
+
*/
|
705
|
+
private JSONObject getParent(String filePath) throws JSONException {
|
706
|
+
filePath = stripFileProtocol(filePath);
|
707
|
+
|
708
|
+
if (atRootDirectory(filePath)) {
|
709
|
+
return getEntry(filePath);
|
710
|
+
}
|
711
|
+
return getEntry(new File(filePath).getParent());
|
712
|
+
}
|
713
|
+
|
714
|
+
/**
|
715
|
+
* Checks to see if we are at the root directory. Useful since we are
|
716
|
+
* not allow to delete this directory.
|
717
|
+
*
|
718
|
+
* @param filePath to directory
|
719
|
+
* @return true if we are at the root, false otherwise.
|
720
|
+
*/
|
721
|
+
private boolean atRootDirectory(String filePath) {
|
722
|
+
filePath = stripFileProtocol(filePath);
|
723
|
+
|
724
|
+
if (filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + ctx.getPackageName() + "/cache") ||
|
725
|
+
filePath.equals(Environment.getExternalStorageDirectory().getAbsolutePath()) ||
|
726
|
+
filePath.equals("/data/data/" + ctx.getPackageName())) {
|
727
|
+
return true;
|
728
|
+
}
|
729
|
+
return false;
|
730
|
+
}
|
731
|
+
|
732
|
+
/**
|
733
|
+
* This method removes the "file://" from the passed in filePath
|
734
|
+
*
|
735
|
+
* @param filePath to be checked.
|
736
|
+
* @return
|
737
|
+
*/
|
738
|
+
private String stripFileProtocol(String filePath) {
|
739
|
+
if (filePath.startsWith("file://")) {
|
740
|
+
filePath = filePath.substring(7);
|
741
|
+
}
|
742
|
+
return filePath;
|
743
|
+
}
|
744
|
+
|
745
|
+
/**
|
746
|
+
* Create a File object from the passed in path
|
747
|
+
*
|
748
|
+
* @param filePath
|
749
|
+
* @return
|
750
|
+
*/
|
751
|
+
private File createFileObject(String filePath) {
|
752
|
+
filePath = stripFileProtocol(filePath);
|
753
|
+
|
754
|
+
File file = new File(filePath);
|
755
|
+
return file;
|
756
|
+
}
|
757
|
+
|
758
|
+
/**
|
759
|
+
* Look up metadata about this entry.
|
760
|
+
*
|
761
|
+
* @param filePath to entry
|
762
|
+
* @return a Metadata object
|
763
|
+
* @throws FileNotFoundException
|
764
|
+
* @throws JSONException
|
765
|
+
*/
|
766
|
+
private JSONObject getMetadata(String filePath) throws FileNotFoundException, JSONException {
|
767
|
+
File file = createFileObject(filePath);
|
768
|
+
|
769
|
+
if (!file.exists()) {
|
770
|
+
throw new FileNotFoundException("Failed to find file in getMetadata");
|
771
|
+
}
|
772
|
+
|
773
|
+
JSONObject metadata = new JSONObject();
|
774
|
+
metadata.put("modificationTime", file.lastModified());
|
775
|
+
|
776
|
+
return metadata;
|
777
|
+
}
|
778
|
+
|
779
|
+
/**
|
780
|
+
* Returns a File that represents the current state of the file that this FileEntry represents.
|
781
|
+
*
|
782
|
+
* @param filePath to entry
|
783
|
+
* @return returns a JSONObject represent a W3C File object
|
784
|
+
* @throws FileNotFoundException
|
785
|
+
* @throws JSONException
|
786
|
+
*/
|
787
|
+
private JSONObject getFileMetadata(String filePath) throws FileNotFoundException, JSONException {
|
788
|
+
File file = createFileObject(filePath);
|
789
|
+
|
790
|
+
if (!file.exists()) {
|
791
|
+
throw new FileNotFoundException("File: " + filePath + " does not exist.");
|
792
|
+
}
|
793
|
+
|
794
|
+
JSONObject metadata = new JSONObject();
|
795
|
+
metadata.put("size", file.length());
|
796
|
+
metadata.put("type", getMimeType(filePath));
|
797
|
+
metadata.put("name", file.getName());
|
798
|
+
metadata.put("fullPath", file.getAbsolutePath());
|
799
|
+
metadata.put("lastModifiedDate", file.lastModified());
|
800
|
+
|
801
|
+
return metadata;
|
802
|
+
}
|
803
|
+
|
804
|
+
/**
|
805
|
+
* Requests a filesystem in which to store application data.
|
806
|
+
*
|
807
|
+
* @param type of file system requested
|
808
|
+
* @return a JSONObject representing the file system
|
809
|
+
* @throws IOException
|
810
|
+
* @throws JSONException
|
811
|
+
*/
|
812
|
+
private JSONObject requestFileSystem(int type) throws IOException, JSONException {
|
813
|
+
JSONObject fs = new JSONObject();
|
814
|
+
if (type == TEMPORARY) {
|
815
|
+
File fp;
|
816
|
+
fs.put("name", "temporary");
|
817
|
+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
818
|
+
fp = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
|
819
|
+
"/Android/data/" + ctx.getPackageName() + "/cache/");
|
820
|
+
// Create the cache dir if it doesn't exist.
|
821
|
+
fp.mkdirs();
|
822
|
+
fs.put("root", getEntry(Environment.getExternalStorageDirectory().getAbsolutePath() +
|
823
|
+
"/Android/data/" + ctx.getPackageName() + "/cache/"));
|
824
|
+
} else {
|
825
|
+
fp = new File("/data/data/" + ctx.getPackageName() + "/cache/");
|
826
|
+
// Create the cache dir if it doesn't exist.
|
827
|
+
fp.mkdirs();
|
828
|
+
fs.put("root", getEntry("/data/data/" + ctx.getPackageName() + "/cache/"));
|
829
|
+
}
|
830
|
+
}
|
831
|
+
else if (type == PERSISTENT) {
|
832
|
+
fs.put("name", "persistent");
|
833
|
+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
834
|
+
fs.put("root", getEntry(Environment.getExternalStorageDirectory()));
|
835
|
+
} else {
|
836
|
+
fs.put("root", getEntry("/data/data/" + ctx.getPackageName()));
|
837
|
+
}
|
838
|
+
}
|
839
|
+
else {
|
840
|
+
throw new IOException("No filesystem of type requested");
|
841
|
+
}
|
842
|
+
|
843
|
+
return fs;
|
844
|
+
}
|
845
|
+
|
846
|
+
/**
|
847
|
+
* Returns a JSON Object representing a directory on the device's file system
|
848
|
+
*
|
849
|
+
* @param path to the directory
|
850
|
+
* @return
|
851
|
+
* @throws JSONException
|
852
|
+
*/
|
853
|
+
public JSONObject getEntry(File file) throws JSONException {
|
854
|
+
JSONObject entry = new JSONObject();
|
855
|
+
|
856
|
+
entry.put("isFile", file.isFile());
|
857
|
+
entry.put("isDirectory", file.isDirectory());
|
858
|
+
entry.put("name", file.getName());
|
859
|
+
entry.put("fullPath", "file://" + file.getAbsolutePath());
|
860
|
+
// I can't add the next thing it as it would be an infinite loop
|
861
|
+
//entry.put("filesystem", null);
|
862
|
+
|
863
|
+
return entry;
|
864
|
+
}
|
865
|
+
|
866
|
+
/**
|
867
|
+
* Returns a JSON Object representing a directory on the device's file system
|
868
|
+
*
|
869
|
+
* @param path to the directory
|
870
|
+
* @return
|
871
|
+
* @throws JSONException
|
872
|
+
*/
|
873
|
+
private JSONObject getEntry(String path) throws JSONException {
|
874
|
+
return getEntry(new File(path));
|
875
|
+
}
|
876
|
+
|
877
|
+
/**
|
878
|
+
* Identifies if action to be executed returns a value and should be run synchronously.
|
879
|
+
*
|
880
|
+
* @param action The action to execute
|
881
|
+
* @return T=returns value
|
882
|
+
*/
|
883
|
+
public boolean isSynch(String action) {
|
884
|
+
if (action.equals("testSaveLocationExists")) {
|
885
|
+
return true;
|
886
|
+
}
|
887
|
+
else if (action.equals("getFreeDiskSpace")) {
|
888
|
+
return true;
|
889
|
+
}
|
890
|
+
else if (action.equals("testFileExists")) {
|
891
|
+
return true;
|
892
|
+
}
|
893
|
+
else if (action.equals("testDirectoryExists")) {
|
894
|
+
return true;
|
895
|
+
}
|
896
|
+
return false;
|
897
|
+
}
|
898
|
+
|
899
|
+
//--------------------------------------------------------------------------
|
900
|
+
// LOCAL METHODS
|
901
|
+
//--------------------------------------------------------------------------
|
902
|
+
|
903
|
+
/**
|
904
|
+
* Read content of text file.
|
905
|
+
*
|
906
|
+
* @param filename The name of the file.
|
907
|
+
* @param encoding The encoding to return contents as. Typical value is UTF-8.
|
908
|
+
* (see http://www.iana.org/assignments/character-sets)
|
909
|
+
* @return Contents of file.
|
910
|
+
* @throws FileNotFoundException, IOException
|
911
|
+
*/
|
912
|
+
public String readAsText(String filename, String encoding) throws FileNotFoundException, IOException {
|
913
|
+
byte[] bytes = new byte[1000];
|
914
|
+
BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
|
915
|
+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
916
|
+
int numRead = 0;
|
917
|
+
while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
|
918
|
+
bos.write(bytes, 0, numRead);
|
919
|
+
}
|
920
|
+
return new String(bos.toByteArray(), encoding);
|
921
|
+
}
|
922
|
+
|
923
|
+
/**
|
924
|
+
* Read content of text file and return as base64 encoded data url.
|
925
|
+
*
|
926
|
+
* @param filename The name of the file.
|
927
|
+
* @return Contents of file = data:<media type>;base64,<data>
|
928
|
+
* @throws FileNotFoundException, IOException
|
929
|
+
*/
|
930
|
+
public String readAsDataURL(String filename) throws FileNotFoundException, IOException {
|
931
|
+
byte[] bytes = new byte[1000];
|
932
|
+
BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
|
933
|
+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
934
|
+
int numRead = 0;
|
935
|
+
while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
|
936
|
+
bos.write(bytes, 0, numRead);
|
937
|
+
}
|
938
|
+
|
939
|
+
// Determine content type from file name
|
940
|
+
String contentType = null;
|
941
|
+
if (filename.startsWith("content:")) {
|
942
|
+
Uri fileUri = Uri.parse(filename);
|
943
|
+
contentType = this.ctx.getContentResolver().getType(fileUri);
|
944
|
+
}
|
945
|
+
else {
|
946
|
+
contentType = getMimeType(filename);
|
947
|
+
}
|
948
|
+
|
949
|
+
byte[] base64 = Base64.encodeBase64(bos.toByteArray());
|
950
|
+
String data = "data:" + contentType + ";base64," + new String(base64);
|
951
|
+
return data;
|
952
|
+
}
|
953
|
+
|
954
|
+
/**
|
955
|
+
* Looks up the mime type of a given file name.
|
956
|
+
*
|
957
|
+
* @param filename
|
958
|
+
* @return a mime type
|
959
|
+
*/
|
960
|
+
public static String getMimeType(String filename) {
|
961
|
+
MimeTypeMap map = MimeTypeMap.getSingleton();
|
962
|
+
return map.getMimeTypeFromExtension(map.getFileExtensionFromUrl(filename));
|
963
|
+
}
|
964
|
+
|
965
|
+
/**
|
966
|
+
* Write contents of file.
|
967
|
+
*
|
968
|
+
* @param filename The name of the file.
|
969
|
+
* @param data The contents of the file.
|
970
|
+
* @param offset The position to begin writing the file.
|
971
|
+
* @throws FileNotFoundException, IOException
|
972
|
+
*/
|
973
|
+
/**/
|
974
|
+
public long write(String filename, String data, int offset) throws FileNotFoundException, IOException {
|
975
|
+
filename = stripFileProtocol(filename);
|
976
|
+
|
977
|
+
boolean append = false;
|
978
|
+
if (offset > 0) {
|
979
|
+
this.truncateFile(filename, offset);
|
980
|
+
append = true;
|
981
|
+
}
|
982
|
+
|
983
|
+
byte [] rawData = data.getBytes();
|
984
|
+
ByteArrayInputStream in = new ByteArrayInputStream(rawData);
|
985
|
+
FileOutputStream out = new FileOutputStream(filename, append);
|
986
|
+
byte buff[] = new byte[rawData.length];
|
987
|
+
in.read(buff, 0, buff.length);
|
988
|
+
out.write(buff, 0, rawData.length);
|
989
|
+
out.flush();
|
990
|
+
out.close();
|
991
|
+
|
992
|
+
return data.length();
|
993
|
+
}
|
994
|
+
|
995
|
+
/**
|
996
|
+
* Truncate the file to size
|
997
|
+
*
|
998
|
+
* @param filename
|
999
|
+
* @param size
|
1000
|
+
* @throws FileNotFoundException, IOException
|
1001
|
+
*/
|
1002
|
+
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
|
1003
|
+
filename = stripFileProtocol(filename);
|
1004
|
+
|
1005
|
+
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
|
1006
|
+
|
1007
|
+
if (raf.length() >= size) {
|
1008
|
+
FileChannel channel = raf.getChannel();
|
1009
|
+
channel.truncate(size);
|
1010
|
+
return size;
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
return raf.length();
|
1014
|
+
}
|
1015
|
+
|
1016
|
+
/**
|
1017
|
+
* Get an input stream based on file path or content:// uri
|
1018
|
+
*
|
1019
|
+
* @param path
|
1020
|
+
* @return an input stream
|
1021
|
+
* @throws FileNotFoundException
|
1022
|
+
*/
|
1023
|
+
private InputStream getPathFromUri(String path) throws FileNotFoundException {
|
1024
|
+
if (path.startsWith("content")) {
|
1025
|
+
Uri uri = Uri.parse(path);
|
1026
|
+
return ctx.getContentResolver().openInputStream(uri);
|
1027
|
+
}
|
1028
|
+
else {
|
1029
|
+
path = stripFileProtocol(path);
|
1030
|
+
return new FileInputStream(path);
|
1031
|
+
}
|
1032
|
+
}
|
1033
|
+
|
1034
|
+
/**
|
1035
|
+
* Queries the media store to find out what the file path is for the Uri we supply
|
1036
|
+
*
|
1037
|
+
* @param contentUri the Uri of the audio/image/video
|
1038
|
+
* @param ctx the current applicaiton context
|
1039
|
+
* @return the full path to the file
|
1040
|
+
*/
|
1041
|
+
protected static String getRealPathFromURI(Uri contentUri, CordovaInterface ctx) {
|
1042
|
+
String[] proj = { _DATA };
|
1043
|
+
Cursor cursor = ctx.managedQuery(contentUri, proj, null, null, null);
|
1044
|
+
int column_index = cursor.getColumnIndexOrThrow(_DATA);
|
1045
|
+
cursor.moveToFirst();
|
1046
|
+
return cursor.getString(column_index);
|
1047
|
+
}
|
1048
|
+
}
|