mirror of
https://github.com/jfoucry/Pilldroid.git
synced 2024-11-18 02:51:38 +01:00
Optimize import;
make jobSchedule every 24h.
This commit is contained in:
parent
a03c2dfe77
commit
9291e2ecbe
1 changed files with 39 additions and 7 deletions
|
@ -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))) {
|
||||||
.setPersisted(true)
|
info = new JobInfo.Builder(24560, componentName)
|
||||||
.setPeriodic(15 *60 *1000)
|
.setPersisted(true)
|
||||||
.build();
|
.setPeriodic(dateAtNoon(today).getTime())
|
||||||
|
.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;
|
||||||
|
|
Loading…
Reference in a new issue