Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing the collection.anki2 database with other Anki applications on device #6057

Closed
sudomain opened this issue Apr 20, 2020 · 6 comments
Closed

Comments

@sudomain
Copy link
Contributor

sudomain commented Apr 20, 2020

Some of you may be familiar with the instructions I posted for installing Anki desktop on an android device. I've released a project that installs desktop Anki in Termux. I want AnkiDroid and Anki desktop to share the collection database (collection.anki2) and associated directories so that users don't have to have two copies of their collection and collection media on device. I've been able to do this by changing my AnkiDroid directory from /storage/emulated/0/AnkiDroid to /storage/emulated/0/AnkiRootDir/User 1 like so:

Screenshot_2020-04-20-15-53-42

There are a couple issues to work out relating to file locations:
Instead of requiring users to change this setting, I think AnkiDroid should by default store the AnkiDroid/ directory in an empty directory such as /storage/emulated/0/Anki2/AnkiDroid. This will allow the two projects to share a collection without users moving directories and changing settings. Not only that, but when AnkiDroid adds support for multiple profiles (#2545), AnkiDroid should probably use the same folder structure as desktop:

Anki2/ contains
prefs21.db
addons21/
User 1/
and other files.

Possible relevant code if I get approval to change the default Ankidroid dir:

public static synchronized void initializeAnkiDroidDirectory(String path) throws StorageAccessException {

The other issue is that if the collection is currently open in Anki then the user opens the same collection AnkiDroid (or vice-versa), the user will be told that there is a corrupt database when there isn't one (checking the database doesn't give any errors). I think this is an issue with how AnkiDroid checks for a database lock (in CollectionHelper.java?). I think this is an AnkiDroid specific issue because it's possible to run two different versions of Anki on a computer without having database issues. For instance I have Anki 2.1 and 2.0 installed on a windows machine. If my collection is open in 2.1 and I try to open the same collection in 2.0, the collection will not open. The same is true if I have it open in 2.0 and attempt to open 2.1.

Edit: added link to my project

@mikehardy
Copy link
Member

I think this is cool that it works together but we just finished stabilizing AnkiDroid from crashing, and I'm in the middle of trying to cut a 2.10 release branch before I contemplate a number of complicated performance changes for 2.11.

So even the idea of changing the default directory right now frankly scares the 💩 out of me, based on previous experiences.

We have some deep thinking to do in this area (i.e. #2545 as you mention, but also and maybe more important #5304 which would try to get rid of storage permission entirely).

By linking both here we can contemplate this at the same time but I need to be honest - I'm not going to have time to get to this for probably months. If anyone else can present a coherent solution that:

  1. handles multiple profiles
  2. does not require storage permission
  3. is still customizable by users (just in case)
  4. and also can interoperate with AnkiDesktop running on Android

...then I can contemplate it

However you should be aware that libanki is actually being ported to Rust right now and unless you've successfully gotten master of Anki Desktop (with rust libanki parts) to run on Android then you might be headed into a dead end as future Anki Desktop won't work.

@sudomain
Copy link
Contributor Author

A lot to respond to, forgive me if I missed something:

However you should be aware that libanki is actually being ported to Rust right now and unless you've successfully gotten master of Anki Desktop (with rust libanki parts) to run on Android then you might be headed into a dead end as future Anki Desktop won't work.

This I'm not terribly worried about. My project installs an instance of Arch Linux ARM(a port of Arch for ARM devices) in a chroot environment (technically it's a PRoot) and uses that project's packaged version of Anki (2.1.15). iirc Arch Linux Arm gets its Anki package from the Arch community repo and the community-testing repo currently has 2.1.23. Plus rust 1.42 is available as a package, though I haven't tested it.

We have some deep thinking to do in this area (i.e. #2545 as you mention, but also and maybe more important #5304 which would try to get rid of storage permission entirely).

I hadn't seen #5304 before and my knowledge of Android storage is limited so please correct me if I'm wrong. I've learned a bit from this doc. According to that doc, using getFilesDir() or getExternalFilesDir() (as suggested in #5304) means that the Anki collection would be considered an "App-specific file" to AnkiDroid? According to the same doc, I believe would those files would be removed if AnkiDroid is uninstalled? If this is the case, it would really hinder the ability of a program (like mine) to use the same collection database.

Regarding the two issues I brought up in my OP:
AnkiTermuxArch can adapt to the directory issue (I'm thinking an optional script that moves the collection and prompts the user to change the AnkiDroid directory setting). Today I'm having trouble reproducing the other issue of a reported database corruption when the two apps try to open the same collection. They still can't be used at the same time, but it's better than getting a message that collection is corrupt.

@sudomain
Copy link
Contributor Author

regarding the profile issue, AnkiDroid should probably do what Anki does. I don't know everything about how Anki does profiles, but iirc there's some profile information in prefs21.db and a separate directory for each profile in the ANKI_BASE directory

@sudomain
Copy link
Contributor Author

It seems like the "database corrupt" error is sporadic when AnkiTermuxArch and AnkiDroid share a collection. A fix would likely involve changing code in CollectionHelper.java as that handles loading the collection. Currently it only checks for AnkiDroid's internal idea of a database lock; the mCollectionLocked variable rather than checking the sqlite database.

@sudomain
Copy link
Contributor Author

My original reasons for opening this issue are resolved. I think the DB lock issue was fixed with PR #6100 (thanks for that). Additionally I can work around the default directory issue with an opt-in script for AnkiTermuxArch that moves the collection and prompts the user to change the AnkiDroid Directory setting.

I'll leave it up to the project maintainers if they want to close this issue or leave it open for discussion of handling profiles.

@mikehardy
Copy link
Member

I value the directory discussion here and am happy that is cross linked to the tracking issue for that now but have no shortage of work so if its the same otherwise / doesn't have an urgent separate need I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants