put error notification into notification service

This commit is contained in:
iNPUTmice 2014-11-18 15:26:28 +01:00
parent 1122e084a0
commit 66087cb360
7 changed files with 42 additions and 46 deletions

View file

@ -20,6 +20,7 @@ import android.util.DisplayMetrics;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -31,6 +32,7 @@ import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity; import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.ui.ManageAccountActivity;
public class NotificationService { public class NotificationService {
@ -40,6 +42,8 @@ public class NotificationService {
public static int NOTIFICATION_ID = 0x2342; public static int NOTIFICATION_ID = 0x2342;
public static int FOREGROUND_NOTIFICATION_ID = 0x8899; public static int FOREGROUND_NOTIFICATION_ID = 0x8899;
public static int ERROR_NOTIFICATION_ID = 0x5678;
private Conversation mOpenConversation; private Conversation mOpenConversation;
private boolean mIsInForeground; private boolean mIsInForeground;
private long mLastNotification; private long mLastNotification;
@ -380,4 +384,39 @@ public class NotificationService {
mBuilder.setPriority(NotificationCompat.PRIORITY_MIN); mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
return mBuilder.build(); return mBuilder.build();
} }
public void updateErrorNotification() {
NotificationManager mNotificationManager = (NotificationManager) mXmppConnectionService.getSystemService(Context.NOTIFICATION_SERVICE);
List<Account> errors = new ArrayList<>();
for (Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus()) {
errors.add(account);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService);
if (errors.size() == 0) {
mNotificationManager.cancel(ERROR_NOTIFICATION_ID);
return;
} else if (errors.size() == 1) {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
mBuilder.setContentText(errors.get(0).getJid().toBareJid().toString());
} else {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_accounts));
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_fix));
}
mBuilder.setOngoing(true);
mBuilder.setLights(0xffffffff, 2000, 4000);
mBuilder.setSmallIcon(R.drawable.ic_stat_alert_warning);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mXmppConnectionService);
stackBuilder.addParentStack(ConversationActivity.class);
Intent manageAccountsIntent = new Intent(mXmppConnectionService,ManageAccountActivity.class);
stackBuilder.addNextIntent(manageAccountsIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
mNotificationManager.notify(ERROR_NOTIFICATION_ID, notification);
}
} }

View file

@ -204,8 +204,7 @@ public class XmppConnectionService extends Service {
scheduleWakeupCall((int) (next * 1.2), false); scheduleWakeupCall((int) (next * 1.2), false);
} }
} }
UIHelper.showErrorNotification(getApplicationContext(), getNotificationService().updateErrorNotification();
getAccounts());
} }
}; };
private Integer accountChangedListenerCount = 0; private Integer accountChangedListenerCount = 0;
@ -1071,7 +1070,7 @@ public class XmppConnectionService extends Service {
databaseBackend.updateAccount(account); databaseBackend.updateAccount(account);
reconnectAccount(account, false); reconnectAccount(account, false);
updateAccountUi(); updateAccountUi();
UIHelper.showErrorNotification(getApplicationContext(), getAccounts()); getNotificationService().updateErrorNotification();
} }
public void deleteAccount(Account account) { public void deleteAccount(Account account) {
@ -1092,7 +1091,7 @@ public class XmppConnectionService extends Service {
databaseBackend.deleteAccount(account); databaseBackend.deleteAccount(account);
this.accounts.remove(account); this.accounts.remove(account);
updateAccountUi(); updateAccountUi();
UIHelper.showErrorNotification(getApplicationContext(), getAccounts()); getNotificationService().updateErrorNotification();
} }
} }

View file

@ -106,48 +106,6 @@ public class UIHelper {
} }
} }
public static void showErrorNotification(Context context,
List<Account> accounts) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
List<Account> accountsWproblems = new ArrayList<>();
for (Account account : accounts) {
if (account.hasErrorStatus()) {
accountsWproblems.add(account);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
if (accountsWproblems.size() == 0) {
mNotificationManager.cancel(1111);
return;
} else if (accountsWproblems.size() == 1) {
mBuilder.setContentTitle(context
.getString(R.string.problem_connecting_to_account));
mBuilder.setContentText(accountsWproblems.get(0).getJid().toBareJid().toString());
} else {
mBuilder.setContentTitle(context
.getString(R.string.problem_connecting_to_accounts));
mBuilder.setContentText(context.getString(R.string.touch_to_fix));
}
mBuilder.setOngoing(true);
mBuilder.setLights(0xffffffff, 2000, 4000);
mBuilder.setSmallIcon(R.drawable.ic_notification);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ConversationActivity.class);
Intent manageAccountsIntent = new Intent(context,
ManageAccountActivity.class);
stackBuilder.addNextIntent(manageAccountsIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
mNotificationManager.notify(1111, notification);
}
private final static class EmoticonPattern { private final static class EmoticonPattern {
Pattern pattern; Pattern pattern;
String replacement; String replacement;

Binary file not shown.

After

Width:  |  Height:  |  Size: 757 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 425 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB