You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ca...@apache.org on 2017/01/27 22:13:43 UTC
[02/50] git commit: [flex-asjs] [refs/heads/feature/mdl] - Add
storage provider for AndroidExternalStorage
Add storage provider for AndroidExternalStorage
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/b0145403
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/b0145403
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/b0145403
Branch: refs/heads/feature/mdl
Commit: b0145403991ff4e6699fc5d35970f9a7d9460516
Parents: f777543
Author: Alex Harui <ah...@apache.org>
Authored: Thu Jan 19 09:30:45 2017 -0800
Committer: Alex Harui <ah...@apache.org>
Committed: Thu Jan 19 09:31:41 2017 -0800
----------------------------------------------------------------------
.../Storage/src/main/flex/StorageClasses.as | 1 +
.../providers/AndroidExternalStorageProvider.as | 248 +++++++++++++++++++
2 files changed, 249 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/b0145403/frameworks/projects/Storage/src/main/flex/StorageClasses.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/StorageClasses.as b/frameworks/projects/Storage/src/main/flex/StorageClasses.as
index 9fb9ea3..2ca937e 100644
--- a/frameworks/projects/Storage/src/main/flex/StorageClasses.as
+++ b/frameworks/projects/Storage/src/main/flex/StorageClasses.as
@@ -39,6 +39,7 @@ internal class StorageClasses
import org.apache.flex.storage.providers.AirStorageProvider; AirStorageProvider;
import org.apache.flex.storage.providers.WebStorageProvider; WebStorageProvider;
+ import org.apache.flex.storage.providers.AndroidExternalStorageProvider; AndroidExternalStorageProvider;
}
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/b0145403/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AndroidExternalStorageProvider.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AndroidExternalStorageProvider.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AndroidExternalStorageProvider.as
new file mode 100644
index 0000000..00244fa
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AndroidExternalStorageProvider.as
@@ -0,0 +1,248 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// 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.flex.storage.providers
+{
+ import org.apache.flex.events.EventDispatcher;
+ import org.apache.flex.events.IEventDispatcher;
+
+ import org.apache.flex.storage.events.FileEvent;
+ import org.apache.flex.storage.events.FileErrorEvent;
+ import org.apache.flex.storage.file.DataInputStream;
+ import org.apache.flex.storage.file.DataOutputStream;
+
+ /**
+ * The AndroidStorageProvider class implements the IPermanentStorageProvider
+ * interface for saving files to a Android external storage.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ * @flexjsignorecoercion FileEntry
+ * @flexjsignorecoercion FileWriter
+ * @flexjsignorecoercion window
+ * @flexjsignorecoercion Blob
+ */
+ public class AndroidExternalStorageProvider extends EventDispatcher implements IPermanentStorageProvider
+ {
+ /**
+ * Constructor.
+ *
+ * @param target The target dispatcher for events as files are read and written.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function AndroidExternalStorageProvider(target:IEventDispatcher=null)
+ {
+ super();
+ _target = target;
+ }
+
+ /**
+ * @private
+ */
+ private var _target:IEventDispatcher;
+
+ /**
+ * The target dispatcher for events as files are read and written.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function get target():IEventDispatcher
+ {
+ return _target;
+ }
+ public function set target(value:IEventDispatcher):void
+ {
+ _target = value;
+ }
+
+ /**
+ * A convenience function to read an entire file as a single
+ * string of text. The file is storaged in the application's
+ * data storage directory. Dispatches a FileRead event once
+ * the data is available.
+ *
+ * @param fileName The name of the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function readTextFromDataFile( fileName:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["externalDataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(fullPath, function (fileEntry):void {
+ fileEntry.file(function (file):void {
+ var reader:FileReader = new FileReader();
+ reader.onloadend = function (e):void {
+ var newEvent:FileEvent = new FileEvent("READ");
+ newEvent.data = this.result;
+ _target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
+ };
+ reader.readAsText(file);
+ }, function (e):void {
+ var err1Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err1Event.errorMessage = "Cannot open file for reading";
+ err1Event.errorCode = 2;
+ _target.dispatchEvent(err1Event);
+ });
+ }, function (e):void {
+ var err2Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err2Event.errorMessage = "File does not exist";
+ err2Event.errorCode = 1;
+ _target.dispatchEvent(err2Event);
+ });
+ }
+ }
+
+ /**
+ * Opens an input stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * read data from the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openInputDataStream( fileName:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["externalDataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(fullPath, function (fileEntry):void {
+ fileEntry.file(function (file):void {
+ var inputStream:DataInputStream = new DataInputStream(_target, file, new FileReader());
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = inputStream;
+ _target.dispatchEvent(newEvent);
+ }, function (e):void {
+ var err1Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err1Event.errorMessage = "Cannot open file for reading";
+ err1Event.errorCode = 2;
+ _target.dispatchEvent(err1Event);
+ });
+ }, function (e):void {
+ var err2Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err2Event.errorMessage = "File does not exist";
+ err2Event.errorCode = 1;
+ _target.dispatchEvent(err2Event);
+ });
+ }
+ }
+
+ /**
+ * A convenience function write a string into a file that resides in the
+ * application's data storage directory. If the file already exists it is
+ * replaced with the string. Dispatches a FileWrite event once the file
+ * has been written.
+ *
+ * @param fileName The name of file.
+ * @param text The string to be stored.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function writeTextToDataFile( fileName:String, text:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["externalDataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(fullPath, function (fileEntry):void {
+ fileEntry.createWriter(function (fileWriter):void {
+ fileWriter.onwriteend = function (e):void {
+ var newEvent:FileEvent = new FileEvent("WRITE");
+ _target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
+ };
+
+ fileWriter.onerror = function (e):void {
+ var newEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ newEvent.errorMessage = "Failed to write the file.";
+ newEvent.errorCode = 3;
+ _target.dispatchEvent(newEvent);
+ };
+
+ var blob:Blob = new Blob([text], { type: 'text/plain' });
+ fileWriter.write(blob);
+ }, function(e):void {
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ errEvent.errorMessage = "Cannot open file for writing.";
+ errEvent.errorCode = 1;
+ _target.dispatchEvent(errEvent);
+ });
+ }, function(e):void {
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ errEvent.errorMessage = "Cannot create file.";
+ errEvent.errorCode = 4;
+ _target.dispatchEvent(errEvent);
+ });
+ }
+ }
+
+ /**
+ * Opens an output stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * write data to the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openOutputDataStream( fileName:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["externalDataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(fullPath, function (directoryEntry):void {
+ directoryEntry.getFile(fileName, { 'create': true }, function (fileEntry):void {
+ fileEntry.createWriter(function (fileWriter):void {
+ var outputStream:DataOutputStream = new DataOutputStream(_target, fileEntry, fileWriter);
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = outputStream;
+ _target.dispatchEvent(newEvent);
+ });
+ });
+ });
+ }
+ }
+ }
+}