Skip to content

Commit 39a5b5e

Browse files
committed
Start migration to target API 34
1 parent b98d5e2 commit 39a5b5e

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
defaultConfig {
88
applicationId "org.cgutman.usbipserverforandroid"
99
minSdk 12
10-
targetSdk 19
10+
targetSdk 34
1111
}
1212

1313
buildTypes {

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@
88

99
<uses-permission android:name="android.permission.INTERNET" />
1010
<uses-permission android:name="android.permission.WAKE_LOCK" />
11+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
12+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
13+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
1114

1215
<application
1316
android:allowBackup="true"
1417
android:icon="@drawable/ic_launcher"
1518
android:label="@string/app_name"
16-
android:theme="@style/AppTheme" >
19+
android:theme="@style/AppTheme">
1720

1821
<service
1922
android:label="@string/service_name"
20-
android:name="org.cgutman.usbip.service.UsbIpService" />
23+
android:name="org.cgutman.usbip.service.UsbIpService"
24+
android:foregroundServiceType="specialUse"
25+
android:exported="false">
26+
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" android:value="Allows continued access to USB devices via USB/IP while in the background" />
27+
</service>
2128

2229
<activity
2330
android:name="org.cgutman.usbip.config.UsbIpConfig"
2431
android:exported="true"
32+
android:resizeableActivity="true"
2533
android:label="@string/app_name" >
2634
<intent-filter>
2735
<action android:name="android.intent.action.MAIN" />

app/src/main/java/org/cgutman/usbip/service/UsbIpService.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import android.annotation.SuppressLint;
3333
import android.app.Notification;
34+
import android.app.NotificationChannel;
35+
import android.app.NotificationManager;
3436
import android.app.PendingIntent;
3537
import android.app.Service;
3638
import android.content.BroadcastReceiver;
@@ -65,6 +67,8 @@ public class UsbIpService extends Service implements UsbRequestHandler {
6567
private static final boolean DEBUG = false;
6668

6769
private static final int NOTIFICATION_ID = 100;
70+
71+
private final static String CHANNEL_ID = "serviceInfo";
6872

6973
private static final String ACTION_USB_PERMISSION =
7074
"org.cgutman.usbip.USB_PERMISSION";
@@ -86,16 +90,29 @@ public void onReceive(Context context, Intent intent) {
8690
private void updateNotification() {
8791
Intent intent = new Intent(this, UsbIpConfig.class);
8892
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
89-
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
90-
91-
Notification.Builder builder = new Notification.Builder(this);
92-
builder
93-
.setTicker("USB/IP Server Running")
94-
.setContentTitle("USB/IP Server Running")
95-
.setAutoCancel(false)
96-
.setOngoing(true)
97-
.setSmallIcon(R.drawable.notification_icon)
98-
.setContentIntent(pendIntent);
93+
94+
int intentFlags = 0;
95+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
96+
intentFlags |= PendingIntent.FLAG_IMMUTABLE;
97+
}
98+
99+
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, intentFlags);
100+
101+
Notification.Builder builder = new Notification.Builder(this)
102+
.setTicker("USB/IP Server Running")
103+
.setContentTitle("USB/IP Server Running")
104+
.setAutoCancel(false)
105+
.setOngoing(true)
106+
.setSmallIcon(R.drawable.notification_icon)
107+
.setContentIntent(pendIntent);
108+
109+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
110+
builder.setChannelId(CHANNEL_ID);
111+
}
112+
113+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
114+
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
115+
}
99116

100117
if (connections.size() == 0) {
101118
builder.setContentText("No devices currently shared");
@@ -146,6 +163,12 @@ public void onCreate() {
146163

147164
server = new UsbIpServer();
148165
server.start(this);
166+
167+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
168+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Service Info", NotificationManager.IMPORTANCE_DEFAULT);
169+
NotificationManager notificationManager = getSystemService(NotificationManager.class);
170+
notificationManager.createNotificationChannel(channel);
171+
}
149172

150173
updateNotification();
151174
}

0 commit comments

Comments
 (0)