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 f0f52a2a8..000000000 Binary files a/res/drawable-hdpi-v9/icon_notification.png and /dev/null differ diff --git a/res/drawable-mdpi-v11/icon_notification.png b/res/drawable-mdpi-v11/icon_notification.png index c28f3fb71..05c2cd40c 100644 Binary files a/res/drawable-mdpi-v11/icon_notification.png and b/res/drawable-mdpi-v11/icon_notification.png differ diff --git a/res/drawable-xxxhdpi-v11/icon_notification.png b/res/drawable-xxxhdpi-v11/icon_notification.png deleted file mode 100644 index 5b0ac80ce..000000000 Binary files a/res/drawable-xxxhdpi-v11/icon_notification.png and /dev/null differ 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)); + } + } +}