Access Web Storage or IndexedDB from outside the browser in Android
Asked Answered
M

3

5

I want to build an offline browser-based app using HTML and javascript to collect survey data on Android tablets. The app would consist of some static pages with forms for users to enter data, which would then be stored locally using Web Storage or IndexedDB. However, I also want to build a small native Android app which would grab this data and transfer it to other devices. Is this possible, and if so how would I go about it?

Specifically, I want to understand if and how the native app would access the browser's data store (I can manage the rest). I would prefer to use the Android browser but can use any other if that makes it easier. I have found this blog post which suggests that it might be possible but I would appreciate some pointers as to where the data is stored by the Android browser and how easily it can be accessed by another app.

Metallography answered 10/5, 2014 at 22:42 Comment(1)
I'm not sure you can do it easily the way in which you are asking but you can probably use these examples to create an app that both displays the web page and manages the data using normal android data storage rather than browser based data storage by passing data from js to the native app. #6357691 #5264989Bedfast
S
4

Unfortunately I don't think the data flow can work the way you want it. In the Chromium WebKit implementation, IDB stores data in levelDB files that you should not be able to access (by design).

So how do we get Java and JavaScript to play nice together? That's a great question! As I see it, the only good way to transform Java data into IDB data is via the client-side.

I've got good IDB chops but my Android experience is non-production. From what I understand of it, here's a proposed solution:

  • collect data via native application views
  • write a string to a file in your sandbox with the data stored as a JSON blob or in an .html <script> attached to a JavaScript global
  • load a webview that can access a local URI like file://android_asset/blah.json and then run some IDB code to bulk insert it into IDB
  • use your IDB store to drive your web-based views

So the answer to "if and how the native app would access the browser's data store" would be: try the opposite. Architect it to let your browser access your native app data store.

Srini answered 14/5, 2014 at 13:40 Comment(3)
Thanks for this. I can see that having a separate native app and offline web app probably won't work, and I should use webviews within a native app instead. But in that case why use the IDB at all? Won't it be better to do all the data storage work in the native app?Metallography
Yes, probably. There's only one good reason to use IDB and it's if you need indexed key-value stores "at scale" on the web. If you're going native, I'd use a native data store such as sqllite. (In fact, that's how Firefox implements IDB: atop SQL.)Srini
I'm trying to think about an option for web->native data flow: you could have your client-side code create data URIs that Java can consume. Current stumper: how do you pass off these URIs from client-side to native unless they share a filesystem? Can they share a fs via FileWriter API?Srini
I
1

Easiest and most robust way to serialise all your records and load into your app when it first run.

Inborn answered 11/5, 2014 at 6:25 Comment(1)
Thanks for the suggestion. Do you mean that the browser app would serialise the records and the user would then have to save this as a file, which the native app would then access?Metallography
C
0

It's possible if you're willing to try a slightly different approach:

  1. Pulling data from an HTML5 app is tricky but pushing data to a native app is easier. Your HTML5 app must have a native container. Does the container API include a way to access native ContentProviders? If not, can you add your own native code to the container to do that? Basically, if you can access ContentProviders then your native app need only implement a ContentProvider with insert privileges (which can be restricted to only your HTML5 app). After an insert, the native app can do whatever it wishes with the data, including broadcasting it to to other devices.

  2. If you insist on pulling data from the HTML5 app, this may only be possible if the native and HTML5 app are actually the same app, and that is only possible if your container allows you to add your own native code. Then you will have direct access to the WebView's storage via the WebStorage class.

Cruzeiro answered 20/5, 2014 at 0:3 Comment(2)
Suggesting native containers without any research or documentation seems unhelpful. This isn't possible, that I know of. I'd be interested in seeing supporting documentation if I'm wrong and I'll change my downvote, but to my knowledge, Cordova only supports IDB for Windows and BlackBerry. Other "containers" do not support IDB.Srini
I never said anything about IDB. You are confusing your own solution with mine.Cruzeiro

© 2022 - 2024 — McMap. All rights reserved.