Skip to content

Commit

Permalink
Merge pull request #223 from teixeira0x/development
Browse files Browse the repository at this point in the history
feat: update main activity layout
  • Loading branch information
CrazyMarvin authored Apr 27, 2024
2 parents 15ecf73 + 3b4ddaf commit 9354527
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package rocks.poopjournal.flashy.activities;

import android.animation.LayoutTransition;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.google.android.material.color.MaterialColors;
import com.google.android.material.slider.Slider;
import com.skydoves.colorpickerview.ColorPickerView;
import com.skydoves.colorpickerview.listeners.ColorListener;

import me.tankery.lib.circularseekbar.CircularSeekBar;

import rocks.poopjournal.flashy.NoFlashlightDialog;
import rocks.poopjournal.flashy.R;
import rocks.poopjournal.flashy.databinding.MainActivityBinding;
Expand All @@ -38,7 +37,7 @@

public class MainActivity extends AppCompatActivity {
//Fields
RelativeLayout root_layout ;
private int invertedBackgroundColor = 0;
private int brightness = -999;
private Window window;
private SharedPreferences legacyPreferences; //kept for legacy reasons
Expand Down Expand Up @@ -100,7 +99,7 @@ protected void onCreate(Bundle savedInstanceState) {
legacyPreferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
applyListeners();
init();
if (savedInstanceState != null && legacyPreferences.getInt("default_option", 1) == 2) {
if (savedInstanceState != null && !isFlashOption()) {
brightness = savedInstanceState.getInt("brightness");
WindowManager.LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = (float) brightness / 100;
Expand All @@ -109,10 +108,6 @@ protected void onCreate(Bundle savedInstanceState) {

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
getSupportFragmentManager().setFragmentResultListener(NoFlashlightDialog.NO_FLASH_DIALOG_DISMISSED, this, ((requestKey, result) -> binding.bgOptions.callOnClick()));
binding.sosButton.setVisibility(View.GONE);
binding.sosIcon.setVisibility(View.GONE);
binding.stroboscopeButton.setVisibility(View.GONE);
binding.stroboscopeIcon.setVisibility(View.GONE);
binding.stroboscopeInterval.setVisibility(View.GONE);
binding.stroboscopeIntervalSlider.setVisibility(View.GONE);
} else {
Expand All @@ -123,8 +118,8 @@ protected void onCreate(Bundle savedInstanceState) {
binding.stroboscopeInterval.setVisibility(isOn ? View.VISIBLE : View.GONE);
binding.stroboscopeIntervalSlider.setVisibility(isOn ? View.VISIBLE : View.GONE);
}));
binding.sosButton.setOnClickListener(v -> helper.toggleSos(this));
binding.stroboscopeButton.setOnClickListener(v -> helper.toggleStroboscope(this));
binding.sosIcon.setOnClickListener(v -> helper.toggleSos(this));
binding.stroboscopeIcon.setOnClickListener(v -> helper.toggleStroboscope(this));
float stroboscopeIntervalInPreferences = defaultPreferences.getFloat("stroboscope_interval", -1);
helper.setStroboscopeInterval(stroboscopeIntervalInPreferences != -1 ? (int) (stroboscopeIntervalInPreferences * 1000) : 500);
binding.stroboscopeIntervalSlider.setValue(stroboscopeIntervalInPreferences != -1 ? stroboscopeIntervalInPreferences : 0.5F);
Expand All @@ -139,13 +134,12 @@ public void onStopTrackingTouch(@NonNull Slider slider) {
}
});
}
root_layout = findViewById(R.id.root_layout);
ColorPickerView colorPickerView = findViewById(R.id.colorPickerView);
colorPickerView.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, boolean fromUser) {

root_layout.setBackgroundColor(color);
updateUIColors(color);
}
});
}
Expand All @@ -162,36 +156,19 @@ protected void onDestroy() {
turnOffFlashlightOnScreenOffReceiver.unregisterWith(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.settings_menu_item) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (id == R.id.about_menu_item) {
startActivity(new Intent(this, AboutActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}

void applyListeners() {
binding.bgOptions.setOnClickListener(view -> {
SharedPreferences.Editor editor = legacyPreferences.edit();
editor.putInt("default_option", legacyPreferences.getInt("default_option", 1) == 1 ? 2 : 1);
editor.putInt("default_option", isFlashOption() ? 2 : 1);
editor.apply();
init();
});
binding.aboutIcon.setOnClickListener(view -> startActivity(new Intent(this, AboutActivity.class)));
binding.settingsIcon.setOnClickListener(view -> startActivity(new Intent(this, SettingsActivity.class)));
}

void init() {
if (legacyPreferences.getInt("default_option", 1) == 1) {
if (isFlashOption()) {
updateOptionsUI(true);
refreshActivityForFlashLight();
} else {
Expand Down Expand Up @@ -235,21 +212,21 @@ public void onStartTrackingTouch(@Nullable CircularSeekBar circularSeekBar) {
binding.progressCircular.setPointerColor(Color.parseColor("#AAAABB"));
binding.powerCenter.setOnClickListener(v -> helper.toggleNormalFlash(this));
}
binding.rootLayout.setBackgroundColor(Color.parseColor("#00000000")); //transparent
binding.colorPickerView.setEnabled(false);
binding.colorPickerView.setVisibility(View.GONE);
updateUIColors(Color.parseColor("#00000000")); //transparent
}

private void changeButtonColors(FlashlightMode mode, boolean isTurnedOn) {
switch (mode) {
case NORMAL:
binding.powerCenter.setColorFilter(isTurnedOn ? Color.parseColor("#28FFB137") : Color.parseColor("#F3F3F7"));
binding.powerCenter.setColorFilter(isTurnedOn ? Color.parseColor("#28FFB137") : invertedBackgroundColor);
binding.powerIcon.setColorFilter(isTurnedOn ? Color.parseColor("#FFB137") : Color.parseColor("#AAAABB"));
break;
case SOS:
binding.sosButton.setColorFilter(isTurnedOn ? Color.parseColor("#28FFB137") : Color.parseColor("#F3F3F7"));
binding.sosIcon.setColorFilter(isTurnedOn ? Color.parseColor("#FFB137") : Color.parseColor("#AAAABB"));
break;
case STROBOSCOPE:
binding.stroboscopeButton.setColorFilter(isTurnedOn ? Color.parseColor("#28FFB137") : Color.parseColor("#F3F3F7"));
binding.stroboscopeIcon.setColorFilter(isTurnedOn ? Color.parseColor("#FFB137") : Color.parseColor("#AAAABB"));
break;
default:
Expand Down Expand Up @@ -281,7 +258,8 @@ void refreshActivityForScreenLight() {
binding.progressCircular.setEnabled(true);
if (defaultPreferences.getBoolean("no_flash_when_screen", true) && getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
helper.turnOffAll(this);
binding.rootLayout.setBackgroundColor(Color.parseColor("#FFFFFF")); //force set white, because it does not make sense for the app to be dark when using screen light
binding.colorPickerView.setEnabled(true);
binding.colorPickerView.setVisibility(View.VISIBLE);
if (binding.progressCircular.getProgress() > 0) {
binding.progressCircular.setOnSeekBarChangeListener(null);
binding.progressCircular.setProgress(0f);
Expand All @@ -306,12 +284,34 @@ public void onStartTrackingTouch(CircularSeekBar seekBar) {
}
});
binding.powerCenter.setOnClickListener(view -> binding.progressCircular.setProgress(brightness != 100 ? 100 : 0));
updateUIColors(Color.parseColor("#FFFFFF")); //force set white, because it does not make sense for the app to be dark when using screen light
}

void updateUIColors(int backgroundColor) {
invertedBackgroundColor = Utils.invertColor(isFlashOption() ? MaterialColors.getColor(this, android.R.attr.colorBackground, 0) : backgroundColor);
PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(invertedBackgroundColor, PorterDuff.Mode.SRC_ATOP);
binding.toolbar.setTitleTextColor(invertedBackgroundColor);
binding.powerCenter.setColorFilter(colorFilter);
binding.bgOptions.getBackground().setColorFilter(colorFilter);
binding.bgFlashlightMode.getBackground().setColorFilter(colorFilter);
binding.aboutIcon.setColorFilter(colorFilter);
binding.settingsIcon.setColorFilter(colorFilter);

binding.rootLayout.setBackgroundColor(backgroundColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(backgroundColor);
window.setNavigationBarColor(backgroundColor);
}
}

boolean isFlashOption() {
return legacyPreferences.getInt("default_option", 1) == 1;
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (legacyPreferences.getInt("default_option", 1) == 2) {
if (!isFlashOption()) {
outState.putInt("brightness", brightness);
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/rocks/poopjournal/flashy/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rocks.poopjournal.flashy.utils;

import android.graphics.Color;
import static android.hardware.Camera.Parameters.FLASH_MODE_AUTO;
import static android.hardware.Camera.Parameters.FLASH_MODE_ON;
import static android.hardware.Camera.Parameters.FLASH_MODE_TORCH;
Expand All @@ -12,6 +13,7 @@
import android.hardware.Camera;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.ColorUtils;
import androidx.preference.PreferenceManager;

import java.util.List;
Expand Down Expand Up @@ -77,4 +79,10 @@ public static void applyThemeFromSettings(Context context) {
context.setTheme(preferences.getBoolean("md3", false) ? R.style.AppTheme_Material3 : R.style.AppTheme);
}

public static int invertColor(int color) {
int red = 255 - Color.red(color);
int green = 255 - Color.green(color);
int blue = 255 - Color.blue(color);
return Color.rgb(red, green, blue);
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24"
android:tint="#89000000">

<path
android:fillColor="@android:color/white"
android:pathData="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0,0 0,2 12A10,10 0,0 0,12 22A10,10 0,0 0,22 12A10,10 0,0 0,12 2M11,17H13V11H11V17Z" />

</vector>
3 changes: 2 additions & 1 deletion app/src/main/res/drawable/ic_rectangle_bg_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
android:viewportHeight="40">
<path
android:pathData="M20,0L68,0A20,20 0,0 1,88 20L88,20A20,20 0,0 1,68 40L20,40A20,20 0,0 1,0 20L0,20A20,20 0,0 1,20 0z"
android:fillColor="#F3F3F7"/>
android:fillColor="#FFFFFF"
android:fillAlpha="0.16"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24"
android:tint="#89000000">

<path
android:fillColor="@android:color/white"
android:pathData="M12,8A4,4 0,0 1,16 12A4,4 0,0 1,12 16A4,4 0,0 1,8 12A4,4 0,0 1,12 8M12,10A2,2 0,0 0,10 12A2,2 0,0 0,12 14A2,2 0,0 0,14 12A2,2 0,0 0,12 10M10,22C9.75,22 9.54,21.82 9.5,21.58L9.13,18.93C8.5,18.68 7.96,18.34 7.44,17.94L4.95,18.95C4.73,19.03 4.46,18.95 4.34,18.73L2.34,15.27C2.21,15.05 2.27,14.78 2.46,14.63L4.57,12.97L4.5,12L4.57,11L2.46,9.37C2.27,9.22 2.21,8.95 2.34,8.73L4.34,5.27C4.46,5.05 4.73,4.96 4.95,5.05L7.44,6.05C7.96,5.66 8.5,5.32 9.13,5.07L9.5,2.42C9.54,2.18 9.75,2 10,2H14C14.25,2 14.46,2.18 14.5,2.42L14.87,5.07C15.5,5.32 16.04,5.66 16.56,6.05L19.05,5.05C19.27,4.96 19.54,5.05 19.66,5.27L21.66,8.73C21.79,8.95 21.73,9.22 21.54,9.37L19.43,11L19.5,12L19.43,13L21.54,14.63C21.73,14.78 21.79,15.05 21.66,15.27L19.66,18.73C19.54,18.95 19.27,19.04 19.05,18.95L16.56,17.95C16.04,18.34 15.5,18.68 14.87,18.93L14.5,21.58C14.46,21.82 14.25,22 14,22H10M11.25,4L10.88,6.61C9.68,6.86 8.62,7.5 7.85,8.39L5.44,7.35L4.69,8.65L6.8,10.2C6.4,11.37 6.4,12.64 6.8,13.8L4.68,15.36L5.43,16.66L7.86,15.62C8.63,16.5 9.68,17.14 10.87,17.38L11.24,20H12.76L13.13,17.39C14.32,17.14 15.37,16.5 16.14,15.62L18.57,16.66L19.32,15.36L17.2,13.81C17.6,12.64 17.6,11.37 17.2,10.2L19.31,8.65L18.56,7.35L16.15,8.39C15.38,7.5 14.32,6.86 13.12,6.62L12.75,4H11.25Z" />

</vector>
Binary file not shown.
Loading

0 comments on commit 9354527

Please sign in to comment.