10
10
import java .util .concurrent .ThreadPoolExecutor ;
11
11
import java .util .concurrent .TimeUnit ;
12
12
13
+ import org .cgutman .usbip .config .UsbIpConfig ;
13
14
import org .cgutman .usbip .server .UsbDeviceInfo ;
14
15
import org .cgutman .usbip .server .UsbIpServer ;
15
16
import org .cgutman .usbip .server .UsbRequestHandler ;
22
23
import org .cgutman .usbip .usb .DescriptorReader ;
23
24
import org .cgutman .usbip .usb .UsbDeviceDescriptor ;
24
25
import org .cgutman .usbip .usb .XferUtils ;
26
+ import org .cgutman .usbipserverforandroid .R ;
25
27
26
28
import android .annotation .SuppressLint ;
29
+ import android .app .Notification ;
27
30
import android .app .PendingIntent ;
28
31
import android .app .Service ;
29
32
import android .content .BroadcastReceiver ;
@@ -46,6 +49,9 @@ public class UsbIpService extends Service implements UsbRequestHandler {
46
49
private UsbManager usbManager ;
47
50
private SparseArray <AttachedDeviceContext > connections ;
48
51
private SparseArray <Boolean > permission ;
52
+ private UsbIpServer server ;
53
+
54
+ private static final int NOTIFICATION_ID = 100 ;
49
55
50
56
private static final String ACTION_USB_PERMISSION =
51
57
"org.cgutman.usbip.USB_PERMISSION" ;
@@ -64,6 +70,31 @@ public void onReceive(Context context, Intent intent) {
64
70
}
65
71
};
66
72
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
+
67
98
@ SuppressLint ("UseSparseArrays" )
68
99
@ Override
69
100
public void onCreate () {
@@ -77,7 +108,10 @@ public void onCreate() {
77
108
IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION );
78
109
registerReceiver (usbReceiver , filter );
79
110
80
- new UsbIpServer ().start (this );
111
+ server = new UsbIpServer ();
112
+ server .start (this );
113
+
114
+ updateNotification ();
81
115
}
82
116
83
117
@ Override
@@ -562,6 +596,8 @@ public boolean attachToDevice(String busId) {
562
596
new LinkedBlockingQueue <Runnable >(), new ThreadPoolExecutor .DiscardPolicy ());
563
597
564
598
connections .put (dev .getDeviceId (), context );
599
+
600
+ updateNotification ();
565
601
return true ;
566
602
}
567
603
@@ -578,7 +614,7 @@ public void detachFromDevice(String busId) {
578
614
}
579
615
580
616
// Clear the this attachment's context
581
- connections .put (dev .getDeviceId (), null );
617
+ connections .remove (dev .getDeviceId ());
582
618
583
619
// Release our claim to the interfaces
584
620
for (int i = 0 ; i < dev .getInterfaceCount (); i ++) {
@@ -587,6 +623,8 @@ public void detachFromDevice(String busId) {
587
623
588
624
// Close the connection
589
625
context .devConn .close ();
626
+
627
+ updateNotification ();
590
628
}
591
629
592
630
class UrbContext {
0 commit comments