Skip to content

Commit

Permalink
v6.13: Fixed an occasional Push notification service killing by Andro…
Browse files Browse the repository at this point in the history
…id OS
  • Loading branch information
vmayorow committed Dec 10, 2024
1 parent abe2be3 commit 0816202
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ android {
applicationId "com.hmdm.launcher"
minSdkVersion 16
targetSdkVersion 34
versionCode 15120
versionName "6.12"
versionCode 15130
versionName "6.13"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
dataBinding {
enabled = true
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.REQUEST_PASSWORD_COMPLEXITY" />
Expand Down Expand Up @@ -253,8 +254,11 @@
</service>

<service android:name="org.eclipse.paho.android.service.MqttService"
android:foregroundServiceType="dataSync"
android:exported="false"/>
android:foregroundServiceType="specialUse|systemExempted"
android:exported="false">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Push notification service"/>
</service>

<service android:name=".service.LocationService"
android:foregroundServiceType="location"
Expand All @@ -264,8 +268,11 @@
android:exported="false"/>

<service android:name=".service.PushLongPollingService"
android:foregroundServiceType="dataSync"
android:exported="false"/>
android:foregroundServiceType="specialUse|systemExempted"
android:exported="false">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Push notification service"/>
</service>

</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
Expand All @@ -28,6 +27,7 @@
import com.hmdm.launcher.server.ServerService;
import com.hmdm.launcher.server.ServerServiceKeeper;
import com.hmdm.launcher.util.RemoteLogger;
import com.hmdm.launcher.util.Utils;
import com.hmdm.launcher.worker.PushNotificationProcessor;

import org.eclipse.paho.android.service.MqttService;
Expand Down Expand Up @@ -204,11 +204,7 @@ private void startAsForeground() {
.setContentText(getString(R.string.mqtt_service_text))
.setSmallIcon(R.drawable.ic_mqtt_service).build();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(NOTIFICATION_ID, notification);
}
Utils.startStableForegroundService(this, NOTIFICATION_ID, notification);
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/hmdm/launcher/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.app.admin.SystemUpdatePolicy;
import android.content.ComponentName;
Expand All @@ -35,6 +37,7 @@
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.media.AudioManager;
import android.net.ConnectivityManager;
Expand Down Expand Up @@ -1077,4 +1080,21 @@ public static boolean writeStringToFile(String fileName, String fileContent, boo

}

/**
* Starting foreground service of special use
*/
public static void startStableForegroundService(Service service, int notificationId, Notification notification) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
int serviceType = Utils.isDeviceOwner(service) ?
ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED :
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE;
try {
service.startForeground(notificationId, notification, serviceType);
} catch (/* ForegroundServiceTypeNotAllowed*/Exception e) {
service.startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
}
} else {
service.startForeground(notificationId, notification);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ServiceInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
Expand All @@ -42,6 +41,7 @@
import com.hmdm.launcher.helper.SettingsHelper;
import com.hmdm.launcher.pro.ProUtils;
import com.hmdm.launcher.util.PushNotificationMqttWrapper;
import com.hmdm.launcher.util.Utils;

import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
Expand Down Expand Up @@ -739,11 +739,7 @@ private void startAsForeground() {
.setContentText(getString(R.string.mqtt_service_text))
.setSmallIcon(R.drawable.ic_mqtt_service).build();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(NOTIFICATION_ID, notification);
}
Utils.startStableForegroundService(this, NOTIFICATION_ID, notification);
}


Expand Down

0 comments on commit 0816202

Please sign in to comment.