Android Open Source - SpunkyCharts Global Chart Preferences






From Project

Back to project page SpunkyCharts.

License

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.

Java Source Code

package com.jogden.spunkycharts;
/* //  w w w.  j a  v a  2  s  .  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 com.jogden.spunkycharts.ApplicationPreferences.ChartPreferencesSetup;
import com.jogden.spunkycharts.traditionalchart.TraditionalChartPreferences;
import com.jogden.spunkycharts.traditionalchart.TraditionalChartPreferences.Frequency;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

/** This is the conceptual base preference object. It will be used
 * in all chart fragments unlike a custom preference object which
 * will implement ChartPreferencesSetup but only be applied
 * to the appropriate custom chart fragment object.
 * </p>
 * Custom preference objects  and their layouts will be imported via 
 * chart_manifest.xml into the DefaultChartPreferencesFragment in 
 * ApplicationPreferences. This is why you need to implement a 
 * number of functions concerning default values. The handling of 
 * default values will be done automatically for you within the larger
 * Preference Framework via callback functionality you pass in and
 * how you define the appropriate abstract functions. 
 * </p>
 * You'll interact more directly with your object when you  override
 * getLocalPreferencesDialog() in your Chart Fragment to handle with
 * chart local preferences (for one particular chart).
 * </p>
 * These Objects allow us: </br>
 *     1) a modular approach to chart types and preferences</br>
 *     2) straight-forward interoperability with and extension of
 *          Android's Preference Framework</br>
 *     3) the ability to customize and reuse a preference layout 
 *         in different situations for different 'types' of preferences</br>
 *     4)  the same interface for setting local chart 
 *         preferences AND application wide defaults for the same
 *         preferences</br>
 *     5) to hide the details of saving and loading persistent state 
 *         data with some simple getters and setters that we can safely
 *         use throughout the application </br>
 *         
 */
