Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- Permission for foreground service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!-- Permission to check network state -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Currently used only to fetch IOCs -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class AdbManager(applicationContext: Context) {
}
}

// Cancel the notification, if it's still showing
// Cancel the notification, if it's still showing.
// Note: keep this cleanup despite onTimeout() in AdbPairingService, because Android
// versions < 34 don't call onTimeout().
val stopIntent = AdbPairingService.stopIntent(appContext)
appContext?.stopService(stopIntent)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.osservatorionessuno.bugbane.utils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand All @@ -12,6 +13,8 @@
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import android.util.Log;

import java.net.InetAddress;
Expand Down Expand Up @@ -65,6 +68,8 @@ public void onCreate() {
nm.createNotificationChannel(channel);
}

// See docs below on ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
@SuppressLint("InlinedApi")
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Notification notification = null;
Expand All @@ -88,7 +93,14 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
}
if (notification != null) {
try {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
// From https://developer.android.com/reference/android/content/pm/ServiceInfo:
// [E]ven though FOREGROUND_SERVICE_TYPE_SHORT_SERVICE was added on Android version
// Build.VERSION_CODES.UPSIDE_DOWN_CAKE, it can be also used on on prior android
// versions (just like other new foreground service types can be used).
// However, because Service.onTimeout(int) did not exist on prior versions, it will
// never called on such versions. Because of this, developers must make sure to stop
// the foreground service even if Service.onTimeout(int) is not called on such versions.
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE);
} catch (Throwable th) {
Log.e(TAG, "startForeground failed", th);
getSystemService(NotificationManager.class).notify(NOTIFICATION_ID, notification);
Expand Down Expand Up @@ -123,6 +135,13 @@ public void onDestroy() {
stopSearch();
}

@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void onTimeout(int service) {
// Called by system if SHORT_SERVICE exceeds 3 minute lifespan
stopSelf();
}

private Notification onStart() {
startSearch();
return searchingNotification();
Expand Down