Skip to content

Commit f60d76c

Browse files
committed
OnBootReceiver: Attempt at checking for interface availability at boot
It probably needs some more polish
1 parent 6a318b9 commit f60d76c

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

app/src/main/java/net/christianbeier/droidvnc_ng/OnBootReceiver.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,29 @@
3030
import android.content.SharedPreferences;
3131
import android.os.Build;
3232
import androidx.preference.PreferenceManager;
33+
import androidx.core.app.NotificationCompat;
34+
import androidx.core.app.NotificationManagerCompat;
35+
import android.app.NotificationManager;
36+
import android.app.NotificationChannel;
3337
import android.os.SystemClock;
3438
import android.util.Log;
3539

40+
import java.util.ArrayList;
41+
42+
import java.net.InetAddress;
43+
import java.net.Socket;
44+
import java.net.InetSocketAddress;
45+
import java.io.IOException;
46+
47+
3648

3749
public class OnBootReceiver extends BroadcastReceiver {
3850

3951
private static final String TAG = "OnBootReceiver";
4052

53+
public static String NOTIFICATION_ID = "notification-id";
54+
public static String NOTIFICATION = "notification" ;
55+
4156
public void onReceive(Context context, Intent arg1)
4257
{
4358
if(Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
@@ -52,9 +67,18 @@ public void onReceive(Context context, Intent arg1)
5267
return;
5368
}
5469

70+
// Check for availability of listenIf
71+
String listenIf = prefs.getString(Constants.PREFS_KEY_SETTINGS_LISTEN_INTERFACE, defaults.getListenInterface());
72+
NetworkInterfaceTester nit = NetworkInterfaceTester.getInstance(context);
73+
if (!nit.isIfEnabled(listenIf)) {
74+
Log.w(TAG, "onReceive: interface \"" + listneIf + "\" not available, sending a notification");
75+
this.sendNotification(context, listenIf);
76+
return;
77+
}
78+
5579
Intent intent = new Intent(context, MainService.class);
5680
intent.setAction(MainService.ACTION_START);
57-
intent.putExtra(MainService.EXTRA_LISTEN_INTERFACE, prefs.getString(Constants.PREFS_KEY_SETTINGS_LISTEN_INTERFACE, defaults.getListenInterface()));
81+
intent.putExtra(MainService.EXTRA_LISTEN_INTERFACE, listenIf);
5882
intent.putExtra(MainService.EXTRA_PORT, prefs.getInt(Constants.PREFS_KEY_SETTINGS_PORT, defaults.getPort()));
5983
intent.putExtra(MainService.EXTRA_PASSWORD, prefs.getString(Constants.PREFS_KEY_SETTINGS_PASSWORD, defaults.getPassword()));
6084
intent.putExtra(MainService.EXTRA_FILE_TRANSFER, prefs.getBoolean(Constants.PREFS_KEY_SETTINGS_FILE_TRANSFER, defaults.getFileTransfer()));
@@ -88,4 +112,27 @@ public void onReceive(Context context, Intent arg1)
88112
}
89113
}
90114

91-
}
115+
116+
private void sendNotification(Context context, String listenIf) {
117+
NotificationManager manager = context.getSystemService(NotificationManager.class);
118+
119+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
120+
NotificationChannel serviceChannel = new NotificationChannel(
121+
context.getPackageName(),
122+
"ListenIf NA Channel",
123+
NotificationManager.IMPORTANCE_DEFAULT
124+
);
125+
manager.createNotificationChannel(serviceChannel);
126+
}
127+
128+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, context.getPackageName())
129+
.setSmallIcon(R.drawable.ic_notification)
130+
.setContentTitle(context.getResources().getString(R.string.app_name))
131+
.setContentText(
132+
String.format("Failed to connect to interface \"%s\", is it down perhaps?", listenIf))
133+
.setSilent(false)
134+
.setOngoing(true);
135+
136+
manager.notify(9, builder.build());
137+
}
138+
}

0 commit comments

Comments
 (0)