Skip to content

Commit

Permalink
Reschedule update check job on channel change
Browse files Browse the repository at this point in the history
  • Loading branch information
Chirayu Desai committed Aug 22, 2019
1 parent e81a4f9 commit 2536b88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/app/seamlessupdate/client/PeriodicJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.os.SystemProperties;
import android.util.Log;

public class PeriodicJob extends JobService {
Expand All @@ -15,8 +17,10 @@ public class PeriodicJob extends JobService {
private static final int JOB_ID_RETRY = 2;
private static final long INTERVAL_MILLIS = 4 * 60 * 60 * 1000;
private static final long MIN_LATENCY_MILLIS = 4 * 60 * 1000;
private static final String EXTRA_JOB_CHANNEL = "extra_job_channel";

static void schedule(final Context context) {
final String channel = SystemProperties.get("sys.update.channel", Settings.getChannel(context));
final int networkType = Settings.getNetworkType(context);
final boolean batteryNotLow = Settings.getBatteryNotLow(context);
final JobScheduler scheduler = context.getSystemService(JobScheduler.class);
Expand All @@ -25,16 +29,20 @@ static void schedule(final Context context) {
jobInfo.getNetworkType() == networkType &&
jobInfo.isRequireBatteryNotLow() == batteryNotLow &&
jobInfo.isPersisted() &&
jobInfo.getIntervalMillis() == INTERVAL_MILLIS) {
jobInfo.getIntervalMillis() == INTERVAL_MILLIS &&
jobInfo.getExtras().getString(EXTRA_JOB_CHANNEL).equals(channel)) {
Log.d(TAG, "Periodic job already registered");
return;
}
PersistableBundle extras = new PersistableBundle();
extras.putString(EXTRA_JOB_CHANNEL, channel);
final ComponentName serviceName = new ComponentName(context, PeriodicJob.class);
final int result = scheduler.schedule(new JobInfo.Builder(JOB_ID_PERIODIC, serviceName)
.setRequiredNetworkType(networkType)
.setRequiresBatteryNotLow(batteryNotLow)
.setPersisted(true)
.setPeriodic(INTERVAL_MILLIS)
.setExtras(extras)
.build());
if (result == JobScheduler.RESULT_FAILURE) {
Log.d(TAG, "Periodic job schedule failed");
Expand Down
9 changes: 9 additions & 0 deletions src/app/seamlessupdate/client/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public void onCreate(final Bundle savedInstanceState) {
return true;
});

final Preference channel = findPreference(KEY_CHANNEL);
channel.setOnPreferenceChangeListener((final Preference preference, final Object newValue) -> {
getPreferences(this).edit().putString(KEY_CHANNEL,(String) newValue).apply();
if (!getPreferences(this).getBoolean(KEY_WAITING_FOR_REBOOT, false)) {
PeriodicJob.schedule(this);
}
return true;
});

final Preference networkType = findPreference(KEY_NETWORK_TYPE);
networkType.setOnPreferenceChangeListener((final Preference preference, final Object newValue) -> {
final int value = Integer.parseInt((String) newValue);
Expand Down

0 comments on commit 2536b88

Please sign in to comment.