From fdb2273af10149cfae7521ad9e9edaf6d67f6709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=B6rflinger?= Date: Tue, 8 Apr 2025 13:21:26 +0200 Subject: [PATCH] [Flashlight] Added custom color functionality Added a new button and setting to make use of a custom color for the on-screen flashlight. --- app/build.gradle | 3 + .../familyhelpers/FlashlightActivity.java | 33 +++ .../apps/familyhelpers/SettingsActivity.java | 12 ++ .../main/res/layout/activity_flashlight.xml | 194 ++++++++++-------- app/src/main/res/values-de/strings.xml | 4 + app/src/main/res/values-en/strings.xml | 4 + app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/root_preferences.xml | 7 + build.gradle | 1 + 9 files changed, 178 insertions(+), 86 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 77431a5..a639ca8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/aldo/apps/familyhelpers/FlashlightActivity.java b/app/src/main/java/com/aldo/apps/familyhelpers/FlashlightActivity.java index 6b10fef..fdc92c3 100644 --- a/app/src/main/java/com/aldo/apps/familyhelpers/FlashlightActivity.java +++ b/app/src/main/java/com/aldo/apps/familyhelpers/FlashlightActivity.java @@ -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. * 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 17e2726..e21e549 100644 --- a/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java +++ b/app/src/main/java/com/aldo/apps/familyhelpers/SettingsActivity.java @@ -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); + } + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_flashlight.xml b/app/src/main/res/layout/activity_flashlight.xml index 0591abf..43bd5c1 100644 --- a/app/src/main/res/layout/activity_flashlight.xml +++ b/app/src/main/res/layout/activity_flashlight.xml @@ -18,102 +18,125 @@ android:layout_marginTop="@dimen/flashlight_category_header_margin_top" android:layout_marginBottom="@dimen/flashlight_category_header_margin_bottom"/> -