diff options
author | James Bottomley <James.Bottomley@HansenPartnership.com> | 2019-11-15 15:03:12 -0800 |
---|---|---|
committer | James Bottomley <James.Bottomley@HansenPartnership.com> | 2019-11-18 06:52:42 -0800 |
commit | e3dacec85f041800a8af9c314a881c9ced26725b (patch) | |
tree | 21700921668b6c18026a8f29646c5fd1eeeb4d41 | |
parent | 9846390032d6df3adf351363895c78ab627a45f7 (diff) | |
download | Osmand-nextcloud-maps.tar.gz |
FavouritesDbHelper: add support for periodic resyncsnextcloud-maps
The API allows the resync handler to be started whenever the plugin is
enabled, by setting plug to non-null and disabled when it is set to
null.
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
-rw-r--r-- | OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java | 68 |
1 files changed, 44 insertions, 24 deletions
diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index c577bbc408..e1a158e392 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -37,6 +37,7 @@ public class FavouritesDbHelper { void addFavourite(FavouritePoint p); void deleteFavourite(FavouritePoint p); boolean loadFavourites(FavouritesDbHelper h); + long getSyncIntervalMillis(); } private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(FavouritesDbHelper.class); @@ -54,8 +55,15 @@ public class FavouritesDbHelper { private static final String DELIMETER = "__"; private static FavouritesPlugin plug = null; - public static void setFavouritesPlugin(FavouritesPlugin p) { + public void setFavouritesPlugin(FavouritesPlugin p) { plug = p; + if (p == null) + handler.removeCallbacks(syncWork); + else if (initialized) + pluginStartSync(); + // if initialized isn't set, then loadFavourites will + // take care of starting the sync after the favourites + // are loaded and sorted from files } private HandlerThread handlerThread; @@ -98,6 +106,11 @@ public class FavouritesDbHelper { cachedFavoritePoints.remove(p); } + // This flag tells us if we've done the initial load. Any + // remote sync plugin can't start until *after* this point + // because our points cache is empty + private boolean initialized = false; + public void loadFavorites() { flatGroups.clear(); favoriteGroups.clear(); @@ -117,13 +130,6 @@ public class FavouritesDbHelper { loadGPXFile(getExternalFile(), extPoints); boolean changed = merge(extPoints, points); - // don't remove these sorts; they ensure the favourite - // list is ready for consumption by the FavoriteView - // as soon as this API returns regardless of how long - // it takes to sync with the plugin. - // - // If the plugin has to add or remove points, these - // sorts will get done again addToFavourites(points.values()); String time = Long.toString(System.currentTimeMillis()/1000); @@ -134,24 +140,14 @@ public class FavouritesDbHelper { } } - final boolean chg = changed; - - handler.post(new Runnable() { - public void run() { - boolean changed = chg; + // at this point the cachedFavouritePoints is ready to go + initialized = true; - if (plug != null) { - if (plug.loadFavourites(FavouritesDbHelper.this)) - changed = true; - } + if (changed) + saveCurrentPointsIntoFile(); - if(changed) { - saveCurrentPointsIntoFile(); - } - favouritesUpdated(); - } - }); - + favouritesUpdated(); + pluginStartSync(); } private void favouritesUpdated(){ @@ -243,6 +239,30 @@ public class FavouritesDbHelper { } } + private Runnable syncWork = new Runnable() { + public void run() { + boolean changed = false; + + if (plug != null) + changed = plug.loadFavourites(FavouritesDbHelper.this); + + if(changed) + saveCurrentPointsIntoFile(); + + favouritesUpdated(); + if (plug != null) { + long delayMillis = plug.getSyncIntervalMillis(); + handler.postDelayed(this, delayMillis); + } + } + }; + + // public to allow the plugin to call it on being enabled + public void pluginStartSync() { + if (plug != null) + handler.post(syncWork); + } + // The premise of this is we must update the local file ASAP. // However, the expectation is that the plugin addFavourite // modifies the added favourite, so we must queue a save file |