Skip to content

Commit

Permalink
Merge pull request moneymanagerex#1742 from wolfsolver/notification
Browse files Browse the repository at this point in the history
Notification Management & Recurring Transaction
  • Loading branch information
guanlisheng authored Jul 27, 2024
2 parents 63b9cab + d049770 commit c9d12f5
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ private void populateScheduledTransactions() {
Intent serviceRepeatingTransaction = new Intent(getApplicationContext(), RecurringTransactionBootReceiver.class);
getApplicationContext().sendBroadcast(serviceRepeatingTransaction);

/* // EP Remove from start intent and set in notification management
if (!autoExecution) return;
QueryBillDeposits billDeposits = new QueryBillDeposits(getApplicationContext());
Expand Down Expand Up @@ -1288,7 +1289,7 @@ private void populateScheduledTransactions() {
}
}
cursor.close();

*/
// TODO persist
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@
import com.money.manager.ex.R;
import com.money.manager.ex.currency.CurrencyService;
import com.money.manager.ex.database.QueryBillDeposits;
import com.money.manager.ex.datalayer.AccountTransactionRepository;
import com.money.manager.ex.domainmodel.AccountTransaction;
import com.money.manager.ex.recurring.transactions.Recurrence;
import com.money.manager.ex.recurring.transactions.RecurringTransactionListActivity;
import com.money.manager.ex.servicelayer.RecurringTransactionService;
import com.money.manager.ex.settings.AppSettings;
import com.money.manager.ex.utils.NotificationUtils;

import info.javaperformance.money.MoneyFactory;
import timber.log.Timber;

public class RecurringTransactionNotifications {

// Notification channel definition will be move into NotificationUtils to centralize loghic
// Notification channel definition will be move into NotificationUtils to centralize logic
// public static String CHANNEL_ID = "RecurringTransaction_NotificationChannel";
private static final int ID_NOTIFICATION = 0x000A;

Expand Down Expand Up @@ -81,53 +85,99 @@ the parameters will not work (for whatever reason).
}

private void showNotification(SyncNotificationModel model) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for ( String line: model.inboxLine ) {
inboxStyle.addLine(line);
}

NotificationManager notificationManager = (NotificationManager) getContext()
.getSystemService(Context.NOTIFICATION_SERVICE);

// create pending intent
Intent intent = new Intent(getContext(), RecurringTransactionListActivity.class);
// set launch from notification // check pin code
intent.putExtra(RecurringTransactionListActivity.INTENT_EXTRA_LAUNCH_NOTIFICATION, true);

PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);

// todo: Actions
// Intent skipIntent = new Intent(intent);
// //skipIntent.setAction(Intent.)
// PendingIntent skipPending = PendingIntent.getActivity(getContext(), 0, skipIntent, 0);
// Intent enterIntent = new Intent(getContext(), RecurringTransactionEditActivity.class);
// PendingIntent enterPending = PendingIntent.getActivity(getContext(), 0, enterIntent, 0);

