diff --git a/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java b/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java
index 6ef3481..17e2726 100644
--- a/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java
+++ b/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java
@@ -1,16 +1,21 @@
package com.aldo.apps.familyhelpers;
+import android.os.Build;
import android.os.Bundle;
+import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
+import com.aldo.apps.familyhelpers.utils.FlashlightHelper;
+
/**
* Simple Settings Activity for some app specific settings.
*/
public class SettingsActivity extends AppCompatActivity {
+ @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
diff --git a/app/src/main/java/com/aldo/apps/familyhelpers/utils/FlashlightHelper.java b/app/src/main/java/com/aldo/apps/familyhelpers/utils/FlashlightHelper.java
index d979a4d..02578d0 100644
--- a/app/src/main/java/com/aldo/apps/familyhelpers/utils/FlashlightHelper.java
+++ b/app/src/main/java/com/aldo/apps/familyhelpers/utils/FlashlightHelper.java
@@ -1,6 +1,7 @@
package com.aldo.apps.familyhelpers.utils;
import android.content.Context;
+import android.content.SharedPreferences;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
@@ -10,6 +11,9 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
+import androidx.preference.PreferenceManager;
+
+import com.aldo.apps.familyhelpers.R;
import io.reactivex.rxjava3.subjects.BehaviorSubject;
@@ -48,6 +52,21 @@ public class FlashlightHelper {
*/
private Integer mCurrentBrightness;
+ /**
+ * The {@link SharedPreferences} holding the app settings.
+ */
+ private final SharedPreferences mSettingsStorage;
+
+ /**
+ * The key of the settings value for storing the last known brightness level.
+ */
+ private final String mStoreDefaultValueKey;
+
+ /**
+ * The key of the settings value for the currently selected brightness level.
+ */
+ private final String mCurrentValueKey;
+
/**
* The {@link CameraManager.TorchCallback} to be invoked when the flashlight status or availability
* changes.
@@ -79,6 +98,9 @@ public class FlashlightHelper {
*/
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
public FlashlightHelper(final Context context) {
+ mSettingsStorage = PreferenceManager.getDefaultSharedPreferences(context);
+ mStoreDefaultValueKey = context.getString(R.string.pref_key_flashlight_store_brightness);
+ mCurrentValueKey = context.getString(R.string.pref_key_flashlight_default_brightness);
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mCameraManager.registerTorchCallback(mTorchCallback, null);
try {
@@ -87,7 +109,9 @@ public class FlashlightHelper {
if (Boolean.TRUE.equals(characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE))) {
mCameraId = camId;
mMaxBrightness = characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
- mCurrentBrightness = characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
+ mCurrentBrightness = getValueFromPercentage(mSettingsStorage.getInt(mCurrentValueKey,
+ context.getResources().getInteger(R.integer.pref_flashlight_def_brightness_default)),
+ mMaxBrightness);
Log.d(TAG, "maxAvailable = [" + mMaxBrightness + "], current = [" + mCurrentBrightness + "]");
break;
}
@@ -127,6 +151,7 @@ public class FlashlightHelper {
/**
* Helper method to toggle the flashlight to on/off based on the current state.
*/
+ @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
public void toggleFlashlight() {
if (mFlashlightStatusObservable.getValue()) {
Log.d(TAG, "toggleFlashlight: Turning off flashlight");
@@ -156,6 +181,11 @@ public class FlashlightHelper {
try {
mCameraManager.turnOnTorchWithStrengthLevel(mCameraId, brightnessLevel);
+ mCurrentBrightness = brightnessLevel;
+ if (mSettingsStorage.getBoolean(mStoreDefaultValueKey, true)) {
+ mSettingsStorage.edit().putInt(mCurrentValueKey,
+ getPercentageOfValue(mCurrentBrightness, mMaxBrightness)).apply();
+ }
} catch (final CameraAccessException e) {
Log.e("setFlashlightBrightness", "Error setting torch brightness: " + e.getMessage());
}
@@ -164,6 +194,7 @@ public class FlashlightHelper {
/**
* Turn on the flashlight without changing the brightness level.
*/
+ @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
public void turnFlashlightOn() {
if (mCameraId == null) {
Log.e("turnFlashlightOn", "No camera with flash found.");
@@ -171,7 +202,7 @@ public class FlashlightHelper {
}
try{
- mCameraManager.setTorchMode(mCameraId, true);
+ mCameraManager.turnOnTorchWithStrengthLevel(mCameraId, mCurrentBrightness);
}catch (final CameraAccessException e){
Log.e("turnFlashlightOn", "Error turning torch on: " + e.getMessage());
}
@@ -193,6 +224,36 @@ public class FlashlightHelper {
}
}
+ /**
+ * Calculates the percentage of a given value relative to a maximum value.
+ *
+ * @param value The value for which to calculate the percentage.
+ * @param max The maximum value.
+ * @return The percentage, as an integer, between 0 and 100.
+ */
+ public int getPercentageOfValue(final int value, final int max) {
+ if (max == 0) {
+ return 0; // Avoid division by zero
+ }
+ return Math.round(100.0f * value / max);
+ }
+
+ /**
+ * Calculates the value based on a given percentage and a maximum value.
+ *
+ * @param percentage The percentage (0-100).
+ * @param max The maximum value.
+ * @return The calculated value, as an integer.
+ */
+ public int getValueFromPercentage(final int percentage, final int max) {
+ if (percentage < 0) {
+ return 0;
+ } else if (percentage > 100) {
+ return max;
+ }
+ return Math.round(max * (percentage / 100.0f));
+ }
+
/**
* Clean up helper method.
*/
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 1387032..1a86218 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -38,4 +38,9 @@
Hohe Genauigkeit erzwingen
Hohe Genauigkeit, leert aber vermutlich den Akku schneller
Niedrigere Genauigkeit, aber bessere Akku-Lebensdauer
+ Start Helligkeit
+ Die Start Helligkeit die ausgewählt sein soll wenn die Activity gestartet wird, in \\%
+ Helligkeit speichern
+ Wenn die Taschenlampe gestartet wird, wird die unten gesetzte Standard Helligkeit genommen.
+ Die zuletzt gewählte Helligkeit wird gespeichert und beim neustart angewandt
\ No newline at end of file
diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml
index fad766b..f747832 100644
--- a/app/src/main/res/values-en/strings.xml
+++ b/app/src/main/res/values-en/strings.xml
@@ -38,4 +38,9 @@
Force high accuracy
High accuracy for location data, may drain battery faster
Lower accuracy for location data, but better battery consumption
+ Default Brightness
+ The default brightness to be set when starting the activity in \\%
+ Remember Brightness
+ The default brightness selected below, will be selected every time you start the flashlight
+ The last selected brightness will be remembered when starting the flashlight again
\ No newline at end of file
diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml
index 8c7e7c4..1dc8fb3 100644
--- a/app/src/main/res/values/dimens.xml
+++ b/app/src/main/res/values/dimens.xml
@@ -41,4 +41,8 @@
60
1
10
+ 1
+ 100
+ 75
+ 1
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3258fad..50a1d56 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -54,9 +54,18 @@
High accuracy for location data, may drain battery faster
Lower accuracy for location data, but better battery consumption
+
+ Remember Brightness
+ The default brightness selected below, will be selected every time you start the flashlight
+ The last selected brightness will be remembered when starting the flashlight again
+ Default Brightness
+ The default brightness to be set when starting the activity in \%
+
location_update_freq
auto_accuracy
manual_accuracy
+ store_last_brightness
+ flashlight_default_brightness
\ No newline at end of file
diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml
index 7870667..a04ef30 100644
--- a/app/src/main/res/xml/root_preferences.xml
+++ b/app/src/main/res/xml/root_preferences.xml
@@ -30,4 +30,27 @@
+
+
+
+
+
+
+
+
\ No newline at end of file