mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
current book synchronization
This commit is contained in:
parent
dd4daa23bd
commit
7f8605666f
4 changed files with 56 additions and 15 deletions
|
@ -97,6 +97,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
final DataService.Connection DataConnection = new DataService.Connection();
|
||||
|
||||
volatile boolean IsPaused = false;
|
||||
private volatile long myResumeTimestamp;
|
||||
volatile Runnable OnResumeAction = null;
|
||||
|
||||
private Intent myCancelIntent = null;
|
||||
|
@ -304,7 +305,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
myOpenBookIntent = null;
|
||||
getCollection().bindToService(this, new Runnable() {
|
||||
public void run() {
|
||||
myFBReaderApp.openBook(myFBReaderApp.Collection.getRecentBook(0), null, null);
|
||||
myFBReaderApp.openBook(null, null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -518,6 +519,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
|
||||
registerReceiver(myBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
IsPaused = false;
|
||||
myResumeTimestamp = System.currentTimeMillis();
|
||||
if (OnResumeAction != null) {
|
||||
final Runnable action = OnResumeAction;
|
||||
OnResumeAction = null;
|
||||
|
@ -551,7 +553,11 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
}
|
||||
});
|
||||
} else {
|
||||
myFBReaderApp.gotoSyncedPosition();
|
||||
getCollection().bindToService(this, new Runnable() {
|
||||
public void run() {
|
||||
myFBReaderApp.useSyncInfo(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
PopupPanel.restoreVisibilities(myFBReaderApp);
|
||||
|
@ -1008,7 +1014,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
|
||||
private BroadcastReceiver mySyncUpdateReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
myFBReaderApp.gotoSyncedPosition();
|
||||
myFBReaderApp.useSyncInfo(myResumeTimestamp + 10 * 1000 > System.currentTimeMillis());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ import org.geometerplus.fbreader.Paths;
|
|||
import org.geometerplus.fbreader.bookmodel.FBTextKind;
|
||||
import org.geometerplus.fbreader.fbreader.*;
|
||||
import org.geometerplus.fbreader.fbreader.options.*;
|
||||
import org.geometerplus.fbreader.network.sync.SyncData;
|
||||
import org.geometerplus.fbreader.network.sync.SyncUtil;
|
||||
//import org.geometerplus.fbreader.tips.TipsManager;
|
||||
|
||||
|
@ -173,6 +174,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
|||
syncOptions.Enabled.setValue(false);
|
||||
enableSynchronisation();
|
||||
syncPreferences.run();
|
||||
new SyncData().reset();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -135,6 +135,16 @@ public final class FBReaderApp extends ZLApplication {
|
|||
openBook(Collection.getBookByFile(BookUtil.getHelpFile()), null, null);
|
||||
}
|
||||
|
||||
private Book getCurrentServerBook() {
|
||||
for (String hash : mySyncData.getServerBookHashes()) {
|
||||
final Book book = Collection.getBookByHash(hash);
|
||||
if (book != null) {
|
||||
return book;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void openBook(Book book, final Bookmark bookmark, Runnable postAction) {
|
||||
if (Model != null) {
|
||||
if (book == null || bookmark == null && book.File.equals(Model.Book.File)) {
|
||||
|
@ -142,8 +152,11 @@ public final class FBReaderApp extends ZLApplication {
|
|||
}
|
||||
}
|
||||
|
||||
if (book == null) {
|
||||
book = getCurrentServerBook();
|
||||
if (book == null) {
|
||||
book = Collection.getRecentBook(0);
|
||||
}
|
||||
if (book == null || !book.File.exists()) {
|
||||
book = Collection.getBookByFile(BookUtil.getHelpFile());
|
||||
}
|
||||
|
@ -457,17 +470,26 @@ public final class FBReaderApp extends ZLApplication {
|
|||
}
|
||||
}
|
||||
|
||||
private volatile SaverThread mySaverThread;
|
||||
private volatile ZLTextPosition myStoredPosition;
|
||||
private volatile Book myStoredPositionBook;
|
||||
public void useSyncInfo(boolean openOtherBook) {
|
||||
if (openOtherBook) {
|
||||
final Book fromServer = getCurrentServerBook();
|
||||
if (fromServer != null && !fromServer.equals(Collection.getRecentBook(0))) {
|
||||
openBook(fromServer, null, null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void gotoSyncedPosition() {
|
||||
if (myStoredPositionBook != null &&
|
||||
mySyncData.hasPosition(Collection.getHash(myStoredPositionBook))) {
|
||||
gotoStoredPosition();
|
||||
storePosition();
|
||||
}
|
||||
}
|
||||
|
||||
private volatile SaverThread mySaverThread;
|
||||
private volatile ZLTextPosition myStoredPosition;
|
||||
private volatile Book myStoredPositionBook;
|
||||
|
||||
private void gotoStoredPosition() {
|
||||
myStoredPositionBook = Model != null ? Model.Book : null;
|
||||
if (myStoredPositionBook == null) {
|
||||
|
|
|
@ -23,8 +23,7 @@ import java.util.*;
|
|||
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
import org.geometerplus.zlibrary.core.options.ZLIntegerOption;
|
||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||
import org.geometerplus.zlibrary.core.options.*;
|
||||
|
||||
import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
|
||||
|
||||
|
@ -38,8 +37,8 @@ public class SyncData {
|
|||
new ZLStringOption("SyncData", "CurrentBookHash", "");
|
||||
private final ZLStringOption myCurrentBookTimestamp =
|
||||
new ZLStringOption("SyncData", "CurrentBookTimestamp", "");
|
||||
private final ZLStringOption myLastSyncTimestamp =
|
||||
new ZLStringOption("SyncData", "LastSyncTimestamp", "");
|
||||
private final ZLStringListOption myServerBookHashes =
|
||||
new ZLStringListOption("SyncData", "ServerBookHashes", Collections.<String>emptyList(), ";");
|
||||
|
||||
private Map<String,Object> position2Map(ZLTextFixedPosition.WithTimestamp pos) {
|
||||
final Map<String,Object> map = new HashMap<String,Object>();
|
||||
|
@ -120,11 +119,10 @@ public class SyncData {
|
|||
|
||||
public boolean updateFromServer(Map<String,Object> data) {
|
||||
System.err.println("RESPONSE = " + data);
|
||||
final int generation = (int)(long)(Long)data.get("generation");
|
||||
if (myGeneration.getValue() == generation) {
|
||||
myGeneration.setValue((int)(long)(Long)data.get("generation"));
|
||||
if (data.size() == 1) {
|
||||
return false;
|
||||
}
|
||||
myGeneration.setValue(generation);
|
||||
|
||||
final List<Map> positions = (List<Map>)data.get("positions");
|
||||
if (positions != null) {
|
||||
|
@ -135,6 +133,11 @@ public class SyncData {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
final Map<String,Object> currentBook = (Map<String,Object>)data.get("currentbook");
|
||||
myServerBookHashes.setValue(currentBook != null
|
||||
? (List<String>)currentBook.get("all_hashes") : Collections.<String>emptyList());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -150,6 +153,10 @@ public class SyncData {
|
|||
return positionOption(hash).getValue().length() > 0;
|
||||
}
|
||||
|
||||
public List<String> getServerBookHashes() {
|
||||
return myServerBookHashes.getValue();
|
||||
}
|
||||
|
||||
public ZLTextFixedPosition.WithTimestamp getAndCleanPosition(String hash) {
|
||||
final ZLStringOption option = positionOption(hash);
|
||||
try {
|
||||
|
@ -160,4 +167,8 @@ public class SyncData {
|
|||
option.setValue("");
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
Config.Instance().removeGroup("SyncData");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue