Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
appsbychris
Posts: 33
Registered: ‎01-17-2013
My Device: BB10 Dev Alpha Sim
My Carrier: Verizon
Accepted Solution

Anyone with an actual device mind testing my app for me? (BB10)

[ Edited ]

(this is for BB10 only)

 

So i am having issues it seems with saving some data to the file system.

 

The app is available in the app world for free:

 

http://appworld.blackberry.com/webstore/content/21795797/

 

 

What i would need you to do is install the app, create a pet (all you need is a name, everything else can be left blank), once you have created a pet, close the app completely, and then relaunch it.

If the pet you created is still there, then its working right.

 

If not, then i still got a problem...

 

I recently updated it, and was told by a couple people it worked correctly, but then i just got a review saying its not working now.

 

I have it working in the emulator (only as long as the app is signed though...)

 

 

Here is the code i use to save to the file system. (This app is bundled with phonegap)

 

var getFileInfo = function(filename) {
	 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
	 	console.log("ENTRY")
	 	fileSystem.root.getDirectory("MyPetInfo", {create: true, exclusive: false}, function(DirReader) {
	 		console.log("CREATED!!!")
	 		var readAsText = function (file) {
		      var reader = new FileReader();
		      reader.onloadend = function(evt) {
		          enyo.Signals.send("onReadFile", {filename: filename, status: true, text: evt.target.result});
		      };
		      reader.readAsText(file);
		  };
		  var gotFile = function (file){
		      readAsText(file);
		  };
		  var failGetFile = function (error) {
		    console.error("GET FILE ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onReadFile", {filename: filename, status: false});
		  };
		  var gotReadFileEntry = function (fileEntry) {
		      fileEntry.file(gotFile, failGetFile);
		      enyo.Signals.send("onReadFileEntry", {filename: filename, status: true});
		  };
		  var failReadFileEntry = function (error) {
		    console.error("GET READ FILE ENTRY ERROR " + filename + " " + error.code + "," + error.message);
		    enyo.Signals.send("onReadFile", {filename: filename, status: false});
		  };
		  DirReader.getFile(filename, {create: true, exclusive: false}, gotReadFileEntry, failReadFileEntry);
	 	}, errorHandler);
	 	
	 }, errorHandler);
  
};

var saveFileInfo = function(filename, text) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
	console.log("ENTRY")
	 	fileSystem.root.getDirectory("MyPetInfo", {create: true, exclusive: false}, function(DirReader) {
		  var gotFileWriter = function (writer) {
		    writer.write(text);
		    enyo.Signals.send("onWriteFile", {status: true, filename: filename});
		  };
		  var failFileWrite = function (error) {
		    console.error("WRITE FILE GET WRITER ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onWriteFile", {filename: filename, status: false});
		  };
		  var gotFileEntry = function (fileEntry) {
		      fileEntry.createWriter(gotFileWriter, failFileWrite);
		      enyo.Signals.send("onWriteFileEntry", {filename: filename, status: true});
		  };
		  var failFileEntry = function (error) {
		    console.error("WRITE FILE ENTRY ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onWriteFile", {filename: filename, status: false});
		  };
		  DirReader.getFile(filename, {create: true}, gotFileEntry, failFileEntry);
		 }, errorHandler);
	 	
	 }, errorHandler);
};

var errorHandler = function (e) {
    var msg = '';

    switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
        msg = 'QUOTA_EXCEEDED_ERR';
        break;
    case FileError.NOT_FOUND_ERR:
        msg = 'NOT_FOUND_ERR';
        break;
    case FileError.SECURITY_ERR:
         msg = 'SECURITY_ERR';
         break;
    case FileError.INVALID_MODIFICATION_ERR:
         msg = 'INVALID_MODIFICATION_ERR';
         break;
    case FileError.INVALID_STATE_ERR:
         msg = 'INVALID_STATE_ERR';
         break;
    case FileError.NO_MODIFICATION_ALLOWED_ERR:
    	msg = 'NO_MODIFICATION_ALLOWED_ERR';
    	break;
    default:
         msg = 'Unknown Error';
        break;
    };

    alert('Error: ' + msg);
};

 

The error is mostly a permission error saying its not allowed to create a folder.

This is my config permissions:

 

