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 30, 2024
1 parent 3e2bc20 commit f133256
Showing 1 changed file with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,35 @@

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import androidx.preference.PreferenceManager;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import java.util.ArrayList;

import java.net.InetAddress;
import java.net.Socket;
import java.net.InetSocketAddress;

import java.io.IOException;

>>>>>>> c7845a4 (OnBootReceiver: Attempt at checking for interface availability at boot)


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 Down Expand Up @@ -87,9 +102,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 \"" + listenIf + "\" 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 All @@ -108,10 +132,43 @@ public void onReceive(Context context, Intent arg1) {
} else {
pendingIntent = PendingIntent.getService(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE);
}
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayMillis, pendingIntent);
} else {
Log.i(TAG, "onReceive: configured NOT to start");
}
Log.i(TAG, "onReceive: configured to start immediately");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.getApplicationContext().startForegroundService(intent);
} else {
context.getApplicationContext().startService(intent);
}

}
} else {
Log.i(TAG, "onReceive: configured NOT to start");
}
}
}

}

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 f133256

Please sign in to comment.