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