<!-- Cordova API -->
  <access subdomains="true" uri="file:///store/home" />
  <access subdomains="true" uri="file:///SDCard" />
  <access subdomains="true" uri="local:///" />
  Expose access to all URIs, including the file and http protocols -->
  <access subdomains="true" uri="*" />
  <rim:permissions>
    <rim:permit>access_shared</rim:permit>
  </rim:permissions>

 

 

So if a couple of you would put it on your device to see if it saves the info, it would be greatly appreciated.

 

 

Thanks,

Chris.

Please use plain text.
Contributor
appsbychris
Posts: 33
Registered: ‎01-17-2013
My Device: BB10 Dev Alpha Sim
My Carrier: Verizon

Re: Anyone with an actual device mind testing my app for me? (BB10)

[ Edited ]

So i've gotten a couple people to test it, and it does indeed fail.

 

Is there anything wrong with my file system code?

 

It works just fine through the simulator, although only when the app is signed.

 

When it is not signed, i can get access to the file system with window.requestFileSystem, but trying to get or create a directory fails.

Please use plain text.
BlackBerry Development Advisor (Retired)
grahamzibar
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10
My Carrier: Bell

Re: Anyone with an actual device mind testing my app for me? (BB10)

[ Edited ]

Hey Chris,

 

I was having a gander at your config file and noticed you're trying to access shared directories:

 

<!-- Cordova API -->
  <access subdomains="true" uri="file:///store/home" />
  <access subdomains="true" uri="file:///SDCard" />
  <access subdomains="true" uri="local:///" />
  Expose access to all URIs, including the file and http protocols -->
  <access subdomains="true" uri="*" />
  <rim:permissions>
    <rim:permit>access_shared</rim:permit>
  </rim:permissions>

I don't believe you will need the access tags unless you'll be navigating pages or opening xmlhttprequests with these protocols.  The one you do need, however, is the <rim: permit>access_shared</rim: permit> tag.  If you're going add this permission, we also need to access the blackberry.io API.  We add the following to our configuration:

 

<feature id="blackberry.io" />

To use BlackBerry APIs, we need to include WebWorks.js into our project as follows:

 

<script type="text/javascript" src="local:///chrome/webworks.js"></script>

From here, we modify our filesystem request to use the constant window.TEMPORARY. The use of these constants is non-normative, and the suggestion is that both are technically persistent storage.  However, window.PERSISTENT is suggested to be storage that can never be removed by the User Agent (i.e. BlackBerry can't delete your storage when it feels it needs to) and the other, window.TEMPORARY, defines storage that can be deleted by the User Agent if it is deemed appropriate to do so (i.e. BlackBerry might delete it if your app is uninstalled, your app is an enterprise app in the work perimeter and you de-activate your enterprise account, etcetera).  The definitions are fairly loose but, ultimately, persistent is not the way to go.

 

Since you also required access to the shared directory in your configuration, we need to unsandbox our filesystem to access the shared directory space.

 

Therefore, make your requests like this:

 

var dir = '';
if (window.blackberry) {
blackberry.io.sandbox = false;
dir = blackberry.io.sharedFolder;
}
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, successCallback, errorCallback);

You use dir to append a path to a specific location on the filesystem before creating your own directories.  Thus, when you request your app's data directory, do the following:

 

fileSystem.root.getDirectory(dir + "/MyPetInfo", {create:true}, successCallback, errorCallback);

 And this should solve all of your woes.  Try this and let me know how it works.

 

Cheers,

Graham

------------------------------
@grahamzibar
-_- zZZ
Please use plain text.
Contributor
appsbychris
Posts: 33
Registered: ‎01-17-2013
My Device: BB10 Dev Alpha Sim
My Carrier: Verizon

Re: Anyone with an actual device mind testing my app for me? (BB10)

Thanks for the input.

 

I am still having some issues.

 

This is the path that it is trying to create:

/accounts/1000/appdata/PetInfoFree.testDev_PetInfoFree25e31659/data/../shared/MyPetInfo

 

Here is my changed code:

 

 

