From e6020f9707c377ef5598e3d20ab119c545d03400 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 14 Nov 2018 03:28:25 +0100 Subject: [PATCH] use permanent notification to keep threads alive --- AndroidManifest.xml | 2 + res/drawable-hdpi-v9/icon_notification.png | Bin 1666 -> 0 bytes res/drawable-mdpi-v11/icon_notification.png | Bin 799 -> 750 bytes .../icon_notification.png | Bin 2052 -> 0 bytes res/values/strings.xml | 1 + .../connect/ApplicationDcContext.java | 17 ++++ .../securesms/connect/ForegroundDetector.java | 3 +- .../securesms/connect/KeepAliveService.java | 88 ++++++++++++++++++ .../securesms/connect/ScreenReceiver.java | 26 ++++++ 9 files changed, 136 insertions(+), 1 deletion(-) delete mode 100644 res/drawable-hdpi-v9/icon_notification.png delete mode 100644 res/drawable-xxxhdpi-v11/icon_notification.png create mode 100644 src/org/thoughtcrime/securesms/connect/KeepAliveService.java create mode 100644 src/org/thoughtcrime/securesms/connect/ScreenReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8dcf62a02..6879e1aef 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -339,6 +339,8 @@ + + diff --git a/res/drawable-hdpi-v9/icon_notification.png b/res/drawable-hdpi-v9/icon_notification.png deleted file mode 100644 index f0f52a2a81229e23ba0ca3b292bfb8705d4e2aa7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1666 zcmaJ?Yfuwc6plbD;@~J0Q7gKx6%k1Gk#|ZELKaCODMCbipe)HkA||^g3kg`YB9XDa zD`;D^Dx+3KEkX;3TI0}Bu?2N3(c%=LQUs(8zUreY-5}Whpzh4>J@?-8opZkPnB9=5 zp5yB?$%n;a`6}ZSNz6Kdd4s**VZN;+7tLUni8Pir6t2xCXdr{gGMHqb>-0GgFz969 ze1Qs9Q89$x5SMQzlJnIXZGN^^q623~0ueSTW57tzIAAj_H(8`M88~29%E<0x2m}UH z=xiDII;d1tA`nBG2|&OVa5h*N@!U!<oJMh#mCB>|~&xrYR`|S*=#CmB%H`Sr8(TNFW%6P?W=Ha4dNy8ny;pz-JDIy{S zd=UzB5S>`W;b-6iPKFTCa$s$yI77_i;kbl9WM??MToe;4lnaFlIUhq1j1e)h7!wPk zV=+Dq!{`uJX|m9`NlOg*HZZ;iv4Xc^r7>m#r%AJhB$p3$L86|dNsFGOfS4F})%ie( z3fCG;?!#H`LcQj#Ak2nbLKkZ$jle*rrG{am;Kjy@6!K_PArc}C13p7Uq!1!4Ptr!i-nkAAa|Smr)`Fgm|k>?Z-#)`ycr|H#LSMF8In^+!2%Y`+z=Eg9$N9AP(Eyms)B!|(9A(mw83??>3h``A)Gd&=l4 zToztfMNN%W_8xq7u@x#<*i|^wIsNDUnt<^2+N)j%`+Q$)8}0w?aZ8(Ev3&>Z+2l}% z6s-R|Ed8$?U0dn{Lkb%zf>N@v?f#X2=nF$@!_WKojR;+U-J9y+wO2i#bt7-V71!n$ zt29*`Z!B?4yD$%Jsx}7pu65q_h?v~4bu>KA`3V~;Y9IA}f_JXNx7M?<;>y7zOM9DB zcB|pq@5W6M#otX|&}sG9X$lBGY5zX8=ow@@vw3Ed<$3PSp6bKuqFV>fG(Dnf>Wb^a z(yjT5y4=v0sBFoSMK2?4`tBaD%Q+!;KI<5(Ew0|WC#rhP5lHDV?brP(b$V$;r_rzA z$&WRMqPFOS^DOykavi%Rx%9r?JGHUd$+3qMr9mC$U#fykf#=s; z2z#Eg{Xs`X4PMgY;&_(_7xC{{D4$5bT5HA9fU}qPc^og(Tv?-Exh$<=)8l|~Ps+i^ h#4f+5{a&j_!K`oOtq(nyB?P+v3QA0^I3Z6j_y@P~d@le1 diff --git a/res/drawable-mdpi-v11/icon_notification.png b/res/drawable-mdpi-v11/icon_notification.png index c28f3fb711929610132ef3d5355a8c2ba8b97f45..05c2cd40cc1df2695cb73d24151a5083bad2ea39 100644 GIT binary patch delta 652 zcmV;70(1SJ2JQurZhvb@L_t(YiM5rzZ;nwI#(#%ut4-6SB9tKUkydm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5$|UbNa&BN9 z`lV@*Gr5pApq~d^rU+=YiI0_8-A&Tfv`g&x)2c1r!K8Z|J(Xr25R|Oi1#F8$Q<{y( zv;K0_y{AsU-OIRX{dgHCAT6)1zA|T=V`w=vkctvoa+y4BC^+!@^WwSS8l7tz&92jO z3ei4}*&`nKEbt#1A>4atH2o_upXK_pf-bQKa!gsQt}F>ug=UV}!6Xf?uoZiSoXvN+ zdCwLwNC{RB^f)!2@U{ha>q1Dh^^q;`*ooOskCk8ENsbAzma~3~5)X9*SjXWp<8vCz z2qcmv(+BO`U=_j*(Y*hO2b1`jnbJNp{Uh9M6exPj()dvK^|AY8(C{hEQ!_rWtqj7HeJ(RPC=q zC9hmkBh`7&#PA)SYw(OVU8thICbfr#frVzcl(A!Fc}kr<>j+|g@X)hCbx^OPlj?-9 zkL;Xgnby<9wQ*}w5CMbxSbiVmlUCzSDbL2I7cCo+oJAhBp&ZKwIDwYz&Fn~4Bjl`k zR`7;(f^^eXWm4UmV%@%KB_GQg$se4LXxln8C~C%9WQEi6ZRY=lqIfz&I!~PFEyD8E zyAX~iXGL+mX`FkaD5Q*UaY|~3&}es5tLyA!*{+Cfercn8)X*CmynLkNdhw^({aA+sEtFV?DL7uO6<+<#5%#%}-s diff --git a/res/values/strings.xml b/res/values/strings.xml index 0311cd23d..d08c25a20 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1534,5 +1534,6 @@ Unarchive chat Delete chat No messages. + Tap to open. diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index fa79a5f7d..1d4d7a529 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -55,6 +55,7 @@ public class ApplicationDcContext extends DcContext { public static final int RECIPIENT_TYPE_CONTACT = 1; public Context context; + public volatile boolean isScreenOn = false; public ApplicationDcContext(Context context) { super("android-dev"); @@ -87,6 +88,22 @@ public class ApplicationDcContext extends DcContext { BroadcastReceiver networkStateReceiver = new NetworkStateReceiver(); context.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); + + IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); + filter.addAction(Intent.ACTION_SCREEN_OFF); + BroadcastReceiver screenReceiver = new ScreenReceiver(); + context.registerReceiver(screenReceiver, filter); + + try { + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + isScreenOn = pm.isScreenOn(); + } catch (Exception e) { + + } + + if( !isScreenOn ) { + context.startService(new Intent(context, KeepAliveService.class)); + } } public File getImexDir() diff --git a/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java b/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java index 03bc5027c..59481baef 100644 --- a/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java +++ b/src/org/thoughtcrime/securesms/connect/ForegroundDetector.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.connect; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Application; +import android.content.Intent; import android.os.Bundle; import org.thoughtcrime.securesms.ApplicationContext; @@ -37,7 +38,7 @@ public class ForegroundDetector implements Application.ActivityLifecycleCallback public void onActivityStarted(Activity activity) { refs++; - //applicationContext.stopService(new Intent(applicationContext, KeepAliveService.class)); + activity.stopService(new Intent(activity, KeepAliveService.class)); application.dcContext.startThreads(); // we call this without checking getPermanentPush() to have a simple guarantee that push is always active when the app is in foregroud (startIdleThread makes sure the thread is not started twice) } diff --git a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java new file mode 100644 index 000000000..38fd785d0 --- /dev/null +++ b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java @@ -0,0 +1,88 @@ +package org.thoughtcrime.securesms.connect; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.os.IBinder; +import android.support.v4.app.NotificationCompat; +import android.util.Log; + +import org.thoughtcrime.securesms.ConversationListActivity; +import org.thoughtcrime.securesms.R; + +import java.util.Locale; + + +public class KeepAliveService extends Service { + + static KeepAliveService s_this = null; + + @Override + public void onCreate() { + Log.i("DeltaChat", "*** KeepAliveService.onCreate()"); + // there's nothing more to do here as all initialisation stuff is already done in + // ApplicationLoader.onCreate() which is called before this broadcast is sended. + s_this = this; + setSelfAsForeground(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // START_STICKY ensured, the service is recreated as soon it is terminated for any reasons. + // as ApplicationLoader.onCreate() is called before a service starts, there is no more to do here, + // the app is just running fine. + Log.i("DeltaChat", "*** KeepAliveService.onStartCommand()"); + return START_STICKY; + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + public void onDestroy() { + Log.i("DeltaChat", "*** KeepAliveService.onDestroy()"); + // the service will be restarted due to START_STICKY automatically, there's nothing more to do. + } + + private void setSelfAsForeground() { + stopForeground(true); + startForeground(FG_NOTIFICATION_ID, createNotification()); // TODO: if we target Android O, we should use startServiceInForeground() + } + + static public KeepAliveService getInstance() + { + return s_this; // may be null + } + + /* The notification + * A notification is required for a foreground service; and without a foreground service, + * Delta Chat won't get new messages reliable + **********************************************************************************************/ + + public static final int FG_NOTIFICATION_ID = 4142; + private Notification createNotification() + { + ApplicationDcContext dcContext = DcHelper.getContext(this); + + Intent intent = new Intent(this, ConversationListActivity.class); + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + // a notification _must_ contain a small icon, a title and a text, see https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Required + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + + builder.setContentTitle(getString(R.string.app_name)); + builder.setContentText(getString(R.string.permanent_notification_subtitle)); + + if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) { + builder.setPriority(NotificationCompat.PRIORITY_MIN); + } + builder.setWhen(0); + builder.setContentIntent(contentIntent); + builder.setSmallIcon(R.drawable.icon_notification); + return builder.build(); + } +} diff --git a/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java new file mode 100644 index 000000000..81f7f98e6 --- /dev/null +++ b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java @@ -0,0 +1,26 @@ +package org.thoughtcrime.securesms.connect; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class ScreenReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + + ApplicationDcContext dcContext = DcHelper.getContext(context); + + if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + Log.i("DeltaChat", "-------------------- Screen off --------------------"); + dcContext.isScreenOn = false; + context.startService(new Intent(context, KeepAliveService.class)); + } + else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { + Log.i("DeltaChat", "-------------------- Screen on --------------------"); + dcContext.isScreenOn = true; + context.stopService(new Intent(context, KeepAliveService.class)); + } + } +}