public class GlobalChartPreferences 
    implements ApplicationPreferences.ChartPreferencesSetup 
{
    public static final String GLOBAL_CHART_PREF = 
        "com.jogden.spunkycharts.GLOBAL_CHART_PREF";
    public static final String GLOBAL_ALPHA =
        "com.jogden.spunkycharts.GLOBAL_ALPHA";
    public static final String GLOBAL_LINE_THICKNESS = 
        "com.jogden.spunkycharts.GLOBAL_LINE_THICKNESS";
    public static final String GLOBAL_REFRESH_RATE = 
        "com.jogden.spunkycharts.GLOBAL_REFRESH_RATE";
    public static final String GLOBAL_BORDER_SIZE = 
        "com.jogden.spunkycharts.GLOBAL_BORDER_SIZE";

    private int _curLineThickness;    
    
    public static enum LineThickness {
        PIX1(1), PIX2(2), PIX3(3), PIX4(4), 
        PIX5(5), PIX6(6), PIX7(7), PIX8(8);
        public int value;
        private LineThickness( int i ){ 
            value = i; 
            }        
        }            
    private static final LineThickness[] LineThicknessVals = 
        LineThickness.values();    
    /* our original default values */
    private static final LineThickness ORIG_DEF_LINE_THICKNESS= 
        LineThickness.PIX3;
    private static final int ORIG_DEF_REFRESH_RATE = 3000;
    private static final float ORIG_DEF_ALPHA = 1.0f;
    private static final int ORIG_DEF_BORDER_SIZE = 1;
    
    /* getters and setters for our default values */    
    public static int getDefaultChartBorder()
    {
        return ApplicationPreferences.loadSharedPref(
            GLOBAL_BORDER_SIZE, ORIG_DEF_BORDER_SIZE
            );
    }
    private static void setDefaultChartBorder(int border)
    {
        ApplicationPreferences.saveSharedPref(
            GLOBAL_BORDER_SIZE, border
            ); 
    }
    public static float getDefaultAlpha()
    {
        return ApplicationPreferences.loadSharedPref(
            GLOBAL_ALPHA, ORIG_DEF_ALPHA
            );
    }
    private static void setDefaultAlpha(float alpha)
    {
        ApplicationPreferences.saveSharedPref(
            GLOBAL_ALPHA, alpha
            ); 
    }
    public static int getDefaultRefreshRate()
    {
        return ApplicationPreferences.loadSharedPref(
            GLOBAL_REFRESH_RATE, ORIG_DEF_REFRESH_RATE
            );
    }
    private static void setDefaultRefreshRate(int refreshRate)
    {
        ApplicationPreferences.saveSharedPref(
            GLOBAL_REFRESH_RATE, refreshRate
            );
    }
    
    public static  LineThickness getDefaultLineThickness()
    {
        return LineThickness.valueOf(
            ApplicationPreferences.loadSharedPref(
                GLOBAL_LINE_THICKNESS, 
                ORIG_DEF_LINE_THICKNESS.toString()
                )
            );
    }
    private static void setDefaultLineThickness(LineThickness thickness)
    {
        ApplicationPreferences.saveSharedPref(
            GLOBAL_LINE_THICKNESS, thickness.toString()
            );
    }

    /* important views in our layout */
    private SeekBar _sbAlpha, _sbRefreshRate;
    private TextView _tvLineThicknessValue;
    private Button _bLineThicknessIncr, _bLineThicknessDecr;
    private RadioGroup _rgChartBorder;                
    
    /* need to find all the views in the layout first */
    @Override
    public boolean findViews(MainApplication.ViewFindable finder)
    {
        return ( 
        (_sbAlpha = (SeekBar)finder.findViewById(
            R.id.localS_alpha_seek_bar )) != null &&
        (_bLineThicknessIncr = (Button) finder.findViewById(
            R.id.localS_line_thickness_incr )) != null &&
        (_bLineThicknessDecr = (Button) finder.findViewById(
            R.id.localS_line_thickness_decr )) != null &&
        (_tvLineThicknessValue = (TextView) finder.findViewById(
            R.id.localS_line_thickness_value )) != null &&
        (_sbRefreshRate = (SeekBar) finder.findViewById(
            R.id.localS_refresh_rate_seek_bar )) != null &&
        (_rgChartBorder = (RadioGroup)finder.findViewById(
            R.id.localS_border_size_radio_group )) != null
            );    
    }
    
    /* define behavior when user wants to reset defaults to originals */
    @Override
    public void resetDefaults(Activity parent)
    {
        setDefaultLineThickness(ORIG_DEF_LINE_THICKNESS);
        setDefaultRefreshRate(ORIG_DEF_REFRESH_RATE);
        setDefaultAlpha(ORIG_DEF_ALPHA);        
        setDefaultChartBorder(ORIG_DEF_BORDER_SIZE);
        setup(
            getBundleOfDefaults(),
            getCallbackForDefaults(),
            parent
            );
    }
    
    /* provide a bundle of current defaults */
    @Override
    public Bundle getBundleOfDefaults()
    {
        Bundle bundle = new Bundle();                    
        bundle.putFloat( GLOBAL_ALPHA, getDefaultAlpha() ); 
        bundle.putInt( 
            GLOBAL_REFRESH_RATE, getDefaultRefreshRate() 
            );
        bundle.putString( 
            GLOBAL_LINE_THICKNESS, 
            getDefaultLineThickness().toString()
            );
        bundle.putInt( 
            GLOBAL_BORDER_SIZE, 
            getDefaultChartBorder()
            );
        return bundle;
    }
    
    /* return a callback that will be called when a default is changed */
    @Override
    public ChartPreferencesSetup.Callback getCallbackForDefaults()
    {
        return new ChartPreferencesSetup.Callback(){
            @Override
            public <T> void callback(String pref, T val){
                if ( pref.equals(GLOBAL_ALPHA) )                
                    setDefaultAlpha((Float)val);                
                else if ( pref.equals(GLOBAL_LINE_THICKNESS) )    
                    setDefaultLineThickness(LineThickness.valueOf((String)val));                        
                else if ( pref.equals(GLOBAL_REFRESH_RATE) )
                    setDefaultRefreshRate((Integer)val);    
                else if ( pref.equals(GLOBAL_BORDER_SIZE))
                    setDefaultChartBorder((Integer)val);
            }
        };        
    }
    
    /* define how all the layout views will deal with user interaction */
    @Override
    public void setup(    
        Bundle initVals,
        final ApplicationPreferences.ChartPreferencesSetup.Callback callback,
        final Activity parent
    ){                    
        if( parent == null)
            throw new IllegalArgumentException( "parent can not be null" );    
        /* chart opacity seekbar */
        _sbAlpha.setMax(100);    
        _sbAlpha.setProgress(
            (int)(initVals.getFloat(GLOBAL_ALPHA) * 100)
            );        
        _sbAlpha.setOnSeekBarChangeListener(
                new OnSeekBarChangeListener(){
                    int _progress;
                    @Override
                    public void onProgressChanged( 
                        SeekBar seekBar, int progress, boolean fromUser
                    ){
                        _progress = progress;                                        
                    }
                    @Override
                    public void onStartTrackingTouch( SeekBar seekBar){}
                    @Override
                    public void onStopTrackingTouch( SeekBar seekBar){        
                        callback.callback(
                            GLOBAL_ALPHA, 
                            (float)_progress/100
                            );                                    
                        }                                        
                    }
                );            
        /* refresh-rate seekbar */        
        _sbRefreshRate.setMax(100);
        _sbRefreshRate.setProgress(
            (int)((initVals.getInt(GLOBAL_REFRESH_RATE) -100)/99)
            );            
        _sbRefreshRate .setOnSeekBarChangeListener(
            new OnSeekBarChangeListener(){
                int _progress;
                @Override
                public void onProgressChanged( 
                    SeekBar seekBar, int progress, boolean fromUser
                ){
                    _progress = progress;                                        
                }
                @Override
                public void onStartTrackingTouch( SeekBar seekBar){}
                @Override
                public void onStopTrackingTouch( SeekBar seekBar){        
                    callback.callback(
                        GLOBAL_REFRESH_RATE,
                        (_progress * 99)+100
                        );                
                    }                                        
                }
            );        
        /* line thickness +/- views */    
        _curLineThickness = 
            LineThickness.valueOf( 
                initVals.getString(GLOBAL_LINE_THICKNESS) 
                ).value;    
        _tvLineThicknessValue.setText( String.valueOf(_curLineThickness) ); 
        _bLineThicknessIncr.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {                    
                        callback.callback(
                            GLOBAL_LINE_THICKNESS, 
                            LineThicknessVals[                                            
                                ((_curLineThickness < LineThickness.PIX8.value) ?
                                    ++_curLineThickness : _curLineThickness) - 1 ].toString()                        
                            );        
                        _tvLineThicknessValue.setText( 
                            String.valueOf(_curLineThickness) 
                            );
                        }
                    }
                );
            _bLineThicknessDecr.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {                    
                        callback.callback(
                            GLOBAL_LINE_THICKNESS, 
                            LineThicknessVals[
                                ((_curLineThickness > LineThickness.PIX1.value) ?
                                --_curLineThickness : _curLineThickness) - 1].toString()    
                            );
                        _tvLineThicknessValue.setText( 
                            String.valueOf(_curLineThickness) 
                            );
                        }                
                    }
                );
        /* border thickness radio group */
        _rgChartBorder.check( 
            borderValToIdSwitch(                        
                    initVals.getInt(GLOBAL_BORDER_SIZE)                            
                )
            );        
        _rgChartBorder.setOnCheckedChangeListener(
            new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    callback.callback(
                        GLOBAL_BORDER_SIZE, 
                        borderIdToValSwitch(checkedId)
                        );                                    
                    }                
                }
            );        
        }
    
    /*provide convenience view-to-value switches if necessary */
    public static int borderValToIdSwitch(int border)
    {
        switch(border)
        {
        case 0: return R.id.localS_border_size_radio_none;        
        case 2: return R.id.localS_border_size_radio_thick;            
        case 4: return R.id.localS_border_size_radio_thickest;    
        default: return R.id.localS_border_size_radio_thin;
        }        
    }
    public static int borderIdToValSwitch(int id)
    {
        switch(id)
        {
        case R.id.localS_border_size_radio_none: return 0;
        case R.id.localS_border_size_radio_thick: return 2;                
        case R.id.localS_border_size_radio_thickest: return 4;        
        default: return 1;    
        }        
    }
}




