From b0f5f96eeea7cea883d066510d345b376507ffef Mon Sep 17 00:00:00 2001 From: olegivo Date: Thu, 13 Aug 2020 07:40:49 -0500 Subject: [PATCH] Konvert StyledResources Co-authored-by: Alinson S. Xavier --- .../androidbase/utils/StyledResources.java | 118 ------------------ .../androidbase/utils/StyledResources.kt | 93 ++++++++++++++ .../org/isoron/uhabits/utils/PaletteUtils.kt | 4 +- 3 files changed, 95 insertions(+), 120 deletions(-) delete mode 100644 android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.java create mode 100644 android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.kt diff --git a/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.java b/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.java deleted file mode 100644 index d00588a90..000000000 --- a/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker 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. - * - * Loop Habit Tracker 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 this program. If not, see . - */ - -package org.isoron.androidbase.utils; - -import android.content.*; -import android.content.res.*; -import android.graphics.drawable.*; - -import androidx.annotation.AttrRes; -import androidx.annotation.NonNull; - -import org.isoron.androidbase.*; - -public class StyledResources -{ - private static Integer fixedTheme; - - private final Context context; - - public StyledResources(@NonNull Context context) - { - this.context = context; - } - - public static void setFixedTheme(Integer theme) - { - fixedTheme = theme; - } - - public boolean getBoolean(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - boolean bool = ta.getBoolean(0, false); - ta.recycle(); - - return bool; - } - - public int getDimension(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - int dim = ta.getDimensionPixelSize(0, 0); - ta.recycle(); - - return dim; - } - - public int getColor(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - int color = ta.getColor(0, 0); - ta.recycle(); - - return color; - } - - public Drawable getDrawable(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - Drawable drawable = ta.getDrawable(0); - ta.recycle(); - - return drawable; - } - - public float getFloat(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - float f = ta.getFloat(0, 0); - ta.recycle(); - - return f; - } - - public int[] getPalette() - { - int resourceId = getResource(R.attr.palette); - if (resourceId < 0) throw new RuntimeException("resource not found"); - - return context.getResources().getIntArray(resourceId); - } - - public int getResource(@AttrRes int attrId) - { - TypedArray ta = getTypedArray(attrId); - int resourceId = ta.getResourceId(0, -1); - ta.recycle(); - - return resourceId; - } - - private TypedArray getTypedArray(@AttrRes int attrId) - { - int[] attrs = new int[]{ attrId }; - - if (fixedTheme != null) - return context.getTheme().obtainStyledAttributes(fixedTheme, attrs); - - return context.obtainStyledAttributes(attrs); - } -} diff --git a/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.kt b/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.kt new file mode 100644 index 000000000..3ef22d052 --- /dev/null +++ b/android/android-base/src/main/java/org/isoron/androidbase/utils/StyledResources.kt @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker 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. + * + * Loop Habit Tracker 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 this program. If not, see . + */ +package org.isoron.androidbase.utils + +import android.content.Context +import android.content.res.TypedArray +import android.graphics.drawable.Drawable +import androidx.annotation.AttrRes +import org.isoron.androidbase.R + +class StyledResources(private val context: Context) { + + fun getBoolean(@AttrRes attrId: Int): Boolean { + val ta = getTypedArray(attrId) + val bool = ta.getBoolean(0, false) + ta.recycle() + return bool + } + + fun getDimension(@AttrRes attrId: Int): Int { + val ta = getTypedArray(attrId) + val dim = ta.getDimensionPixelSize(0, 0) + ta.recycle() + return dim + } + + fun getColor(@AttrRes attrId: Int): Int { + val ta = getTypedArray(attrId) + val color = ta.getColor(0, 0) + ta.recycle() + return color + } + + fun getDrawable(@AttrRes attrId: Int): Drawable? { + val ta = getTypedArray(attrId) + val drawable = ta.getDrawable(0) + ta.recycle() + return drawable + } + + fun getFloat(@AttrRes attrId: Int): Float { + val ta = getTypedArray(attrId) + val f = ta.getFloat(0, 0f) + ta.recycle() + return f + } + + fun getPalette(): IntArray { + val resourceId = getResource(R.attr.palette) + if (resourceId < 0) throw RuntimeException("palette resource not found") + return context.resources.getIntArray(resourceId) + } + + fun getResource(@AttrRes attrId: Int): Int { + val ta = getTypedArray(attrId) + val resourceId = ta.getResourceId(0, -1) + ta.recycle() + return resourceId + } + + private fun getTypedArray(@AttrRes attrId: Int): TypedArray { + val attrs = intArrayOf(attrId) + if (fixedTheme != null) { + return context.theme.obtainStyledAttributes(fixedTheme!!, attrs) + } + return context.obtainStyledAttributes(attrs) + } + + companion object { + private var fixedTheme: Int? = null + + @JvmStatic + fun setFixedTheme(theme: Int?) { + fixedTheme = theme + } + } +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/PaletteUtils.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/PaletteUtils.kt index 6e205a5a6..aa8fe20af 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/PaletteUtils.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/PaletteUtils.kt @@ -9,7 +9,7 @@ object PaletteUtils { @JvmStatic fun colorToPaletteIndex(context: Context, color: Int): Int { - val palette = StyledResources(context).palette + val palette = StyledResources(context).getPalette() return palette.indexOf(color) } @@ -43,7 +43,7 @@ object PaletteUtils { @JvmStatic fun getColor(context: Context, paletteColor: Int): Int { - val palette = StyledResources(context).palette + val palette = StyledResources(context).getPalette() return if (paletteColor in palette.indices) { palette[paletteColor] } else {