1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 15:51:17 +00:00

Reduce unnecessary applyCustomRunConditions after service state change (#435)

Ref.: #7e7b2c9850

Improve RunConditionMonitor#updateShouldRunDecision ( 7e7b2c9850)
This commit is contained in:
Catfriend1 2019-07-11 12:23:17 +02:00 committed by GitHub
parent 0046a75078
commit 6b9745c800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 30 deletions

View file

@ -253,19 +253,39 @@ public class RunConditionMonitor {
* We then need to decide if syncthing should run. * We then need to decide if syncthing should run.
*/ */
public void updateShouldRunDecision() { public void updateShouldRunDecision() {
// Check if the current conditions changed the result of decideShouldRun()
// compared to the last determined result.
boolean newShouldRun = decideShouldRun(); boolean newShouldRun = decideShouldRun();
if (newShouldRun != lastDeterminedShouldRun) { if (newShouldRun) {
if (mOnShouldRunChangedListener != null) { /**
mOnShouldRunChangedListener.onShouldRunDecisionChanged(newShouldRun); * Trigger:
} * a) Sync pre-conditions changed
lastDeterminedShouldRun = newShouldRun; * a1) AND SyncthingService.State should remain ACTIVE
* a2) AND SyncthingService.State should transition from INIT/DISABLED to ACTIVE
* b) Sync pre-conditions did not change
* b1) AND SyncthingService.State should remain ACTIVE
* because a reevaluation of the run conditions was forced from code.
* Action:
* SyncthingService will evaluate custom per-object run conditions
* and pause/unpause objects accordingly.
*/
if (mOnSyncPreconditionChangedListener != null) {
mOnSyncPreconditionChangedListener.onSyncPreconditionChanged(this);
}
} }
// Notify about changed preconditions. /**
if (mOnSyncPreconditionChangedListener != null) { * Check if the current conditions changed the result of decideShouldRun()
mOnSyncPreconditionChangedListener.onSyncPreconditionChanged(this); * compared to the last determined result.
*/
if (newShouldRun != lastDeterminedShouldRun) {
/**
* Notify SyncthingService in case it has to transition from
* a) INIT/DISABLED => STARTING => ACTIVE
* b) ACTIVE => DISABLED
*/
if (mOnShouldRunChangedListener != null) {
mOnShouldRunChangedListener.onShouldRunDecisionChanged(newShouldRun);
lastDeterminedShouldRun = newShouldRun;
}
} }
} }
@ -461,6 +481,7 @@ public class RunConditionMonitor {
/** /**
* Check if an object's individual sync conditions are met. * Check if an object's individual sync conditions are met.
* Precondition: Object must own pref "...CustomSyncConditionsEnabled == true".
*/ */
public Boolean checkObjectSyncConditions(String objectPrefixAndId) { public Boolean checkObjectSyncConditions(String objectPrefixAndId) {
// Sync on mobile data? // Sync on mobile data?

View file

@ -252,19 +252,6 @@ public class SyncthingService extends Service {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
/**
* Send current service state to listening endpoints.
* This is required that components know about the service State.DISABLED
* if RunConditionMonitor does not send a "shouldRun = true" callback
* to start the binary according to preferences shortly after its creation.
* See {@link mLastDeterminedShouldRun} defaulting to "false".
*/
if (mCurrentState == State.DISABLED) {
synchronized (mStateLock) {
onServiceStateChange(mCurrentState);
}
}
if (mPrefBroadcastServiceControl) { if (mPrefBroadcastServiceControl) {
Log.i(TAG, "onStartCommand: mPrefBroadcastServiceControl == true, RunConditionMonitor is disabled."); Log.i(TAG, "onStartCommand: mPrefBroadcastServiceControl == true, RunConditionMonitor is disabled.");
/** /**
@ -584,9 +571,6 @@ public class SyncthingService extends Service {
} }
onServiceStateChange(State.ACTIVE); onServiceStateChange(State.ACTIVE);
} }
if (mRestApi != null && mRunConditionMonitor != null) {
mRestApi.applyCustomRunConditions(mRunConditionMonitor);
}
if (mEventProcessor == null) { if (mEventProcessor == null) {
mEventProcessor = new EventProcessor(SyncthingService.this, mRestApi); mEventProcessor = new EventProcessor(SyncthingService.this, mRestApi);
@ -705,8 +689,10 @@ public class SyncthingService extends Service {
* @see #unregisterOnServiceStateChangeListener * @see #unregisterOnServiceStateChangeListener
*/ */
public void registerOnServiceStateChangeListener(OnServiceStateChangeListener listener) { public void registerOnServiceStateChangeListener(OnServiceStateChangeListener listener) {
// Make sure we don't send an invalid state or syncthing might show a "disabled" message /**
// when it's just starting up. * Initially send the current state to the new subscriber to make sure it doesn't stay
* in undefined state forever until the state next change occurs.
*/
listener.onServiceStateChange(mCurrentState); listener.onServiceStateChange(mCurrentState);
mOnServiceStateChangeListeners.add(listener); mOnServiceStateChangeListeners.add(listener);
} }
@ -726,9 +712,9 @@ public class SyncthingService extends Service {
private void onServiceStateChange(State newState) { private void onServiceStateChange(State newState) {
if (newState == mCurrentState) { if (newState == mCurrentState) {
Log.d(TAG, "onServiceStateChange: Called with unchanged state " + newState); Log.d(TAG, "onServiceStateChange: Called with unchanged state " + newState);
} else { return;
Log.i(TAG, "onServiceStateChange: from " + mCurrentState + " to " + newState);
} }
Log.i(TAG, "onServiceStateChange: from " + mCurrentState + " to " + newState);
mCurrentState = newState; mCurrentState = newState;
mHandler.post(() -> { mHandler.post(() -> {
mNotificationHandler.updatePersistentNotification(this); mNotificationHandler.updatePersistentNotification(this);