// create notification
try {
NotificationUtils.createNotificationChannel(getContext(), NotificationUtils.CHANNEL_ID_RECURRING);

Notification notification = new NotificationCompat.Builder(getContext(), NotificationUtils.CHANNEL_ID_RECURRING)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(mContext.getString(R.string.application_name))
.setContentText(mContext.getString(R.string.notification_repeating_transaction_expired))
.setSubText(mContext.getString(R.string.notification_click_to_check_repeating_transaction))
.setSmallIcon(R.drawable.ic_stat_notification)
.setTicker(mContext.getString(R.string.notification_repeating_transaction_expired))
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setNumber(model.number)
.setStyle(inboxStyle)
.setColor(mContext.getResources().getColor(R.color.md_primary))
// .addAction(R.drawable.ic_action_content_clear_dark, getContext().getString(R.string.skip), skipPending)
// .addAction(R.drawable.ic_action_done_dark, getContext().getString(R.string.enter), enterPending)
.build();

// notify
notificationManager.cancel(ID_NOTIFICATION);
notificationManager.notify(ID_NOTIFICATION, notification);
} catch (Exception e) {
Timber.e(e, "showing notification for recurring transaction");
AppSettings settings = new AppSettings(this.getContext());
boolean autoExecution = settings.getBehaviourSettings().getExecutionScheduledTransaction();

for ( SyncNotificationModel.SyncNotificationModelSingle schedTrx: model.notifications ) {

// NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

if (schedTrx.mode.equals("A") && autoExecution) {
schedTrx.inboxLine = schedTrx.inboxLine.concat(" (AutoExecuted)");
RecurringTransactionService service = new RecurringTransactionService(schedTrx.trxId, this.getContext());
AccountTransactionRepository accountTransactionRepository = new AccountTransactionRepository( this.getContext());
AccountTransaction accountTrx = service.getAccountTransactionFromRecurring();
accountTransactionRepository.insert(accountTrx);
service.moveNextOccurrence();

}


NotificationManager notificationManager = (NotificationManager) getContext()
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent showIntent = new Intent(getContext(), RecurringTransactionListActivity.class);
// set launch from notification // check pin code
showIntent.putExtra(RecurringTransactionListActivity.INTENT_EXTRA_LAUNCH_NOTIFICATION, true);
showIntent.setAction("SHOW/"+schedTrx.trxId);
showIntent.putExtra("ACTION", "SHOW");
showIntent.putExtra("ID", schedTrx.trxId);
PendingIntent showPending = PendingIntent.getActivity(getContext(), 0, showIntent, PendingIntent.FLAG_IMMUTABLE);

// todo: Actions
Intent skipIntent = new Intent(getContext(), RecurringTransactionListActivity.class);
skipIntent.putExtra(RecurringTransactionListActivity.INTENT_EXTRA_LAUNCH_NOTIFICATION, true);
skipIntent.setAction("SKIP/"+schedTrx.trxId);
skipIntent.putExtra( "ACTION", "SKIP");
skipIntent.putExtra("ID", schedTrx.trxId);
PendingIntent skipPending = PendingIntent.getActivity(getContext(), 0, skipIntent, PendingIntent.FLAG_IMMUTABLE);

Intent enterIntent = new Intent(getContext(), RecurringTransactionListActivity.class);
enterIntent.putExtra(RecurringTransactionListActivity.INTENT_EXTRA_LAUNCH_NOTIFICATION, true);
enterIntent.setAction("ENTER/"+schedTrx.trxId);
enterIntent.putExtra( "ACTION", "ENTER");
enterIntent.putExtra("ID", schedTrx.trxId);
PendingIntent enterPending = PendingIntent.getActivity(getContext(), 0, enterIntent, PendingIntent.FLAG_IMMUTABLE);

// create notification
try {
NotificationUtils.createNotificationChannel(getContext(), NotificationUtils.CHANNEL_ID_RECURRING);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), NotificationUtils.CHANNEL_ID_RECURRING)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_notification)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(showPending)
.setContentTitle(mContext.getString(R.string.application_name))
.setContentText(mContext.getString(R.string.notification_repeating_transaction_expired))
.setContentText(schedTrx.inboxLine)
// .setSubText(mContext.getString(R.strisTE | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
// .setNumber(model.number)
.setStyle(new NotificationCompat.BigTextStyle().bigText(schedTrx.inboxLine))
.setColor(mContext.getResources().getColor(R.color.md_primary));

if (!(schedTrx.mode.equals("A") && autoExecution)) {
builder
.addAction(R.drawable.ic_action_done_dark, getContext().getString(R.string.enter), enterPending)
.addAction(R.drawable.ic_action_content_clear_dark, getContext().getString(R.string.skip), skipPending);
}

Notification notification = builder.build();

/* Notification notification = new NotificationCompat.Builder(getContext(), NotificationUtils.CHANNEL_ID_RECURRING)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_notification)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(showPending)
.setContentTitle(mContext.getString(R.string.application_name))
.setContentText(mContext.getString(R.string.notification_repeating_transaction_expired))
.setContentText(schedTrx.inboxLine)
// .setSubText(mContext.getString(R.strisTE | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
// .setNumber(model.number)
.setStyle(new NotificationCompat.BigTextStyle().bigText(schedTrx.inboxLine))
.setColor(mContext.getResources().getColor(R.color.md_primary))
.addAction(R.drawable.ic_action_done_dark, getContext().getString(R.string.enter), enterPending)
.addAction(R.drawable.ic_action_content_clear_dark, getContext().getString(R.string.skip), skipPending)
// .addAction(R.drawable.ic_action_list_dark , getContext().getString(R.string.show ), showPending)
.build();
*/

