From 3c6b3506e32979b80dc2a8163437ecbab16b8acb Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Wed, 12 Nov 2014 14:41:43 +0100 Subject: [PATCH] turn on foreground service in expert settings --- .../services/NotificationService.java | 27 +++++++++++++++--- .../services/XmppConnectionService.java | 19 ++++++++---- .../conversations/ui/SettingsActivity.java | 2 ++ .../ic_stat_communication_import_export.png | Bin 0 -> 620 bytes .../ic_stat_communication_import_export.png | Bin 0 -> 392 bytes .../ic_stat_communication_import_export.png | Bin 0 -> 972 bytes .../ic_stat_communication_import_export.png | Bin 0 -> 1860 bytes src/main/res/values/strings.xml | 4 +++ src/main/res/xml/preferences.xml | 5 ++++ 9 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/main/res/drawable-hdpi/ic_stat_communication_import_export.png create mode 100644 src/main/res/drawable-mdpi/ic_stat_communication_import_export.png create mode 100644 src/main/res/drawable-xhdpi/ic_stat_communication_import_export.png create mode 100644 src/main/res/drawable-xxhdpi/ic_stat_communication_import_export.png diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 4cb28145c..ec1ffc15c 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -38,6 +38,7 @@ public class NotificationService { private LinkedHashMap> notifications = new LinkedHashMap>(); public static int NOTIFICATION_ID = 0x2342; + public static int FOREGROUND_NOTIFICATION_ID = 0x8899; private Conversation mOpenConversation; private boolean mIsInForeground; private long mLastNotification; @@ -290,9 +291,11 @@ public class NotificationService { Intent viewConversationIntent = new Intent(mXmppConnectionService, ConversationActivity.class); viewConversationIntent.setAction(Intent.ACTION_VIEW); - viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, - conversationUuid); - viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); + if (conversationUuid!=null) { + viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, + conversationUuid); + viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); + } stackBuilder.addNextIntent(viewConversationIntent); @@ -304,7 +307,14 @@ public class NotificationService { private PendingIntent createDeleteIntent() { Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class); - intent.setAction("clear_notification"); + intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION); + return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); + } + + private PendingIntent createDisableForeground() { + Intent intent = new Intent(mXmppConnectionService, + XmppConnectionService.class); + intent.setAction(XmppConnectionService.ACTION_DISABLE_FOREGROUND); return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); } @@ -351,4 +361,13 @@ public class NotificationService { : Config.MINI_GRACE_PERIOD * 2; return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace); } + + public Notification createForegroundNotification() { + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); + mBuilder.setSmallIcon(R.drawable.ic_stat_communication_import_export); + mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service)); + mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_disable)); + mBuilder.setContentIntent(createDisableForeground()); + return mBuilder.build(); + } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 831a54e12..97aba64fa 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -97,6 +97,7 @@ public class XmppConnectionService extends Service { public static String ACTION_CLEAR_NOTIFICATION = "clear_notification"; private static String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; + public static String ACTION_DISABLE_FOREGROUND = "disable_foreground"; private ContentObserver contactObserver = new ContentObserver(null) { @Override public void onChange(boolean selfChange) { @@ -345,6 +346,9 @@ public class XmppConnectionService extends Service { return START_NOT_STICKY; } else if (intent.getAction().equals(ACTION_CLEAR_NOTIFICATION)) { mNotificationService.clear(); + } else if (intent.getAction().equals(ACTION_DISABLE_FOREGROUND)) { + getPreferences().edit().putBoolean("keep_foreground_service",false).commit(); + toggleForegroundService(); } } this.wakeLock.acquire(); @@ -459,18 +463,23 @@ public class XmppConnectionService extends Service { this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE); this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "XmppConnectionService"); + toggleForegroundService(); } - @Override - public void onDestroy() { - super.onDestroy(); - this.logoutAndSave(); + public void toggleForegroundService() { + if (getPreferences().getBoolean("keep_foreground_service",false)) { + startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); + } else { + stopForeground(true); + } } @Override public void onTaskRemoved(Intent rootIntent) { super.onTaskRemoved(rootIntent); - this.logoutAndSave(); + if (!getPreferences().getBoolean("keep_foreground_service",false)) { + this.logoutAndSave(); + } } private void logoutAndSave() { diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index aba601757..b6f3a077d 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -70,6 +70,8 @@ public class SettingsActivity extends XmppActivity implements } } } + } else if (name.equals("keep_foreground_service")) { + xmppConnectionService.toggleForegroundService(); } } diff --git a/src/main/res/drawable-hdpi/ic_stat_communication_import_export.png b/src/main/res/drawable-hdpi/ic_stat_communication_import_export.png new file mode 100644 index 0000000000000000000000000000000000000000..f929996e7d3880ea069ed8ff1791b9bfd01038b3 GIT binary patch literal 620 zcmV-y0+aoTP)0s8}@Clv{x7aN27^$@t71fpahoZ(tp^ZFg)d_W|bNvTAaP&lF$S z2^Zitynq!b!zS!VXj8e*@DA>&#%r6C7An9Q7=;-v*&-~%mV}m-^K18EOf`iNLO$&+ z`f|Tvdv#+MVM9Vi<^0-7%}h&>C95IX4^QETggovK{==D28bZ2{3aaT-oU5l$7u0*n6qj_(cb7L*)0wnT zb!YMjKEhY{X?q(2&wYWHa91_mNejhGb`&nb6L_y>s#FP8lzRnt;H+xGG?-dC-&lgj=S+h{b0~evUL?^*l@Z*YOLh);T2J=wO5Z0000^J literal 0 HcmV?d00001 diff --git a/src/main/res/drawable-mdpi/ic_stat_communication_import_export.png b/src/main/res/drawable-mdpi/ic_stat_communication_import_export.png new file mode 100644 index 0000000000000000000000000000000000000000..b581e174f55924b4333fe8f963bc41edf744ffb0 GIT binary patch literal 392 zcmV;30eAk1P)#z$wC$~L(0Fz@B%E@*x1_vz%EE??q$Fj7 zofT!3OXtlP$2K1EVWHJ=rFwUC2tORaGLCSEHw8T7TJ?E!#s;)w2vbD)@{}ZljbseJo8vz@-=HIC_ zWI$PuU)OLX4=8B@YnsqP(tsbr|6u@~w4oo5Y9E?QtuAlAr9Y@XW?8lrg$!Z^rxp&Q8|KA) zSK0&-mNr2&Xrq;$o~QSD>wbnB3ip1}J@DJey&vZ}?|IL8?|Ds2i){#+nWcj~Py(ii ziJ$;vMD1U{1}8JYOi&3niS=L($d1~-UQLnD0b{^QZ~&YVM?eJ_2ZpEQO31OHHzk0G@!4;)O&URXUB=JLysTpQg>poG(}8>3lQz zV1Mj0xDT#qB`UO9qoVeo0w#fViu@%-z5{#@2>2mEZQz<#Vgo3R%4w!et~TW|Py^0` zCZ$Xl=(c3*3amt<1aq@bFjscN+t|uq*7q{71DpnTKr3hmU*)j3ZvCx8t8f>b0eh8t z(?MpK0A2Bm^sz97^MJ_rHq>}9G}C&YfR z)I#nN0~Nrsx*gm$*Ea}{;t7y96a*CMnU=tN&HAnUSU@Zw77z>gX94`WN|-Bwz8tbN zvP2dR1p(RSnh%?hZ%AxTW!A?yC6xs5dztN@#{hpM;PHURgkHaUQ%1l*Efx^9#R9@G zZE$|TkI+V^)5T(zxnWmpKmZ2=d%(27b47ah*DxS#0i0y;;$w-qAupp_BRF5;tmth( zz$C(k8rb&w*A#q3plIDQ|M?*m8&Ul=Ef=h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5OWZB zEgoo_%;g7vYKXsiwhaGd>o=tLWDGy4*tL?_#mIM4sb^ z6~lInyk}6>Yy3}U#bch_{>OVFG29l)LsrKGqE|5?Gv`FR6~$rE$@}qI%S*TZKQ(j5 z5B3C2jD_1`d5b?Nyj&hOM>%J*Jg2d&MDfjvO90MsaKMH0A3Sq$?9(4LUj**9LPCzV z^3v}%`?qF=5+M4r0(t~@w;A#z|H}$^lAmUU-Rkd2#jMwns##%hrb;=9p2068;Eb&Q zbr;j+Va*aWI<=Q)cx}kcU0ZN>YgJD2zc7UrI7QERc{IWr!1 y$I5$3{~99=v*HqfHI4tJP4#8Dl?My9aoAr4r8AN{zw5040000Scan QR code Show QR code Account details + Conversations + Touch to disable foreground service + Keep service in foreground + Prevents the operating system from killing your connection diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index 15a61e879..3be65b7ad 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -101,6 +101,11 @@ android:key="indicate_received" android:summary="@string/pref_use_indicate_received_summary" android:title="@string/pref_use_indicate_received" /> +