Skip to content

Commit

Permalink
Merge pull request #30 from omnt/feature/update_ping
Browse files Browse the repository at this point in the history
Refactor Ping
  • Loading branch information
derpeter authored Jul 25, 2024
2 parents 248c3c9 + 1cc2786 commit 2086e17
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 71 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ dependencies {
implementation 'com.google.android.gms:play-services-location:21.3.0'
implementation 'com.github.anastr:speedviewlib:1.6.1'
implementation "androidx.viewpager2:viewpager2:1.1.0"
implementation "androidx.compose.material3:material3:1.2.1"
}

configurations.implementation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import de.fraunhofer.fokus.OpenMobileNetworkToolkit.DataProvider.DataProvider;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.DataProvider.NetworkCallback;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingFragment;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SPType;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SharedPreferencesGrouper;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.WorkProfile.WorkProfileActivity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
Expand All @@ -24,8 +26,13 @@
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textview.MaterialTextView;

import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.METRIC_TYPE;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.Metric;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PacketLossLine;
Expand All @@ -42,11 +49,12 @@
public class PingFragment extends Fragment {
private final String TAG = "PingFragment";
private Switch aSwitch;
private MaterialButtonToggleGroup toggleGroup;
private LinearLayout verticalLL;
private LinearLayout horizontalLL1;
private Handler pingLogging;
private FileOutputStream stream;
private EditText input;
private TextInputEditText input;
private Context ct;
private SharedPreferencesGrouper spg;
private Metric rttMetric;
Expand Down Expand Up @@ -107,21 +115,61 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
verticalLL = v.findViewById(R.id.ping_vertical_ll);
horizontalLL1 = verticalLL.findViewById(R.id.ping_horizontal1_ll);

aSwitch = verticalLL.findViewById(R.id.ping_switch);
toggleGroup = verticalLL.findViewById(R.id.ping_toggle_group);
input = verticalLL.findViewById(R.id.ping_input);
input.setText(spg.getSharedPreference(SPType.ping_sp).getString("ping_input", "-w 5 8.8.8.8"));
input.setEnabled(!PingService.isRunning());
saveTextInputToSharedPreferences(input, "ping_input");
aSwitch.setChecked(PingService.isRunning());
Boolean pingRunning = spg.getSharedPreference(SPType.ping_sp).getBoolean("ping_running", false);
if(pingRunning){
v.findViewById(R.id.ping_start).setBackgroundColor(getResources().getColor(R.color.teal_200, null));

}else{
v.findViewById(R.id.ping_stop).setBackgroundColor(getResources().getColor(R.color.teal_200, null));
}
spg.setListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String key) {
if(key.equals("ping_running")){
Boolean isRunning = sharedPreferences.getBoolean("ping_running", false);
handleInput(isRunning);
if(isRunning){
v.findViewById(R.id.ping_start).setBackgroundColor(getResources().getColor(R.color.teal_200, null));
v.findViewById(R.id.ping_stop).setBackgroundColor(Color.TRANSPARENT);
} else {
v.findViewById(R.id.ping_start).setBackgroundColor(Color.TRANSPARENT);
v.findViewById(R.id.ping_stop).setBackgroundColor(getResources().getColor(R.color.teal_200, null));
}

}
}
}, SPType.ping_sp);

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
input.setEnabled(!pingRunning);
toggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d(TAG, "onCheckedChanged: "+b);
if(b) startPingService();
else stopPingService();
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
Log.d(TAG, "onButtonChecked: "+checkedId);
if(!isChecked) return;
switch (checkedId){
case R.id.ping_start:
startPingService();
v.findViewById(R.id.ping_start).setBackgroundColor(getResources().getColor(R.color.teal_200, null));
v.findViewById(R.id.ping_stop).setBackgroundColor(Color.TRANSPARENT);
spg.getSharedPreference(SPType.ping_sp).edit().putBoolean("ping_running", true).apply();

break;
case R.id.ping_stop:
v.findViewById(R.id.ping_start).setBackgroundColor(Color.TRANSPARENT);
v.findViewById(R.id.ping_stop).setBackgroundColor(getResources().getColor(R.color.teal_200, null));
stopPingService();
spg.getSharedPreference(SPType.ping_sp).edit().putBoolean("ping_running", false).apply();
break;
}

}
});

