-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
10 changed files
with
279 additions
and
334 deletions.
There are no files selected for viewing
78 changes: 0 additions & 78 deletions
78
app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/org/secuso/privacyfriendlynotes/ui/helper/NotificationHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.