// notify
notificationManager.cancel(schedTrx.trxId);
notificationManager.notify(schedTrx.trxId, notification);
} catch (Exception e) {
Timber.e(e, "showing notification for recurring transaction");
}
}
}

Expand All @@ -151,11 +201,14 @@ private SyncNotificationModel getNotificationContent(Cursor cursor) {
// compose text
String line = cursor.getString(cursor.getColumnIndex(QueryBillDeposits.NEXTOCCURRENCEDATE)) +
" " + payeeName +
": <b>" + currencyService.getCurrencyFormatted(cursor.getInt(cursor.getColumnIndex(QueryBillDeposits.CURRENCYID)),
": " + currencyService.getCurrencyFormatted(cursor.getInt(cursor.getColumnIndex(QueryBillDeposits.CURRENCYID)),
MoneyFactory.fromDouble(cursor.getDouble(cursor.getColumnIndex(QueryBillDeposits.AMOUNT)))) +
"</b> (" + Recurrence.recurringModeString( recurringMode ) + ")";
" (" + Recurrence.recurringModeString( recurringMode ) + ")";

// result.inboxLine.add( Html.fromHtml("<small>" + line + "</small>").toString());
result.addNotification( line ,
Recurrence.recurringModeString( recurringMode ), cursor.getInt(cursor.getColumnIndex(QueryBillDeposits.BDID)));

result.inboxLine.add( Html.fromHtml("<small>" + line + "</small>").toString());
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,23 @@

public class SyncNotificationModel {
public int number;
public List<String> inboxLine = new ArrayList<String>();;
// public List<String> inboxLine = new ArrayList<String>();;
public List<SyncNotificationModelSingle> notifications = new ArrayList<SyncNotificationModelSingle>();;

public static class SyncNotificationModelSingle {
String inboxLine ;
String mode;
int trxId;

public SyncNotificationModelSingle (String inboxLine, String mode, int trxId) {
this.inboxLine = inboxLine;
this.mode = mode;
this.trxId = trxId;
}
}

public void addNotification(String inboxLine, String mode, Integer trxId) {
notifications.add(new SyncNotificationModelSingle(inboxLine, mode, trxId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.money.manager.ex.recurring.transactions;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
Expand All @@ -26,6 +28,14 @@
import com.money.manager.ex.R;
import com.money.manager.ex.common.MmxBaseFragmentActivity;
import com.money.manager.ex.core.Passcode;
import com.money.manager.ex.core.TransactionTypes;
import com.money.manager.ex.datalayer.AccountTransactionRepository;
import com.money.manager.ex.datalayer.RecurringTransactionRepository;
import com.money.manager.ex.domainmodel.AccountTransaction;
import com.money.manager.ex.domainmodel.RecurringTransaction;
import com.money.manager.ex.notifications.RecurringTransactionNotifications;
import com.money.manager.ex.servicelayer.RecurringTransactionService;
import com.money.manager.ex.settings.AppSettings;

/**
* Not used.
Expand All @@ -44,8 +54,13 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_toolbar_activity);

Integer trxid = 0;
String action = "";

// check if launch from notification
if (getIntent() != null && getIntent().getBooleanExtra(INTENT_EXTRA_LAUNCH_NOTIFICATION, false)) {
action = getIntent().getStringExtra("ACTION");
trxid = getIntent().getIntExtra("ID", 0);
Passcode passcode = new Passcode(getApplicationContext());
if (passcode.hasPasscode()) {
Intent intent = new Intent(this, PasscodeActivity.class);
Expand All @@ -55,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
// start activity
startActivityForResult(intent, INTENT_REQUEST_PASSCODE);
}

}
// set actionbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand All @@ -66,6 +82,36 @@ protected void onCreate(Bundle savedInstanceState) {
if (fm.findFragmentById(R.id.content) == null) {
fm.beginTransaction().add(R.id.content, listFragment, FRAGMENTTAG).commit();
}

if ( action.equals("SKIP") || action.equals("ENTER")) {
// ToDo Skip or enter Occurrence
NotificationManager notificationManager = (NotificationManager) getApplication().getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(trxid);

if (action.equals("SKIP")) {
RecurringTransactionService recurringTransaction = new RecurringTransactionService(trxid, this);
recurringTransaction.moveNextOccurrence();
}

if (action.equals("ENTER")) {
// ToDo: autopost automtically, try to find a way to open directly edit
// showCreateTransactionActivity(trxid);

// boolean isAutoExecution = (new AppSettings(this)).getBehaviourSettings().getNotificationRecurringTransaction();
// if (!isAutoExecution) { // TODO: set with dialog
// showCreateTransactionActivity(trxid);
// } else {
RecurringTransactionService service = new RecurringTransactionService(trxid, this);
AccountTransactionRepository accountTransactionRepository = new AccountTransactionRepository(getApplicationContext());
AccountTransaction accountTrx = service.getAccountTransactionFromRecurring();
accountTransactionRepository.insert(accountTrx);
service.moveNextOccurrence();

// }

}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
// show floating button.
setFloatingActionButtonVisible(true);
attachFloatingActionButtonToListView();

}

@Override
Expand All @@ -120,6 +121,7 @@ public void onCreate(Bundle savedInstanceState) {
MmexApplication.getApp().iocComponent.inject(this);

setHasOptionsMenu(true);
Intent i = getActivity().getParentActivityIntent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.money.manager.ex.Constants;
import com.money.manager.ex.R;
import com.money.manager.ex.core.TransactionTypes;
import com.money.manager.ex.core.UIHelper;
import com.money.manager.ex.database.ISplitTransaction;
import com.money.manager.ex.datalayer.AccountTransactionRepository;
import com.money.manager.ex.datalayer.RecurringTransactionRepository;
import com.money.manager.ex.datalayer.SplitRecurringCategoriesRepository;
import com.money.manager.ex.domainmodel.AccountTransaction;
import com.money.manager.ex.domainmodel.RecurringTransaction;
import com.money.manager.ex.domainmodel.SplitRecurringCategory;
import com.money.manager.ex.recurring.transactions.Recurrence;
Expand Down Expand Up @@ -440,4 +445,27 @@ private void moveDueDateForward() {
tx.setDueDate(newDueDate);
}
}

public AccountTransaction getAccountTransactionFromRecurring () {
RecurringTransactionRepository scheduledRepo = new RecurringTransactionRepository(this.getContext());
RecurringTransaction scheduledTrx = scheduledRepo.load(recurringTransactionId);
return (scheduledTrx == null) ? null : getAccountTransactionFromRecurring( scheduledTrx ) ;
}
public AccountTransaction getAccountTransactionFromRecurring (@NonNull RecurringTransaction scheduledTrx) {
AccountTransaction accountTrx = AccountTransaction.create();
accountTrx.setDate(scheduledTrx.getPaymentDate());
accountTrx.setAccountId(scheduledTrx.getAccountId());
accountTrx.setAccountToId(scheduledTrx.getToAccountId());
accountTrx.setTransactionType(TransactionTypes.valueOf(scheduledTrx.getTransactionCode()));
accountTrx.setStatus(scheduledTrx.getStatus());
accountTrx.setAmount(scheduledTrx.getAmount());
accountTrx.setAmountTo(scheduledTrx.getAmountTo());
accountTrx.setPayeeId(scheduledTrx.getPayeeId());
accountTrx.setCategoryId(scheduledTrx.getCategoryId());
accountTrx.setTransactionNumber(scheduledTrx.getTransactionNumber());
accountTrx.setNotes(scheduledTrx.getNotes());

return accountTrx;
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@
<string name="notification_channel_fileoperation_uploadin">Mmex Uploading</string>
<string name="notification_channel_fileoperation_complete">Mmex Upload Complete</string>
<string name="notification_channel_fileoperation_conflict">Mmex Sync Conflict</string>
<string name="show">Show</string>


</resources>
Loading

0 comments on commit c9d12f5

Please sign in to comment.