Back to project page SpunkyCharts.
The source code is released under:
GNU General Public License
If you think the Android project SpunkyCharts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jogden.spunkycharts; //w ww . j a v a2s .c o m /* Copyright (C) 2014 Jonathon Ogden < jeog.dev@gmail.com > 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. */ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.jogden.spunkycharts.data.DataContentService; import com.jogden.spunkycharts.misc.ColorPaletteDialog; import com.jogden.spunkycharts.misc.Pair; import com.jogden.spunkycharts.traditionalchart.TraditionalChartFragment; import android.app.Activity; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.DialogInterface.OnDismissListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; /** APPLICATION PREFERENCES FRAMEWORK :: * This class is the hub for all things preferences/settings. * It handles basic universal settings ( e.g connectivity ), * ChartType specific universal settings ( e.g axis fonts ), * ChartType specific local settings( e.g chart opacity ), * CharType specific default local settings( e.g default chart * opacity). * </p> * Our framework attemps to interact directly with and * extend Android's Preference Framework. * </p> * Universal settings are all handled by the litany of public * getters and setters found below. ( the chart specific ones * may be moved into a new sub-object in the future ) Their * default values are defined in the relevant XML resource * files. Each setting exposes an appropriately named static * final string prefixed with 'APP_' and static getters and * setters to be used throughout, but it's suggested you work * within Android's Preference Framework. * </p> * We implement Android's PreferenceChangeListener * and handle changes to these preferences accordingly. * </p> * We use the single default SharedPreference object, in * the application context, to save and load state data. WE * provide static save and load methods for other Preference * objects to store state between sessions. (It's import, when * defining your own preference objects, to follow our * protocol of naming the ID/Key strings to represent each * preference. e.g 'public static final String GLOBAL_ALPHA = * "com.jogden.spunkycharts.GLOBAL_ALPHA"; ' * </p> * We define all the nested Preference Fragments we use * towards the bottom. DefaultChartPreferencesFragment is * one that will handle setting the default values for your * custom preference object. */ public class ApplicationPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { /** What you need to implement for your chart preferences. * Use GlobalChartPreferences.java as a template. It handles * the shared preferences for ALL chart types( alpha, refresh etc.) */ public interface ChartPreferencesSetup{ public interface Callback{ public <T> void callback(String pref, T val); } public boolean findViews( MainApplication.ViewFindable activity ); public void setup( Bundle initVals, final Callback callback, final Activity parent ); public void resetDefaults(Activity parent); public Bundle getBundleOfDefaults(); public ChartPreferencesSetup.Callback getCallbackForDefaults(); } @SuppressWarnings("serial") static public class PreferenceTypeException extends RuntimeException{} @SuppressWarnings("serial") static public class PreferenceInitiationException extends RuntimeException { public PreferenceInitiationException(String msg){ super(msg); } } @SuppressWarnings("serial") static public class StaticResourceExtractionException extends RuntimeException { public StaticResourceExtractionException(String msg){ super(msg); } } public static final Map< String,Pair<ChartPreferencesSetup,Integer> > metaChartPrefs = new LinkedHashMap< String,Pair<ChartPreferencesSetup,Integer>>(); public static SharedPreferences appPrefs; private final PreferenceActivity me = this; private static final int DEF_AXIS_MARK_LENGTH, DEF_SEGMENT_WIDTH, DEF_SEGMENT_VERT_PAD, DEF_AXIS_FONT_SIZE, DEF_AXIS_FONT_COLOR, DEF_AXIS_MARK_COLOR, DEF_AXIS_MARK_THICKNESS, DEF_TAB_TEXT_SIZE, DEF_TAB_TEXT_COLOR, DEF_SYMBOL_BAR_COLOR, DEF_PANEL_SNAP_THRESHOLD, DEF_CHART_ANIM_FREQ, DEF_CHART_ANIM_TIMEOUT, DEF_CHART_HOLD_THRESHOLD, DEF_TIMEOUT_INCR, DEF_TIMEOUT_LONG, DEF_TIMEOUT_SHORT, DEF_MAX_PREEMPT_COUNT, DEF_XAXIS_PAD_SIZE, DEF_DATA_CACHE_DAYS, DEF_CALLBACKS_B4_TRIM; private static final String DEF_CHART_SELECT_ANIM; private static final float DEF_CHART_ANIM_SEVERITY, DEF_PANEL_OFLOW_ALLOW; private static final int[] DEF_DPANEL_BACK_COLORS; private static final boolean DEF_AXIS_MINOR_MARK_BOOL; private static final boolean DEF_AXIS_MAJOR_MARK_BOOL; static{ MainApplication app = MainApplication.getInstance(); if( app == null) throw new IllegalStateException( "ApplicationPreferences static block evaluated before " + "MainApplication has a reference to itself. Be sure " + "not to reference ApplicationPreferences before " + "'MainApplication.thisApp = this' in " + "MainApplicationonCreate(). " ); appPrefs = PreferenceManager.getDefaultSharedPreferences(app); metaChartPrefs.put( /* not included in chart_manifest.xml */ "Global Charts", new Pair<ChartPreferencesSetup,Integer>( new GlobalChartPreferences(), R.layout.chart_settings_global ) ); Resources r = app.getResources(); DEF_SEGMENT_WIDTH = r.getDimensionPixelSize( R.dimen.chart_frag_trad_def_seg_width ); DEF_SEGMENT_VERT_PAD = r.getDimensionPixelSize( R.dimen.chart_frag_trad_def_vert_pad ); DEF_AXIS_MARK_LENGTH = r.getDimensionPixelSize( R.dimen.chart_frag_trad_def_mark_len ); if( DEF_AXIS_MARK_LENGTH > r.getDimensionPixelSize( R.dimen.chart_frag_trad_inner_axis_height )){ throw new StaticResourceExtractionException( "'R.dimen.chart_frag_trad_def_mark_len' can not be larger " + "than R.dimen.chart_frag_trad_inner_axis_height" ); } DEF_PANEL_SNAP_THRESHOLD = r.getDimensionPixelSize( R.dimen.panel_chart_snap_threshold ); DEF_PANEL_OFLOW_ALLOW = r.getFraction( R.fraction.panel_chart_overlflow_allow,1,1 ); DEF_AXIS_FONT_SIZE = r.getDimensionPixelSize(R.dimen.text_size_small); DEF_AXIS_MARK_THICKNESS =r.getDimensionPixelSize( R.dimen.chart_frag_trad_def_mark_thickness ); DEF_AXIS_MINOR_MARK_BOOL = r.getBoolean(R.bool.chart_frag_trad_minor_mark_bool); DEF_AXIS_MAJOR_MARK_BOOL = r.getBoolean(R.bool.chart_frag_trad_major_mark_bool); DEF_TAB_TEXT_SIZE = r.getDimensionPixelSize(R.dimen.text_size_normal); DEF_AXIS_FONT_COLOR = r.getColor(R.color.black); DEF_AXIS_MARK_COLOR = r.getColor(R.color.black); DEF_TAB_TEXT_COLOR = r.getColor(R.color.black); DEF_SYMBOL_BAR_COLOR = r.getColor(R.color.black); DEF_CHART_SELECT_ANIM = r.getString(R.string.anim_chart_def_select); DEF_CHART_ANIM_SEVERITY = r.getFraction(R.fraction.anim_chart_def_severity, 1, 1); DEF_CHART_ANIM_FREQ = r.getInteger(R.integer.anim_chart_def_frequency); DEF_CHART_ANIM_TIMEOUT = r.getInteger(R.integer.anim_chart_def_timeout); DEF_CHART_HOLD_THRESHOLD = r.getInteger(R.integer.chart_base_def_hold_threshold); DEF_TIMEOUT_INCR = r.getInteger(R.integer.perf_def_timeout_incr); DEF_TIMEOUT_LONG = r.getInteger(R.integer.perf_def_timeout_long); DEF_TIMEOUT_SHORT = r.getInteger(R.integer.perf_def_timeout_short); DEF_DPANEL_BACK_COLORS = new int[]{ r.getColor(R.color.white), r.getColor(R.color.white), r.getColor(R.color.white), r.getColor(R.color.white), r.getColor(R.color.white) }; DEF_MAX_PREEMPT_COUNT = r.getInteger(R.integer.perf_max_preempt_count); DEF_XAXIS_PAD_SIZE = r.getDimensionPixelSize( R.dimen.chart_frag_trad_def_xaxis_pad ); DEF_DATA_CACHE_DAYS = r.getInteger(R.integer.data_cache_days); DEF_CALLBACKS_B4_TRIM = r.getInteger(R.integer.data_callbacks_b4_trim); } public static final String APP_PANEL_SNAP_THRESHOLD = "APP_PANEL_SNAP_THRESHOLD"; // NO USER INTERFACE YET public static final String APP_PANEL_OFLOW_ALLOW = "APP_PANEL_OFLOW_ALLOW"; public static final String APP_XAXIS_PAD_SIZE = "APP_XAXIS_PAD_SIZE"; public static final String APP_SEGMENT_WIDTH= "APP_SEGMENT_WIDTH"; public static final String APP_SEGMENT_VERT_PAD= "APP_SEGMENT_VERT_PAD"; public static final String APP_MAX_PREEMPT_COUNT = "APP_MAX_PREEMPT_COUNT"; public static final String APP_TIMEOUT_INCR = "APP_DATA_TIMEOUT_INCR"; public static final String APP_TIMEOUT_LONG = "APP_TIMEOUT_LONG"; public static final String APP_TIMEOUT_SHORT = "APP_TIMEOUT_SHORT"; public static final String APP_TAB_TEXT_SIZE = "APP_TAB_TEXT_SIZE"; public static final String APP_TAB_TEXT_COLOR = "APP_TAB_TEXT_COLOR"; public static final String APP_SYMBOL_BAR_COLOR = "APP_SYMBOL_BAR_COLOR"; public static final String APP_AXIS_MARK_LENGTH= "APP_AXIS_MARK_LENGTH"; // NO USER INTERFACE YET public static final String APP_AXIS_FONT_SIZE = "APP_AXIS_FONT_SIZE"; public static final String APP_AXIS_FONT_COLOR = "APP_AXIS_FONT_COLOR"; public static final String APP_AXIS_MARK_COLOR = "APP_AXIS_MARK_COLOR"; public static final String APP_AXIS_MARK_THICKNESS = "APP_AXIS_MARK_THICKNESS"; public static final String APP_AXIS_MINOR_MARK_BOOL = "APP_AXIS_MINOR_MARK_BOOL"; public static final String APP_AXIS_MAJOR_MARK_BOOL = "APP_AXIS_MAJOR_MARK_BOOL"; public static final String APP_CHART_SELECT_ANIM = "APP_CHART_SELECT_ANIM"; public static final String APP_CHART_ENTEX_ANIM = "APP_CHART_ENTEX_ANIM"; // NOT IMPLEMENTED YET public static final String APP_CHART_ANIM_SEVERITY = "APP_CHART_ANIM_SEVERITY"; public static final String APP_CHART_ANIM_FREQ = "APP_CHART_ANIM_FREQ"; public static final String APP_CHART_ANIM_TIMEOUT = "APP_CHART_ANIM_TIMEOUT"; public static final String APP_CHART_HOLD_THRESHOLD = "APP_CHART_HOLD_THRESHOLD"; private static final String APP_DPANEL_BACK_COLOR = "APP_DPANEL_BACK_COLOR"; public static final String[] APP_DPANEL_BACK_COLORS = { "APP_DPANEL_BACK_COLOR-0", "APP_DPANEL_BACK_COLOR-1", "APP_DPANEL_BACK_COLOR-2", "APP_DPANEL_BACK_COLOR-3", "APP_DPANEL_BACK_COLOR-4"}; public static final String APP_DATA_CACHE_DAYS = "APP_DATA_CACHE_DAYS"; public static final String APP_CALLBACKS_B4_TRIM = "APP_CALLBACKS_B4_TRIM"; public static final String RESET_CHART_DEF_BUTTON = "RESET_CHART_DEFAULTS_BUTTON"; public static final String RESET_TIMEOUT_VALS_BUTTON = "RESET_TIMEOUT_VALS_BUTTON"; public static final String RESET_DATA_VALS_BUTTON = "RESET_DATA_VALS_BUTTON"; // temporary values to backup bad string input private static int tmpChartHoldThreshold = getChartHoldThreshold(); private static float tmpPanelOflowAllow = getPanelOverflowAllowed(); private static int tmpPanelSnapThreshold = getPanelSnapThreshold(); private static float tmpChartAnimSeverity = getSelectAnimationSeverity(); private static int tmpChartAnimFreq = getSelectAnimationFrequency(); private static int tmpChartAnimTimeout = getSelectAnimationTimeout(); private static int tmpTimeoutLong = getLongTimeout(); private static int tmpTimeoutShort = getShortTimeout(); private static int tmpTimeoutIncr = getTimeoutIncrement(); private static int tmpMaxPreemptCount = getMaxPreemptCount(); private static int tmpDataCacheDays = getDataCacheDays(); private static int tmpCallbacksB4Trim = getCallbacksB4Trim(); @SuppressWarnings("static-access") @Override public void onSharedPreferenceChanged( SharedPreferences sharedPreferences,String key ){ if(key.equals(APP_TAB_TEXT_SIZE)) for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setTabTextSize( getTabTextSize() ); else if(key.equals(APP_TAB_TEXT_COLOR)) for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setTabTextColor( getTabTextColor()); else if(key.equals(APP_SYMBOL_BAR_COLOR)) for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setSymbolBarColor( getSymbolBarColor() ); else if(key.equals(APP_AXIS_FONT_SIZE)) for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setAxisFontSize( getAxisFontSize() ); else if(key.equals(APP_AXIS_FONT_COLOR)) for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setAxisFontColor( getAxisFontColor() ); else if(key.equals(APP_AXIS_MARK_COLOR)){ TraditionalChartFragment.setAxisMarkColor(getAxisMarkColor()); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.refreshCharts(); } else if(key.equals(APP_AXIS_MARK_THICKNESS)){ TraditionalChartFragment.setAxisMarkThickness( getAxisMarkThickness() ); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.refreshCharts(); } else if(key.equals(APP_AXIS_MINOR_MARK_BOOL)){ TraditionalChartFragment.setShowMinorMark(getShowMinorMark()); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.refreshCharts(); } else if(key.equals(APP_AXIS_MAJOR_MARK_BOOL)){ TraditionalChartFragment.setShowMajorMark(getShowMajorMark()); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.refreshCharts(); } else if(key.equals(APP_CHART_SELECT_ANIM)){ for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setSelectAnimation( getSelectAnimation()); }else if(key.equals(APP_CHART_ANIM_FREQ)){ int animFreq = tmpChartAnimFreq; try{ animFreq = (int)_boundVal(getSelectAnimationFrequency(),60,0); tmpChartAnimFreq = animFreq; }catch(NumberFormatException e){ }finally{ setSelectAnimationFrequency(animFreq); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.resetAnimationPrefs(); } }else if( key.equals(APP_CHART_ANIM_SEVERITY)){ float animSevr = tmpChartAnimSeverity; try{ animSevr = (float)_boundVal(getSelectAnimationSeverity(),1,0); tmpChartAnimSeverity = animSevr; }catch(NumberFormatException e){ }finally{ setSelectAnimationSeverity(animSevr); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.resetAnimationPrefs(); } }else if( key.equals(APP_CHART_ANIM_TIMEOUT)){ int animTimeout = tmpChartAnimTimeout; try{ animTimeout = (int)getSelectAnimationTimeout(); tmpChartAnimTimeout = animTimeout; }catch(NumberFormatException e){ }finally{ setSelectAnimationTimeout(animTimeout); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.resetAnimationPrefs(); } } else if( key.contains(APP_DPANEL_BACK_COLOR) ){ int indx = Integer.valueOf(key.split("-")[1]); for( DockingPanelActivity dpa : MainApplication.dockingPanels) if(dpa.panelIndx == indx) dpa.setBackgroundColor( getDockingPanelColor(indx) ); } else if( key.equals(APP_XAXIS_PAD_SIZE) ){ for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setXAxisPaddingSize( getXAxisPaddingSize() ); } else if( key.equals(APP_TIMEOUT_LONG) ){ int timeout = tmpTimeoutLong; try{ timeout = (int)getLongTimeout(); tmpTimeoutLong = timeout; }catch(NumberFormatException e){ }finally{ setLongTimeout(timeout); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setAxisTimeout( timeout ); } } else if( key.equals(APP_TIMEOUT_SHORT) ){ int timeout = tmpTimeoutShort; try{ timeout = (int)getShortTimeout(); tmpTimeoutShort = timeout; }catch(NumberFormatException e){ }finally{ setShortTimeout(timeout); } } else if( key.equals(APP_TIMEOUT_INCR) ){ int incr= tmpTimeoutIncr; try{ incr = (int)getTimeoutIncrement(); tmpTimeoutIncr = incr;; }catch(NumberFormatException e){ }finally{ setTimeoutIncrement(incr); for( DockingPanelActivity dpa : MainApplication.dockingPanels) dpa.setTimeoutIncrement( incr ); } } else if( key.equals(APP_PANEL_OFLOW_ALLOW) ){ float overflow = tmpPanelOflowAllow; try{ overflow = _boundVal(getPanelOverflowAllowed(),1,0); tmpPanelOflowAllow = overflow; }catch(NumberFormatException e){ }finally{ setPanelOverflowAllowed(overflow ); } } else if( key.equals(APP_PANEL_SNAP_THRESHOLD) ){ int threshold = tmpPanelSnapThreshold; try{ threshold = (int)_boundVal(getPanelSnapThreshold(),100,0); tmpPanelSnapThreshold = threshold; }catch(NumberFormatException e){ }finally{ setPanelSnapThreshold(threshold); } } else if( key.equals(APP_MAX_PREEMPT_COUNT) ){ int max= tmpMaxPreemptCount; try{ max = getMaxPreemptCount(); tmpMaxPreemptCount = max; }catch(NumberFormatException e){ }finally{ setMaxPreemptCount(max); } } else if( key.equals(APP_CHART_HOLD_THRESHOLD) ){ int threshold = tmpChartHoldThreshold; try{ threshold = getChartHoldThreshold(); tmpChartHoldThreshold = threshold; }catch(NumberFormatException e){ }finally{ setChartHoldThreshold(threshold); } }else if(key.equals(APP_DATA_CACHE_DAYS)){ int cacheDays = tmpDataCacheDays; try{ cacheDays = (int)_boundVal(getDataCacheDays(),365,0); tmpDataCacheDays = cacheDays; }catch(NumberFormatException e){ }finally{ setDataCacheDays(cacheDays); DataContentService.setDataCacheDays(cacheDays); } }else if(key.equals(APP_CALLBACKS_B4_TRIM)){ int callbacks = tmpCallbacksB4Trim; try{ callbacks = (int)_boundVal(getCallbacksB4Trim(),60,0); tmpCallbacksB4Trim = callbacks; }catch(NumberFormatException e){ }finally{ setCallbacksB4Trim(callbacks); DataContentService.setCallbacksB4Trim(callbacks); } } } private float _boundVal(float val, float high, float low){ if(high<low) throw new IllegalArgumentException( "high must be >= than low" ); if(val < low) val = low; else if(val > high) val = high; return val; } public static int getDataCacheDays(){ return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_DATA_CACHE_DAYS, String.valueOf(DEF_DATA_CACHE_DAYS) ) ); } public static void setDataCacheDays(int val){ ApplicationPreferences.saveSharedPref( APP_DATA_CACHE_DAYS, String.valueOf(val) ); } public static int getCallbacksB4Trim(){ return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_CALLBACKS_B4_TRIM, String.valueOf(DEF_CALLBACKS_B4_TRIM) ) ); } public static void setCallbacksB4Trim(int val){ ApplicationPreferences.saveSharedPref( APP_CALLBACKS_B4_TRIM, String.valueOf(val) ); } public static float getPanelOverflowAllowed(){ return Float.valueOf( ApplicationPreferences.loadSharedPref( APP_PANEL_OFLOW_ALLOW, String.valueOf(DEF_PANEL_OFLOW_ALLOW) ) ); } public static void setPanelOverflowAllowed(float val){ ApplicationPreferences.saveSharedPref( APP_PANEL_OFLOW_ALLOW, String.valueOf(val) ); } public static int getPanelSnapThreshold(){ return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_PANEL_SNAP_THRESHOLD, String.valueOf(DEF_PANEL_SNAP_THRESHOLD) ) ); } public static void setPanelSnapThreshold(int val){ ApplicationPreferences.saveSharedPref( APP_PANEL_SNAP_THRESHOLD, String.valueOf(val) ); } public static void setXAxisPaddingSize(int pixSize) { ApplicationPreferences.saveSharedPref( APP_XAXIS_PAD_SIZE, String.valueOf(pixSize) ); } public static int getXAxisPaddingSize() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_XAXIS_PAD_SIZE, String.valueOf(DEF_XAXIS_PAD_SIZE) ) ); } public static void setMaxPreemptCount(int count) { ApplicationPreferences.saveSharedPref( APP_MAX_PREEMPT_COUNT, String.valueOf(count) ); } public static int getMaxPreemptCount() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_MAX_PREEMPT_COUNT, String.valueOf(DEF_MAX_PREEMPT_COUNT) ) ); } public static void setAxisMarkLength(int pixSize) { ApplicationPreferences.saveSharedPref( APP_AXIS_MARK_LENGTH, String.valueOf(pixSize) ); } public static int getAxisMarkLength() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_AXIS_MARK_LENGTH, String.valueOf(DEF_AXIS_MARK_LENGTH) ) ); } public static void setSegmentWidth(int pixSize) { ApplicationPreferences.saveSharedPref( APP_SEGMENT_WIDTH, String.valueOf(pixSize) ); } public static int getSegmentWidth() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_SEGMENT_WIDTH, String.valueOf(DEF_SEGMENT_WIDTH) ) ); } public static void setSegmentVerticalPadding(int pixSize) { ApplicationPreferences.saveSharedPref( APP_SEGMENT_VERT_PAD, String.valueOf(pixSize) ); } public static int getSegmentVerticalPadding() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_SEGMENT_VERT_PAD, String.valueOf(DEF_SEGMENT_VERT_PAD) ) ); } public static boolean getShowMinorMark() { return ApplicationPreferences.loadSharedPref( APP_AXIS_MINOR_MARK_BOOL, DEF_AXIS_MINOR_MARK_BOOL ); } public static void setShowMinorMark(boolean show) { ApplicationPreferences.saveSharedPref( APP_AXIS_MINOR_MARK_BOOL, show ); } public static boolean getShowMajorMark() { return ApplicationPreferences.loadSharedPref( APP_AXIS_MAJOR_MARK_BOOL, DEF_AXIS_MAJOR_MARK_BOOL ); } public static void setShowMajorMark(boolean show) { ApplicationPreferences.saveSharedPref( APP_AXIS_MAJOR_MARK_BOOL, show ); } public static int getAxisMarkThickness() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_AXIS_MARK_THICKNESS, String.valueOf(DEF_AXIS_MARK_THICKNESS) ) ); } public static void setAxisMarkThickness(int pixSize) { ApplicationPreferences.saveSharedPref( APP_AXIS_MARK_THICKNESS, String.valueOf(pixSize) ); } public static int getAxisMarkColor() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_AXIS_MARK_COLOR, String.valueOf(DEF_AXIS_MARK_COLOR) ) ); } public static void setAxisMarkColor(int colorId) { ApplicationPreferences.saveSharedPref( APP_AXIS_MARK_COLOR, String.valueOf(colorId) ); } public static int getLongTimeout() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_TIMEOUT_LONG, String.valueOf(DEF_TIMEOUT_LONG) ) ); } public static void setLongTimeout( int val ) { ApplicationPreferences.saveSharedPref( APP_TIMEOUT_LONG, String.valueOf(val) ); } public static int getShortTimeout() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_TIMEOUT_SHORT, String.valueOf(DEF_TIMEOUT_SHORT) ) ); } public static void setShortTimeout( int val ) { ApplicationPreferences.saveSharedPref( APP_TIMEOUT_SHORT, String.valueOf(val) ); } public static int getTimeoutIncrement() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_TIMEOUT_INCR, String.valueOf(DEF_TIMEOUT_INCR) ) ); } public static void setTimeoutIncrement( int val ) { ApplicationPreferences.saveSharedPref( APP_TIMEOUT_INCR, String.valueOf(val) ); } public static int getDockingPanelColor(int indx) { return ApplicationPreferences.loadSharedPref( APP_DPANEL_BACK_COLORS[indx], DEF_DPANEL_BACK_COLORS[indx] ); } public static void setDockingPanelColor(int indx, int colorId) { ApplicationPreferences.saveSharedPref( APP_DPANEL_BACK_COLORS[indx], colorId ); } public static int getChartHoldThreshold() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_CHART_HOLD_THRESHOLD, String.valueOf(DEF_CHART_HOLD_THRESHOLD) ) ); } public static void setChartHoldThreshold( int val ) { ApplicationPreferences.saveSharedPref( APP_CHART_HOLD_THRESHOLD, String.valueOf(val) ); } public static String getSelectAnimation() { return ApplicationPreferences.loadSharedPref( APP_CHART_SELECT_ANIM, DEF_CHART_SELECT_ANIM ); } public static void setSelectAnimation(String animName) { ApplicationPreferences.saveSharedPref( APP_CHART_SELECT_ANIM, animName ); } public static int getSelectAnimationTimeout() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_CHART_ANIM_TIMEOUT, String.valueOf(DEF_CHART_ANIM_TIMEOUT) ) ); } public static void setSelectAnimationTimeout( int timeout ) { ApplicationPreferences.saveSharedPref( APP_CHART_ANIM_TIMEOUT, String.valueOf(timeout) ); } public static int getSelectAnimationFrequency() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_CHART_ANIM_FREQ, String.valueOf(DEF_CHART_ANIM_FREQ) ) ); } public static void setSelectAnimationFrequency( int freq ) { ApplicationPreferences.saveSharedPref( APP_CHART_ANIM_FREQ, String.valueOf(freq) ); } public static float getSelectAnimationSeverity() { return Float.valueOf( ApplicationPreferences.loadSharedPref( APP_CHART_ANIM_SEVERITY, String.valueOf(DEF_CHART_ANIM_SEVERITY) ) ); } public static void setSelectAnimationSeverity( float severity ) { ApplicationPreferences.saveSharedPref( APP_CHART_ANIM_SEVERITY, String.valueOf(severity) ); } public static int getTabTextSize() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_TAB_TEXT_SIZE, String.valueOf(DEF_TAB_TEXT_SIZE) ) ); } public static void setTabTextSize(int sp) { ApplicationPreferences.saveSharedPref( APP_TAB_TEXT_SIZE, String.valueOf(sp) ); } public static int getTabTextColor() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_TAB_TEXT_COLOR, String.valueOf(DEF_TAB_TEXT_COLOR) ) ); } public static void setTabTextColor(int colorId) { ApplicationPreferences.saveSharedPref( APP_TAB_TEXT_COLOR, String.valueOf(colorId) ); } public static int getSymbolBarColor() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_SYMBOL_BAR_COLOR, String.valueOf(DEF_SYMBOL_BAR_COLOR) ) ); } public static void setSymbolBarColor(int colorId) { ApplicationPreferences.saveSharedPref( APP_SYMBOL_BAR_COLOR, String.valueOf(colorId) ); } public static int getAxisFontSize() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_AXIS_FONT_SIZE, String.valueOf(DEF_AXIS_FONT_SIZE) ) ); } public static void setAxisFontSize(int sp) { ApplicationPreferences.saveSharedPref( APP_AXIS_FONT_SIZE, String.valueOf(sp) ); } public static int getAxisFontColor() { return Integer.valueOf( ApplicationPreferences.loadSharedPref( APP_AXIS_FONT_COLOR, String.valueOf(DEF_AXIS_FONT_COLOR) ) ); } public static void setAxisFontColor(int colorId) { ApplicationPreferences.saveSharedPref( APP_AXIS_FONT_COLOR, String.valueOf(colorId) ); } @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); appPrefs.registerOnSharedPreferenceChangeListener(this); Button resetButton = new Button(this); resetButton.setText("Reset All Defaults"); this.setListFooter(resetButton); resetButton.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { resetAllDefaults(); me.recreate(); } } ); } @Override public void onBuildHeaders(List<Header> t) { loadHeadersFromResource(R.xml.preference_headers,t); } @Override public boolean isValidFragment(String s) { return s.equals( DefaultChartPreferencesFragment.class.getName() ) || s.equals( ConnectivityPreferencesFragment.class.getName() ) || s.equals( StylePreferencesFragment.class.getName() ) || s.equals( KineticPreferencesFragment.class.getName() ) || s.equals( PerformancePreferencesFragment.class.getName() ) || s.equals( DataPreferencesFragment.class.getName() ); } /* Your ChartPreferencesSetup Implementation will use these two * static functions in its ...Default... methods to save and load * preferences permanently. Throws PreferenceTypeException if * you pass a type other than the four supported. */ @SuppressWarnings("unchecked") public static <T> T loadSharedPref(String key, T defVal ) { if( defVal instanceof Integer) return (T)((Integer)appPrefs.getInt(key, (Integer)defVal)); else if( defVal instanceof Float) return (T)((Float)appPrefs.getFloat(key, (Float)defVal)); else if( defVal instanceof String) return (T)((String)appPrefs.getString(key, (String)defVal)); else if( defVal instanceof Boolean) return (T)((Boolean)appPrefs.getBoolean(key, (Boolean)defVal)); else throw new PreferenceTypeException(); } public static <T> void saveSharedPref(String key, T val) { SharedPreferences.Editor editor = appPrefs.edit(); if( val instanceof Integer) editor.putInt(key, (Integer)val); else if( val instanceof Float) editor.putFloat(key, (Float)val); else if( val instanceof String) editor.putString(key, (String)val); else if( val instanceof Boolean) editor.putBoolean(key, (Boolean)val); else throw new PreferenceTypeException(); editor.apply(); } static public void resetAllDefaults() { setSegmentWidth(DEF_SEGMENT_WIDTH); setSegmentVerticalPadding(DEF_SEGMENT_VERT_PAD); setAxisMarkLength(DEF_AXIS_MARK_LENGTH); setAxisFontSize(DEF_AXIS_FONT_SIZE); setAxisFontColor(DEF_AXIS_FONT_COLOR); setAxisMarkColor(DEF_AXIS_MARK_COLOR); setAxisMarkThickness(DEF_AXIS_MARK_THICKNESS); setShowMinorMark(DEF_AXIS_MINOR_MARK_BOOL); setShowMajorMark(DEF_AXIS_MAJOR_MARK_BOOL); setTabTextSize(DEF_TAB_TEXT_SIZE); setTabTextColor(DEF_TAB_TEXT_COLOR); setSymbolBarColor(DEF_SYMBOL_BAR_COLOR); setSelectAnimation(DEF_CHART_SELECT_ANIM); setSelectAnimationSeverity(DEF_CHART_ANIM_SEVERITY); setSelectAnimationFrequency(DEF_CHART_ANIM_FREQ); setSelectAnimationTimeout(DEF_CHART_ANIM_TIMEOUT); setChartHoldThreshold(DEF_CHART_HOLD_THRESHOLD); setLongTimeout(DEF_TIMEOUT_LONG); setShortTimeout(DEF_TIMEOUT_SHORT); setTimeoutIncrement(DEF_TIMEOUT_INCR); setMaxPreemptCount(DEF_MAX_PREEMPT_COUNT); setXAxisPaddingSize(DEF_XAXIS_PAD_SIZE); setPanelOverflowAllowed(DEF_PANEL_OFLOW_ALLOW); setPanelSnapThreshold(DEF_PANEL_SNAP_THRESHOLD); setDataCacheDays(DEF_DATA_CACHE_DAYS); setCallbacksB4Trim(DEF_CALLBACKS_B4_TRIM); for(int i = 0; i < DEF_DPANEL_BACK_COLORS.length; ++i) setDockingPanelColor(i,DEF_DPANEL_BACK_COLORS[i]); } /* This Frag handles displaying your preference layouts in the main menu so users can set permanent defaults. Be sure to add the appropriate element(s) to /xml/default_chart_prefs.xml w/ your layout ID. This loops through the preference implementations asynchronously to allow the views a chance to be created. If your findViews Override returns false for long enough it will stop, log a debug message, and continue on to the next...*/ static public class DefaultChartPreferencesFragment extends PreferenceFragment { private Activity parentActivity; private DefaultChartPreferencesFragment me = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.default_chart_prefs); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); parentActivity = this.getActivity(); } @Override public void onStart() { super.onStart(); (new AsyncInitDefPrefs(new Handler())).start(); } private final class AsyncInitDefPrefs extends Thread { Handler rootThread; final int INCR = getTimeoutIncrement(); final int TOUT = getLongTimeout(); int timeOutCount = 0; public AsyncInitDefPrefs(Handler rootThread) { this.rootThread = rootThread; } @Override public void run() { PreferenceScreen prefScrn = me.getPreferenceScreen(); PreferenceCategory pc = null; Preference p = null; int order = 1; /* add all the prefs first, then try to grab the sub-views */ for(String chartName : metaChartPrefs.keySet()) { final Pair<ChartPreferencesSetup,Integer> pref = metaChartPrefs.get(chartName); pc = new PreferenceCategory(parentActivity); pc.setTitle(chartName); pc.setOrder(order++); p = new Preference(parentActivity); p.setLayoutResource(pref.second); prefScrn.addPreference(pc); pc.addPreference(p); } for(final Pair<ChartPreferencesSetup,Integer> pref : metaChartPrefs.values() ){ MainApplication.ViewFindable finder = new MainApplication.ViewFindable(){ public View findViewById(int id){ return parentActivity.findViewById(id); } }; while(true) if( pref.first.findViews(finder) ){ rootThread.post( new Runnable(){ public void run(){ pref.first.setup( pref.first.getBundleOfDefaults(), pref.first.getCallbackForDefaults(), parentActivity ); me.getView().invalidate(); }}); break; } else { try { Thread.sleep(INCR); if( (timeOutCount+=INCR) > TOUT) { timeOutCount = 0; Log.d( "SpunkyCharts - DefaultPreferencesFragment", "Timed Out waiting to find views for " + pref.first.toString() ); rootThread.post( new Runnable(){ public void run(){ Toast.makeText( parentActivity, pref.first.toString() + " may have failed to " + "load. If values are not shown Please exit " + "Preferences and try again.", Toast.LENGTH_LONG ).show(); } } ); break; } } catch (InterruptedException e) {} } } // this can go in the main listener Preference resetPref = me.findPreference(RESET_CHART_DEF_BUTTON); resetPref.setOnPreferenceClickListener( new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick( Preference preference ){ for( Pair<ChartPreferencesSetup,Integer> pref : metaChartPrefs.values() ) pref.first.resetDefaults(parentActivity); rootThread.post( new Runnable(){ public void run(){ me.getView().invalidate(); }}); return true; } } ); } } } /* This Frag handles any universal font, color, size etc. settings * for the entire app. A number of the chart-specific settings * should be handled by your preference implementation */ static public class StylePreferencesFragment extends PreferenceFragment { private Activity parentActivity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.style_prefs ); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); parentActivity = this.getActivity(); } @Override public void onStart() { super.onStart(); (new AsyncInitStylePrefs(new Handler())).start(); } private final class AsyncInitStylePrefs extends Thread { Handler rootThread; final int INCR = getTimeoutIncrement(); final int TOUT = getLongTimeout(); int timeOutCount = 0; public AsyncInitStylePrefs(Handler rootThread) { this.rootThread = rootThread; } @Override public void run() { while(true) { View v = parentActivity.findViewById(R.id.dp_table); if (v != null) { final View cViews[] = { v.findViewById(R.id.dp1_color_back_select), v.findViewById(R.id.dp2_color_back_select), v.findViewById(R.id.dp3_color_back_select), v.findViewById(R.id.dp4_color_back_select), v.findViewById(R.id.dp5_color_back_select) }; final CheckBox boxViews[] = { (CheckBox)v.findViewById(R.id.dp1_grid_checkbox), (CheckBox)v.findViewById(R.id.dp2_grid_checkbox), (CheckBox)v.findViewById(R.id.dp3_grid_checkbox), (CheckBox)v.findViewById(R.id.dp4_grid_checkbox), (CheckBox)v.findViewById(R.id.dp5_grid_checkbox) }; rootThread.post( new Runnable(){ public void run(){ for(int i = 0; i < cViews.length; ++i){ final int ii = i; cViews[ii].setBackgroundColor( getDockingPanelColor(ii) ); cViews[ii].setOnClickListener( new OnClickListener(){ @Override public void onClick(View v){ ColorPaletteDialog cpd = new ColorPaletteDialog( parentActivity, getDockingPanelColor(ii) ); cpd.setOnDismissListener( new OnDismissListener(){ @Override public void onDismiss(DialogInterface dialog){ setDockingPanelColor( ii, ((ColorPaletteDialog)dialog).getColorId() ); cViews[ii].setBackgroundColor( getDockingPanelColor(ii) ); cViews[ii].invalidate(); } } ); cpd.show(); } } ); boxViews[ii].setOnCheckedChangeListener( new OnCheckedChangeListener(){ @Override public void onCheckedChanged( CompoundButton cButton, boolean isChecked ){ // TODO: implement this Toast.makeText( parentActivity, "N/A", Toast.LENGTH_LONG ).show(); } } ); } } } ); break; } else try { Thread.sleep(INCR); if( (timeOutCount+=INCR) > TOUT) { timeOutCount = 0; Log.d( "SpunkyCharts - StylePreferencesFragment", "Timed Out waiting to find Docking Panel Table" ); break; } } catch (InterruptedException e) {} } } } } /* This Frag handles any connectivity, network, and data * settings for the entire app. */ static public class ConnectivityPreferencesFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource( R.xml.connectivity_prefs); } } /* This frag handles the movement and animation related * settings. (e.g how a chart behaves on touch) */ static public class KineticPreferencesFragment extends PreferenceFragment { private Activity parentActivity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource( R.xml.kinetic_prefs); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); parentActivity = this.getActivity(); } @Override public void onStart() { super.onStart(); ListPreference selectAnimPref = (ListPreference)this.findPreference( APP_CHART_SELECT_ANIM ); CharSequence[] entries = new CharSequence[ MainApplication.metaSelectAnimTypes.size() ]; MainApplication.metaSelectAnimTypes.keySet().toArray( entries ); selectAnimPref.setEntries(entries); selectAnimPref.setEntryValues(entries); selectAnimPref.setDefaultValue( getSelectAnimation() ); Preference entexAnimPref = this.findPreference(APP_CHART_ENTEX_ANIM); entexAnimPref.setOnPreferenceClickListener( new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick(Preference pref){ // TODO implement this ( change to ListPreference ) Toast.makeText( parentActivity, "N/A", Toast.LENGTH_LONG ).show(); return true; } } ); } } /* This Frag handles performance fine-tuning for the entire app. */ static public class PerformancePreferencesFragment extends PreferenceFragment { private PerformancePreferencesFragment me = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource( R.xml.performance_prefs); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onStart() { super.onStart(); Preference resetPref = this.findPreference(RESET_TIMEOUT_VALS_BUTTON); resetPref.setOnPreferenceClickListener( new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick(Preference pref){ setLongTimeout(DEF_TIMEOUT_LONG); setShortTimeout(DEF_TIMEOUT_SHORT); setTimeoutIncrement(DEF_TIMEOUT_INCR); me.getView().invalidate(); return true; } } ); } } /* This Frag handles settings for the data back-end */ static public class DataPreferencesFragment extends PreferenceFragment { private DataPreferencesFragment me = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource( R.xml.data_prefs); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onStart() { super.onStart(); Preference resetPref = this.findPreference(RESET_DATA_VALS_BUTTON); resetPref.setOnPreferenceClickListener( new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick(Preference pref){ setDataCacheDays(DEF_DATA_CACHE_DAYS); setCallbacksB4Trim(DEF_CALLBACKS_B4_TRIM); me.getView().invalidate(); return true; } } ); } } }