2016-10-13 16:56:31 +02:00
|
|
|
/*
|
|
|
|
This file is part of the Diaspora for Android.
|
|
|
|
|
|
|
|
Diaspora for Android is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Diaspora for Android is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with the Diaspora for Android.
|
|
|
|
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
This class is inspired by org.horasapps.LeafPic
|
|
|
|
*/
|
2016-10-26 16:23:14 +02:00
|
|
|
package com.github.dfa.diaspora_android.ui.theme;
|
2016-10-13 16:56:31 +02:00
|
|
|
|
2016-11-06 16:23:18 +01:00
|
|
|
import android.content.DialogInterface;
|
2016-10-22 23:30:22 +02:00
|
|
|
import android.content.res.ColorStateList;
|
|
|
|
import android.graphics.Color;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.graphics.PorterDuff;
|
2016-10-22 23:30:22 +02:00
|
|
|
import android.os.Build;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.support.design.widget.TabLayout;
|
2016-10-22 23:30:22 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v4.widget.CompoundButtonCompat;
|
2016-11-06 16:23:18 +01:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.support.v7.widget.ActionMenuView;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.View;
|
2016-11-06 16:23:18 +01:00
|
|
|
import android.widget.Button;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ProgressBar;
|
2016-10-22 23:30:22 +02:00
|
|
|
import android.widget.RadioButton;
|
|
|
|
import android.widget.RadioGroup;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-10-22 23:30:22 +02:00
|
|
|
import com.github.dfa.diaspora_android.R;
|
2016-10-26 16:23:14 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.AppSettings;
|
2016-10-13 16:56:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton that can be used to color views
|
|
|
|
* Created by vanitas on 06.10.16.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ThemeHelper {
|
|
|
|
private AppSettings appSettings;
|
|
|
|
private static ThemeHelper instance;
|
|
|
|
|
|
|
|
private ThemeHelper(AppSettings appSettings) {
|
|
|
|
this.appSettings = appSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ThemeHelper getInstance(AppSettings appSettings) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (instance == null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
instance = new ThemeHelper(appSettings);
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ThemeHelper getInstance() {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (instance == null)
|
|
|
|
throw new IllegalStateException("ThemeHelper must be initialized using getInstance(AppSettings) before it can be used!");
|
2016-10-13 16:56:31 +02:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateEditTextColor(EditText editText) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (editText != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
editText.setHighlightColor(getInstance().appSettings.getAccentColor());
|
2016-10-26 16:23:14 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
2016-10-22 23:30:22 +02:00
|
|
|
editText.getBackground().mutate().setColorFilter(getAccentColor(), PorterDuff.Mode.SRC_ATOP);
|
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateCheckBoxColor(CheckBox checkBox) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (checkBox != null) {
|
2016-10-22 23:30:22 +02:00
|
|
|
int states[][] = {{android.R.attr.state_checked}, {}};
|
|
|
|
int colors[] = {ThemeHelper.getAccentColor(), getNeutralGreyColor()};
|
|
|
|
CompoundButtonCompat.setButtonTintList(checkBox, new ColorStateList(states, colors));
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateTabLayoutColor(TabLayout tabLayout) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (tabLayout != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
tabLayout.setBackgroundColor(getInstance().appSettings.getPrimaryColor());
|
|
|
|
tabLayout.setSelectedTabIndicatorColor(getInstance().appSettings.getAccentColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:04:32 +02:00
|
|
|
public static void updateTextViewLinkColor(TextView textView) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (textView != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
textView.setHighlightColor(getInstance().appSettings.getAccentColor());
|
|
|
|
textView.setLinkTextColor(getInstance().appSettings.getAccentColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:04:32 +02:00
|
|
|
public static void updateTextViewTextColor(TextView textView) {
|
2016-10-26 16:23:14 +02:00
|
|
|
if (textView != null) {
|
2016-10-25 17:04:32 +02:00
|
|
|
textView.setTextColor(getInstance().appSettings.getAccentColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
public static void updateToolbarColor(Toolbar toolbar) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (toolbar != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
toolbar.setBackgroundColor(getInstance().appSettings.getPrimaryColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateActionMenuViewColor(ActionMenuView actionMenuView) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (actionMenuView != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
actionMenuView.setBackgroundColor(getInstance().appSettings.getPrimaryColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getPrimaryColor() {
|
|
|
|
return getInstance().appSettings.getPrimaryColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getAccentColor() {
|
|
|
|
return getInstance().appSettings.getAccentColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setPrimaryColorAsBackground(View view) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (view != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
view.setBackgroundColor(getPrimaryColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getPrimaryDarkColor() {
|
|
|
|
return ColorPalette.getObscuredColor(getPrimaryColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateProgressBarColor(ProgressBar progressBar) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (progressBar != null && progressBar.getProgressDrawable() != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
progressBar.getProgressDrawable().setColorFilter(getAccentColor(), PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 23:30:22 +02:00
|
|
|
|
|
|
|
public static void updateRadioGroupColor(RadioGroup radioGroup) {
|
2016-10-26 16:23:14 +02:00
|
|
|
if (radioGroup != null && Build.VERSION.SDK_INT >= 21) {
|
2016-10-22 23:30:22 +02:00
|
|
|
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
|
|
|
|
RadioButton btn = ((RadioButton) radioGroup.getChildAt(i));
|
|
|
|
btn.setButtonTintList(new ColorStateList(
|
2016-10-26 16:23:14 +02:00
|
|
|
new int[][]{new int[]{-android.R.attr.state_enabled}, new int[]{android.R.attr.state_enabled}},
|
|
|
|
new int[]{Color.BLACK, ThemeHelper.getAccentColor()}));
|
2016-10-22 23:30:22 +02:00
|
|
|
btn.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getNeutralGreyColor() {
|
|
|
|
return ContextCompat.getColor(getInstance().appSettings.getApplicationContext(), R.color.md_grey_800);
|
|
|
|
}
|
2016-11-06 16:23:18 +01:00
|
|
|
|
|
|
|
public static void updateAlertDialogColor(AlertDialog alertDialog) {
|
2016-12-18 15:07:23 +01:00
|
|
|
if (alertDialog != null) {
|
|
|
|
for (int i : new int[]{
|
2016-11-06 16:23:18 +01:00
|
|
|
DialogInterface.BUTTON_POSITIVE,
|
|
|
|
DialogInterface.BUTTON_NEUTRAL,
|
|
|
|
DialogInterface.BUTTON_NEGATIVE}) {
|
|
|
|
Button b = alertDialog.getButton(i);
|
2016-12-18 15:07:23 +01:00
|
|
|
if (b != null) {
|
2016-11-06 16:23:18 +01:00
|
|
|
b.setTextColor(getAccentColor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 16:21:06 +01:00
|
|
|
|
|
|
|
public static void updateButtonTextColor(Button button) {
|
|
|
|
if (button != null) {
|
|
|
|
button.setTextColor(getAccentColor());
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|