Merge pull request #1591 from oxen-io/feature/service-clean-up

Feature/service clean up
This commit is contained in:
ThomasSession 2024-08-02 08:57:09 +10:00 committed by GitHub
commit a56e1d0b91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 21 additions and 168 deletions

View File

@ -125,7 +125,7 @@
<activity <activity
android:name="org.thoughtcrime.securesms.home.HomeActivity" android:name="org.thoughtcrime.securesms.home.HomeActivity"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:launchMode="standard" android:launchMode="singleTask"
android:theme="@style/Theme.Session.DayNight.NoActionBar" /> android:theme="@style/Theme.Session.DayNight.NoActionBar" />
<activity <activity
android:name="org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity" android:name="org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity"
@ -328,7 +328,6 @@
<action android:name="android.service.chooser.ChooserTargetService" /> <action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter> </intent-filter>
</service> </service>
<service android:name="org.thoughtcrime.securesms.service.GenericForegroundService" />
<receiver <receiver
android:name="org.thoughtcrime.securesms.notifications.MarkReadReceiver" android:name="org.thoughtcrime.securesms.notifications.MarkReadReceiver"
android:enabled="true" android:enabled="true"
@ -440,9 +439,7 @@
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<service
android:name="org.thoughtcrime.securesms.jobmanager.KeepAliveService"
android:enabled="@bool/enable_alarm_manager" />
<uses-library <uses-library
android:name="com.sec.android.app.multiwindow" android:name="com.sec.android.app.multiwindow"
android:required="false" /> android:required="false" />

View File

@ -143,8 +143,6 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) { override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady) super.onCreate(savedInstanceState, isReady)
if (!isTaskRoot) { finish(); return }
// Set content view // Set content view
binding = ActivityHomeBinding.inflate(layoutInflater) binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)

View File

@ -1,24 +0,0 @@
package org.thoughtcrime.securesms.jobmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
/**
* Service that keeps the application in memory while the app is closed.
*
* Important: Should only be used on API < 26.
*/
public class KeepAliveService extends Service {
@Override
public @Nullable IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
}

View File

@ -1,128 +0,0 @@
package org.thoughtcrime.securesms.service;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import org.session.libsignal.utilities.Log;
import org.session.libsignal.utilities.guava.Preconditions;
import org.thoughtcrime.securesms.home.HomeActivity;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import network.loki.messenger.R;
public class GenericForegroundService extends Service {
private static final String TAG = GenericForegroundService.class.getSimpleName();
private static final int NOTIFICATION_ID = 827353982;
private static final String EXTRA_TITLE = "extra_title";
private static final String EXTRA_CHANNEL_ID = "extra_channel_id";
private static final String EXTRA_ICON_RES = "extra_icon_res";
private static final String ACTION_START = "start";
private static final String ACTION_STOP = "stop";
private int foregroundCount;
private String activeTitle;
private String activeChannelId;
private int activeIconRes;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
synchronized (GenericForegroundService.class) {
if (intent != null && ACTION_START.equals(intent.getAction())) handleStart(intent);
else if (intent != null && ACTION_STOP.equals(intent.getAction())) handleStop();
else throw new IllegalStateException("Action needs to be START or STOP.");
return START_NOT_STICKY;
}
}
private void handleStart(@NonNull Intent intent) {
String title = Preconditions.checkNotNull(intent.getStringExtra(EXTRA_TITLE));
String channelId = Preconditions.checkNotNull(intent.getStringExtra(EXTRA_CHANNEL_ID));
int iconRes = intent.getIntExtra(EXTRA_ICON_RES, R.drawable.ic_notification);
Log.i(TAG, "handleStart() Title: " + title + " ChannelId: " + channelId);
foregroundCount++;
if (foregroundCount == 1) {
Log.d(TAG, "First request. Title: " + title + " ChannelId: " + channelId);
activeTitle = title;
activeChannelId = channelId;
activeIconRes = iconRes;
}
postObligatoryForegroundNotification(activeTitle, activeChannelId, activeIconRes);
}
private void handleStop() {
Log.i(TAG, "handleStop()");
postObligatoryForegroundNotification(activeTitle, activeChannelId, activeIconRes);
foregroundCount--;
if (foregroundCount == 0) {
Log.d(TAG, "Last request. Ending foreground service.");
stopForeground(true);
stopSelf();
}
}
private void postObligatoryForegroundNotification(String title, String channelId, @DrawableRes int iconRes) {
startForeground(NOTIFICATION_ID, new NotificationCompat.Builder(this, channelId)
.setSmallIcon(iconRes)
.setContentTitle(title)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, HomeActivity.class), PendingIntent.FLAG_IMMUTABLE))
.build());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static void startForegroundTask(@NonNull Context context, @NonNull String task) {
startForegroundTask(context, task, NotificationChannels.OTHER);
}
public static void startForegroundTask(@NonNull Context context, @NonNull String task, @NonNull String channelId) {
startForegroundTask(context, task, channelId, R.drawable.ic_notification);
}
public static void startForegroundTask(@NonNull Context context, @NonNull String task, @NonNull String channelId, @DrawableRes int iconRes) {
Intent intent = new Intent(context, GenericForegroundService.class);
intent.setAction(ACTION_START);
intent.putExtra(EXTRA_TITLE, task);
intent.putExtra(EXTRA_CHANNEL_ID, channelId);
intent.putExtra(EXTRA_ICON_RES, iconRes);
ContextCompat.startForegroundService(context, intent);
}
public static void stopForegroundTask(@NonNull Context context) {
Intent intent = new Intent(context, GenericForegroundService.class);
intent.setAction(ACTION_STOP);
ContextCompat.startForegroundService(context, intent);
}
}

