Optimize import;

make jobSchedule every 24h.
This commit is contained in:
jacques 2020-09-19 17:59:01 +02:00
parent a03c2dfe77
commit 9291e2ecbe

View file

@ -40,6 +40,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import static net.foucry.pilldroid.UtilDate.date2String; import static net.foucry.pilldroid.UtilDate.date2String;
import static net.foucry.pilldroid.UtilDate.dateAtNoon;
import static net.foucry.pilldroid.Utils.intRandomExclusive; import static net.foucry.pilldroid.Utils.intRandomExclusive;
/** /**
@ -66,11 +67,16 @@ public class MedicamentListActivity extends AppCompatActivity {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
Log.d(TAG, "Remove old notification"); Log.d(TAG, "Remove old notification and old job");
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (nm != null) { if (nm != null) {
nm.cancelAll(); nm.cancelAll();
} }
JobScheduler js = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
if (js != null) {
js.cancelAll();
}
} }
@Override @Override
@ -365,35 +371,61 @@ public class MedicamentListActivity extends AppCompatActivity {
mAdapter.addItem(med); mAdapter.addItem(med);
dbHelper.addDrug(med); dbHelper.addDrug(med);
} }
/**
* scheduleJob
* call at onPause, schedule job for next 24 hours
*/
public void scheduleJob() { public void scheduleJob() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime(); Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
Date scheduleDate;
JobInfo info;
ComponentName componentName = new ComponentName(this, PillDroidJobService.class); ComponentName componentName = new ComponentName(this, PillDroidJobService.class);
JobInfo info = new JobInfo.Builder(24560, componentName) if (today.before(dateAtNoon(today))) {
info = new JobInfo.Builder(24560, componentName)
.setPersisted(true) .setPersisted(true)
.setPeriodic(15 *60 *1000) .setPeriodic(dateAtNoon(today).getTime())
.build(); .build();
scheduleDate = today;
} else {
info = new JobInfo.Builder(24560, componentName)
.setPersisted(true)
.setPeriodic(dateAtNoon(tomorrow).getTime())
.build();
scheduleDate = tomorrow;
}
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info); int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) { if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, ("Job scheduled " + UtilDate.convertDate(now.getTime()+15 * 60*1000))); Log.d(TAG, "Job scheduled at " + dateAtNoon(scheduleDate));
} else { } else {
Log.d(TAG, "Job scheduling failed"); Log.d(TAG, "Job scheduling failed");
} }
} }
public void cancelJob(View v) { public void cancelJob(View v) {
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(24560); scheduler.cancel(24560);
Log.d(TAG, "Job cancelled"); Log.d(TAG, "Job cancelled");
} }
/**
*
* @param recyclerView
*/
private void setupRecyclerView(@NonNull RecyclerView recyclerView) { private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext())); recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
mAdapter = new SimpleItemRecyclerViewAdapter(medicaments); mAdapter = new SimpleItemRecyclerViewAdapter(medicaments);
recyclerView.setAdapter(mAdapter); recyclerView.setAdapter(mAdapter);
} }
// TDOO: remove in release
private String getAppName() { private String getAppName() {
PackageManager packageManager = getApplicationContext().getPackageManager(); PackageManager packageManager = getApplicationContext().getPackageManager();
ApplicationInfo applicationInfo = null; ApplicationInfo applicationInfo = null;