mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +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;
|
||||
|
||||
final DataService.Connection DataConnection = new DataService.Connection();
|
||||
/*
|
||||
private final ServiceConnection mySyncConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder binder) {
|
||||
|
@ -104,6 +105,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
public void onServiceDisconnected(ComponentName componentName) {
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
volatile boolean IsPaused = false;
|
||||
volatile Runnable OnResumeAction = null;
|
||||
|
@ -497,11 +499,14 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
SyncService.enableSync(this, true);
|
||||
/*
|
||||
bindService(
|
||||
new Intent(this, SyncService.class),
|
||||
mySyncConnection,
|
||||
SyncService.BIND_AUTO_CREATE
|
||||
);
|
||||
*/
|
||||
|
||||
myStartTimer = true;
|
||||
Config.Instance().runOnConnect(new Runnable() {
|
||||
|
@ -580,7 +585,7 @@ public final class FBReader extends Activity implements ZLApplicationWindow {
|
|||
setButtonLight(true);
|
||||
}
|
||||
myFBReaderApp.onWindowClosing();
|
||||
unbindService(mySyncConnection);
|
||||
//unbindService(mySyncConnection);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ import org.geometerplus.android.fbreader.FBReader;
|
|||
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||
import org.geometerplus.android.fbreader.network.auth.ActivityNetworkContext;
|
||||
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.DeviceType;
|
||||
|
@ -157,6 +158,10 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void enableSyncService() {
|
||||
SyncService.enableSync(PreferenceActivity.this, syncOptions.Enabled.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClick() {
|
||||
super.onClick();
|
||||
|
@ -166,7 +171,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
|||
|
||||
if (!isChecked()) {
|
||||
syncOptions.Enabled.setValue(false);
|
||||
setOnSummary(null);
|
||||
enableSyncService();
|
||||
syncPreferences.run();
|
||||
return;
|
||||
}
|
||||
|
@ -180,6 +185,7 @@ public class PreferenceActivity extends ZLPreferenceActivity {
|
|||
public void processResponse(Object response) {
|
||||
final String account = (String)((Map)response).get("user");
|
||||
syncOptions.Enabled.setValue(account != null);
|
||||
enableSyncService();
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
setOnSummary(account);
|
||||
|
|
|
@ -22,15 +22,18 @@ package org.geometerplus.android.fbreader.sync;
|
|||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import android.app.Service;
|
||||
import android.app.*;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
import org.geometerplus.zlibrary.core.network.*;
|
||||
import org.geometerplus.zlibrary.core.options.Config;
|
||||
import org.geometerplus.zlibrary.core.util.MiscUtil;
|
||||
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
||||
import org.geometerplus.fbreader.book.*;
|
||||
|
@ -40,6 +43,17 @@ import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
|||
import org.geometerplus.android.fbreader.network.auth.ServiceNetworkContext;
|
||||
|
||||
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 {
|
||||
AlreadyUploaded(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 volatile boolean myHashTablesAreInitialized = false;
|
||||
|
||||
private PendingIntent syncIntent() {
|
||||
return PendingIntent.getService(
|
||||
this, 0, new Intent(this, getClass()).setAction(Action.SYNC), 0
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
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);
|
||||
myNetworkContext.cookieStore().reset();
|
||||
}
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -171,14 +219,15 @@ public class SyncService extends Service implements IBookCollection.Listener, Ru
|
|||
|
||||
@Override
|
||||
public synchronized void run() {
|
||||
if (!mySyncOptions.Enabled.getValue()) {
|
||||
return;
|
||||
}
|
||||
myNetworkContext.cookieStore().reset();
|
||||
|
||||
myCollection.addListener(this);
|
||||
if (ourSynchronizationThread == null) {
|
||||
ourSynchronizationThread = new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
final long start = System.currentTimeMillis();
|
||||
int count = 0;
|
||||
final Map<Status,Integer> statusCounts = new HashMap<Status,Integer>();
|
||||
|
|
|
@ -429,6 +429,9 @@ public class BookCollection extends AbstractBookCollection {
|
|||
synchronized (myFilesToRescan) {
|
||||
processFilesQueue();
|
||||
}
|
||||
for (Book book : new ArrayList<Book>(myBooksByFile.values())) {
|
||||
getHash(book);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue