2424
2525import android .app .AlarmManager ;
2626import android .app .PendingIntent ;
27+ import android .app .NotificationChannel ;
28+ import android .app .NotificationManager ;
2729import android .content .BroadcastReceiver ;
2830import android .content .Context ;
2931import android .content .Intent ;
3234import android .os .SystemClock ;
3335import android .util .Log ;
3436
37+ import androidx .core .app .NotificationCompat ;
38+ import androidx .core .app .NotificationManagerCompat ;
3539import androidx .preference .PreferenceManager ;
3640
3741
38- public class OnBootReceiver extends BroadcastReceiver {
42+ import java .util .ArrayList ;
43+
44+ import java .net .InetAddress ;
45+ import java .net .Socket ;
46+ import java .net .InetSocketAddress ;
47+
48+ import java .io .IOException ;
49+
50+
3951
52+ public class OnBootReceiver extends BroadcastReceiver {
4053 private static final String TAG = "OnBootReceiver" ;
54+ public static String NOTIFICATION_ID = "notification-id" ;
55+ public static String NOTIFICATION = "notification" ;
56+
4157
4258 public void onReceive (Context context , Intent arg1 ) {
4359 if (Intent .ACTION_BOOT_COMPLETED .equals (arg1 .getAction ())) {
@@ -52,8 +68,18 @@ public void onReceive(Context context, Intent arg1) {
5268 return ;
5369 }
5470
71+ // Check for availability of listenIf
72+ String listenIf = prefs .getString (Constants .PREFS_KEY_SETTINGS_LISTEN_INTERFACE , defaults .getListenInterface ());
73+ NetworkInterfaceTester nit = NetworkInterfaceTester .getInstance (context );
74+ if (!nit .isIfEnabled (listenIf )) {
75+ Log .w (TAG , "onReceive: interface \" " + listenIf + "\" not available, sending a notification" );
76+ this .sendNotification (context , listenIf );
77+ return ;
78+ }
79+
5580 Intent intent = new Intent (context .getApplicationContext (), MainService .class );
5681 intent .setAction (MainService .ACTION_START );
82+ intent .putExtra (MainService .EXTRA_LISTEN_INTERFACE , listenIf );
5783 intent .putExtra (MainService .EXTRA_PORT , prefs .getInt (Constants .PREFS_KEY_SETTINGS_PORT , defaults .getPort ()));
5884 intent .putExtra (MainService .EXTRA_PASSWORD , prefs .getString (Constants .PREFS_KEY_SETTINGS_PASSWORD , defaults .getPassword ()));
5985 intent .putExtra (MainService .EXTRA_FILE_TRANSFER , prefs .getBoolean (Constants .PREFS_KEY_SETTINGS_FILE_TRANSFER , defaults .getFileTransfer ()));
@@ -80,38 +106,34 @@ public void onReceive(Context context, Intent arg1) {
80106 } else {
81107 context .getApplicationContext ().startService (intent );
82108 }
83- // autostart needs InputService on Android 10 and newer, both for the activity starts from MainService
84- // (could be reworked) but most importantly for fallback screen capture
85- if (Build .VERSION .SDK_INT >= 30 && !InputService .isConnected ()) {
86- Log .w (TAG , "onReceive: configured to start, but on Android 10+ and InputService not set up, bailing out" );
87- return ;
88- }
89-
90- Intent intent = new Intent (context , MainService .class );
91- intent .setAction (MainService .ACTION_START );
92- intent .putExtra (MainService .EXTRA_LISTEN_INTERFACE , prefs .getString (Constants .PREFS_KEY_SETTINGS_LISTEN_INTERFACE , defaults .getListenInterface ()));
93- intent .putExtra (MainService .EXTRA_PORT , prefs .getInt (Constants .PREFS_KEY_SETTINGS_PORT , defaults .getPort ()));
94- intent .putExtra (MainService .EXTRA_PASSWORD , prefs .getString (Constants .PREFS_KEY_SETTINGS_PASSWORD , defaults .getPassword ()));
95- intent .putExtra (MainService .EXTRA_FILE_TRANSFER , prefs .getBoolean (Constants .PREFS_KEY_SETTINGS_FILE_TRANSFER , defaults .getFileTransfer ()));
96- intent .putExtra (MainService .EXTRA_VIEW_ONLY , prefs .getBoolean (Constants .PREFS_KEY_SETTINGS_VIEW_ONLY , defaults .getViewOnly ()));
97- intent .putExtra (MainService .EXTRA_SCALING , prefs .getFloat (Constants .PREFS_KEY_SETTINGS_SCALING , defaults .getScaling ()));
98- intent .putExtra (MainService .EXTRA_ACCESS_KEY , prefs .getString (Constants .PREFS_KEY_SETTINGS_ACCESS_KEY , defaults .getAccessKey ()));
99- intent .putExtra (MainService .EXTRA_FALLBACK_SCREEN_CAPTURE , true ); // want this on autostart
100-
101- long delayMillis = 1000L * prefs .getInt (Constants .PREFS_KEY_SETTINGS_START_ON_BOOT_DELAY , defaults .getStartOnBootDelay ());
102- if (delayMillis > 0 ) {
103- Log .i (TAG , "onReceive: configured to start delayed by " + delayMillis /1000 + "s" );
104- AlarmManager alarmManager = (AlarmManager ) context .getSystemService (Context .ALARM_SERVICE );
105- PendingIntent pendingIntent ;
106- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
107- pendingIntent = PendingIntent .getForegroundService (context .getApplicationContext (), 0 , intent , PendingIntent .FLAG_IMMUTABLE );
108- } else {
109- pendingIntent = PendingIntent .getService (context .getApplicationContext (), 0 , intent , PendingIntent .FLAG_IMMUTABLE );
110109 }
111110 } else {
112111 Log .i (TAG , "onReceive: configured NOT to start" );
113112 }
114113 }
115114 }
116115
116+
117+ private void sendNotification (Context context , String listenIf ) {
118+ NotificationManager manager = context .getSystemService (NotificationManager .class );
119+
120+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
121+ NotificationChannel serviceChannel = new NotificationChannel (
122+ context .getPackageName (),
123+ "ListenIf NA Channel" ,
124+ NotificationManager .IMPORTANCE_DEFAULT
125+ );
126+ manager .createNotificationChannel (serviceChannel );
127+ }
128+
129+ NotificationCompat .Builder builder = new NotificationCompat .Builder (context , context .getPackageName ())
130+ .setSmallIcon (R .drawable .ic_notification )
131+ .setContentTitle (context .getResources ().getString (R .string .app_name ))
132+ .setContentText (
133+ String .format ("Failed to connect to interface \" %s\" , is it down perhaps?" , listenIf ))
134+ .setSilent (false )
135+ .setOngoing (true );
136+
137+ manager .notify (9 , builder .build ());
138+ }
117139}
0 commit comments