mirror of
https://github.com/jfoucry/Pilldroid.git
synced 2024-11-22 04:29:22 +01:00
End merge feature/fixNotification
This commit is contained in:
parent
d6a30a54f0
commit
8d10e9115a
4 changed files with 149 additions and 31 deletions
|
@ -1,6 +1,9 @@
|
|||
package net.foucry.pilldroid;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.job.JobInfo;
|
||||
import android.app.job.JobScheduler;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
|
@ -38,7 +41,7 @@ import java.util.Locale;
|
|||
|
||||
import static net.foucry.pilldroid.UtilDate.date2String;
|
||||
import static net.foucry.pilldroid.Utils.intRandomExclusive;
|
||||
import net.foucry.pilldroid.PillDroidJobService;
|
||||
|
||||
/**
|
||||
* An activity representing a list of Medicaments. This activity
|
||||
* has different presentations for handset and tablet-size devices. On
|
||||
|
@ -216,11 +219,13 @@ public class MedicamentListActivity extends AppCompatActivity {
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
scheduleJob();
|
||||
}
|
||||
|
||||
();
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
/** scanNow
|
||||
|
@ -360,6 +365,28 @@ public class MedicamentListActivity extends AppCompatActivity {
|
|||
mAdapter.addItem(med);
|
||||
dbHelper.addDrug(med);
|
||||
}
|
||||
public void scheduleJob() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date now = calendar.getTime();
|
||||
|
||||
ComponentName componentName = new ComponentName(this, PillDroidJobService.class);
|
||||
JobInfo info = new JobInfo.Builder(24560, componentName)
|
||||
.setPersisted(true)
|
||||
.setPeriodic(15 *60 *1000)
|
||||
.build();
|
||||
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
|
||||
int resultCode = scheduler.schedule(info);
|
||||
if (resultCode == JobScheduler.RESULT_SUCCESS) {
|
||||
Log.d(TAG, ("Job scheduled " + UtilDate.convertDate(now.getTime()+15 * 60*1000)));
|
||||
} else {
|
||||
Log.d(TAG, "Job scheduling failed");
|
||||
}
|
||||
}
|
||||
public void cancelJob(View v) {
|
||||
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
|
||||
scheduler.cancel(24560);
|
||||
Log.d(TAG, "Job cancelled");
|
||||
}
|
||||
|
||||
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
|
||||
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
|
||||
|
|
|
@ -1,42 +1,134 @@
|
|||
package net.foucry.pilldroid;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.job.JobParameters;
|
||||
import android.app.job.JobService;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.icu.util.Calendar;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Created by jacques on 17/09/16.
|
||||
*/
|
||||
|
||||
public class PillDroidJobService extends JobService {
|
||||
private static final String TAG = "JobService";
|
||||
private static final String TAG = JobService.class.getName();
|
||||
private boolean jobCancelled = false;
|
||||
private String CHANNEL_ID = "pillDroid";
|
||||
private DBHelper dbHelper = new DBHelper(this);
|
||||
|
||||
private Handler mJobHandler = new Handler(new Handler.Callback() {
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
// Toast.makeText( getApplicationContext(), "PillDroid - Calcul nouveau stocks", Toast.LENGTH_SHORT).show();
|
||||
// MedicamentListActivity.newStockCalculation(getApplicationContext());
|
||||
|
||||
jobFinished( (JobParameters) msg.obj,false);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
public boolean onStartJob (JobParameters params) {
|
||||
Log.i(TAG, "on Start Job: " + params.getJobId());
|
||||
mJobHandler.sendMessage(Message.obtain(mJobHandler, 1,params));
|
||||
return false;
|
||||
public boolean onStartJob(JobParameters params) {
|
||||
Log.d(TAG, "Job started");
|
||||
createNotificationChannel();
|
||||
doBackgroundWork(params);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab sorted list of medicaments
|
||||
* test dateAlert of the first of the list
|
||||
* if dateAlert < now
|
||||
* schedule notification
|
||||
* @param JobParameters params
|
||||
*/
|
||||
private void doBackgroundWork(final JobParameters params) {
|
||||
|
||||
if (jobCancelled) {
|
||||
return;
|
||||
}
|
||||
List<Medicament> medicaments = dbHelper.getAllDrugs();
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date now = calendar.getTime();
|
||||
|
||||
long dateSchedule;
|
||||
|
||||
Medicament firstMedicament = null;
|
||||
|
||||
try {
|
||||
firstMedicament = medicaments.get(0);
|
||||
}
|
||||
catch (Exception ignored){}
|
||||
|
||||
if (firstMedicament != null) {
|
||||
Date dateAlert = UtilDate.removeDaysToDate(firstMedicament.getAlertThreshold(), firstMedicament.getDateEndOfStock());
|
||||
|
||||
if (dateAlert.getTime() < now.getTime()) {
|
||||
dateSchedule = now.getTime() + 120000; // If dateAlert < now we schedule an alert for now + 120 seconds
|
||||
} else {
|
||||
dateSchedule = dateAlert.getTime(); // If dateAlert > now we use dateAlert as scheduleDate
|
||||
}
|
||||
|
||||
long delay = dateSchedule - now.getTime();
|
||||
scheduleNotification(delay);
|
||||
}
|
||||
|
||||
Log.d(TAG, "Job finished");
|
||||
jobFinished(params, false);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onStopJob(JobParameters params) {
|
||||
mJobHandler.removeMessages(1);
|
||||
return false;
|
||||
Log.d(TAG, "Job cancelled before completion");
|
||||
jobCancelled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule Notification for the delay
|
||||
* @param Context context
|
||||
* @param long delay - date for the notification in millisecond
|
||||
*/
|
||||
private void scheduleNotification(long delay) {
|
||||
Log.d(TAG, "scheduleNotification delay == " + delay);
|
||||
createNotificationChannel();
|
||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_pill)
|
||||
.setContentTitle(getString(R.string.app_name))
|
||||
.setContentText(getString(R.string.notification_text))
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setAutoCancel(true);
|
||||
|
||||
}
|
||||
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
||||
int notificationId = 666;
|
||||
notificationManager.notify(notificationId, builder.build());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* createNotificationChannelid for android API >= 28
|
||||
*/
|
||||
private void createNotificationChannel() {
|
||||
|
||||
Log.d(TAG, "start create notification channel");
|
||||
CharSequence name = getString(R.string.channel_name);
|
||||
String description = getString(R.string.channel_description);
|
||||
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
||||
channel.setDescription(description);
|
||||
// Register the channel with the system; you can't change the importance
|
||||
// or other notification behaviors after this
|
||||
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
||||
try {
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
} catch (Exception e) {
|
||||
// This will catch any exception, because they are all descended from Exception
|
||||
Log.e(TAG, e.toString());
|
||||
//At the level Exception Class handle the error in Exception Table
|
||||
// Exception Create That Error Object and throw it
|
||||
//E.g: FileNotFoundException ,etc
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,11 +21,9 @@
|
|||
<string name="button_keyboard">Utilisez le clavier</string>
|
||||
<string name="detail_view">Vue de détail</string>
|
||||
<string name="scan_action">Scanner un code barre de médicament</string>
|
||||
<string name="save_button">Save</string>
|
||||
<string name="flashlighButton">Allumez/Eteindre le Flash</string>
|
||||
<string name="enter_cip_13_here">Entrez le code CIP 13 ici…</string>
|
||||
<string name="save_button">Enrefistrez</string>
|
||||
<string name="flashlighButton">Allumez/Éteignez le Flash</string>
|
||||
<string name="enter_cip_13_here">Enter cip 13 here..</string>
|
||||
<string name="enter_cip_13">Enter CIP 13</string>
|
||||
<string name="input_cip13">Saisissez le code CIP13 avec le clavier</string>
|
||||
<string name="pharmacy">Vous devez passer à la pharmacie</string>
|
||||
|
||||
</resources>
|
||||
<string name="input_cip13">Saissisez le code CIP 13 à l\'aide du clavier</string>
|
||||
</resources>
|
|
@ -25,5 +25,6 @@
|
|||
<string name="flashlighButton">Toggle Flash</string>
|
||||
<string name="enter_cip_13_here">Enter cip 13 here..</string>
|
||||
<string name="enter_cip_13">Enter CIP 13</string>
|
||||
<string name="channel_description" translatable="false">PillDroid Channel</string>
|
||||
<string name="channel_description" translatable="false">PillDroid_NotificationChannel</string>
|
||||
<string name="channel_name" translatable="false">PillDroidChannel</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue