diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.java b/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.java
deleted file mode 100644
index 904dd086..00000000
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- 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 .
- */
-package org.secuso.privacyfriendlynotes.receiver;
-
-import android.app.AlarmManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Build;
-import android.widget.ArrayAdapter;
-
-import androidx.lifecycle.LifecycleOwner;
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProvider;
-import androidx.lifecycle.ViewModelStoreOwner;
-
-import org.secuso.privacyfriendlynotes.R;
-import org.secuso.privacyfriendlynotes.room.NoteDatabase;
-import org.secuso.privacyfriendlynotes.ui.notes.CreateEditNoteViewModel;
-import org.secuso.privacyfriendlynotes.room.model.Notification;
-import org.secuso.privacyfriendlynotes.service.NotificationService;
-
-import java.util.List;
-
-public class BootReceiver extends BroadcastReceiver {
- List allNotifications;
-
- public BootReceiver() {}
-
- @Override
- public void onReceive(Context context, Intent intent) {
- if(!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) return;
-
- AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-
- //CreateEditNoteViewModel createEditNoteViewModel = new ViewModelProvider(context.getApplicationContext()).get(CreateEditNoteViewModel.class);
- //ArrayAdapter adapter = new ArrayAdapter(context, R.layout.simple_spinner_item);
-
- allNotifications = NoteDatabase.getInstance(context).notificationDao().getAllNotifications();
-
-// createEditNoteViewModel.getAllNotifications().observe((LifecycleOwner) context, new Observer>() {
-// @Override
-// public void onChanged(List notifications) {
-// allNotifications = notifications;
-// }
-// });
-
-
- for(Notification currentNot: allNotifications){
-
- int notification_id = currentNot.get_noteId();
- long alarmTime = currentNot.getTime();
- //Create the intent that is fired by AlarmManager
- Intent i = new Intent(context, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
-
- PendingIntent pi = PendingIntent.getService(context, notification_id, i, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pi);
- } else {
- alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
- }
- }
- }
-}
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.kt
new file mode 100644
index 00000000..186c401c
--- /dev/null
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/receiver/BootReceiver.kt
@@ -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 .
+ */
+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? = 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 adapter = new ArrayAdapter(context, R.layout.simple_spinner_item);
+ allNotifications = NoteDatabase.getInstance(context).notificationDao().getAllNotifications()
+
+// createEditNoteViewModel.getAllNotifications().observe((LifecycleOwner) context, new Observer>() {
+// @Override
+// public void onChanged(List 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)
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/room/dao/NoteDao.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/room/dao/NoteDao.kt
index bb6573d4..7c300f20 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/room/dao/NoteDao.kt
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/room/dao/NoteDao.kt
@@ -61,4 +61,7 @@ interface NoteDao {
@Query("SELECT * FROM notes")
fun getNotesDebug() : List
+
+ @Query("SELECT * FROM notes WHERE _id = :id")
+ fun getNoteByID(id: Long): Note?
}
\ No newline at end of file
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/service/NotificationService.java b/app/src/main/java/org/secuso/privacyfriendlynotes/service/NotificationService.java
index 0e850817..e5b803be 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/service/NotificationService.java
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/service/NotificationService.java
@@ -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;
@@ -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) {
@@ -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());
}
}
}
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/helper/NotificationHelper.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/helper/NotificationHelper.kt
new file mode 100644
index 00000000..78caefea
--- /dev/null
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/helper/NotificationHelper.kt
@@ -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()
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.java b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.java
index 461d06c1..fcaf5a2f 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.java
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.java
@@ -14,16 +14,13 @@
package org.secuso.privacyfriendlynotes.ui.notes;
import android.Manifest;
-import android.app.AlarmManager;
import android.app.DatePickerDialog;
-import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
-import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
@@ -34,18 +31,6 @@
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.core.app.ActivityCompat;
-import androidx.core.content.ContextCompat;
-import androidx.core.content.FileProvider;
-import androidx.core.view.MenuItemCompat;
-import androidx.appcompat.app.AlertDialog;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.ShareActionProvider;
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProvider;
-
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@@ -67,15 +52,25 @@
import android.widget.TimePicker;
import android.widget.Toast;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.app.ActivityCompat;
+import androidx.core.content.ContextCompat;
+import androidx.core.content.FileProvider;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProvider;
+
+import org.secuso.privacyfriendlynotes.R;
+import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
import org.secuso.privacyfriendlynotes.room.DbContract;
import org.secuso.privacyfriendlynotes.room.model.Category;
import org.secuso.privacyfriendlynotes.room.model.Note;
import org.secuso.privacyfriendlynotes.room.model.Notification;
-import org.secuso.privacyfriendlynotes.service.NotificationService;
-import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
-import org.secuso.privacyfriendlynotes.R;
-import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import org.secuso.privacyfriendlynotes.ui.SettingsActivity;
+import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper;
+import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import java.io.File;
import java.io.FileInputStream;
@@ -125,7 +120,6 @@ public class AudioNoteActivity extends AppCompatActivity implements View.OnClick
private boolean shouldSave = true;
private int id = -1;
private int currentCat;
- Cursor notificationCursor = null;
private CreateEditNoteViewModel createEditNoteViewModel;
List allCategories;
@@ -133,6 +127,7 @@ public class AudioNoteActivity extends AppCompatActivity implements View.OnClick
private Menu menu;
private MenuItem item;
private Notification notification;
+ private String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -218,7 +213,6 @@ public void onChanged(String s) {
// observe notifications
notification = new Notification(-1,-1);
- CreateEditNoteViewModel createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.getAllNotifications().observe(this, new Observer>() {
@Override
public void onChanged(@Nullable List notifications) {
@@ -288,18 +282,20 @@ public void onNothingSelected(AdapterView> parent) {
if (edit) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- Intent intent = getIntent();
- etName.setText(intent.getStringExtra(EXTRA_TITLE));
- mFileName = intent.getStringExtra(EXTRA_CONTENT);
- mFilePath = getFilesDir().getPath() + "/audio_notes" + mFileName;
- btnPlayPause.setVisibility(View.VISIBLE);
- btnRecord.setVisibility(View.INVISIBLE);
- tvRecordingTime.setVisibility(View.INVISIBLE);
- //find the current category and set spinner to that
- currentCat = intent.getIntExtra(EXTRA_CATEGORY, -1);
-
- findViewById(R.id.btn_delete).setEnabled(true);
- ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ createEditNoteViewModel.getNoteByID(id).observe(this, noteFromDB -> {
+ title = noteFromDB.getName();
+ etName.setText(title);
+ mFileName = noteFromDB.getContent();
+ mFilePath = getFilesDir().getPath() + "/audio_notes" + mFileName;
+ btnPlayPause.setVisibility(View.VISIBLE);
+ btnRecord.setVisibility(View.INVISIBLE);
+ tvRecordingTime.setVisibility(View.INVISIBLE);
+ //find the current category and set spinner to that
+ currentCat = noteFromDB.getCategory();
+
+ findViewById(R.id.btn_delete).setEnabled(true);
+ ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ });
} else {
findViewById(R.id.btn_delete).setEnabled(false);
mFileName = "/recording_" + System.currentTimeMillis() + ".aac";
@@ -608,7 +604,6 @@ private void updateNote(){
fillNameIfEmpty();
Note note = new Note(etName.getText().toString(),mFileName,DbContract.NoteEntry.TYPE_AUDIO,currentCat);
note.set_id(id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.update(note);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
@@ -616,7 +611,6 @@ private void updateNote(){
private void saveNote(){
fillNameIfEmpty();
Note note = new Note(etName.getText().toString(),mFileName,DbContract.NoteEntry.TYPE_AUDIO,currentCat);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.insert(note);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
@@ -685,7 +679,6 @@ public void onClick(DialogInterface dialog, int which) {
Note note = new Note(intent.getStringExtra(EXTRA_TITLE),intent.getStringExtra(EXTRA_CONTENT),DbContract.NoteEntry.TYPE_AUDIO,intent.getIntExtra(EXTRA_CATEGORY,-1));
note.set_id(id);
note.setIn_trash(intent.getIntExtra(EXTRA_ISTRASH,0));
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
if(note.getIn_trash() == 1){
createEditNoteViewModel.delete(note);
} else {
@@ -729,7 +722,7 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth
this.year = year;
final Calendar c = Calendar.getInstance();
if (hasAlarm) {
- c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
+ c.setTimeInMillis(notification.getTime());
}
TimePickerDialog tpd = new TimePickerDialog(AudioNoteActivity.this, this, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
tpd.show();
@@ -742,7 +735,6 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notificationTimeSet = new Notification(id, (int) alarmtime.getTimeInMillis());
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
if (hasAlarm) {
@@ -758,34 +750,15 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//Store a reference for the notification in the database. This is later used by the service.
- //Create the intent that is fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notificationTimeSet.get_noteId());
-
- PendingIntent pi = PendingIntent.getService(this, notificationTimeSet.get_noteId(), i, PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
+ NotificationHelper.addNotificationToAlarmManager(this,id,DbContract.NoteEntry.TYPE_AUDIO,title,alarmtime.getTimeInMillis());
+ NotificationHelper.showAlertScheduledToast(this,dayOfMonth,monthOfYear,year,hourOfDay,minute);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- } else {
- alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- }
- Toast.makeText(getApplicationContext(), String.format(getString(R.string.toast_alarm_scheduled), dayOfMonth + "." + (monthOfYear+1) + "." + year + " " + hourOfDay + ":" + String.format("%02d",minute)), Toast.LENGTH_SHORT).show();
loadActivity(false);
}
private void cancelNotification(){
- //Create the intent that would be fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification.get_noteId());
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
-
-
- PendingIntent pi = PendingIntent.getService(this, notification.get_noteId(), i, PendingIntent.FLAG_UPDATE_CURRENT);
+ NotificationHelper.removeNotificationFromAlarmManager(this,id,DbContract.NoteEntry.TYPE_AUDIO,title);
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(pi);
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notification = new Notification(id, 0);
@@ -799,7 +772,7 @@ public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_reminder_edit) {
final Calendar c = Calendar.getInstance();
- c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
+ c.setTimeInMillis(notification.getTime());
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.java b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.java
index bd7a408e..eed3f1f7 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.java
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.java
@@ -71,6 +71,7 @@
import org.secuso.privacyfriendlynotes.service.NotificationService;
import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
import org.secuso.privacyfriendlynotes.R;
+import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper;
import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import org.secuso.privacyfriendlynotes.ui.SettingsActivity;
import org.secuso.privacyfriendlynotes.ui.util.CheckListAdapter;
@@ -110,7 +111,6 @@ public class ChecklistNoteActivity extends AppCompatActivity implements View.OnC
private boolean hasAlarm = false;
private boolean shouldSave = true;
private int id = -1;
- private int notification_id = -1;
private int currentCat;
Cursor notificationCursor = null;
@@ -139,7 +139,7 @@ protected void onCreate(Bundle savedInstanceState) {
spinner = (Spinner) findViewById(R.id.spinner_category);
//CategorySpinner
- CreateEditNoteViewModel createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
+ createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
adapter = new ArrayAdapter(this,R.layout.simple_spinner_item);
adapter.add(getString(R.string.default_category));
@@ -316,34 +316,35 @@ public void onDestroyActionMode(ActionMode mode) {
//fill in values if update
if (edit) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- Intent intent = getIntent();
- etName.setText(intent.getStringExtra(EXTRA_TITLE));
- try {
- JSONArray content = new JSONArray(intent.getStringExtra(EXTRA_CONTENT));
- for (int i=0; i < content.length(); i++) {
- JSONObject o = content.getJSONObject(i);
- itemNamesList.add(new CheckListItem(o.getBoolean("checked"), o.getString("name")));
+
+ createEditNoteViewModel.getNoteByID(id).observe(this, noteFromDB -> {
+ title = noteFromDB.getName();
+ etName.setText(title);
+ try {
+ JSONArray content = new JSONArray(noteFromDB.getContent());
+ itemNamesList.clear();
+ for (int i = 0; i < content.length(); i++) {
+ JSONObject o = content.getJSONObject(i);
+ itemNamesList.add(new CheckListItem(o.getBoolean("checked"), o.getString("name")));
+ }
+ ((ArrayAdapter) lvItemList.getAdapter()).notifyDataSetChanged();
+ } catch (Exception e) {
+ e.printStackTrace();
}
- ((ArrayAdapter)lvItemList.getAdapter()).notifyDataSetChanged();
- } catch (Exception e) {
- e.printStackTrace();
- }
- //find the current category and set spinner to that
- currentCat = intent.getIntExtra(EXTRA_CATEGORY, -1);
+ //find the current category and set spinner to that
+ currentCat = noteFromDB.getCategory();
- //fill the notificationCursor
- if(notification.get_noteId() >= 0) {
- hasAlarm = true;
- } else {
- hasAlarm = false;
- }
+ //fill the notificationCursor
+ if (notification.get_noteId() >= 0) {
+ hasAlarm = true;
+ } else {
+ hasAlarm = false;
+ }
- if (hasAlarm) {
- notification_id = notification.get_noteId();
- }
- findViewById(R.id.btn_delete).setEnabled(true);
- ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ findViewById(R.id.btn_delete).setEnabled(true);
+ ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ });
} else {
findViewById(R.id.btn_delete).setEnabled(false);
}
@@ -418,9 +419,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else {
hasAlarm = false;
}
- if (hasAlarm) {
- notification_id = notification.get_noteId();
- }
if (hasAlarm) {
//ask whether to delete or update the current alarm
@@ -523,7 +521,6 @@ private void updateNote(){
fillNameIfEmpty();
Note note = new Note(etName.getText().toString(),jsonArray.toString(),DbContract.NoteEntry.TYPE_CHECKLIST,currentCat);
note.set_id(id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.update(note);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
@@ -547,7 +544,6 @@ private void saveNote(){
//id = DbAccess.addNote(getBaseContext(), etName.getText().toString(), jsonArray.toString(), DbContract.NoteEntry.TYPE_CHECKLIST, currentCat);
Note note = new Note(etName.getText().toString(),jsonArray.toString(),DbContract.NoteEntry.TYPE_CHECKLIST,currentCat);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.insert(note);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
@@ -589,7 +585,6 @@ public void onClick(DialogInterface dialog, int which) {
private void displayTrashDialog() {
SharedPreferences sp = getSharedPreferences(PreferenceKeys.SP_DATA, Context.MODE_PRIVATE);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
Intent intent = getIntent();
Note note = new Note(intent.getStringExtra(EXTRA_TITLE),intent.getStringExtra(EXTRA_CONTENT),DbContract.NoteEntry.TYPE_CHECKLIST,intent.getIntExtra(EXTRA_CATEGORY,-1));
note.set_id(id);
@@ -638,7 +633,7 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth
this.year = year;
final Calendar c = Calendar.getInstance();
if (hasAlarm) {
- c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
+ c.setTimeInMillis(notification.getTime());
}
TimePickerDialog tpd = new TimePickerDialog(ChecklistNoteActivity.this, this, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
tpd.show();
@@ -652,7 +647,6 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notificationTimeSet = new Notification(id, (int) alarmtime.getTimeInMillis());
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
if (hasAlarm) {
@@ -668,34 +662,15 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
//Store a reference for the notification in the database. This is later used by the service.
- //Create the intent that is fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
+ NotificationHelper.addNotificationToAlarmManager(this,id,DbContract.NoteEntry.TYPE_CHECKLIST,title,alarmtime.getTimeInMillis());
+ NotificationHelper.showAlertScheduledToast(this,dayOfMonth,monthOfYear,year,hourOfDay,minute);
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- } else {
- alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- }
- Toast.makeText(getApplicationContext(), String.format(getString(R.string.toast_alarm_scheduled), dayOfMonth + "." + (monthOfYear+1) + "." + year + " " + hourOfDay + ":" + String.format("%02d",minute)), Toast.LENGTH_SHORT).show();
loadActivity(false);
}
private void cancelNotification(){
- //Create the intent that would be fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
-
-
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_UPDATE_CURRENT);
+ NotificationHelper.removeNotificationFromAlarmManager(this,id,DbContract.NoteEntry.TYPE_CHECKLIST,title);
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(pi);
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notification = new Notification(id, 0);
@@ -709,7 +684,7 @@ public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_reminder_edit) {
final Calendar c = Calendar.getInstance();
- c.setTimeInMillis(notificationCursor.getLong(notificationCursor.getColumnIndexOrThrow(DbContract.NotificationEntry.COLUMN_TIME)));
+ c.setTimeInMillis(notification.getTime());
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt
index c39d2caf..c5d78c79 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/CreateEditNoteViewModel.kt
@@ -14,10 +14,8 @@
package org.secuso.privacyfriendlynotes.ui.notes
import android.app.Application
-import androidx.lifecycle.AndroidViewModel
-import androidx.lifecycle.LiveData
-import androidx.lifecycle.MediatorLiveData
-import androidx.lifecycle.viewModelScope
+import android.util.Log
+import androidx.lifecycle.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -115,4 +113,17 @@ class CreateEditNoteViewModel(application: Application) : AndroidViewModel(appli
database.noteDao().delete(note)
}
}
+
+ fun getNoteByID(id: Long): LiveData {
+ val note = MutableLiveData()
+ Log.d(TAG, "Fetching note $id from database")
+ viewModelScope.launch(Dispatchers.IO) {
+ note.postValue(database.noteDao().getNoteByID(id))
+ }
+ return note
+ }
+
+ companion object{
+ private val TAG = "CreateEditNoteViewModel"
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.java b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.java
index 298df003..f25ce292 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.java
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.java
@@ -14,9 +14,7 @@
package org.secuso.privacyfriendlynotes.ui.notes;
import android.Manifest;
-import android.app.AlarmManager;
import android.app.DatePickerDialog;
-import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
@@ -34,18 +32,6 @@
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.core.app.ActivityCompat;
-import androidx.core.content.ContextCompat;
-import androidx.core.content.FileProvider;
-import androidx.core.view.MenuItemCompat;
-import androidx.appcompat.app.AlertDialog;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.ShareActionProvider;
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProvider;
-
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@@ -61,17 +47,27 @@
import android.widget.TimePicker;
import android.widget.Toast;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.app.ActivityCompat;
+import androidx.core.content.ContextCompat;
+import androidx.core.content.FileProvider;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProvider;
+
import com.simplify.ink.InkView;
+import org.secuso.privacyfriendlynotes.R;
+import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
import org.secuso.privacyfriendlynotes.room.DbContract;
import org.secuso.privacyfriendlynotes.room.model.Category;
import org.secuso.privacyfriendlynotes.room.model.Note;
import org.secuso.privacyfriendlynotes.room.model.Notification;
-import org.secuso.privacyfriendlynotes.service.NotificationService;
-import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
-import org.secuso.privacyfriendlynotes.R;
-import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import org.secuso.privacyfriendlynotes.ui.SettingsActivity;
+import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper;
+import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import java.io.File;
import java.io.FileNotFoundException;
@@ -80,7 +76,6 @@
import java.util.Calendar;
import java.util.Date;
import java.util.List;
-import java.util.Objects;
import petrov.kristiyan.colorpicker.ColorPicker;
@@ -113,7 +108,6 @@ public class SketchActivity extends AppCompatActivity implements View.OnClickLis
private boolean hasAlarm = false;
private boolean shouldSave = true;
private int id = -1;
- private int notification_id = -1;
private int currentCat;
private Notification notification;
@@ -245,21 +239,20 @@ public void onNothingSelected(AdapterView> parent) {
//fill in values if update
if (edit) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- Intent intent = getIntent();
- id = intent.getIntExtra(EXTRA_ID, -1);
- title = intent.getStringExtra(EXTRA_TITLE);
- etName.setText(title);
- mFileName = intent.getStringExtra(EXTRA_CONTENT);
- mFilePath = getFilesDir().getPath() + "/sketches" + mFileName;
- //find the current category and set spinner to that
- currentCat = intent.getIntExtra(EXTRA_CATEGORY, -1);
-
+ createEditNoteViewModel.getNoteByID(id).observe(this, noteFromDB -> {
+ title = noteFromDB.getName();
+ etName.setText(title);
+ mFileName = noteFromDB.getContent();
+ mFilePath = getFilesDir().getPath() + "/sketches" + mFileName;
+ //find the current category and set spinner to that
+ currentCat = noteFromDB.getCategory();
- findViewById(R.id.btn_delete).setEnabled(true);
- ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
- drawView.setBackground(new BitmapDrawable(getResources(), mFilePath));
+ findViewById(R.id.btn_delete).setEnabled(true);
+ ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ drawView.setBackground(new BitmapDrawable(getResources(), mFilePath));
+ });
} else {
findViewById(R.id.btn_delete).setEnabled(false);
mFileName = "/sketch_" + System.currentTimeMillis() + ".PNG";
@@ -345,7 +338,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
hasAlarm = false;
}
if (hasAlarm) {
- notification_id = notification.get_noteId();
//ask whether to delete or update the current alarm
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.action_reminder));
popupMenu.inflate(R.menu.reminder);
@@ -458,7 +450,6 @@ private void updateNote(){
}
Note note = new Note(etName.getText().toString(),mFileName,DbContract.NoteEntry.TYPE_SKETCH,currentCat);
note.set_id(id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.update(note);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
@@ -478,7 +469,6 @@ private void saveNote(){
}
Note note = new Note(etName.getText().toString(),mFileName,DbContract.NoteEntry.TYPE_SKETCH,currentCat);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.insert(note);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
@@ -547,7 +537,6 @@ public void onClick(DialogInterface dialog, int which) {
Note note = new Note(intent.getStringExtra(EXTRA_TITLE),intent.getStringExtra(EXTRA_CONTENT),DbContract.NoteEntry.TYPE_SKETCH,intent.getIntExtra(EXTRA_CATEGORY,-1));
note.set_id(id);
note.setIn_trash(intent.getIntExtra(EXTRA_ISTRASH,0));
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
if(note.getIn_trash() == 1){
createEditNoteViewModel.delete(note);
} else {
@@ -623,35 +612,14 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//Store a reference for the notification in the database. This is later used by the service.
- //Create the intent that is fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
- i.putExtra(NotificationService.NOTIFICATION_TYPE, DbContract.NoteEntry.TYPE_SKETCH);
- i.putExtra(NotificationService.NOTIFICATION_TITLE, title);
-
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- } else {
- alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- }
- Toast.makeText(getApplicationContext(), String.format(getString(R.string.toast_alarm_scheduled), dayOfMonth + "." + (monthOfYear+1) + "." + year + " " + hourOfDay + ":" + String.format("%02d",minute)), Toast.LENGTH_SHORT).show();
+ NotificationHelper.addNotificationToAlarmManager(this,id, DbContract.NoteEntry.TYPE_SKETCH, title, alarmtime.getTimeInMillis());
+ NotificationHelper.showAlertScheduledToast(this, dayOfMonth,monthOfYear,year,hourOfDay,minute);
loadActivity(false);
}
private void cancelNotification(){
- //Create the intent that would be fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
-
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(pi);
+ NotificationHelper.removeNotificationFromAlarmManager(this,id, DbContract.NoteEntry.TYPE_SKETCH, title);
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.java b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.java
index bb4d3e69..23e5a928 100644
--- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.java
+++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.java
@@ -15,9 +15,7 @@
import android.Manifest;
import android.annotation.SuppressLint;
-import android.app.AlarmManager;
import android.app.DatePickerDialog;
-import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
@@ -33,24 +31,10 @@
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.core.app.ActivityCompat;
-import androidx.core.content.ContextCompat;
-import androidx.core.view.MenuItemCompat;
-import androidx.appcompat.app.AlertDialog;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.ShareActionProvider;
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProvider;
-
-import android.text.Editable;
import android.text.Html;
-import android.text.InputFilter;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
-import android.text.SpannedString;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
@@ -66,19 +50,28 @@
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Spinner;
-import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.ShareActionProvider;
+import androidx.core.app.ActivityCompat;
+import androidx.core.content.ContextCompat;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProvider;
+
+import org.secuso.privacyfriendlynotes.R;
+import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
import org.secuso.privacyfriendlynotes.room.DbContract;
import org.secuso.privacyfriendlynotes.room.model.Category;
import org.secuso.privacyfriendlynotes.room.model.Note;
import org.secuso.privacyfriendlynotes.room.model.Notification;
-import org.secuso.privacyfriendlynotes.service.NotificationService;
-import org.secuso.privacyfriendlynotes.preference.PreferenceKeys;
-import org.secuso.privacyfriendlynotes.R;
-import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import org.secuso.privacyfriendlynotes.ui.SettingsActivity;
+import org.secuso.privacyfriendlynotes.ui.helper.NotificationHelper;
+import org.secuso.privacyfriendlynotes.ui.manageCategories.ManageCategoriesActivity;
import java.io.File;
import java.io.IOException;
@@ -113,7 +106,6 @@ public class TextNoteActivity extends AppCompatActivity implements View.OnClickL
private boolean hasAlarm = false;
private boolean shouldSave = true;
private int id = -1;
- private int notification_id = -1;
private int currentCat;
@@ -148,7 +140,7 @@ protected void onCreate(Bundle savedInstanceState) {
spinner = (Spinner) findViewById(R.id.spinner_category);
//CategorySpinner
- CreateEditNoteViewModel createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
+ createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
adapter = new ArrayAdapter(this,R.layout.simple_spinner_item);
adapter.add(getString(R.string.default_category));
@@ -270,24 +262,24 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
//fill in values if update
if (edit) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- Intent intent = getIntent();
- etName.setText(intent.getStringExtra(EXTRA_TITLE));
- etContent.setText(Html.fromHtml(intent.getStringExtra(EXTRA_CONTENT)));
- //find the current category and set spinner to that
- currentCat = intent.getIntExtra(EXTRA_CATEGORY, -1);
+ createEditNoteViewModel.getNoteByID(id).observe(this, noteFromDB -> {
+ title = noteFromDB.getName();
+ etName.setText(title);
+ etContent.setText(Html.fromHtml(noteFromDB.getContent()));
+ //find the current category and set spinner to that
+ currentCat = noteFromDB.getCategory();
- //fill the notificationCursor
- if(notification.get_noteId() >= 0) {
- hasAlarm = true;
- } else {
- hasAlarm = false;
- }
- if (hasAlarm) {
- notification_id = notification.get_noteId();
- }
- findViewById(R.id.btn_delete).setEnabled(true);
- ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+
+ //fill the notificationCursor
+ if(notification.get_noteId() >= 0) {
+ hasAlarm = true;
+ } else {
+ hasAlarm = false;
+ }
+ findViewById(R.id.btn_delete).setEnabled(true);
+ ((Button) findViewById(R.id.btn_save)).setText(getString(R.string.action_update));
+ });
} else {
findViewById(R.id.btn_delete).setEnabled(false);
}
@@ -362,9 +354,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else {
hasAlarm = false;
}
- if (hasAlarm) {
- notification_id = notification.get_noteId();
- }
if (hasAlarm) {
//ask whether to delete or update the current alarm
@@ -642,7 +631,6 @@ private void updateNote(){
fillNameIfEmpty();
Note note = new Note(etName.getText().toString(),Html.toHtml(etContent.getText()),DbContract.NoteEntry.TYPE_TEXT,currentCat);
note.set_id(id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.update(note);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
@@ -650,7 +638,6 @@ private void updateNote(){
private void saveNote(){
fillNameIfEmpty();
Note note = new Note(etName.getText().toString(),Html.toHtml(etContent.getText()),DbContract.NoteEntry.TYPE_TEXT,currentCat);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
createEditNoteViewModel.insert(note);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
@@ -688,7 +675,6 @@ public void onClick(DialogInterface dialog, int which) {
private void displayTrashDialog() {
SharedPreferences sp = getSharedPreferences(PreferenceKeys.SP_DATA, Context.MODE_PRIVATE);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
Intent intent = getIntent();
Note note = new Note(intent.getStringExtra(EXTRA_TITLE),intent.getStringExtra(EXTRA_CONTENT),DbContract.NoteEntry.TYPE_TEXT,intent.getIntExtra(EXTRA_CATEGORY,-1));
note.set_id(id);
@@ -750,7 +736,6 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notificationTimeSet = new Notification(id, (int) alarmtime.getTimeInMillis());
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
if (hasAlarm) {
@@ -767,33 +752,14 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//Store a reference for the notification in the database. This is later used by the service.
- //Create the intent that is fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
-
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- } else {
- alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
- }
- Toast.makeText(getApplicationContext(), String.format(getString(R.string.toast_alarm_scheduled), dayOfMonth + "." + (monthOfYear+1) + "." + year + " " + hourOfDay + ":" + String.format("%02d",minute)), Toast.LENGTH_SHORT).show();
+ NotificationHelper.addNotificationToAlarmManager(this,id,DbContract.NoteEntry.TYPE_TEXT,title,alarmtime.getTimeInMillis());
+ NotificationHelper.showAlertScheduledToast(this,dayOfMonth,monthOfYear,year,hourOfDay,minute);
loadActivity(false);
}
private void cancelNotification(){
//Create the intent that would be fired by AlarmManager
- Intent i = new Intent(this, NotificationService.class);
- i.putExtra(NotificationService.NOTIFICATION_ID, notification_id);
- createEditNoteViewModel = new ViewModelProvider(this).get(CreateEditNoteViewModel.class);
-
- PendingIntent pi = PendingIntent.getService(this, notification_id, i, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
-
- AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(pi);
+ NotificationHelper.removeNotificationFromAlarmManager(this,id,DbContract.NoteEntry.TYPE_TEXT,title);
Intent intent = getIntent();
id = intent.getIntExtra(EXTRA_ID, -1);
Notification notification = new Notification(id, 0);