Skip to content

Commit

Permalink
Even more mutability fixes...
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsFo committed Jan 1, 2023
1 parent 5bb2716 commit 12e3a8e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,34 +427,6 @@ public boolean onPreferenceClick(Preference preference) {
updateTimeAndDatePreference();
}

/**
* @Override public void onStart() {
* super.onStart();
* Timber.i("TimeAndDate: Start");
* updateTimeAndDatePreference();
* service = Executors.newFixedThreadPool(1);
* <p>
* service.submit(new Runnable() {
* @Override public void run() {
* while (true) {
* Timber.i("Running in parallel: Update Preview");
* try {
* //updateTimeAndDatePreference();
* Thread.sleep(1000);
* } catch (InterruptedException e) {
* Timber.e(e);
* }
* }
* }
* });
* }
* @Override public void onPause() {
* super.onPause();
* Timber.i("TimeAndDate: Pause");
* service.shutdown();
* }
**/

public void updateTimeAndDatePreference() {
Timber.i("Updating Time and Date preview.");
Preference preview = findPreference("prefs_time_preview");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -49,9 +50,11 @@ public NoteAdapter(Context context, int resource, List<Note> objects) {
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
Note note = getItem(position);
@SuppressLint("ViewHolder") View view = inflater.inflate(R.layout.note_row, parent, false);
if (note == null) {
Toast.makeText(getContext(), R.string.error_internal_error, Toast.LENGTH_LONG).show();
}

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
@SuppressLint("ViewHolder") View view = inflater.inflate(R.layout.note_row, parent, false);
TextView timestampTF = (TextView) view.findViewById(R.id.note_row_timestamp);
TextView positionTF = (TextView) view.findViewById(R.id.note_row_position);
ImageButton enabledBT = (ImageButton) view.findViewById(R.id.note_row_image_bt);
Expand Down Expand Up @@ -122,8 +125,10 @@ public void onClick(View view) {
});

TimeUtils utils = new TimeUtils(getContext());

long time = note.getTimestamp();
long time = Note.DEFAULT_TIMESTAMP;
if (note != null) {
time = note.getTimestamp();
}
if (utils.isRelativeTimePrefered()) {
timestampTF.setText(getContext().getString(R.string.last_edited, utils.formatRelative(time)));
RelativeTimeTextfieldContainer.getContainer().add(timestampTF, time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
int appWidgetId = appWidgetIds[i];

// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Intent intent = new Intent(context, MainActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// Get the layout for the App Widget and attach an on-click listener
// to the button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public void showNoteNotifications() {
}

//Version 6 or lower: Summarized, single notification that displays all notes in a single notification!

text = String.format(context.getString(R.string.notification_multiple_notes), String.valueOf(getNoteNotificationCount()));
String noteNotificationCount = String.valueOf(getNoteNotificationCount());
text = String.format(context.getString(R.string.notification_multiple_notes), noteNotificationCount);
builder.setNumber(getNoteNotificationCount());
bigText = "";
for (int i = 0; i < notesList.size(); i++) {
Expand All @@ -172,10 +172,16 @@ public void showNoteNotifications() {

builder.setContentText(text);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
builder.setDeleteIntent(createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE));
builder.addAction(R.drawable.baseline_notifications_off_black_24, context.getString(R.string.action_mark_disabled_all), createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE));
builder.setCategory(NotificationCompat.CATEGORY_REMINDER);

PendingIntent dismissIntent = createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE);
if (dismissIntent == null) {
Toast.makeText(context, R.string.error_notification_pending_intent_failure, Toast.LENGTH_LONG).show();
} else {
builder.setDeleteIntent(dismissIntent);
builder.addAction(R.drawable.baseline_notifications_off_black_24, context.getString(R.string.action_mark_disabled_all), createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE));
}

