mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 17:59:33 +02:00
scheduled synchronization
This commit is contained in:
parent
7df971eeae
commit
c70b16d21d
4 changed files with 73 additions and 10 deletions
|
@ -95,6 +95,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
||||||
private String myMenuLanguage;
|
private String myMenuLanguage;
|
||||||
|
|
||||||
final DataService.Connection DataConnection = new DataService.Connection();
|
final DataService.Connection DataConnection = new DataService.Connection();
|
||||||
|
/*
|
||||||
private final ServiceConnection mySyncConnection = new ServiceConnection() {
|
private final ServiceConnection mySyncConnection = new ServiceConnection() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
||||||
|
@ -104,6 +105,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
||||||
public void onServiceDisconnected(ComponentName componentName) {
|
public void onServiceDisconnected(ComponentName componentName) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
volatile boolean IsPaused = false;
|
volatile boolean IsPaused = false;
|
||||||
volatile Runnable OnResumeAction = null;
|
volatile Runnable OnResumeAction = null;
|
||||||
|
@ -497,11 +499,14 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
|
SyncService.enableSync(this, true);
|
||||||
|
/*
|
||||||
bindService(
|
bindService(
|
||||||
new Intent(this, SyncService.class),
|
new Intent(this, SyncService.class),
|
||||||
mySyncConnection,
|
mySyncConnection,
|
||||||
SyncService.BIND_AUTO_CREATE
|
SyncService.BIND_AUTO_CREATE
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
|
|
||||||
myStartTimer = true;
|
myStartTimer = true;
|
||||||
Config.Instance().runOnConnect(new Runnable() {
|
Config.Instance().runOnConnect(new Runnable() {
|
||||||
|
@ -580,7 +585,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
||||||
setButtonLight(true);
|
setButtonLight(true);
|
||||||
}
|
}
|
||||||
myFBReaderApp.onWindowClosing();
|
myFBReaderApp.onWindowClosing();
|
||||||
unbindService(mySyncConnection);
|
//unbindService(mySyncConnection);
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ import org.geometerplus.android.fbreader.FBReader;
|
||||||
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||||
import org.geometerplus.android.fbreader.network.auth.ActivityNetworkContext;
|
import org.geometerplus.android.fbreader.network.auth.ActivityNetworkContext;
|
||||||
import org.geometerplus.android.fbreader.preferences.fileChooser.FileChooserCollection;
|
import org.geometerplus.android.fbreader.preferences.fileChooser.FileChooserCollection;
|
||||||
|
import org.geometerplus.android.fbreader.sync.SyncService;
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
import org.geometerplus.android.util.DeviceType;
|
import org.geometerplus.android.util.DeviceType;
|
||||||
|
@ -157,6 +158,10 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enableSyncService() {
|
||||||
|
SyncService.enableSync(PreferenceActivity.this, syncOptions.Enabled.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onClick() {
|
protected void onClick() {
|
||||||
super.onClick();
|
super.onClick();
|
||||||
|
@ -166,7 +171,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
||||||
|
|
||||||
if (!isChecked()) {
|
if (!isChecked()) {
|
||||||
syncOptions.Enabled.setValue(false);
|
syncOptions.Enabled.setValue(false);
|
||||||
setOnSummary(null);
|
enableSyncService();
|
||||||
syncPreferences.run();
|
syncPreferences.run();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -180,6 +185,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
||||||
public void processResponse(Object response) {
|
public void processResponse(Object response) {
|
||||||
final String account = (String)((Map)response).get("user");
|
final String account = (String)((Map)response).get("user");
|
||||||
syncOptions.Enabled.setValue(account != null);
|
syncOptions.Enabled.setValue(account != null);
|
||||||
|
enableSyncService();
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
setOnSummary(account);
|
setOnSummary(account);
|
||||||
|
|
|
@ -22,15 +22,18 @@ package org.geometerplus.android.fbreader.sync;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.*;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.SystemClock;
|
||||||
|
|
||||||
import org.json.simple.JSONValue;
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.network.*;
|
import org.geometerplus.zlibrary.core.network.*;
|
||||||
|
import org.geometerplus.zlibrary.core.options.Config;
|
||||||
import org.geometerplus.zlibrary.core.util.MiscUtil;
|
import org.geometerplus.zlibrary.core.util.MiscUtil;
|
||||||
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
||||||
import org.geometerplus.fbreader.book.*;
|
import org.geometerplus.fbreader.book.*;
|
||||||
|
@ -40,6 +43,17 @@ import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||||
import org.geometerplus.android.fbreader.network.auth.ServiceNetworkContext;
|
import org.geometerplus.android.fbreader.network.auth.ServiceNetworkContext;
|
||||||
|
|
||||||
public class SyncService extends Service implements IBookCollection.Listener, Runnable {
|
public class SyncService extends Service implements IBookCollection.Listener, Runnable {
|
||||||
|
public static void enableSync(Context context, boolean enable) {
|
||||||
|
final String action = enable ? Action.START : Action.STOP;
|
||||||
|
context.startService(new Intent(context, SyncService.class).setAction(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface Action {
|
||||||
|
String START = "android.fbreader.action.sync.START";
|
||||||
|
String STOP = "android.fbreader.action.sync.STOP";
|
||||||
|
String SYNC = "android.fbreader.action.sync.SYNC";
|
||||||
|
}
|
||||||
|
|
||||||
private enum Status {
|
private enum Status {
|
||||||
AlreadyUploaded(Book.SYNCHRONIZED_LABEL),
|
AlreadyUploaded(Book.SYNCHRONIZED_LABEL),
|
||||||
Uploaded(Book.SYNCHRONIZED_LABEL),
|
Uploaded(Book.SYNCHRONIZED_LABEL),
|
||||||
|
@ -94,11 +108,45 @@ public class SyncService extends Service implements IBookCollection.Listener, Ru
|
||||||
private final Set<String> myDeletedHashesFromServer = new HashSet<String>();
|
private final Set<String> myDeletedHashesFromServer = new HashSet<String>();
|
||||||
private volatile boolean myHashTablesAreInitialized = false;
|
private volatile boolean myHashTablesAreInitialized = false;
|
||||||
|
|
||||||
|
private PendingIntent syncIntent() {
|
||||||
|
return PendingIntent.getService(
|
||||||
|
this, 0, new Intent(this, getClass()).setAction(Action.SYNC), 0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
final String action = intent.getAction();
|
||||||
|
if (Action.START.equals(action)) {
|
||||||
|
final AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
|
||||||
|
alarmManager.cancel(syncIntent());
|
||||||
|
Config.Instance().runOnConnect(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
if (!mySyncOptions.Enabled.getValue()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
alarmManager.setInexactRepeating(
|
||||||
|
AlarmManager.ELAPSED_REALTIME,
|
||||||
|
SystemClock.elapsedRealtime(),
|
||||||
|
10 * 1000,
|
||||||
|
syncIntent()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (Action.STOP.equals(action)) {
|
||||||
|
final AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
|
||||||
|
alarmManager.cancel(syncIntent());
|
||||||
|
} else if (Action.SYNC.equals(action)) {
|
||||||
|
System.err.println("SYNC-SYNC-SYNC " + new Date());
|
||||||
|
SQLiteCookieDatabase.init(this);
|
||||||
|
myCollection.bindToService(this, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return START_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
SQLiteCookieDatabase.init(this);
|
|
||||||
myCollection.bindToService(this, this);
|
|
||||||
myNetworkContext.cookieStore().reset();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,14 +219,15 @@ public class SyncService extends Service implements IBookCollection.Listener, Ru
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void run() {
|
public synchronized void run() {
|
||||||
|
if (!mySyncOptions.Enabled.getValue()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
myNetworkContext.cookieStore().reset();
|
||||||
|
|
||||||
myCollection.addListener(this);
|
myCollection.addListener(this);
|
||||||
if (ourSynchronizationThread == null) {
|
if (ourSynchronizationThread == null) {
|
||||||
ourSynchronizationThread = new Thread() {
|
ourSynchronizationThread = new Thread() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
|
||||||
sleep(10000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
final long start = System.currentTimeMillis();
|
final long start = System.currentTimeMillis();
|
||||||
int count = 0;
|
int count = 0;
|
||||||
final Map<Status,Integer> statusCounts = new HashMap<Status,Integer>();
|
final Map<Status,Integer> statusCounts = new HashMap<Status,Integer>();
|
||||||
|
|
|
@ -429,6 +429,9 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
synchronized (myFilesToRescan) {
|
synchronized (myFilesToRescan) {
|
||||||
processFilesQueue();
|
processFilesQueue();
|
||||||
}
|
}
|
||||||
|
for (Book book : new ArrayList<Book>(myBooksByFile.values())) {
|
||||||
|
getHash(book);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue