diff options
Diffstat (limited to 'app/src/main/java/ch/blinkenlights/android/vanilla/PreferencesTheme.java')
| -rw-r--r-- | app/src/main/java/ch/blinkenlights/android/vanilla/PreferencesTheme.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/app/src/main/java/ch/blinkenlights/android/vanilla/PreferencesTheme.java b/app/src/main/java/ch/blinkenlights/android/vanilla/PreferencesTheme.java new file mode 100644 index 0000000..032da0c --- /dev/null +++ b/app/src/main/java/ch/blinkenlights/android/vanilla/PreferencesTheme.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2016 Adrian Ulrich <adrian@blinkenlights.ch> + * + * This program 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. + * + * This program 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 <http://www.gnu.org/licenses/>. + */ + +package ch.blinkenlights.android.vanilla; + +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; + + +public class PreferencesTheme extends PreferenceFragment + implements Preference.OnPreferenceClickListener +{ + private Context mContext; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mContext = getActivity(); + + // Themes are 'pre-compiled' in themes-list: get all values + // and append them to our newly created PreferenceScreen + PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(mContext); + final String[] entries = getResources().getStringArray(R.array.theme_entries); + final String[] values = getResources().getStringArray(R.array.theme_values); + final String[] ids = getResources().getStringArray(R.array.theme_ids); + + for (int i = 0; i < entries.length; i++) { + + int[] attrs = decodeValue(values[i]); + + final Preference pref = new Preference(mContext); + pref.setPersistent(false); + pref.setOnPreferenceClickListener(this); + pref.setTitle(entries[i]); + pref.setKey(ids[i]); // preference value of this theme + pref.setIcon(generateThemePreview(attrs)); + screen.addPreference(pref); + } + setPreferenceScreen(screen); + } + + @Override + public boolean onPreferenceClick(Preference pref) { + SharedPreferences.Editor editor = SharedPrefHelper.getSettings(mContext).edit(); + editor.putString(PrefKeys.SELECTED_THEME, pref.getKey()); + editor.apply(); + return true; + } + + + private int[] decodeValue(String v) { + String[] parts = v.split(","); + int[] values = new int[parts.length]; + for (int i=0; i<parts.length; i++) { + long parsedLong = (long)Long.decode(parts[i]); // the colors overflow an int, so we first must parse it as Long to make java happy. + values[i] = (int)parsedLong; + } + return values; + } + + private Drawable generateThemePreview(int[] colors) { + final int size = (int) getResources().getDimension(R.dimen.cover_size); + final int step = size / colors.length; + + Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565); + Canvas canvas = new Canvas(bitmap); + Paint paint = new Paint(); + + paint.setStyle(Paint.Style.FILL); + for (int i=0; i < colors.length; i++) { + paint.setColor(colors[i]); + canvas.drawRect(0, step*i, size, size, paint); + } + + Drawable d = new BitmapDrawable(mContext.getResources(), bitmap); + return d; + } + + + + + +} |
