Skip to content

Commit

Permalink
Fix notifications
Browse files Browse the repository at this point in the history
- Fix BootReceiver accessing database from main thread
- Fix crash in NotificationService
- Add NotificationHelper for unified notification creation
- Update note activities
  - Remove unnecessary calls of new ViewModelProvider()
  - Fetch current note state from database in loadActivity()
  - Use NotificationHelper to schedule notifications
- Add method to get note by id from database
  • Loading branch information
Tobias Länge authored and Kamuno committed Aug 6, 2022
1 parent 8a3a212 commit 841b831
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 334 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
This file is part of the application Privacy Friendly Notes.
Privacy Friendly Notes is free software:
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or any later version.
Privacy Friendly Notes is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Privacy Friendly Notes. If not, see <http://www.gnu.org/licenses/>.
*/
package org.secuso.privacyfriendlynotes.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.secuso.privacyfriendlynotes.room.NoteDatabase
import org.secuso.privacyfriendlynotes.room.model.Notification
import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper

class BootReceiver : BroadcastReceiver() {
var allNotifications: List<Notification>? = null
override fun onReceive(context: Context, intent: Intent) {
if (Intent.ACTION_BOOT_COMPLETED != intent.action && Intent.ACTION_AIRPLANE_MODE_CHANGED != intent.action) return
Log.d(javaClass.simpleName, "Running onReceive...")
GlobalScope.launch {

//CreateEditNoteViewModel createEditNoteViewModel = new ViewModelProvider(context.getApplicationContext()).get(CreateEditNoteViewModel.class);
//ArrayAdapter<CharSequence> adapter = new ArrayAdapter(context, R.layout.simple_spinner_item);
allNotifications = NoteDatabase.getInstance(context).notificationDao().getAllNotifications()

// createEditNoteViewModel.getAllNotifications().observe((LifecycleOwner) context, new Observer<List<Notification>>() {
// @Override
// public void onChanged(List<Notification> notifications) {
// allNotifications = notifications;
// }
// });
Log.d(javaClass.simpleName, allNotifications!!.size.toString() + " Notifications found.")
for ((notification_id, time) in allNotifications!!) {

val alarmTime = time.toLong()
val note = NoteDatabase.getInstance(context).noteDao().getNoteByID(notification_id.toLong())
note?.let {
NotificationHelper.addNotificationToAlarmManager(context, note._id, note.type, note.name, alarmTime)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ interface NoteDao {

@Query("SELECT * FROM notes")
fun getNotesDebug() : List<Note>

@Query("SELECT * FROM notes WHERE _id = :id")
fun getNoteByID(id: Long): Note?
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
package org.secuso.privacyfriendlynotes.service;

import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

Expand All @@ -35,18 +40,20 @@ public class NotificationService extends IntentService {
public static final String NOTIFICATION_TYPE = "notification_type";
public static final String NOTIFICATION_TITLE = "notification_title";

public static final String NOTIFICATION_CHANNEL = "Notes_Notifications";


public NotificationService (){
public NotificationService() {
super("Notification service");
}

@Override
protected void onHandleIntent(Intent intent) {
int notification_id = intent.getIntExtra(NOTIFICATION_ID, -1);
int type = intent.getIntExtra(NOTIFICATION_TYPE,-1);
int type = intent.getIntExtra(NOTIFICATION_TYPE, -1);
String name = intent.getStringExtra(NOTIFICATION_TITLE);
if (notification_id != -1) {
Log.d(getClass().getSimpleName(), "onHandleIntent(" + NOTIFICATION_ID + ":" + notification_id + ";" + NOTIFICATION_TYPE + ":" + type + ";" + NOTIFICATION_TITLE + ":" + name + ")");
if (notification_id != -1 && type != -1) {
//Gather the info for the notification itself
Intent i = null;
switch (type) {
Expand All @@ -68,14 +75,23 @@ protected void onHandleIntent(Intent intent) {
break;
}

PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext());
Log.d(getClass().getSimpleName(), "Creating intent for " + getBaseContext() + " with type " + type + " and intent " + intent + " and id " + notification_id + " and name " + name);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, i, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);


NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(getString(R.string.app_name));
mNotifyMgr.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext(), NOTIFICATION_CHANNEL);
mBuilder.setSmallIcon(R.mipmap.ic_notification)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(name)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

mNotifyMgr.notify(notification_id, mBuilder.build());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.secuso.privacyfriendlynotes.ui.helper

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import org.secuso.privacyfriendlynotes.R
import org.secuso.privacyfriendlynotes.service.NotificationService

object NotificationHelper {
private val TAG = "NotificationHelper"

@JvmStatic
fun addNotificationToAlarmManager(context: Context, note_id: Int, noteType: Int, notificationTitle: String, alarmTimeMillis: Long) {
Log.d(TAG, "Scheduling notification. ID=$note_id;Type=$noteType;Title=$notificationTitle;Time=$alarmTimeMillis")

//Create the intent that is fired by AlarmManager
val pi = createNotificationPendingIntent(context, note_id, noteType, notificationTitle)

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pi)
}

@JvmStatic
fun removeNotificationFromAlarmManager(context: Context, note_id: Int, noteType: Int, notificationTitle: String) {
//Create the intent that would be fired by AlarmManager
val pi = createNotificationPendingIntent(context, note_id, noteType, notificationTitle)

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.cancel(pi)
}

private fun createNotificationPendingIntent(context: Context, note_id: Int, noteType: Int, notificationTitle: String): PendingIntent {
val i = Intent(context, NotificationService::class.java)
i.putExtra(NotificationService.NOTIFICATION_ID, note_id)
i.putExtra(NotificationService.NOTIFICATION_TYPE, noteType)
i.putExtra(NotificationService.NOTIFICATION_TITLE, notificationTitle)

val pi = PendingIntent.getService(context, note_id, i, PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
return pi
}

@JvmStatic
fun showAlertScheduledToast(context: Context, dayOfMonth: Int, monthOfYear: Int, year: Int, hourOfDay: Int, minute: Int) {
Toast.makeText(
context.applicationContext,
String.format(
context.getString(R.string.toast_alarm_scheduled),
dayOfMonth.toString() + "." + (monthOfYear + 1) + "." + year + " " + hourOfDay + ":" + String.format("%02d", minute)
),
Toast.LENGTH_SHORT
).show()
}
}
Loading

0 comments on commit 841b831

Please sign in to comment.