Refactor notifications.

This commit is contained in:
jacques 2020-07-21 17:33:41 +02:00
parent 2a3dab3398
commit a36febfd5e
2 changed files with 51 additions and 44 deletions

View file

@ -1,22 +1,12 @@
package net.foucry.pilldroid;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@ -29,6 +19,16 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import com.google.zxing.client.android.Intents;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
@ -37,10 +37,6 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.google.zxing.client.android.Intents;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID;
import static net.foucry.pilldroid.UtilDate.date2String;
import static net.foucry.pilldroid.Utils.intRandomExclusive;
@ -55,7 +51,6 @@ import static net.foucry.pilldroid.Utils.intRandomExclusive;
*/
public class MedicamentListActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "MedicamentCHANEL";
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
@ -265,7 +260,7 @@ public class MedicamentListActivity extends AppCompatActivity {
}
long delay = dateSchedule - now.getTime();
scheduleNotification(getNotification(getString(R.string.pharmacy)), delay);
scheduleNotification(this, delay);
Log.d(TAG, "Notification scheduled for " + UtilDate.convertDate(dateSchedule));
}
@ -409,12 +404,20 @@ public class MedicamentListActivity extends AppCompatActivity {
recyclerView.setAdapter(mAdapter);
}
private void scheduleNotification(Notification notification, long delay) {
/**
* Schedule Notification for the delay
* @param Context context
* @param long delay - date for the notification in milliseconds
*/
private void scheduleNotification(Context context, long delay) {
Log.d(TAG, "scheduleNotification delay == " + delay);
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.putExtra(NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy));
notificationIntent.putExtra((NotificationPublisher.KEY_TITLE, getString(R.string.app_name));
notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
@ -424,28 +427,6 @@ public class MedicamentListActivity extends AppCompatActivity {
}
}
private Notification getNotification(String content) {
Log.d(TAG, "getNotification");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getAppName())
.setContentText(content)
.setSmallIcon(R.drawable.ic_pill)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.mipmap.ic_launcher));
return builder.build();
}
private String getAppName() {
PackageManager packageManager = getApplicationContext().getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = packageManager.getApplicationInfo(this.getPackageName(), 0);
} catch (final PackageManager.NameNotFoundException ignored) {}
return (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");
}
/**
* SimpleItemRecyclerViewAdapter
*/

View file

@ -5,6 +5,8 @@ import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import androidx.core.app.NotificationCompat;
@ -17,26 +19,50 @@ import androidx.core.app.NotificationCompat;
public class NotificationPublisher extends BroadcastReceiver {
private static String TAG = NotificationPublisher.class.getName();
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
private static final String TAG = NotificationPublisher.class.getName();
public static String NOTIFICATION_ID = "notification_id";
public static String KEY_MESSAGE = "key_message";
public static String KEY_TITLE = "key_title";
public static String KEY_EXPAND = "key_expand";
public static String KEY_SOUND = "key_sound";
public static String KEY_MULTIPLE = "key_multiple";
public static String APP_NAME = "PillDroid";
/**
* onReceive notification
* @param Context context
* @param Intent intent
*/
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Receive notification");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
String message = intent.getStringExtra(KEY_MESSAGE);
String title = intent.getStringExtra(KEY_TITLE);
boolean isEnabledExpand = intent.getBooleanExtra(KEY_EXPAND, false);
boolean isEnableSound = intent.getBooleanExtra(KEY_SOUND, false);
boolean isEnabledMultiple = intent.getBooleanExtra(KEY_MULTIPLE, false);
String channel_id = createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id)
.setSmallIcon(R.drawable.ic_pill)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.pharmacy))
.setChannelId(APP_NAME)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(context.getString(R.string.pharmacy)))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (isEnableSound) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
}
notificationManager.notify(notificationId, builder.build());
}
/**