mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
use permanent notification to keep threads alive
This commit is contained in:
@@ -339,6 +339,8 @@
|
||||
<service android:enabled="true" android:exported="false" android:name=".service.KeyCachingService"/>
|
||||
<service android:enabled="true" android:name=".service.MessageRetrievalService"/>
|
||||
|
||||
<service android:name=".connect.KeepAliveService" android:enabled="true"/>
|
||||
|
||||
<service android:name=".service.AccountAuthenticatorService" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.accounts.AccountAuthenticator" />
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.6 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 750 B |
Binary file not shown.
|
Before Width: | Height: | Size: 2.0 KiB |
@@ -1534,5 +1534,6 @@
|
||||
<string name="conversation__menu_unarchive_chat">Unarchive chat</string>
|
||||
<string name="conversation__menu_delete_chat">Delete chat</string>
|
||||
<string name="conversation__no_messages">No messages.</string>
|
||||
<string name="permanent_notification_subtitle">Tap to open.</string>
|
||||
|
||||
</resources>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user