Skip to content

Commit 7ba29ec

Browse files
committed
Finish migration to API 34
1 parent 39a5b5e commit 7ba29ec

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

app/build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66

77
defaultConfig {
88
applicationId "org.cgutman.usbipserverforandroid"
9-
minSdk 12
9+
minSdk 19
1010
targetSdk 34
1111
}
1212

@@ -23,3 +23,12 @@ android {
2323
}
2424
}
2525
}
26+
27+
dependencies {
28+
implementation 'androidx.core:core:1.12.0'
29+
implementation 'androidx.activity:activity:1.8.0'
30+
}
31+
32+
configurations.implementation {
33+
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
34+
}

app/src/main/java/org/cgutman/usbip/config/UsbIpConfig.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,35 @@
33
import org.cgutman.usbip.service.UsbIpService;
44
import org.cgutman.usbipserverforandroid.R;
55

6-
import android.app.Activity;
6+
import android.Manifest;
77
import android.app.ActivityManager;
88
import android.app.ActivityManager.RunningServiceInfo;
99
import android.content.Context;
1010
import android.content.Intent;
11+
import android.content.pm.PackageManager;
12+
import android.os.Build;
1113
import android.os.Bundle;
1214
import android.view.View;
1315
import android.view.View.OnClickListener;
1416
import android.widget.Button;
1517
import android.widget.TextView;
1618

17-
public class UsbIpConfig extends Activity {
19+
import androidx.activity.ComponentActivity;
20+
import androidx.activity.result.ActivityResultLauncher;
21+
import androidx.activity.result.contract.ActivityResultContracts;
22+
import androidx.core.content.ContextCompat;
23+
24+
public class UsbIpConfig extends ComponentActivity {
1825
private Button serviceButton;
1926
private TextView serviceStatus;
2027

2128
private boolean running;
29+
30+
private ActivityResultLauncher<String> requestPermissionLauncher =
31+
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
32+
// We don't actually care if the permission is granted or not. We will launch the service anyway.
33+
startService(new Intent(UsbIpConfig.this, UsbIpService.class));
34+
});
2235

2336
private void updateStatus() {
2437
if (running) {
@@ -61,7 +74,13 @@ public void onClick(View v) {
6174
stopService(new Intent(UsbIpConfig.this, UsbIpService.class));
6275
}
6376
else {
64-
startService(new Intent(UsbIpConfig.this, UsbIpService.class));
77+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
78+
if (ContextCompat.checkSelfPermission(UsbIpConfig.this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
79+
startService(new Intent(UsbIpConfig.this, UsbIpService.class));
80+
} else {
81+
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
82+
}
83+
}
6584
}
6685

6786
running = !running;

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import android.os.PowerManager.WakeLock;
5454
import android.util.SparseArray;
5555

56+
import androidx.core.app.NotificationCompat;
57+
5658
public class UsbIpService extends Service implements UsbRequestHandler {
5759

5860
private UsbManager usbManager;
@@ -97,22 +99,16 @@ private void updateNotification() {
9799
}
98100

99101
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, intentFlags);
100-
101-
Notification.Builder builder = new Notification.Builder(this)
102+
103+
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
104+
.setSmallIcon(R.drawable.notification_icon)
105+
.setOngoing(true)
106+
.setSilent(true)
102107
.setTicker("USB/IP Server Running")
103108
.setContentTitle("USB/IP Server Running")
104109
.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-
}
110+
.setContentIntent(pendIntent)
111+
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE);
116112

117113
if (connections.size() == 0) {
118114
builder.setContentText("No devices currently shared");
@@ -121,7 +117,7 @@ private void updateNotification() {
121117
builder.setContentText(String.format("Sharing %d device(s)", connections.size()));
122118
}
123119

124-
startForeground(NOTIFICATION_ID, builder.getNotification());
120+
startForeground(NOTIFICATION_ID, builder.build());
125121
}
126122

127123
@SuppressLint("UseSparseArrays")

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
android.useAndroidX=true

0 commit comments

Comments
 (0)