[Flashlight] Added Settings for flashlight
Added some settings for the flashlight functionality.
This commit is contained in:
@@ -1,16 +1,21 @@
|
|||||||
package com.aldo.apps.familyhelpers;
|
package com.aldo.apps.familyhelpers;
|
||||||
|
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
|
|
||||||
|
import com.aldo.apps.familyhelpers.utils.FlashlightHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Settings Activity for some app specific settings.
|
* Simple Settings Activity for some app specific settings.
|
||||||
*/
|
*/
|
||||||
public class SettingsActivity extends AppCompatActivity {
|
public class SettingsActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceState) {
|
protected void onCreate(final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.aldo.apps.familyhelpers.utils;
|
package com.aldo.apps.familyhelpers.utils;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.hardware.camera2.CameraAccessException;
|
import android.hardware.camera2.CameraAccessException;
|
||||||
import android.hardware.camera2.CameraCharacteristics;
|
import android.hardware.camera2.CameraCharacteristics;
|
||||||
import android.hardware.camera2.CameraManager;
|
import android.hardware.camera2.CameraManager;
|
||||||
@@ -10,6 +11,9 @@ import android.util.Log;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
|
import androidx.preference.PreferenceManager;
|
||||||
|
|
||||||
|
import com.aldo.apps.familyhelpers.R;
|
||||||
|
|
||||||
import io.reactivex.rxjava3.subjects.BehaviorSubject;
|
import io.reactivex.rxjava3.subjects.BehaviorSubject;
|
||||||
|
|
||||||
@@ -48,6 +52,21 @@ public class FlashlightHelper {
|
|||||||
*/
|
*/
|
||||||
private Integer mCurrentBrightness;
|
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
|
* The {@link CameraManager.TorchCallback} to be invoked when the flashlight status or availability
|
||||||
* changes.
|
* changes.
|
||||||
@@ -79,6 +98,9 @@ public class FlashlightHelper {
|
|||||||
*/
|
*/
|
||||||
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
|
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
|
||||||
public FlashlightHelper(final Context context) {
|
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 = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
|
||||||
mCameraManager.registerTorchCallback(mTorchCallback, null);
|
mCameraManager.registerTorchCallback(mTorchCallback, null);
|
||||||
try {
|
try {
|
||||||
@@ -87,7 +109,9 @@ public class FlashlightHelper {
|
|||||||
if (Boolean.TRUE.equals(characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE))) {
|
if (Boolean.TRUE.equals(characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE))) {
|
||||||
mCameraId = camId;
|
mCameraId = camId;
|
||||||
mMaxBrightness = characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
|
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 + "]");
|
Log.d(TAG, "maxAvailable = [" + mMaxBrightness + "], current = [" + mCurrentBrightness + "]");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -127,6 +151,7 @@ public class FlashlightHelper {
|
|||||||
/**
|
/**
|
||||||
* Helper method to toggle the flashlight to on/off based on the current state.
|
* Helper method to toggle the flashlight to on/off based on the current state.
|
||||||
*/
|
*/
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
|
||||||
public void toggleFlashlight() {
|
public void toggleFlashlight() {
|
||||||
if (mFlashlightStatusObservable.getValue()) {
|
if (mFlashlightStatusObservable.getValue()) {
|
||||||
Log.d(TAG, "toggleFlashlight: Turning off flashlight");
|
Log.d(TAG, "toggleFlashlight: Turning off flashlight");
|
||||||
@@ -156,6 +181,11 @@ public class FlashlightHelper {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
mCameraManager.turnOnTorchWithStrengthLevel(mCameraId, brightnessLevel);
|
mCameraManager.turnOnTorchWithStrengthLevel(mCameraId, brightnessLevel);
|
||||||
|
mCurrentBrightness = brightnessLevel;
|
||||||
|
if (mSettingsStorage.getBoolean(mStoreDefaultValueKey, true)) {
|
||||||
|
mSettingsStorage.edit().putInt(mCurrentValueKey,
|
||||||
|
getPercentageOfValue(mCurrentBrightness, mMaxBrightness)).apply();
|
||||||
|
}
|
||||||
} catch (final CameraAccessException e) {
|
} catch (final CameraAccessException e) {
|
||||||
Log.e("setFlashlightBrightness", "Error setting torch brightness: " + e.getMessage());
|
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.
|
* Turn on the flashlight without changing the brightness level.
|
||||||
*/
|
*/
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
|
||||||
public void turnFlashlightOn() {
|
public void turnFlashlightOn() {
|
||||||
if (mCameraId == null) {
|
if (mCameraId == null) {
|
||||||
Log.e("turnFlashlightOn", "No camera with flash found.");
|
Log.e("turnFlashlightOn", "No camera with flash found.");
|
||||||
@@ -171,7 +202,7 @@ public class FlashlightHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
mCameraManager.setTorchMode(mCameraId, true);
|
mCameraManager.turnOnTorchWithStrengthLevel(mCameraId, mCurrentBrightness);
|
||||||
}catch (final CameraAccessException e){
|
}catch (final CameraAccessException e){
|
||||||
Log.e("turnFlashlightOn", "Error turning torch on: " + e.getMessage());
|
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.
|
* Clean up helper method.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -38,4 +38,9 @@
|
|||||||
<string name="pref_manual_accuracy_title">Hohe Genauigkeit erzwingen</string>
|
<string name="pref_manual_accuracy_title">Hohe Genauigkeit erzwingen</string>
|
||||||
<string name="pref_manual_accuracy_summary_on">Hohe Genauigkeit, leert aber vermutlich den Akku schneller</string>
|
<string name="pref_manual_accuracy_summary_on">Hohe Genauigkeit, leert aber vermutlich den Akku schneller</string>
|
||||||
<string name="pref_manual_accuracy_summary_off">Niedrigere Genauigkeit, aber bessere Akku-Lebensdauer</string>
|
<string name="pref_manual_accuracy_summary_off">Niedrigere Genauigkeit, aber bessere Akku-Lebensdauer</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_title">Start Helligkeit</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_summary">Die Start Helligkeit die ausgewählt sein soll wenn die Activity gestartet wird, in \\%</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_title">Helligkeit speichern</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_off">Wenn die Taschenlampe gestartet wird, wird die unten gesetzte Standard Helligkeit genommen.</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_on">Die zuletzt gewählte Helligkeit wird gespeichert und beim neustart angewandt</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -38,4 +38,9 @@
|
|||||||
<string name="pref_manual_accuracy_title">Force high accuracy</string>
|
<string name="pref_manual_accuracy_title">Force high accuracy</string>
|
||||||
<string name="pref_manual_accuracy_summary_on">High accuracy for location data, may drain battery faster</string>
|
<string name="pref_manual_accuracy_summary_on">High accuracy for location data, may drain battery faster</string>
|
||||||
<string name="pref_manual_accuracy_summary_off">Lower accuracy for location data, but better battery consumption</string>
|
<string name="pref_manual_accuracy_summary_off">Lower accuracy for location data, but better battery consumption</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_title">Default Brightness</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_summary">The default brightness to be set when starting the activity in \\%</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_title">Remember Brightness</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_off">The default brightness selected below, will be selected every time you start the flashlight</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_on">The last selected brightness will be remembered when starting the flashlight again</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -41,4 +41,8 @@
|
|||||||
<integer name="pref_location_update_freq_max">60</integer>
|
<integer name="pref_location_update_freq_max">60</integer>
|
||||||
<integer name="pref_location_update_freq_increment">1</integer>
|
<integer name="pref_location_update_freq_increment">1</integer>
|
||||||
<integer name="pref_location_update_freq_default">10</integer>
|
<integer name="pref_location_update_freq_default">10</integer>
|
||||||
|
<integer name="pref_flashlight_def_brightness_min">1</integer>
|
||||||
|
<integer name="pref_flashlight_def_brightness_max">100</integer>
|
||||||
|
<integer name="pref_flashlight_def_brightness_default">75</integer>
|
||||||
|
<integer name="pref_flashlight_def_brightness_increment">1</integer>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -54,9 +54,18 @@
|
|||||||
<string name="pref_manual_accuracy_summary_on">High accuracy for location data, may drain battery faster</string>
|
<string name="pref_manual_accuracy_summary_on">High accuracy for location data, may drain battery faster</string>
|
||||||
<string name="pref_manual_accuracy_summary_off">Lower accuracy for location data, but better battery consumption</string>
|
<string name="pref_manual_accuracy_summary_off">Lower accuracy for location data, but better battery consumption</string>
|
||||||
|
|
||||||
|
<!-- Flashlight Preference -->
|
||||||
|
<string name="pref_flashlight_store_brightness_title">Remember Brightness</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_off">The default brightness selected below, will be selected every time you start the flashlight</string>
|
||||||
|
<string name="pref_flashlight_store_brightness_summary_on">The last selected brightness will be remembered when starting the flashlight again</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_title">Default Brightness</string>
|
||||||
|
<string name="pref_flashlight_default_brightness_summary">The default brightness to be set when starting the activity in \%</string>
|
||||||
|
|
||||||
<!-- Preference Keys -->
|
<!-- Preference Keys -->
|
||||||
<string name="pref_key_share_location_freq" translatable="false">location_update_freq</string>
|
<string name="pref_key_share_location_freq" translatable="false">location_update_freq</string>
|
||||||
<string name="pref_key_share_location_auto_acc" translatable="false">auto_accuracy</string>
|
<string name="pref_key_share_location_auto_acc" translatable="false">auto_accuracy</string>
|
||||||
<string name="pref_key_share_location_man_acc" translatable="false">manual_accuracy</string>
|
<string name="pref_key_share_location_man_acc" translatable="false">manual_accuracy</string>
|
||||||
|
<string name="pref_key_flashlight_store_brightness" translatable="false">store_last_brightness</string>
|
||||||
|
<string name="pref_key_flashlight_default_brightness" translatable="false">flashlight_default_brightness</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -30,4 +30,27 @@
|
|||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
<PreferenceCategory app:title="@string/title_flashlight">
|
||||||
|
|
||||||
|
<SwitchPreference
|
||||||
|
app:key="@string/pref_key_flashlight_store_brightness"
|
||||||
|
app:disableDependentsState="true"
|
||||||
|
app:defaultValue="true"
|
||||||
|
app:summaryOff="@string/pref_flashlight_store_brightness_summary_off"
|
||||||
|
app:summaryOn="@string/pref_flashlight_store_brightness_summary_on"
|
||||||
|
app:title="@string/pref_flashlight_store_brightness_title" />
|
||||||
|
|
||||||
|
<SeekBarPreference
|
||||||
|
app:dependency="@string/pref_key_flashlight_store_brightness"
|
||||||
|
app:key="@string/pref_key_flashlight_default_brightness"
|
||||||
|
app:title="@string/pref_flashlight_default_brightness_title"
|
||||||
|
app:summary="@string/pref_flashlight_default_brightness_summary"
|
||||||
|
app:min="@integer/pref_flashlight_def_brightness_min"
|
||||||
|
android:max="@integer/pref_flashlight_def_brightness_max"
|
||||||
|
app:defaultValue="@integer/pref_flashlight_def_brightness_default"
|
||||||
|
app:seekBarIncrement="@integer/pref_flashlight_def_brightness_increment"
|
||||||
|
app:showSeekBarValue="true"/>
|
||||||
|
|
||||||
|
</PreferenceCategory>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
Reference in New Issue
Block a user