View File

@ -35,6 +35,7 @@ import android.os.SystemClock;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import org.session.libsession.utilities.ServiceUtil; import org.session.libsession.utilities.ServiceUtil;
import org.session.libsession.utilities.TextSecurePreferences; import org.session.libsession.utilities.TextSecurePreferences;
@ -252,11 +253,18 @@ public class KeyCachingService extends Service {
builder.setContentIntent(buildLaunchIntent()); builder.setContentIntent(buildLaunchIntent());
stopForeground(true); stopForeground(true);
int type = 0;
if (Build.VERSION.SDK_INT >= 34) { if (Build.VERSION.SDK_INT >= 34) {
startForeground(SERVICE_RUNNING_ID, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE); type = ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE;
} else {
startForeground(SERVICE_RUNNING_ID, builder.build());
} }
ServiceCompat.startForeground(
this,
SERVICE_RUNNING_ID,
builder.build(),
type
);
} }
private PendingIntent buildLockIntent() { private PendingIntent buildLockIntent() {

View File

@ -7,9 +7,12 @@ import android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.IntentFilter import android.content.IntentFilter
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.media.AudioManager import android.media.AudioManager
import android.os.Build
import android.os.ResultReceiver import android.os.ResultReceiver
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
@ -723,9 +726,11 @@ class WebRtcCallService : LifecycleService(), CallManager.WebRtcListener {
private fun setCallInProgressNotification(type: Int, recipient: Recipient?) { private fun setCallInProgressNotification(type: Int, recipient: Recipient?) {
try { try {
startForeground( ServiceCompat.startForeground(
this,
CallNotificationBuilder.WEBRTC_NOTIFICATION, CallNotificationBuilder.WEBRTC_NOTIFICATION,
CallNotificationBuilder.getCallInProgressNotification(this, type, recipient) CallNotificationBuilder.getCallInProgressNotification(this, type, recipient),
if (Build.VERSION.SDK_INT >= 30) ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE else 0
) )
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Log.e(TAG, "Failed to setCallInProgressNotification as a foreground service for type: ${type}, trying to update instead", e) Log.e(TAG, "Failed to setCallInProgressNotification as a foreground service for type: ${type}, trying to update instead", e)

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<bool name="enable_alarm_manager">false</bool>
<bool name="enable_job_service">true</bool> <bool name="enable_job_service">true</bool>
</resources> </resources>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<bool name="enable_alarm_manager">true</bool>
<bool name="enable_job_service">false</bool> <bool name="enable_job_service">false</bool>
<attr name="searchBackgroundColor" format="color"/> <attr name="searchBackgroundColor" format="color"/>
<attr name="searchIconColor" format="color|reference"/> <attr name="searchIconColor" format="color|reference"/>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<bool name="enable_alarm_manager">true</bool>
<bool name="enable_job_service">false</bool> <bool name="enable_job_service">false</bool>
<bool name="screen_security_default">true</bool> <bool name="screen_security_default">true</bool>
</resources> </resources>