var getFileInfo = function(filename) {
	var dir = "";
	if (window.blackberry) {
	    blackberry.io.sandbox = false;
	    dir = blackberry.io.sharedFolder;
	}
	 window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(fileSystem) {
	 	console.log(dir + "/MyPetInfo");
	 	fileSystem.root.getDirectory(dir + "/MyPetInfo", {create: true}, function(DirReader) {
	 		console.log("CREATED!!!")
	 		var readAsText = function (file) {
		      var reader = new FileReader();
		      reader.onloadend = function(evt) {
		          enyo.Signals.send("onReadFile", {filename: filename, status: true, text: evt.target.result});
		      };
		      reader.readAsText(file);
		  };
		  var gotFile = function (file){
		      readAsText(file);
		  };
		  var failGetFile = function (error) {
		    console.error("GET FILE ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onReadFile", {filename: filename, status: false});
		  };
		  var gotReadFileEntry = function (fileEntry) {
		      fileEntry.file(gotFile, failGetFile);
		      enyo.Signals.send("onReadFileEntry", {filename: filename, status: true});
		  };
		  var failReadFileEntry = function (error) {
		    console.error("GET READ FILE ENTRY ERROR " + filename + " " + error.code + "," + error.message);
		    enyo.Signals.send("onReadFile", {filename: filename, status: false});
		  };
		  DirReader.getFile(filename, {create: true}, gotReadFileEntry, failReadFileEntry);
	 	}, errorHandler);
	 	
	 }, errorHandler);
  
};

var saveFileInfo = function(filename, text) {
	var dir = "";
	if (window.blackberry) {
	    blackberry.io.sandbox = false;
	    dir = blackberry.io.sharedFolder;
	}
	window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(fileSystem) {
	console.log("ENTRY")
	 	fileSystem.root.getDirectory(dir + "\MyPetInfo", {create: true}, function(DirReader) {
		  var gotFileWriter = function (writer) {
		    writer.write(text);
		    enyo.Signals.send("onWriteFile", {status: true, filename: filename});
		  };
		  var failFileWrite = function (error) {
		    console.error("WRITE FILE GET WRITER ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onWriteFile", {filename: filename, status: false});
		  };
		  var gotFileEntry = function (fileEntry) {
		      fileEntry.createWriter(gotFileWriter, failFileWrite);
		      enyo.Signals.send("onWriteFileEntry", {filename: filename, status: true});
		  };
		  var failFileEntry = function (error) {
		    console.error("WRITE FILE ENTRY ERROR " + filename + " " + error.code);
		    enyo.Signals.send("onWriteFile", {filename: filename, status: false});
		  };
		  DirReader.getFile(filename, {create: true}, gotFileEntry, failFileEntry);
		 }, errorHandler);
	 	
	 }, errorHandler);
};

 

 

This is my full config.xml:

 

<widget xmlns="http://www.w3.org/ns/widgets"
        xmlns:rim="http://www.blackberry.com/ns/widgets"
	version="1.0.0.1" id="PetInfoFree">
 
  <author href="http://www.appsbychris.net/"
          rim:copyright="Copyright 2013 Apps By Chris">Apps by Chris</author>
 
  <name>Pet Info</name>
 
  <description>
    Pet Info
  </description>
  
  <icon src="icon.png"/>
 
  <rim:splash src="splash-1280x768.png"/>
  <rim:splash src="splash-768x1280.png"/>

  <!-- Cordova API -->
    <feature id="blackberry.system" required="true" version="1.0.0.0" />
  <feature id="org.apache.cordova" required="true" version="1.0.0" />
  
  <feature id="blackberry.identity" required="true" version="1.0.0.0" />
  
 
  <feature id="blackberry.io.file" required="true" version="1.0.0.0" />
  <feature id="blackberry.utils"   required="true" version="1.0.0.0" />
  <feature id="blackberry.io.dir"  required="true" version="1.0.0.0" />

  <feature id="blackberry.app" required="true" version="1.0.0.0" />
  <feature id="blackberry.widgetcache" required="true" version="1.0.0.0"/>
  
  <feature id="blackberry.ui.dialog" />
  <feature id="blackberry.connection" />
  <feature id="blackberry.bbm.platform" />
  <feature id="blackberry.invoke.card" />
  <feature id="blackberry.io.filetransfer" />
  <feature id="blackberry.io" />
  <feature id="blackberry.invoke" />
  <feature id="blackberry.invoked" />
  <feature id="blackberry.push" />
  


  <rim:loadingScreen backgroundColor="#CFCFCF"
                     foregroundImage="splash-768x1280.png"
		     onFirstLaunch="true">
    <rim:transitionEffect type="fadeOut" />
  </rim:loadingScreen>

