You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/10/08 21:51:16 UTC

git commit: First stab at file-extras plugin (CB-285)

Updated Branches:
  refs/heads/plugins 9d5b7a8bd -> aaf61d4fb


First stab at file-extras plugin (CB-285)

More testing & discussion required before putting this on the registry.


Project: http://git-wip-us.apache.org/repos/asf/cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-labs/commit/aaf61d4f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-labs/tree/aaf61d4f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-labs/diff/aaf61d4f

Branch: refs/heads/plugins
Commit: aaf61d4fb7a554eadf918fa476eebe95b9799485
Parents: 9d5b7a8
Author: Andrew Grieve <ag...@chromium.org>
Authored: Tue Oct 8 15:45:51 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Oct 8 15:50:43 2013 -0400

----------------------------------------------------------------------
 file-extras/android/FileExtras.java | 85 ++++++++++++++++++++++++++++
 file-extras/fileextras.js           | 91 ++++++++++++++++++++++++++++++
 file-extras/ios/FileExtras.m        | 97 ++++++++++++++++++++++++++++++++
 file-extras/plugin.xml              | 53 +++++++++++++++++
 4 files changed, 326 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/aaf61d4f/file-extras/android/FileExtras.java
----------------------------------------------------------------------
diff --git a/file-extras/android/FileExtras.java b/file-extras/android/FileExtras.java
new file mode 100644
index 0000000..0b84405
--- /dev/null
+++ b/file-extras/android/FileExtras.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.cordova.fileextras;
+
+import android.os.Environment;
+
+import org.apache.cordova.CallbackContext;
+import org.apache.cordova.CordovaArgs;
+import org.apache.cordova.CordovaPlugin;
+import org.json.JSONException;
+
+import java.io.File;
+
+public class FileExtras extends CordovaPlugin {
+    private static final int PURPOSE_DATA = 0;
+    private static final int PURPOSE_DOCUMENTS = 1;
+    private static final int PURPOSE_CACHE = 2;
+    private static final int PURPOSE_TEMP = 3;
+
+    @Override
+    public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
+        if ("getDirectoryForPurpose".equals(action)) {
+            getDirectoryForPurpose(args, callbackContext);
+            return true;
+        }
+
+        return false;
+    }
+
+    private void getDirectoryForPurpose(final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
+        int purpose = args.getInt(0);
+        boolean sandboxed = args.getBoolean(1);
+        // boolean syncable = args.getInt(2);
+
+        String path = null;
+        switch (purpose) {
+            case PURPOSE_DATA:
+                if (sandboxed) {
+                    path = cordova.getActivity().getApplicationContext().getFilesDir().getAbsolutePath();
+                } else {
+                    path = cordova.getActivity().getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
+                }
+                break;
+            case PURPOSE_DOCUMENTS:
+                if (sandboxed) {
+                    path = new File(cordova.getActivity().getApplicationContext().getFilesDir(), "Documents").getAbsolutePath();
+                } else {
+                    path = Environment.getExternalStorageDirectory().getAbsolutePath();
+                }
+                break;
+            case PURPOSE_CACHE:
+            case PURPOSE_TEMP:
+                if (sandboxed) {
+                    path = cordova.getActivity().getApplicationContext().getCacheDir().getAbsolutePath();
+                } else {
+                    path = cordova.getActivity().getApplicationContext().getExternalCacheDir().getAbsolutePath();
+                }
+                break;
+        }
+
+        if (path == null) {
+            callbackContext.error("No path found.");
+            return;
+        }
+
+        callbackContext.success(path);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/aaf61d4f/file-extras/fileextras.js
----------------------------------------------------------------------
diff --git a/file-extras/fileextras.js b/file-extras/fileextras.js
new file mode 100644
index 0000000..1f8f88f
--- /dev/null
+++ b/file-extras/fileextras.js
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+var exec = require('cordova/exec');
+var argscheck = require('cordova/argscheck');
+var DirectoryEntry = require('org.apache.cordova.file.DirectoryEntry');
+
+
+// Open Discussion:
+// * Should we have a purpose to expose the assets?
+//   * Would it be any different from resolveLocalFileSystemURL('/')
+// * Should we instead expose URLs instead of DirectoryEntry objects?
+//   * e.g. On iOS, app-data://icloud=yes/, app-documents://icloud=no/, app-temp://, app-bundle://
+//   * e.g. On Android, could use same schemes for 3.0+, or use content://cordova-app/app-data://... for 2.3
+//   * This would mean APIs could be synchronous, and platform-specific locations can be kept on native side.
+//   * This would allow things to be used as URLs for images.
+//   * APIs (such as FileTransfer) work better with URLs
+//   * Entry have a toURL() already. Without custom schemes, it won't work for Android resources & assets
+// * Add support resolveLocalFileSystemURL()?
+
+
+var fileextras = exports;
+
+var Purpose = {
+    'data': 0, // General application data (default)
+    'documents': 1, // Files that are meaningful to other applciations (e.g. Office files)
+    'cache': 2, // Temporary files that should survive app restarts
+    'temp': 3, // Files that can should be deleted on app restarts
+    'app-bundle': 4 // The application bundle (iOS only)
+};
+
+/**
+ * Supplies a DirectoryEntry that matches the given constraints to the given callback.
+ */
+fileextras.getDirectoryForPurpose = function(purpose, options, successCallback, failureCallback) {
+    argscheck.checkArgs('sOfF', 'fileextras.getDirectoryForPurpose', arguments);
+    var augmentedSuccessCallback = successCallback && function(fullPath) {
+        var lastSegment = fullPath.replace(/.*\//, '');
+        // Note: the fileSystem property is null
+        var directoryEntry = new DirectoryEntry(lastSegment, fullPath);
+        successCallback(directoryEntry);
+    };
+
+    var purposeInt = Persistence[purpose];
+    if (typeof purposeInt == 'undefined') {
+        throw new Error('getDirectoryForPurpose: invalid purpose: ' + purpose);
+    }
+    options = options || {};
+
+    var sandboxed = typeof options.sandboxed == 'undefined' ? true : !!options.sandboxed;
+    var syncable = typeof options.syncable == 'undefined' ? true : !!options.syncable;
+
+    var args = [purposeInt, sandboxed, syncable];
+    exec(augmentedSuccessCallback, failureCallback, "FileExtras", "getDirectoryForPurpose", args);
+};
+
+fileextras.getDataDirectory = function(syncable, successCallback) {
+    argscheck.checkArgs('*f', 'fileextras.getDataDirectory', arguments);
+    fileextras.getDirectoryForPurpose('data', { syncable: syncable }, successCallback);
+};
+
+// On Android, this is the root of the SD card.
+fileextras.getDocumentsDirectory = function(successCallback) {
+    fileextras.getDirectoryForPurpose('documents', { syncable: true, sandboxed: false }, successCallback);
+};
+
+fileextras.getTempDirectory = function(successCallback) {
+    fileextras.getDirectoryForPurpose('temp', null, successCallback);
+};
+
+fileextras.getCacheDirectory = function(successCallback) {
+    fileextras.getDirectoryForPurpose('cache', null, successCallback);
+};
+

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/aaf61d4f/file-extras/ios/FileExtras.m
----------------------------------------------------------------------
diff --git a/file-extras/ios/FileExtras.m b/file-extras/ios/FileExtras.m
new file mode 100644
index 0000000..827126c
--- /dev/null
+++ b/file-extras/ios/FileExtras.m
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+#import <Cordova/CDVPlugin.h>
+
+enum FileSystemPurpose {
+    DATA = 0,
+    DOCUMENTS = 1,
+    CACHE = 2,
+    TEMP = 3,
+    IOS_BUNDLE = 4,
+};
+typedef int FileSystemPurpose;
+
+@interface FileExtras : CDVPlugin
+@end
+
+@implementation FileExtras
+
+- (void)makeNonSyncable:(NSString*)path {
+    [[NSFileManager defaultManager] createDirectoryAtPath:path
+              withIntermediateDirectories:YES
+                               attributes:nil
+                                    error:nil];
+    NSURL* url = [NSURL fileURLWithPath:path];
+    [url setResourceValue: [NSNumber numberWithBool: YES]
+                   forKey: NSURLIsExcludedFromBackupKey error:nil];
+
+}
+
+- (void)getDirectoryForPurpose:(CDVInvokedUrlCommand *)command {
+    FileSystemPurpose purpose = [[command argumentAtIndex:0] intValue];
+    // BOOL sandboxed = [[command argumentAtIndex:1] boolValue];
+    BOOL syncable = [[command argumentAtIndex:2] boolValue];
+
+    NSString *path = nil;
+
+    switch (purpose) {
+        case DATA:
+            path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+            if (!syncable) {
+                path = [path stringByAppendingPathComponent:@"NoCloud"];
+                [self makeNonSyncable:path];
+            }
+            break;
+      case DOCUMENTS:
+            path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+            if (!syncable) {
+                path = [path stringByAppendingPathComponent:@"NoCloud"];
+                [self makeNonSyncable:path];
+            }
+            break;
+      case CACHE:
+          path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+          break;
+      case TEMP:
+          path = NSTemporaryDirectory();
+          break;
+      case IOS_BUNDLE:
+          path = [[NSBundle mainBundle] bundlePath];
+          break;
+    }
+
+    CDVPluginResult *pluginResult = nil;
+
+    if (!path) {
+        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
+    } else {
+        // Remove the trailing slash if it's there.
+        if ([path hasSuffix:@"/"]) {
+            path = [path substringToIndex:[path length] - 1];
+        }
+
+        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:path];
+    }
+
+    [[self commandDelegate] sendPluginResult:pluginResult callbackId:[command callbackId]];
+}
+
+@end

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/aaf61d4f/file-extras/plugin.xml
----------------------------------------------------------------------
diff --git a/file-extras/plugin.xml b/file-extras/plugin.xml
new file mode 100644
index 0000000..4df84a4
--- /dev/null
+++ b/file-extras/plugin.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
+    id="org.apache.cordova.file-extras"
+    version="0.1.0">
+  <engines>
+    <engine name="cordova" version=">=3.0.0" />
+  </engines>
+
+  <name>File Extras</name>
+  <dependency id="org.apache.cordova.file" />
+
+  <js-module src="fileextras.js" name="FileExtras">
+    <clobbers target="cordova.plugins.fileExtras" />
+  </js-module>
+
+  <platform name="android">
+    <source-file src="android/FileExtras.java" target-dir="src/org/apache/cordova/fileextras" />
+
+    <config-file target="res/xml/config.xml" parent="/*">
+      <feature name="FileExtras">
+        <param name="android-package" value="org.apache.cordova.fileextras.FileExtras"/>
+      </feature>
+    </config-file>
+  </platform>
+
+  <platform name="ios">
+    <source-file src="ios/FileExtras.m" />
+
+    <config-file target="config.xml" parent="/*">
+      <feature name="FileExtras">
+        <param name="ios-package" value="FileExtras"/>
+      </feature>
+    </config-file>
+  </platform>
+</plugin>