diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/MainActivity.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/MainActivity.java index 9b54556..8495e89 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/MainActivity.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/MainActivity.java @@ -269,7 +269,10 @@ protected void onCreate(Bundle savedInstanceState) { } - + String target = getIntent().getStringExtra("navigateToFragment"); + if (target != null && target.equals("PingFragment")) { + navController.navigate(R.id.ping_fragment); + } diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Parameter/PingParameter.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Parameter/PingParameter.java index 72886ea..586be41 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Parameter/PingParameter.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Parameter/PingParameter.java @@ -43,7 +43,7 @@ public int getCount() { return count; } - public int getTimeoutMillis() { + public double getTimeoutMillis() { return timeoutMillis; } @@ -62,7 +62,7 @@ public Network getNetwork() { private String destination; private int count; - private int timeoutMillis; + private double timeoutMillis; private int packetSize; private double intervalMillis; private Network network; @@ -118,7 +118,7 @@ public PingParameter(String stringParameter, String testUUID) { count = Integer.parseInt(parts[i + 1]); break; case "-W": - timeoutMillis = Integer.parseInt(parts[i + 1]); + timeoutMillis = Double.parseDouble(parts[i + 1]); break; case "-s": packetSize = Integer.parseInt(parts[i + 1]); @@ -182,7 +182,7 @@ protected PingParameter(Parcel in) { super(in); destination = in.readString(); count = in.readInt(); - timeoutMillis = in.readInt(); + timeoutMillis = in.readDouble(); packetSize = in.readInt(); intervalMillis = in.readDouble(); network = in.readParcelable(Network.class.getClassLoader()); @@ -210,7 +210,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(destination); dest.writeInt(count); - dest.writeInt(timeoutMillis); + dest.writeDouble(timeoutMillis); dest.writeInt(packetSize); dest.writeDouble(intervalMillis); dest.writeParcelable(network, flags); diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingFragment.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingFragment.java index 81e69de..56fd161 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingFragment.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingFragment.java @@ -10,6 +10,7 @@ import static android.view.View.GONE; import static android.view.View.INVISIBLE; +import static android.view.View.VISIBLE; import android.annotation.SuppressLint; import android.content.Context; @@ -52,6 +53,7 @@ import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.METRIC_TYPE; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.MetricCalculator; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.MetricView; +import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PacketLossLine; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.RTTLine; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.Worker.PingWorker; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SPType; @@ -79,6 +81,7 @@ public class PingFragment extends Fragment { private LiveData workInfoLiveData; private MaterialTextView pingTextView; + private int counter = 0; public PingFragment() { } @@ -140,7 +143,9 @@ private void checkLastUUID(String uuidStr) { Log.d(TAG, "registerObserver: workInfo-State: " + workInfo.getState()); String rtt = progress.getString(PingWorker.RTT); if(rtt != null) { + Log.d(TAG, "checkLastUUID: got RTT line"); RTTLine rttLine = new Gson().fromJson(rtt, RTTLine.class); + Log.d(TAG, "checkLastUUID: got ICMP-SEQ: "+rttLine.getIcmpSeq()); rttMetric.update(rttLine.getRtt()); StringBuilder sb = new StringBuilder(); sb.append("Host: ").append(rttLine.getHost()).append(" "); @@ -148,25 +153,39 @@ private void checkLastUUID(String uuidStr) { sb.append("TTL: ").append(rttLine.getTtl()).append(" "); sb.append("RTT: ").append(rttLine.getRtt()).append(" ms"); pingTextView.setText(sb.toString()+"\n"+pingTextView.getText()); - + counter++; + if(counter > 1e4){ + pingTextView.setText(""); + rttMetric.getMetricCalculator().resetMetric(); + packetLossMetric.getMetricCalculator().resetMetric(); + counter = 0; + } } - - double packetLoss = progress.getDouble(PingWorker.PACKET_LOSS, -1.0); - - if(packetLoss != -1.0) { - packetLossMetric.setVisibility(View.VISIBLE); - Log.d(TAG, "onChanged: Packet Loss: " + packetLoss); - packetLossMetric.update(packetLoss); + String packetLoss = progress.getString(PingWorker.PACKET_LOSS); + if(packetLoss != null){ + Log.d(TAG, "checkLastUUID: got PACKETLOSS line"); + PacketLossLine packetLossLine = new Gson().fromJson(packetLoss,PacketLossLine.class); + packetLossMetric.update(packetLossLine.getPacketLoss()); + packetLossMetric.setVisibility(VISIBLE); } + switch (workInfo.getState()) { case RUNNING: case SUCCEEDED: break; case FAILED: + Log.e(TAG, "checkLastUUID: Work failed: " + workInfo.getOutputData().getString(PingWorker.REASON)); + pingTextView.setText(workInfo.getOutputData().getString(PingWorker.REASON)); + rttMetric.getMetricCalculator().resetMetric(); + packetLossMetric.getMetricCalculator().resetMetric(); + packetLossMetric.setVisibility(GONE); + spg.getSharedPreference(SPType.ping_sp).edit().putBoolean("ping_running", false).apply(); + toggleGroup.check(R.id.ping_stop); + break; case CANCELLED: - workInfoLiveData.removeObserver(observer); // Optionally clean up +// workInfoLiveData.removeObserver(observer); // Optionally clean up break; default: break; diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingInformations/PacketLossLine.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingInformations/PacketLossLine.java index b8cae2e..0dac9f5 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingInformations/PacketLossLine.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingInformations/PacketLossLine.java @@ -51,4 +51,11 @@ public Point getPoint(){ .time(System.currentTimeMillis(), WritePrecision.MS); //ping does not provide timestamp for packet loss line } + @Override + public String toString() { + return "PacketLossLine{" + + "packetLoss=" + packetLoss + + ", packetsTransmitted=" + packetsTransmitted + + ", packetsReceived=" + packetsReceived + '}'; + } } diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingService.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingService.java index 2ac9b35..044df28 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingService.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/PingService.java @@ -95,6 +95,7 @@ private void stopWorker(){ return; } workManager.cancelWorkById(UUID.fromString(lastUUID)); + workManager.cancelAllWorkByTag(PingWorker.TAG); } diff --git a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/Worker/PingWorker.java b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/Worker/PingWorker.java index 3150c69..e167f97 100644 --- a/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/Worker/PingWorker.java +++ b/app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Ping/Worker/PingWorker.java @@ -12,7 +12,9 @@ import android.app.Notification; import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; import android.graphics.Color; import android.util.Log; @@ -32,13 +34,16 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs.PingInput; +import de.fraunhofer.fokus.OpenMobileNetworkToolkit.MainActivity; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Parameter.PingParameter; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PacketLossLine; import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PingInformation; @@ -93,7 +98,18 @@ public PingWorker(@NonNull Context context, @NonNull WorkerParameters workerPara } private ForegroundInfo createForegroundInfo(String progress) { - notification = notificationBuilder + Intent intent = new Intent(this.ct, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + intent.putExtra("navigateToFragment", "PingFragment"); // Identifier for your fragment + + PendingIntent pendingIntent = PendingIntent.getActivity( + this.ct, + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE + ); + + Notification notification = notificationBuilder .setContentTitle("Ping") .setContentText(progress) .setOngoing(true) @@ -101,11 +117,14 @@ private ForegroundInfo createForegroundInfo(String progress) { .setColor(Color.WHITE) .setSmallIcon(R.mipmap.ic_launcher_foreground) .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_DEFAULT) + .setContentIntent(pendingIntent) // 👈 attach pending intent here .build(); + return new ForegroundInfo(notificationID, notification, FOREGROUND_SERVICE_TYPE); } + @NonNull @Override public Result doWork() { @@ -128,6 +147,7 @@ public Result doWork() { Log.d(TAG, "doWork: executing " + String.join(" ", command)); int result = -1; + String error = ""; try { @@ -137,6 +157,7 @@ public Result doWork() { FileOutputStream pingStream = new FileOutputStream(pingInput.getPingParameter().getLogfile(), true); PingParser pingParser = new PingParser(); String line; + while ((line = reader.readLine()) != null) { Log.d(TAG, "doWork: "+line); pingStream.write((line + "\n").getBytes()); @@ -151,16 +172,9 @@ public Result doWork() { switch (pingInformation.getLineType()){ case RTT: rtt = ((RTTLine)pingInformation).getRtt(); - - progressOutput.putDouble(RTT, rtt); progressOutput.putString(RTT, new Gson().toJson((RTTLine)pingInformation)); - - - setProgressAsync(progressOutput.build()); +// setProgressAsync(progressOutput.build()); setForegroundAsync(createForegroundInfo(((RTTLine) pingInformation).getHost()+": " + rtt + " ms")); - - - Log.d(TAG, "doWork: RTT: " + rtt); break; case UNREACHABLE: @@ -171,25 +185,36 @@ public Result doWork() { progressOutput.putString(REASON, "Request timeout"); break; case PACKET_LOSS: - double packetLoss = ((PacketLossLine)pingInformation).getPacketLoss(); - setProgressAsync(new Data.Builder().putDouble(PACKET_LOSS, packetLoss).build()); - Log.d(TAG, "doWork: Packet Loss: " + packetLoss); + PacketLossLine packetLoss = ((PacketLossLine)pingInformation); + progressOutput.putString(PACKET_LOSS, new Gson().toJson(packetLoss)); +// setProgressAsync(progressOutput.build()); + Log.d(TAG, "doWork: Packet Loss: " + packetLoss.toString()); break; case UNKNOWN: Log.w(TAG, "doWork: Unknown line type"); break; } + setProgressAsync(progressOutput.build()); if(this.isStopped()){ break; } + } + error = new BufferedReader( + new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining("\n")); process.destroy(); pingStream.close(); reader.close(); result = process.waitFor(); + + + + Thread.sleep(200); // Sleep so that the Observer can process the last lines } catch (IOException e) { Log.e(TAG, "Error while executing ping command: " + e.toString()); return Result.failure(output.putString(REASON, "Error while executing ping command.").build()); @@ -198,9 +223,10 @@ public Result doWork() { return Result.failure(output.putString(REASON, "Error while waiting for ping command.").build()); } - if (result != 0) { + if (result != 0) {; Log.e(TAG, "Ping command failed with result: " + result); - return Result.failure(output.putString(REASON, "Ping command failed with result: " + result).build()); + Log.e(TAG, "Ping command failed because: "+error); + return Result.failure(output.putString(REASON, "Ping command failed: " + error).build()); } return Result.success(output.putBoolean(PING, true).build());