diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-09-17 20:19:20 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-09-17 20:26:50 +0200 |
commit | 9522577e5ba97fa52277a5d7ba9163687743ba24 (patch) | |
tree | d5deda0fd87a8f3048c08ffa75839a5be6de3c15 | |
parent | 58a4b628a6d21195ffc5a2b1e924dc1d3d90a225 (diff) |
Foregroundservice can be deactivated in expert options (default = foregroundservice active)
Fixes #138
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/Config.java | 2 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/services/NotificationService.java | 21 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 16 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/SettingsActivity.java | 4 | ||||
-rw-r--r-- | src/main/res/values-de/strings.xml | 4 | ||||
-rw-r--r-- | src/main/res/values/defaults.xml | 1 | ||||
-rw-r--r-- | src/main/res/values/strings.xml | 2 | ||||
-rw-r--r-- | src/main/res/xml/preferences.xml | 5 |
8 files changed, 28 insertions, 27 deletions
diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 87e494af2..2dc8d191a 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -54,8 +54,6 @@ public final class Config { public static final boolean HIDE_MESSAGE_TEXT_IN_NOTIFICATION = false; public static final boolean SHOW_CONNECTED_ACCOUNTS = false; //show number of connected accounts in foreground notification - public static final boolean SHOW_DISABLE_FOREGROUND = false; //if set to true the foreground notification has a button to disable it - public static final boolean USE_ALWAYS_FOREGROUND = true; //if set to true the foreground service is always enabled public static final boolean ALWAYS_NOTIFY_BY_DEFAULT = false; diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index 5e86ebb91..f1a43dd28 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -625,13 +625,6 @@ public class NotificationService { return PendingIntent.getService(mXmppConnectionService, (conversation.getUuid().hashCode() % NOTIFICATION_ID_MULTIPLIER) + 16 * NOTIFICATION_ID_MULTIPLIER, intent, PendingIntent.FLAG_UPDATE_CURRENT); } - private PendingIntent createDisableForeground() { - final Intent intent = new Intent(mXmppConnectionService, - XmppConnectionService.class); - intent.setAction(XmppConnectionService.ACTION_DISABLE_FOREGROUND); - return PendingIntent.getService(mXmppConnectionService, 34, intent, 0); - } - private PendingIntent createTryAgainIntent() { final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_TRY_AGAIN); @@ -721,18 +714,6 @@ public class NotificationService { } else { mBuilder.setSmallIcon(R.drawable.ic_unlink_white_24dp); } - if (Config.SHOW_DISABLE_FOREGROUND && !Config.USE_ALWAYS_FOREGROUND) { - final int cancelIcon; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - mBuilder.setCategory(Notification.CATEGORY_SERVICE); - cancelIcon = R.drawable.ic_cancel_white_24dp; - } else { - cancelIcon = R.drawable.ic_action_cancel; - } - mBuilder.addAction(cancelIcon, - mXmppConnectionService.getString(R.string.disable_foreground_service), - createDisableForeground()); - } return mBuilder.build(); } @@ -748,7 +729,7 @@ public class NotificationService { errors.add(account); } } - if (Config.USE_ALWAYS_FOREGROUND) { + if (mXmppConnectionService.showForegroundService()) { notificationManager.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification()); } final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index fd431798c..71fb36014 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -160,7 +160,6 @@ public class XmppConnectionService extends Service { public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations"; public static final String ACTION_MARK_AS_READ = "mark_as_read"; public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification"; - public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground"; public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error"; public static final String ACTION_IDLE_PING = "idle_ping"; @@ -1121,6 +1120,11 @@ public class XmppConnectionService extends Service { Log.d(Config.LOGTAG, "number of restarts exceeds threshold."); } + if (this.accounts.size() == 0 && Arrays.asList("Sony","Sony Ericsson").contains(Build.MANUFACTURER)) { + getPreferences().edit().putBoolean(SettingsActivity.SHOW_FOREGROUND_SERVICE, true).commit(); + Log.d(Config.LOGTAG, Build.MANUFACTURER + " is on blacklist. enabling foreground service"); + } + restoreFromDatabase(); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); @@ -1203,7 +1207,7 @@ public class XmppConnectionService extends Service { } public void toggleForegroundService() { - if (Config.USE_ALWAYS_FOREGROUND) { + if (showForegroundService()) { startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); } else { stopForeground(true); @@ -1213,7 +1217,7 @@ public class XmppConnectionService extends Service { @Override public void onTaskRemoved(final Intent rootIntent) { super.onTaskRemoved(rootIntent); - if (!Config.USE_ALWAYS_FOREGROUND) { + if (!showForegroundService()) { this.logoutAndSave(false); } else { Log.d(Config.LOGTAG,"ignoring onTaskRemoved because foreground service is activated"); @@ -2005,6 +2009,7 @@ public class XmppConnectionService extends Service { this.accounts.add(account); this.reconnectAccountInBackground(account); updateAccountUi(); + toggleForegroundService(); } public void createAccountFromKey(final String alias, final OnAccountCreated callback) { @@ -2078,6 +2083,7 @@ public class XmppConnectionService extends Service { reconnectAccountInBackground(account); updateAccountUi(); getNotificationService().updateErrorNotification(); + toggleForegroundService(); return true; } else { return false; @@ -4177,6 +4183,10 @@ public class XmppConnectionService extends Service { return getPreferences().getBoolean(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, getResources().getBoolean(R.bool.btbv)); } + public boolean showForegroundService() { + return getPreferences().getBoolean(SettingsActivity.SHOW_FOREGROUND_SERVICE, getResources().getBoolean(R.bool.show_foreground_service)); + } + public void pushMamPreferences(Account account, Element prefs) { IqPacket set = new IqPacket(IqPacket.TYPE.SET); set.addChild(prefs); diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index 9c92c8be5..1f5395748 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -36,7 +36,6 @@ import de.pixart.messenger.xmpp.jid.Jid; public class SettingsActivity extends XmppActivity implements OnSharedPreferenceChangeListener { - public static final String KEEP_FOREGROUND_SERVICE = "keep_foreground_service"; public static final String AWAY_WHEN_SCREEN_IS_OFF = "away_when_screen_off"; public static final String TREAT_VIBRATE_AS_SILENT = "treat_vibrate_as_silent"; public static final String DND_ON_SILENT_MODE = "dnd_on_silent_mode"; @@ -46,6 +45,7 @@ public class SettingsActivity extends XmppActivity implements public static final String BROADCAST_LAST_ACTIVITY = "last_activity"; public static final String THEME = "theme"; public static final String SHOW_DYNAMIC_TAGS = "show_dynamic_tags"; + public static final String SHOW_FOREGROUND_SERVICE = "show_foreground_service"; public static final int REQUEST_WRITE_LOGS = 0xbf8701; private SettingsFragment mSettingsFragment; @@ -261,6 +261,8 @@ public class SettingsActivity extends XmppActivity implements } } } + } else if (name.equals(SHOW_FOREGROUND_SERVICE)) { + xmppConnectionService.toggleForegroundService(); } else if (resendPresence.contains(name)) { if (xmppConnectionServiceBound) { if (name.equals(AWAY_WHEN_SCREEN_IS_OFF) diff --git a/src/main/res/values-de/strings.xml b/src/main/res/values-de/strings.xml index f615049f0..7e593dd12 100644 --- a/src/main/res/values-de/strings.xml +++ b/src/main/res/values-de/strings.xml @@ -732,11 +732,13 @@ <string name="yesterday">Gestern</string> <string name="pref_validate_hostname">Überprüfe den Hostnamen mit DNSSEC</string> <string name="pref_validate_hostname_summary">Serverzertifikate, die den überprüften Hostnamen enthalten, werden als verifiziert betrachtet</string> - <string name="pref_treat_vibrate_as_silent">Vibration als \\\"Lautlos\\\" behandeln</string> + <string name="pref_treat_vibrate_as_silent">Vibration als \"Lautlos\" behandeln</string> <string name="certificate_does_not_contain_jid">Zertifikat enthält keine Jabber-ID</string> <string name="error_no_keys_to_trust_presence">Für diesen Kontakt sind keine benutzbaren Schlüssel verfügbar.\\nStelle sicher, dass du den gegenseitigen Online-Status aktiviert hast.</string> <string name="mark_as_read">Als gelesen markieren</string> <string name="network_is_unreachable">Netzwerk nicht verfügbar</string> <string name="server_info_partial">teilweise</string> + <string name="pref_show_foreground_service">Nutze Vordergrunddienst</string> + <string name="pref_show_foreground_service_summary">Einige Android Geräte schließen manche Apps nach einer gewissen Zeitspanne automatisch und du wirst keine neuen Nachrichten mehr empfangen können. Falls du solche Probleme hast, versuche den Vordergrunddienst zu aktivieren. Bitte beachte, dass dadurch ein permanente Eintrag in der Benachrichtigungsleiste erscheint.</string> </resources> diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index 5a7a1c057..6059f1002 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -47,4 +47,5 @@ <bool name="send_crashreport">true</bool> <bool name="plain_text_logs">false</bool> <bool name="validate_hostname">false</bool> + <bool name="show_foreground_service">true</bool> </resources>
\ No newline at end of file diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 481337cb9..3fb0c39e2 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -786,4 +786,6 @@ <string name="error_no_keys_to_trust_presence">There are no usable keys available for this contact.\\nMake sure you have mutual presence subscription.</string> <string name="mark_as_read">Mark as read</string> <string name="server_info_partial">partial</string> + <string name="pref_show_foreground_service_summary">Some Android devices will close some apps after a period of time and you wont be able to receive new messages. If you have such problems, try to activate the foreground service. But keep in mind, that you will have a permanent notification with this option checked.</string> + <string name="pref_show_foreground_service">Use foreground service</string> </resources> diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index a3cfc15ed..c1ccd31c4 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -245,6 +245,11 @@ android:key="resource" android:summary="@string/pref_xmpp_resource_summary" android:title="@string/pref_xmpp_resource" /> + <CheckBoxPreference + android:defaultValue="@bool/show_foreground_service" + android:key="show_foreground_service" + android:summary="@string/pref_show_foreground_service_summary" + android:title="@string/pref_show_foreground_service"/> </PreferenceCategory> <PreferenceCategory android:title="@string/pref_ui_options"> <CheckBoxPreference |