Skip to content

Commit

Permalink
OnBootReceiver: Attempt at checking for interface availability at boot
Browse files Browse the repository at this point in the history
It probably needs some more polish
  • Loading branch information
elluisian committed Sep 1, 2024
1 parent 6a318b9 commit f60d76c
Showing 1 changed file with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand All @@ -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()));
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f60d76c

Please sign in to comment.