<content src="local:///index.html" rim:allowInvokeParams="true"/>

  <rim:permissions>
    <rim:permit>access_shared</rim:permit>
  </rim:permissions>
  <feature id="blackberry.app">
    <param name="orientation" value="auto" />
  </feature>
  <feature id="blackberry.app">
    <param name="websecurity" value="disable" />
  </feature>
  <feature id="blackberry.app">
    <param name="backgroundColor" value="0xFFFFFFFF" />
  </feature>
</widget>

The error i am getting is FileError.NO_MODIFICATION_ALLOWED_ERR

 

 

Still fails when i try to create anything.

 

Thanks for any help.

 

Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: Anyone with an actual device mind testing my app for me? (BB10)

Quick little suggestion as I had problems in this area as well

 

Switch to window.PERSISTENT and give that a go

 




Click the like button if you find my posts useful!
Please use plain text.
BlackBerry Development Advisor (Retired)
grahamzibar
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10
My Carrier: Bell

Re: Anyone with an actual device mind testing my app for me? (BB10)

Ahhhh, I totally forgot to say you can't write into the root of the shared directory, and that the code for accesing the directory should look like this:

 

dir + '/Documents/MyPetInfo'

 My bad!  You can read and write from any subdiretory of the shared directory.  Let me know if you're still having any issues!

 

Cheers,

Graham

------------------------------
@grahamzibar
-_- zZZ
Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: Anyone with an actual device mind testing my app for me? (BB10)

In PERSISTANT do we get write to shared root?

 

There are a load of Shared directories bit what if I wanna create a Pets one?




Click the like button if you find my posts useful!
Please use plain text.
Contributor
appsbychris
Posts: 33
Registered: ‎01-17-2013
My Device: BB10 Dev Alpha Sim
My Carrier: Verizon

Re: Anyone with an actual device mind testing my app for me? (BB10)

[ Edited ]

Still no go.

 

var dir = ""
if (window.blackberry) {
        blackberry.io.sandbox = false;
        dir = blackberry.io.sharedFolder;
    }
     window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(fileSystem) {
console.log(dir + "/Documents/MyPetInfo"); fileSystem.root.getDirectory(dir + "/Documents/MyPetInfo", {create: true}, function(DirReader) {

 

With this, i get:

 

FileError.NOT_FOUND_ERR

 

 

The path is now:

/accounts/1000/appdata/PetInfoFree.testDev_PetInfoFree25e31659/data/../shared/Documents/MyPetInfo

 

Also tried persistant again but get the same error

 

Please use plain text.
BlackBerry Development Advisor (Retired)
grahamzibar
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10
My Carrier: Bell

Re: Anyone with an actual device mind testing my app for me? (BB10)

I'll try building a stripped down version of this and see what's-what. Sorry you're running into so many issues! The Filesystem is always so tricky :smileysad:

I shall post my findings tomorrow!
------------------------------
@grahamzibar
-_- zZZ
Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: Anyone with an actual device mind testing my app for me? (BB10)

I hope this helps - dunno exactly what you're doing but here goes...

 

An obvious something comes to mind

 

All this is Asynchronous and all the callbacks make it exteremly hard to read

 

I had a similar issuewhen I was playing with re-writing a DOM on the fly converting images into Base64s

 

Anyway...

 

I had three images and four fetches in a very clear order - they kept on coming out random and or broken

 

What was happening was that the image I was expecting was actually behind in the Asynch scheme of things so I'd  ask for images ABCA then (stupidly) expect they'd arrive in that order

 

You need some way to identify which resource you thought you were dealing you actually got back

 

Lets say that you've got files A-Z - They are unlikely to arrive ABC...XYZ

 

Personally I twigged this after a few days tearing my hair our

 

You can ask for things in A-Z but you'll more than likely get a random, unpredictable, order back

 

If names are unique then it's easy - push new entries onto a shared stack then when you get one extract it from the pile and forget about it

 

You can't get a file BEFORE you've asked for it - the stack is always valid

 

Which one you get though, is 1 / hight of stack




Click the like button if you find my posts useful!
Please use plain text.