Skip to content

Commit da108db

Browse files
committed
Sharpen edges of the icon. Display a notification while the server is running.
1 parent 9d8e332 commit da108db

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

res/drawable/notification_icon.png

-58 Bytes
Loading

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.ThreadPoolExecutor;
1111
import java.util.concurrent.TimeUnit;
1212

13+
import org.cgutman.usbip.config.UsbIpConfig;
1314
import org.cgutman.usbip.server.UsbDeviceInfo;
1415
import org.cgutman.usbip.server.UsbIpServer;
1516
import org.cgutman.usbip.server.UsbRequestHandler;
@@ -22,8 +23,10 @@
2223
import org.cgutman.usbip.usb.DescriptorReader;
2324
import org.cgutman.usbip.usb.UsbDeviceDescriptor;
2425
import org.cgutman.usbip.usb.XferUtils;
26+
import org.cgutman.usbipserverforandroid.R;
2527

2628
import android.annotation.SuppressLint;
29+
import android.app.Notification;
2730
import android.app.PendingIntent;
2831
import android.app.Service;
2932
import android.content.BroadcastReceiver;
@@ -46,6 +49,9 @@ public class UsbIpService extends Service implements UsbRequestHandler {
4649
private UsbManager usbManager;
4750
private SparseArray<AttachedDeviceContext> connections;
4851
private SparseArray<Boolean> permission;
52+
private UsbIpServer server;
53+
54+
private static final int NOTIFICATION_ID = 100;
4955

5056
private static final String ACTION_USB_PERMISSION =
5157
"org.cgutman.usbip.USB_PERMISSION";
@@ -64,6 +70,31 @@ public void onReceive(Context context, Intent intent) {
6470
}
6571
};
6672

73+
@SuppressWarnings("deprecation")
74+
private void updateNotification() {
75+
Intent intent = new Intent(this, UsbIpConfig.class);
76+
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
77+
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
78+
79+
Notification.Builder builder = new Notification.Builder(this);
80+
builder
81+
.setTicker("USB/IP Server Running")
82+
.setContentTitle("USB/IP Server Running")
83+
.setAutoCancel(false)
84+
.setOngoing(true)
85+
.setSmallIcon(R.drawable.notification_icon)
86+
.setContentIntent(pendIntent);
87+
88+
if (connections.size() == 0) {
89+
builder.setContentText("No devices currently shared");
90+
}
91+
else {
92+
builder.setContentText(String.format("Sharing %d device(s)", connections.size()));
93+
}
94+
95+
startForeground(NOTIFICATION_ID, builder.getNotification());
96+
}
97+
6798
@SuppressLint("UseSparseArrays")
6899
@Override
69100
public void onCreate() {
@@ -77,7 +108,10 @@ public void onCreate() {
77108
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
78109
registerReceiver(usbReceiver, filter);
79110

80-
new UsbIpServer().start(this);
111+
server = new UsbIpServer();
112+
server.start(this);
113+
114+
updateNotification();
81115
}
82116

83117
@Override
@@ -562,6 +596,8 @@ public boolean attachToDevice(String busId) {
562596
new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.DiscardPolicy());
563597

564598
connections.put(dev.getDeviceId(), context);
599+
600+
updateNotification();
565601
return true;
566602
}
567603

@@ -578,7 +614,7 @@ public void detachFromDevice(String busId) {
578614
}
579615

580616
// Clear the this attachment's context
581-
connections.put(dev.getDeviceId(), null);
617+
connections.remove(dev.getDeviceId());
582618

583619
// Release our claim to the interfaces
584620
for (int i = 0; i < dev.getInterfaceCount(); i++) {
@@ -587,6 +623,8 @@ public void detachFromDevice(String busId) {
587623

588624
// Close the connection
589625
context.devConn.close();
626+
627+
updateNotification();
590628
}
591629

592630
class UrbContext {

0 commit comments

Comments
 (0)