NotificationManager manager = getNotificationManagerService();
Notification notification = builder.build();
if (manager != null) {
Expand Down Expand Up @@ -285,20 +291,49 @@ private void displayAndroid7SummaryNoteNotifications() {
manager.notify(DEFAULT_NOTIFICATION_ID, notification);
}

private void applyActionsToIndividualNote(NotificationCompat.Builder builder, Note note) {
builder.setDeleteIntent(createOnNoteDismissIntent((int) note.getDatabaseID()));
builder.addAction(R.drawable.baseline_notifications_off_black_24, context.getString(R.string.action_mark_disabled), createOnNoteDismissIntent((int) note.getDatabaseID()));
private boolean applyActionsToIndividualNote(NotificationCompat.Builder builder, Note note) {
if (note == null) {
return false;
}

PendingIntent dismissIntent = createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE);
if (dismissIntent == null) {
Toast.makeText(context, R.string.error_notification_pending_intent_failure, Toast.LENGTH_LONG).show();
} else {
builder.setDeleteIntent(dismissIntent);
builder.addAction(R.drawable.baseline_notifications_off_black_24, context.getString(R.string.action_mark_disabled_all), createOnNoteDismissIntent(INTENT_EXTRA_NOTE_ID_NONE));
}

Timber.i("Setting up notification actions for note ID " + note.getDatabaseID() + " (" + note.getTextPreview() + ").");
builder.setContentIntent(getIntentToNote(note));
PendingIntent intentToNote = getIntentToNote(note);
if (intentToNote == null) {
Toast.makeText(context, R.string.error_notification_pending_intent_failure, Toast.LENGTH_LONG).show();
} else {
builder.setContentIntent(intentToNote);
}
createOnNoteContentIntent(builder, note);

return true;
}

private PendingIntent createOnNoteDismissIntent(int notificationId) {
Intent intent = new Intent(context, NotificationDismissedReceiver.class);
intent.putExtra(INTENT_EXTRA_NOTE_ID, notificationId);
Timber.i("Creating a dismiss intent. ID: " + notificationId);
return PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent broadcast = null;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
broadcast = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else {
broadcast = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
} catch (Exception e) {
Timber.e(e);
Timber.e("Failed to create on Dismissed event!");
Toast.makeText(context, R.string.error_notification_pending_intent_failure, Toast.LENGTH_LONG).show();
}
return broadcast;
}

private void createOnNoteContentIntent(NotificationCompat.Builder builder, Note note) {
Expand Down Expand Up @@ -340,16 +375,22 @@ private void createOnNoteContentIntent(NotificationCompat.Builder builder, Note
return;
}

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) note.getDatabaseID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(iconID, context.getString(textID), pendingIntent);
}
PendingIntent pendingIntent = null;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getActivity(context, (int) note.getDatabaseID(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, (int) note.getDatabaseID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}catch (Exception e){
Timber.e(e);
Timber.e("Error! Could not set up note context specific intent!");
Toast.makeText(context, R.string.error_notification_pending_intent_failure,Toast.LENGTH_LONG).show();
}

@Deprecated
private PendingIntent createOnNoteDeleteIntent(int notificationId) {
Intent intent = new Intent(context, NotificationDeleteReceiver.class);
intent.putExtra(INTENT_EXTRA_NOTE_ID, notificationId);
Timber.i("Creating a delete intent. ID: " + notificationId);
return PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (pendingIntent != null) {
builder.addAction(iconID, context.getString(textID), pendingIntent);
}
}

private NotificationCompat.Builder getGenericNotificationBuilder(String tickerMessage, boolean ongoing, int priority, String channelID) {
Expand All @@ -368,7 +409,18 @@ private PendingIntent getIntentToNote(Note note) {
intent.putExtra(EditNoteActivity.EXTRA_ACTIVITY_STANDALONE, true);

int requestCode = (int) (REQUEST_CODE_INTENT_OPEN_APP_EDIT_NOTE_DYNAMIC_BASE + note.getDatabaseID());
return PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = null;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
} catch (Exception e) {
Timber.e(e);
Timber.e("Error! Could not set up intent for Note!");
}
return pendingIntent;
}

public void displayNotificationAutomaticBackup(boolean success, String contentText) {
Expand Down Expand Up @@ -452,8 +504,10 @@ private NotificationCompat.Builder getNotesBuilder() {
PendingIntent pendingIntent = getIntentToMainActivity();
if (pendingIntent != null) {
builder.setContentIntent(pendingIntent);
} else {
Toast.makeText(context, R.string.error_notification_pending_intent_failure, Toast.LENGTH_LONG).show();
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ public boolean isEmpty() {
}

public interface NotesRecyclerAdapterListener {
public void onCardNotePressed(long noteID);
void onCardNotePressed(long noteID);

public void onCardNotePressedLong(long noteID);
void onCardNotePressedLong(long noteID);

public void onCardNoteMenuPressed(long noteID, MenuItem itemID);
void onCardNoteMenuPressed(long noteID, MenuItem itemID);

public void onCardNoteToggleImagePressed(long noteID);
void onCardNoteToggleImagePressed(long noteID);
}

class ViewHolder extends RecyclerView.ViewHolder implements PopupMenu.OnMenuItemClickListener {
Expand Down

0 comments on commit 12e3a8e

Please sign in to comment.