Java Source Code List

com.jogden.spunkycharts.ApplicationPreferences.java
com.jogden.spunkycharts.BaseChartFragmentA.java
com.jogden.spunkycharts.ChartPanelSurfaceView.java
com.jogden.spunkycharts.DockingPanelActivity.java
com.jogden.spunkycharts.GlobalChartPreferences.java
com.jogden.spunkycharts.InitActivity.java
com.jogden.spunkycharts.MainApplication.java
com.jogden.spunkycharts.OpeningView.java
com.jogden.spunkycharts.animations.BaseAnimationA.java
com.jogden.spunkycharts.animations.BaseEntExAnimationA.java
com.jogden.spunkycharts.animations.BaseSelectAnimationA.java
com.jogden.spunkycharts.animations.HorizontalBulgeAnimation.java
com.jogden.spunkycharts.animations.HorizontalShakeAnimation.java
com.jogden.spunkycharts.animations.VerticalBulgeAnimation.java
com.jogden.spunkycharts.animations.VerticalShakeAnimation.java
com.jogden.spunkycharts.animations.WiggleAnimation.java
com.jogden.spunkycharts.data.DataClientLocalDebug.java
com.jogden.spunkycharts.data.DataContentService.java
com.jogden.spunkycharts.misc.BorderOverlay.java
com.jogden.spunkycharts.misc.ColorPaletteDialog.java
com.jogden.spunkycharts.misc.HideHorizontalLeftOverflowWrapper.java
com.jogden.spunkycharts.misc.OHLC.java
com.jogden.spunkycharts.misc.Pair.java
com.jogden.spunkycharts.misc.TextInput.java
com.jogden.spunkycharts.misc.Triple.java
com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartFragmentAdapter.java
com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartFragment.java
com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartPanel.java
com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartPreferences.java
com.jogden.spunkycharts.pricebyvolumechart.draw.DrawSemanticsA.java
com.jogden.spunkycharts.pricebyvolumechart.draw.DrawSemantics_SILO.java
com.jogden.spunkycharts.traditionalchart.InnerXAxis.java
com.jogden.spunkycharts.traditionalchart.TraditionalChartFragmentAdapter.java
com.jogden.spunkycharts.traditionalchart.TraditionalChartFragment.java
com.jogden.spunkycharts.traditionalchart.TraditionalChartPanel.java
com.jogden.spunkycharts.traditionalchart.TraditionalChartPreferences.java
com.jogden.spunkycharts.traditionalchart.XAxisTimeLabel.java
com.jogden.spunkycharts.traditionalchart.YAxisPriceLabel.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemanticsA_C.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemanticsA.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_CANDLE.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_LINE.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_OC.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_OHLC.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_POINT.java
com.jogden.spunkycharts.traditionalchart.draw.DrawSemantics_SILO.java