diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/OnBootReceiver.java b/app/src/main/java/net/christianbeier/droidvnc_ng/OnBootReceiver.java index c45eb50b..adad61f4 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/OnBootReceiver.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/OnBootReceiver.java @@ -30,14 +30,29 @@ import android.content.SharedPreferences; import android.os.Build; import androidx.preference.PreferenceManager; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; +import android.app.NotificationManager; +import android.app.NotificationChannel; import android.os.SystemClock; import android.util.Log; +import java.util.ArrayList; + +import java.net.InetAddress; +import java.net.Socket; +import java.net.InetSocketAddress; +import java.io.IOException; + + public class OnBootReceiver extends BroadcastReceiver { private static final String TAG = "OnBootReceiver"; + public static String NOTIFICATION_ID = "notification-id"; + public static String NOTIFICATION = "notification" ; + public void onReceive(Context context, Intent arg1) { if(Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) { @@ -52,9 +67,18 @@ public void onReceive(Context context, Intent arg1) return; } + // Check for availability of listenIf + String listenIf = prefs.getString(Constants.PREFS_KEY_SETTINGS_LISTEN_INTERFACE, defaults.getListenInterface()); + NetworkInterfaceTester nit = NetworkInterfaceTester.getInstance(context); + if (!nit.isIfEnabled(listenIf)) { + Log.w(TAG, "onReceive: interface \"" + listneIf + "\" not available, sending a notification"); + this.sendNotification(context, listenIf); + return; + } + Intent intent = new Intent(context, MainService.class); intent.setAction(MainService.ACTION_START); - intent.putExtra(MainService.EXTRA_LISTEN_INTERFACE, prefs.getString(Constants.PREFS_KEY_SETTINGS_LISTEN_INTERFACE, defaults.getListenInterface())); + intent.putExtra(MainService.EXTRA_LISTEN_INTERFACE, listenIf); intent.putExtra(MainService.EXTRA_PORT, prefs.getInt(Constants.PREFS_KEY_SETTINGS_PORT, defaults.getPort())); intent.putExtra(MainService.EXTRA_PASSWORD, prefs.getString(Constants.PREFS_KEY_SETTINGS_PASSWORD, defaults.getPassword())); 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) } } -} + + private void sendNotification(Context context, String listenIf) { + NotificationManager manager = context.getSystemService(NotificationManager.class); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel = new NotificationChannel( + context.getPackageName(), + "ListenIf NA Channel", + NotificationManager.IMPORTANCE_DEFAULT + ); + manager.createNotificationChannel(serviceChannel); + } + + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, context.getPackageName()) + .setSmallIcon(R.drawable.ic_notification) + .setContentTitle(context.getResources().getString(R.string.app_name)) + .setContentText( + String.format("Failed to connect to interface \"%s\", is it down perhaps?", listenIf)) + .setSilent(false) + .setOngoing(true); + + manager.notify(9, builder.build()); + } +} \ No newline at end of file