[Flashlight] Added custom color functionality

Added a new button and setting to make use of a custom
color for the on-screen flashlight.
This commit is contained in:
Alexander Dörflinger
2025-04-08 13:21:26 +02:00
parent 2a02a22001
commit fdb2273af1
9 changed files with 178 additions and 86 deletions

View File

@@ -39,6 +39,9 @@ dependencies {
implementation 'androidx.car.app:app:1.4.0'
implementation 'androidx.concurrent:concurrent-futures:1.2.0'
// Color Picker Preference
implementation 'com.github.martin-stone:hsv-alpha-color-picker-android:3.1.0'
//Google Maps SDK
implementation 'com.google.android.gms:play-services-maps:19.1.0'
implementation 'com.google.android.gms:play-services-location:21.3.0'

View File

@@ -5,15 +5,19 @@ import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.ColorUtils;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.preference.PreferenceManager;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.aldo.apps.familyhelpers.ui.FlashLightLevelShifter;
@@ -32,6 +36,17 @@ public class FlashlightActivity extends AppCompatActivity {
*/
private static final String TAG = "FlashlightActivity";
/**
* Value to determine whether a color is light or dark. A luminance bigger than 0.5 will be considered
* light.
*/
private static double COLOR_LUMINANCE_VALUE = 0.5;
/**
* SharedPreferences instance for accessing user preferences.
*/
private SharedPreferences mPreferences;
/**
* {@link ImageView} indicating the current status of the flashlight.
*/
@@ -64,6 +79,7 @@ public class FlashlightActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mFullscreenController = WindowCompat.getInsetsController(getWindow(),
getWindow().getDecorView());
mBrightnessController = new DisplayBrightnessController(findViewById(R.id.tv_current_brightness), this);
@@ -116,6 +132,7 @@ public class FlashlightActivity extends AppCompatActivity {
initializeColorButton(R.id.btn_onscreen_flashlight_gray, R.color.flashlight_onscreen_gray, false);
initializeColorButton(R.id.btn_onscreen_flashlight_white, R.color.flashlight_onscreen_white, true);
initializeColorButton(R.id.btn_onscreen_flashlight_yellow, R.color.flashlight_onscreen_yellow, true);
initCustomColorButton();
}
@Override
@@ -160,6 +177,22 @@ public class FlashlightActivity extends AppCompatActivity {
});
}
private void initCustomColorButton() {
final Button customBtn = findViewById(R.id.btn_onscreen_flashlight_custom);
final int backgroundColor = mPreferences.getInt(
getString(R.string.pref_key_on_screen_flashlight_custom_color),
getColor(R.color.md_theme_secondary));
final boolean isLightColor = ColorUtils.calculateLuminance(backgroundColor) > COLOR_LUMINANCE_VALUE;
customBtn.setBackgroundColor(backgroundColor);
customBtn.setTextColor(isLightColor ? getColor(R.color.black) : getColor(R.color.white));
customBtn.setOnClickListener(v -> {
mOnScreenFlashlight.setBackgroundColor(backgroundColor);
mOnScreenFlashlight.setVisibility(View.VISIBLE);
mFullscreenController.hide(WindowInsetsCompat.Type.systemBars());
mBrightnessController.setLightModeColor(isLightColor);
});
}
/**
* Helper method to print any potential errors while subscribing to the current status observable.
*

View File

@@ -3,12 +3,15 @@ package com.aldo.apps.familyhelpers;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.aldo.apps.familyhelpers.utils.FlashlightHelper;
import com.rarepebble.colorpicker.ColorPreference;
/**
* Simple Settings Activity for some app specific settings.
@@ -40,5 +43,14 @@ public class SettingsActivity extends AppCompatActivity {
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
@Override
public void onDisplayPreferenceDialog(@NonNull final Preference preference) {
if (preference instanceof ColorPreference) {
((ColorPreference) preference).showDialog(this, 0);
} else {
super.onDisplayPreferenceDialog(preference);
}
}
}
}

View File

@@ -18,102 +18,125 @@
android:layout_marginTop="@dimen/flashlight_category_header_margin_top"
android:layout_marginBottom="@dimen/flashlight_category_header_margin_bottom"/>
<Button
android:id="@+id/btn_onscreen_flashlight_red"
<ScrollView
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
android:layout_height="0dp"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_red"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_red"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/tv_onscreen_flashlight"/>
app:layout_constraintTop_toBottomOf="@id/tv_onscreen_flashlight"
app:layout_constraintBottom_toTopOf="@id/tv_camera_flashlight_title">
<Button
android:id="@+id/btn_onscreen_flashlight_blue"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/flashlight_on_screen_color_blue"
android:backgroundTint="@color/flashlight_onscreen_blue"
android:textColor="@color/white"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_red"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_onscreen_flashlight_green"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/flashlight_on_screen_color_green"
android:backgroundTint="@color/flashlight_onscreen_green"
android:textColor="@color/white"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_blue"/>
<Button
android:id="@+id/btn_onscreen_flashlight_red"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_red"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_red"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/btn_onscreen_flashlight_orange"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_orange"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_orange"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_green"/>
<Button
android:id="@+id/btn_onscreen_flashlight_blue"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/flashlight_on_screen_color_blue"
android:backgroundTint="@color/flashlight_onscreen_blue"
android:textColor="@color/white"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_red"/>
<Button
android:id="@+id/btn_onscreen_flashlight_yellow"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_yellow"
android:textColor="@color/black"
android:text="@string/flashlight_on_screen_color_yellow"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_orange"/>
<Button
android:id="@+id/btn_onscreen_flashlight_green"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/flashlight_on_screen_color_green"
android:backgroundTint="@color/flashlight_onscreen_green"
android:textColor="@color/white"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_blue"/>
<Button
android:id="@+id/btn_onscreen_flashlight_gray"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_gray"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_gray"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_yellow"/>
<Button
android:id="@+id/btn_onscreen_flashlight_orange"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_orange"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_orange"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_green"/>
<Button
android:id="@+id/btn_onscreen_flashlight_white"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_white"
android:textColor="@color/black"
android:text="@string/flashlight_on_screen_color_white"
android:layout_marginHorizontal="@dimen/flashlight_onscreen_button_margin_horizontal"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toStartOf="@id/flashlight_level_shifter"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_gray"/>
<Button
android:id="@+id/btn_onscreen_flashlight_yellow"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_yellow"
android:textColor="@color/black"
android:text="@string/flashlight_on_screen_color_yellow"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_orange"/>
<Button
android:id="@+id/btn_onscreen_flashlight_gray"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_gray"
android:textColor="@color/white"
android:text="@string/flashlight_on_screen_color_gray"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_yellow"/>
<Button
android:id="@+id/btn_onscreen_flashlight_white"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/flashlight_onscreen_white"
android:textColor="@color/black"
android:text="@string/flashlight_on_screen_color_white"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_gray"/>
<Button
android:id="@+id/btn_onscreen_flashlight_custom"
android:layout_width="0dp"
android:layout_height="@dimen/flashlight_onscreen_button_layout_height"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/flashlight_on_screen_custom_color"
android:layout_marginTop="@dimen/flashlight_onscreen_button_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_onscreen_flashlight_white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<ImageView
android:id="@+id/flashlight_status_indicator"
@@ -181,6 +204,7 @@
android:text="@string/flashlight_set_max_level"/>
<TextView
android:id="@+id/tv_camera_flashlight_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/flashlight_camera_title"

View File

@@ -29,6 +29,7 @@
<string name="flashlight_set_min_level">Aktiviere die Taschenlampe auf minimaler Helligkeit</string>
<string name="flashlight_set_med_level">Aktiviere die Taschenlampe auf mittlerer Helligkeit</string>
<string name="flashlight_set_max_level">Aktiviere die Taschenlampe auf maximaler Helligkeit</string>
<string name="flashlight_on_screen_title">Bildschirm Taschenlampe</string>
<string name="flashlight_on_screen_color_red">Roter Bildschirm</string>
<string name="flashlight_on_screen_color_blue">Blauer Bildschirm</string>
<string name="flashlight_on_screen_color_green">Grüner Bildschirm</string>
@@ -56,6 +57,7 @@
<string name="pref_on_screen_flashlight_sensitivity_summary">Die Empfindlichkeit mit der auf Swipe Gesten reagiert wird</string>
<string name="pref_on_screen_flashlight_step_size_title">Schrittgröße</string>
<string name="pref_on_screen_flashlight_step_size_summary">Die Größe der Schritte mit der die Helligkeit verändert werden soll</string>
<string name="pref_on_screen_flashlight_custom_color_title">Eigene Farbe</string>
<string name="android_auto_message_currently_sharing">Du teilst bereits deinen Standort mit der Familie, willst du aufhören?</string>
<string name="android_auto_message_not_sharing">Android Auto wurde gestartet, willst du deinen Standort teilen?</string>
<string name="android_auto_actions_start_sharing">Ja, fang an zu teilen</string>
@@ -64,4 +66,6 @@
<string name="android_auto_share_location_start_desc">Anfangen den Standort zu teilen</string>
<string name="android_auto_share_location_stop">Stop</string>
<string name="android_auto_share_location_stop_desc">Aufhören den Standort zu teilen</string>
<string name="pref_on_screen_flashlight_custom_color_summary">Wähle eine eigene Farbe für deine Bildschirm-Taschenlampe</string>
<string name="flashlight_on_screen_custom_color">Eigene Farbe</string>
</resources>

View File

@@ -29,6 +29,7 @@
<string name="flashlight_set_min_level">Activate flashlight on minimum brightness</string>
<string name="flashlight_set_med_level">Activate flashlight on medium brightness</string>
<string name="flashlight_set_max_level">Activate flashlight on maximum brightness</string>
<string name="flashlight_on_screen_title">On-Screen Flashlight</string>
<string name="flashlight_on_screen_color_red">Red On-Screen</string>
<string name="flashlight_on_screen_color_blue">Blue On-Screen</string>
<string name="flashlight_on_screen_color_green">Green On-Screen</string>
@@ -56,6 +57,7 @@
<string name="pref_on_screen_flashlight_sensitivity_summary">The sensitivity of the swipe gesture on the on-screen flashlight</string>
<string name="pref_on_screen_flashlight_step_size_title">Swipe Step Size</string>
<string name="pref_on_screen_flashlight_step_size_summary">The change step size of the on-screen swipe gesture</string>
<string name="pref_on_screen_flashlight_custom_color_title">Custom color</string>
<string name="android_auto_message_currently_sharing">You are already sharing your location with your family, do you wanna stop?</string>
<string name="android_auto_message_not_sharing">Android Auto start has been detected, do you wanna share your location?</string>
<string name="android_auto_actions_start_sharing">Yes, start to share</string>
@@ -64,4 +66,6 @@
<string name="android_auto_share_location_start_desc">Start to share location</string>
<string name="android_auto_share_location_stop">Stop</string>
<string name="android_auto_share_location_stop_desc">Stop to share the location</string>
<string name="pref_on_screen_flashlight_custom_color_summary">Choose an own color to be displayed in the on-screen flashlight.</string>
<string name="flashlight_on_screen_custom_color">Custom Color</string>
</resources>

View File

@@ -49,6 +49,7 @@
<string name="flashlight_on_screen_color_white">White On-Screen</string>
<string name="flashlight_on_screen_color_gray">Gray On-Screen</string>
<string name="flashlight_on_screen_brightness_base_text">Brightness: %d</string>
<string name="flashlight_on_screen_custom_color">Custom Color</string>
<!-- Settings -->
<string name="title_settings">Settings</string>
@@ -74,6 +75,8 @@
<string name="pref_on_screen_flashlight_sensitivity_summary">The sensitivity of the swipe gesture on the on-screen flashlight</string>
<string name="pref_on_screen_flashlight_step_size_title">Swipe Step Size</string>
<string name="pref_on_screen_flashlight_step_size_summary">The change step size of the on-screen swipe gesture</string>
<string name="pref_on_screen_flashlight_custom_color_title">Custom color</string>
<string name="pref_on_screen_flashlight_custom_color_summary">Choose an own color to be displayed in the on-screen flashlight.</string>
<!-- Preference Keys -->
<string name="pref_key_share_location_freq" translatable="false">location_update_freq</string>
@@ -81,8 +84,9 @@
<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>
<string name="pref_key_on_screen_flashlight_sensitivity" translatable="false">on_screen_flash_sensititvity</string>
<string name="pref_key_on_screen_flashlight_sensitivity" translatable="false">on_screen_flash_sensitivity</string>
<string name="pref_key_on_screen_flashlight_step_size" translatable="false">on_screen_flash_step_size</string>
<string name="pref_key_on_screen_flashlight_custom_color" translatable="false">on_screen_flash_custom_color</string>
<!-- Android Auto related Strings -->
<string name="android_auto_message_currently_sharing">You are already sharing your location with your family, do you wanna stop?</string>

View File

@@ -73,6 +73,13 @@
app:defaultValue="@integer/pref_on_screen_flashlight_step_size_def"
app:summary="@string/pref_on_screen_flashlight_step_size_summary"/>
<com.rarepebble.colorpicker.ColorPreference
android:key="@string/pref_key_on_screen_flashlight_custom_color"
android:title="@string/pref_on_screen_flashlight_custom_color_title"
app:summary="@string/pref_on_screen_flashlight_custom_color_summary"
app:colorpicker_showAlpha="false"
android:defaultValue="@color/md_theme_primary"/>
</PreferenceCategory>
</PreferenceCategory>

View File

@@ -8,5 +8,6 @@ allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}