Skip to content

Commit

Permalink
Added speeding alert option
Browse files Browse the repository at this point in the history
  • Loading branch information
ethandudu committed Jul 23, 2024
1 parent c60fc6e commit 74253fa
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 18 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/fr/ethanduault/younglimit/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.PictureDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.Manifest;
import android.location.Location;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class MainActivity extends AppCompatActivity {
private double speed = 0.0;

private int refreshDelay;
private boolean speedAlert = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -111,7 +113,7 @@ private void speedLoop() {
@Override
public void run() {
setSpeed(speed, speedLimit);
handler.postDelayed(this, 500);
handler.postDelayed(this, 1000);
}
}, 500);
}
Expand Down Expand Up @@ -149,6 +151,11 @@ private void setSpeed(double speed, int speedLimit) {
}
if (speed > speedLimit) {
speedBar.setProgress(100);
if (speedAlert) {
//play sound
final MediaPlayer player = MediaPlayer.create(this, R.raw.system_notification_4_universfield);
player.start();
}
} else {
speedBar.setProgress((int) ((speed / speedLimit) * 100));
}
Expand Down Expand Up @@ -265,6 +272,10 @@ private boolean loadSettings() {
startActivity(intent);
return false;
}

if (preferences.getBoolean("speedAlert", false)) {
speedAlert = true;
}
refreshDelay = preferences.getInt("refresh", 5) * 1000;
setTheme(preferences.getBoolean("darkmode", false));
return true;
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/fr/ethanduault/younglimit/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onAboutClick(View view) {
} catch (PackageManager.NameNotFoundException e){
e.printStackTrace();
}
builder.setMessage(appVersion + "\n\nApplication développée par Ethan Duault et la communauté\n\nBasée sur OpenStreetMap et l'API Overpass\n\nInformations données à titre indicatif uniquement");
builder.setMessage(appVersion + getString(R.string.about_text));
builder.setPositiveButton("OK", null);
builder.show();
}
Expand All @@ -61,6 +61,11 @@ private void loadSettings() {
darkmode.setChecked(true);
}

if (preferences.getBoolean("speedAlert", false)) {
SwitchMaterial speedAlert = findViewById(R.id.speedwarning);
speedAlert.setChecked(true);
}

if (preferences.getInt("refresh", 0) != 0){
Slider slider = findViewById(R.id.slider);
slider.setValue(preferences.getInt("refresh", 0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
package fr.ethanduault.younglimit.fragments;

import static android.os.Build.VERSION.SDK_INT;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.slider.Slider;
import com.google.android.material.switchmaterial.SwitchMaterial;

import fr.ethanduault.younglimit.FirstStart;
import fr.ethanduault.younglimit.R;

public class FragmentSettings extends Fragment {
Expand All @@ -43,6 +36,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
SwitchMaterial switchPermission= view.findViewById(R.id.permissions);

SwitchMaterial darkmode = view.findViewById(R.id.darkmode);
SwitchMaterial speedAlert = view.findViewById(R.id.speedalert);

Slider slider = view.findViewById(R.id.slider);

Expand Down Expand Up @@ -76,6 +70,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
SharedPreferences sharedPreferences = requireActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("darkmode", darkmode.isChecked());
editor.putBoolean("speedAlert", speedAlert.isChecked());
editor.putBoolean("isFirstLaunch", false);
editor.putInt("refreshDelay", (int) slider.getValue());
editor.apply();
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,43 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appName" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/sliderLabel"
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/speedwarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:labelFor="@id/slider"
android:text="@string/refresh_rate"
android:layout_marginTop="32dp"
android:text="@string/speedwarning"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/darkmode" />

<com.google.android.material.slider.Slider
android:id="@+id/slider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_marginTop="12dp"
android:layout_marginTop="20dp"
android:stepSize="1"
android:value="5"
android:valueFrom="1"
android:valueTo="20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sliderLabel" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/sliderLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:labelFor="@id/slider"
android:text="@string/refresh_rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/speedwarning" />

<Button
android:id="@+id/about"
android:layout_width="wrap_content"
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/settings" />

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/speedalert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/speedwarning"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/darkmode" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/sliderLabel"
android:layout_width="wrap_content"
Expand All @@ -68,7 +79,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/darkmode" />
app:layout_constraintTop_toBottomOf="@+id/speedalert" />

<com.google.android.material.slider.Slider
android:id="@+id/slider"
Expand Down
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
<string name="permissions">Permissions</string>
<string name="permission_already_granted">Permission already granted</string>
<string name="unknown_speed">0 km/h</string>
<string name="speedwarning">Speed ​​limit exceeded alert</string>
<string name="about_text">Application developed by Ethan Duault and the community \nBased on OpenStreetMap and the Overpass API \nSound Effect by UNIVERSFIELD from Pixabay\nInformation given for informational purposes only</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
<string name="permissions">Permissions</string>
<string name="permission_already_granted">Permission déjà accordée</string>
<string name="unknown_speed">0 km/h</string>
<string name="speedwarning">Alerte de dépassement de la limiation de vitesse</string>
<string name="about_text">" Application développée par Ethan Duault et la communauté \nBasée sur OpenStreetMap et l'API Overpass \nSound Effect by UNIVERSFIELD from Pixabay \nInformations données à titre indicatif uniquement"</string>
</resources>

0 comments on commit 74253fa

Please sign in to comment.