trevordevore / levure

Application development framework for LiveCode
MIT License
32 stars 14 forks source link

Trouble setting application data folder on Android #91

Closed jim1001 closed 6 years ago

jim1001 commented 6 years ago

Was having some trouble setting an application data folder on Android. To test I created a fresh sample Levure project & made the minimum of changes.

This gives "error while initializing app (unable to create folder /data/user/0/files/Application Support: can’t create that directory)".

If there is a value set for the default shared application data folder then this throws a different error on Android. Can post the project if any help.

Also on this wiki page it says the application data folder for Android is

~/Documents/Application Support/com.mycompany.myapplication

I couldn't find this on my Android device & I''ve read there aren't user home directories on Android.

Thanks.

spencerlearning commented 6 years ago

On Android, Levure automatically creates an Application Support folder in the Documents folder for the app. The application data folder for your app is created within the Application Support folder using the name defined by your application data folder setting in the app.yml file.

So if your app.yml setting looks like this:

# app.yml

application data folder:
  user:
    android: myAppName

Your application data folder will be ~/Documents/Application Support/myAppName, where ~ is used to represent your app root.

Do not use the bundle identifier (com.mycompany.myapplication) for the Android application data folder setting. This may cause an error. You cannot leave the setting blank, so use anything but the bundle identifier (I will be changing the example in the Wiki).

When writing your app, you will always get the path to the application data folder with levureApplicationDataFolder so once it is set up you don't need remember the details.

@jim1001 - Are you able to get it working on Android now?

jim1001 commented 6 years ago

Brian,

Yes, got it working with your help - thank you.

I'm used to the ~ character representing the user home directory. I put the folder name under user : android as you specified above and didn't use the bundle identifier. I also checked that the same folder name worked under user : default & it did.

Other points / questions:

Best wishes, Jim

jim1001 commented 6 years ago

Does the folder & prefs file persist when the app closes?

I mean I'm pretty sure they do otherwise users wouldn't be too happy, but just seeking confirmation...

jim1001 commented 6 years ago

Do the folder & prefs file persist when the app closes?

I've tested this and verified that preferences are saved between app sessions. I'd still like to find the folder on the Android filesystem so I can remove it as an easy way of resetting preferences outside the app. It doesn't appear on file browsers I have although I'm still a bit unsure of the path with aliases etc.

I was also having a problem using prefsSave of the Preferences helper. I was calling it from command PreShutdownApplication of app.livecodescript since I thought this was called automatically when the app is closed. Am I mistaken? It doesn’t seem to be executed when I close the app.

Thanks.

spencerlearning commented 6 years ago

@jim1001 ,

Here are some answers to your questions:

Why there can be a user folder but not a shared one for Android?

On Android, apps are isolated and sandboxed similar to iOS so there are no shared folders like you have in macOS and Windows.

Does the folder & prefs file persist when the app closes?

Yes. Regarding preferences, Levure automatically saves the preferences file right before shutdown. Levure does not automatically save preferences whenever you set or change a preference, it only saves before shutdown. You can force preferences to be saved at any time using prefsSave.

levureApplicationDataFolder() returned /data/user/0/org.uom.adft/files/Application Support/adfu1 where org.uom.adft was the app bundle name & adfu was the application data folder I specified in app.yml. Is that an alias to the path you mentioned above?

Yes. On Android, Levure uses the LiveCode function specialFolderPath("documents") to get the path to the Documents folder for your app. In your case, specialFolderPath("documents") should return something like /data/user/0/org.uom.adft/files but you do not need to know the gory path details other than to satisfy your curiosity.

In practice, you will use specialFolderPath("documents") to get the path to the Documents folder which is where your app is able to write and store folders and files on Android. And you will use levureApplicationDataFolder() to get the path to your application data folder within the Documents folder. Again, you do not need to know the path details, you simply trust that those functions return the correct path and then append your specific file and folder names onto that path.

I've tested this and verified that preferences are saved between app sessions. I'd still like to find the folder on the Android filesystem so I can remove it as an easy way of resetting preferences outside the app. It doesn't appear on file browsers I have although I'm still a bit unsure of the path with aliases etc.

You can remove the preferences file in Levure using the prefsGetPreferenceFile function like this:

delete file prefsGetPreferenceFile("user")

I was also having a problem using prefsSave of the Preferences helper. I was calling it from command PreShutdownApplication of app.livecodescript since I thought this was called automatically when the app is closed. Am I mistaken? It doesn’t seem to be executed when I close the app.

Levure calls prefsSave automatically right before shutdown, so you shouldn't need to call it again in preShutDownApplication.

jim1001 commented 6 years ago

Brian,

Thanks for your time going over these.

On Android, apps are isolated and sandboxed similar to iOS so there are no shared folders like you have in macOS and Windows.

To be clear, the terms shared and user are to do with user accounts aren’t they?

Levure automatically saves the preferences file right before shutdown.

Would be useful to document that in the wiki command description (I know the wiki is evolving all the time & already holds a wealth of well-written useful information). The save didn’t seem to happen for me but could have been some other problem preventing it. Where is the code for it? I couldn’t find a prefsSave in app.livecodescript. Is it done some other way?

but you do not need to know the gory path details other than to satisfy your curiosity.

As I said, in my case it would be handy to know so I can delete the preferences file outside LiveCode. I will be setting up some devices to give to users for a trial. I intend to personally install the app & try it. When finished I could just remove any set preferences by deleting the file. A workaround could be to include a “Reset preferences” button in the app to set them all to empty or to default values using the Helper preferences API.

Many thanks again!

trevordevore commented 6 years ago

@jim1001

To be clear, the terms shared and user are to do with user accounts aren’t they?

Yes. "user" is the current user using the app. "shared" is available to all users. "shared" is not available on mobile.

The save didn’t seem to happen for me but could have been some other problem preventing it. Where is the code for it? I couldn’t find a prefsSave in app.livecodescript. Is it done some other way?

Look at the levureShutdownApplication command in levure.livecodescript. It calls prefsSave. levureShutdownApplication isn't called while working in the IDE. Only in a standalone.

trevordevore commented 6 years ago

@jim1001 I've updated the wiki page for preferences.

https://github.com/trevordevore/levure/wiki/Helper-preferences#saving-preferences

I also added notes at the top about "user" and "shared" preferences.