rttMetric = new Metric(METRIC_TYPE.PING_RTT, ct);
packetLossMetric = new Metric(METRIC_TYPE.PING_PACKET_LOSS, ct);
LinearLayout metricsLL = new LinearLayout(ct);
Expand Down Expand Up @@ -157,4 +205,5 @@ public void propertyChange(PropertyChangeEvent evt) {
//packetLossMetric.setVisibility(View.INVISIBLE);
return v;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
Expand All @@ -25,8 +27,11 @@
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import de.fraunhofer.fokus.OpenMobileNetworkToolkit.MainActivity;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PingInformation;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.RTTLine;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SPType;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SharedPreferencesGrouper;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.R;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand All @@ -41,7 +46,6 @@
public class PingWorker extends Worker {

private static final String TAG = "PingWorker";
String host;
Runtime runtime;
private ArrayList<String> lines;
private Process pingProcess;
Expand All @@ -54,10 +58,9 @@ public class PingWorker extends Worker {
private NotificationCompat.Builder notificationBuilder;
private Notification notification;
private final String timeRegex = "\\btime=([0-9]+\\.[0-9]+)\\s+ms\\b";
private final Pattern pattern = Pattern.compile(timeRegex);
private final String line = null;
private double rtt;
private NotificationManager notificationManager;
private SharedPreferencesGrouper spg;


public void parsePingCommand() {
Expand Down Expand Up @@ -87,34 +90,49 @@ public PingWorker(@NonNull Context context, @NonNull WorkerParameters workerPara
super(context, workerParams);
runtime = Runtime.getRuntime();
ct = context;
spg = SharedPreferencesGrouper.getInstance(ct);
}


@Override
public void onStopped() {
super.onStopped();
Log.d(TAG, "onStopped: worker stopped!");
if(pingProcess.isAlive()) pingProcess.destroy();
spg.getSharedPreference(SPType.ping_sp).edit().putBoolean("ping_running", false).apply();

}

private ForegroundInfo createForegroundInfo(@NonNull String progress) {
// Create an Intent to launch the main activity
Intent launchIntent = new Intent(ct, MainActivity.class);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Add a Bundle to the Intent to indicate that the PingFragment should be opened
Bundle bundle = new Bundle();
bundle.putString("openFragment", "PingFragment");
launchIntent.putExtras(bundle);

// Create a PendingIntent with the Intent
PendingIntent pendingIntent = PendingIntent.getActivity(ct, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

PendingIntent cancelIntent = WorkManager.getInstance(ct)
.createCancelPendingIntent(getId());

PendingIntent intent = WorkManager.getInstance(ct)
.createCancelPendingIntent(getId());
notification = notificationBuilder
.setContentTitle("Ping "+ parsedCommand.get("target"))
.setContentText(progress)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setColor(Color.WHITE)
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_DEFAULT)
.addAction(R.drawable.ic_close, "Cancel", intent)
.build();
.setContentTitle("Ping " + parsedCommand.get("target"))
.setContentText(progress)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setColor(Color.WHITE)
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_DEFAULT)
.addAction(R.drawable.ic_close, "Cancel", cancelIntent)
.setContentIntent(pendingIntent) // Set the content intent
.build();
return new ForegroundInfo(notificationID, notification, FOREGROUND_SERVICE_TYPE);
}


Runnable updateNotification = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -171,6 +189,7 @@ public void propertyChange(PropertyChangeEvent evt) {

if(isStopped()){
Log.d(TAG, "doWork: got cancelled because Worker got stopped!");

return Result.success();
}

Expand Down
83 changes: 35 additions & 48 deletions app/src/main/res/layout/fragment_ping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
~ SPDX-License-Identifier: BSD-3-Clause-Clear
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.MaterialComponents.DayNight"
android:theme="@style/Theme.Material3.DayNight"
tools:context=".Ping.PingFragment"
android:id="@+id/fragment_ping">

Expand All @@ -20,74 +21,60 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="UselessParent">
android:paddingHorizontal="16dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

<TextView
android:id="@+id/ping_title"
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@string/ping"
android:textStyle="bold" />
android:layout_marginTop="16dp"
app:endIconMode="clear_text">

<LinearLayout
android:id="@+id/ping_horizontal_ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">

<EditText
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ping_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:forceDarkAllowed="true"
android:inputType="text"
android:paddingStart="16dp"
android:text="@string/ping_command"
android:autofillHints="add Ping Parameters"
android:hint="Ping Command"
tools:ignore="LabelFor,RtlSymmetry" />
</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
<LinearLayout
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/ping_toggle_group"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Switch
android:layout_marginLeft="210dp"
android:layout_marginRight="16dp"
android:id="@+id/ping_switch"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
app:singleSelection="true"
app:selectionRequired="true">

<com.google.android.material.button.MaterialButton
android:id="@+id/ping_start"
style="@style/CustomUnifiedButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ping_run_continuously"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
android:text="Start" />

<com.google.android.material.button.MaterialButton
android:id="@+id/ping_stop"
style="@style/CustomUnifiedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop" />

</com.google.android.material.button.MaterialButtonToggleGroup>

<LinearLayout
android:id="@+id/ping_horizontal1_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:baselineAligned="false">

<ScrollView
android:id="@+id/ping_scrollviewer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:ignore="UselessParent">

<TextView
android:id="@+id/ping_viewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
tools:ignore="SmallSp" />
</ScrollView>
</LinearLayout>
</LinearLayout>

</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
20 changes: 20 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryDark">@color/purple_700</item>
<item name="colorAccent">@color/teal_200</item>
</style>
<style name="CustomUnifiedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="backgroundTint">@android:color/transparent</item>
<item name="android:textColor">@color/teal_700</item>
<item name="strokeColor">@color/teal_700</item>
<item name="strokeWidth">2dp</item>
<item name="android:paddingHorizontal">16dp</item>
<item name="android:paddingVertical">8dp</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="cornerRadius">20dp</item>
</style>

</resources>

0 comments on commit 2086e17

Please sign in to comment.