-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* AndroidManifest: Add SyncTriggerJobService * Add Constants#isRunningOnEmulator * Add Constants: WAIT_FOR_NEXT_SYNC_DELAY_SECS, TRIGGERED_SYNC_DURATION_SECS * WIP: Schedule Job in BootReceiver * Add Util/JobUtils * Util/JobUtils: Improve log text * Add service/SyncTriggerJobService scheduled by JobScheduler. See JobUtils#scheduleSyncTriggerServiceJob for more details. * RunConditionMonitor: Add SyncTriggerReceiver via LocalBroadcastReceiver. * BootReceiver: Add ToDo * Add pref: PREF_RUN_ON_TIME_SCHEDULE * Fine tune debug constants - time intervals * JobUtils: In seconds please * Add strings: en-GB * RunConditionMonitor: Implement hourly sync time frames (fixes #15) * Imported translation: de * Fix lint: .JOB_SCHEDULER_SERVICE, API 21 instead of 23 * JobUtils: Noop on Android API level before 21 (L) * Fix lint: RequiresApi(21) for SyncTriggerJobService * Hide pref "run on time schedule" on Android < 5.x * JobUtils: Show time of grace in brackets when logged * BootReceiver: Realign comment, remove unnecessary code
- Loading branch information
Catfriend1
authored
Mar 29, 2019
1 parent
3e248fa
commit 976d9f9
Showing
10 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/nutomic/syncthingandroid/service/SyncTriggerJobService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.nutomic.syncthingandroid.service; | ||
|
||
import android.app.job.JobParameters; | ||
import android.app.job.JobService; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.support.annotation.RequiresApi; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
// import android.util.Log; | ||
|
||
import com.nutomic.syncthingandroid.service.Constants; | ||
import com.nutomic.syncthingandroid.service.RunConditionMonitor; | ||
import com.nutomic.syncthingandroid.util.JobUtils; | ||
|
||
/** | ||
* SyncTriggerJobService to be scheduled by the JobScheduler. | ||
* See {@link JobUtils#scheduleSyncTriggerServiceJob} for more details. | ||
*/ | ||
@RequiresApi(21) | ||
public class SyncTriggerJobService extends JobService { | ||
private static final String TAG = "SyncTriggerJobService"; | ||
|
||
@Override | ||
public boolean onStartJob(JobParameters params) { | ||
// Log.v(TAG, "onStartJob: Job fired."); | ||
Context context = getApplicationContext(); | ||
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context); | ||
Intent intent = new Intent(RunConditionMonitor.ACTION_SYNC_TRIGGER_FIRED); | ||
localBroadcastManager.sendBroadcast(intent); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onStopJob(JobParameters params) { | ||
return true; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/nutomic/syncthingandroid/util/JobUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.nutomic.syncthingandroid.util; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.job.JobInfo; | ||
import android.app.job.JobScheduler; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.util.Log; | ||
|
||
import com.nutomic.syncthingandroid.service.SyncTriggerJobService; | ||
|
||
public class JobUtils { | ||
|
||
private static final String TAG = "JobUtils"; | ||
|
||
private static final int TOLERATED_INACCURACY_IN_SECONDS = 120; | ||
|
||
@TargetApi(21) | ||
public static void scheduleSyncTriggerServiceJob(Context context, int delayInSeconds) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | ||
return; | ||
} | ||
ComponentName serviceComponent = new ComponentName(context, SyncTriggerJobService.class); | ||
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent); | ||
|
||
// Wait at least "delayInSeconds". | ||
builder.setMinimumLatency(delayInSeconds * 1000); | ||
|
||
// Maximum tolerated delay. | ||
builder.setOverrideDeadline((delayInSeconds + TOLERATED_INACCURACY_IN_SECONDS) * 1000); | ||
|
||
// Schedule the start of "SyncTriggerJobService" in "X" seconds. | ||
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(context.JOB_SCHEDULER_SERVICE); | ||
jobScheduler.schedule(builder.build()); | ||
Log.i(TAG, "Scheduled SyncTriggerJobService to run in " + | ||
Integer.toString(delayInSeconds) + | ||
"(+" + Integer.toString(TOLERATED_INACCURACY_IN_SECONDS) + ") seconds."); | ||
} | ||
|
||
@TargetApi(21) | ||
public static void cancelAllScheduledJobs(Context context) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | ||
return; | ||
} | ||
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(context.JOB_SCHEDULER_SERVICE); | ||
jobScheduler.cancelAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters