2017-02-17 13:11:05 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-03-07 18:28:12 +11:00
|
|
|
import android.graphics.Color;
|
2017-02-18 15:10:46 +11:00
|
|
|
import android.graphics.PorterDuff;
|
2017-02-17 13:11:05 +11:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-02-27 16:21:46 +11:00
|
|
|
import android.support.annotation.AttrRes;
|
2017-03-07 18:28:12 +11:00
|
|
|
import android.support.annotation.ColorInt;
|
2017-02-27 16:21:46 +11:00
|
|
|
import android.support.annotation.DrawableRes;
|
2017-02-17 13:11:05 +11:00
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.util.TypedValue;
|
2017-02-18 15:10:46 +11:00
|
|
|
import android.widget.ImageView;
|
2017-02-17 13:11:05 +11:00
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
class ThemeUtils {
|
2017-02-27 16:21:46 +11:00
|
|
|
static Drawable getDrawable(Context context, @AttrRes int attribute,
|
|
|
|
@DrawableRes int fallbackDrawable) {
|
2017-02-17 13:11:05 +11:00
|
|
|
TypedValue value = new TypedValue();
|
2017-02-27 16:21:46 +11:00
|
|
|
@DrawableRes int resourceId;
|
2017-02-17 13:11:05 +11:00
|
|
|
if (context.getTheme().resolveAttribute(attribute, value, true)) {
|
|
|
|
resourceId = value.resourceId;
|
|
|
|
} else {
|
|
|
|
resourceId = fallbackDrawable;
|
|
|
|
}
|
|
|
|
return ContextCompat.getDrawable(context, resourceId);
|
|
|
|
}
|
|
|
|
|
2017-02-27 16:21:46 +11:00
|
|
|
static @DrawableRes int getDrawableId(Context context, @AttrRes int attribute,
|
|
|
|
@DrawableRes int fallbackDrawableId) {
|
2017-02-17 13:11:05 +11:00
|
|
|
TypedValue value = new TypedValue();
|
|
|
|
if (context.getTheme().resolveAttribute(attribute, value, true)) {
|
|
|
|
return value.resourceId;
|
|
|
|
} else {
|
|
|
|
return fallbackDrawableId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 18:28:12 +11:00
|
|
|
static @ColorInt int getColor(Context context, @AttrRes int attribute) {
|
2017-02-17 13:11:05 +11:00
|
|
|
TypedValue value = new TypedValue();
|
|
|
|
if (context.getTheme().resolveAttribute(attribute, value, true)) {
|
|
|
|
return value.data;
|
|
|
|
} else {
|
2017-03-07 18:28:12 +11:00
|
|
|
return Color.BLACK;
|
2017-02-17 13:11:05 +11:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 15:10:46 +11:00
|
|
|
|
2017-02-27 16:21:46 +11:00
|
|
|
static void setImageViewTint(ImageView view, @AttrRes int attribute) {
|
2017-02-18 15:10:46 +11:00
|
|
|
view.setColorFilter(getColor(view.getContext(), attribute), PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
2017-03-07 18:28:12 +11:00
|
|
|
|
|
|
|
static void setDrawableTint(Context context, Drawable drawable, @AttrRes int attribute) {
|
|
|
|
drawable.setColorFilter(getColor(context, attribute), PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
2017-02-17 13:11:05 +11:00
|
|
|
}
|