com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.jogden.spunkycharts.pricebyvolumechart.PriceByVolumeChartFragment.java

Source

package com.jogden.spunkycharts.pricebyvolumechart;

/* 
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 android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLayoutChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jogden.spunkycharts.ApplicationPreferences;
import com.jogden.spunkycharts.BaseChartFragmentA;
import com.jogden.spunkycharts.MainApplication;
import com.jogden.spunkycharts.R;
import com.jogden.spunkycharts.data.DataContentService;
import com.jogden.spunkycharts.traditionalchart.TraditionalChartPanel;
import com.jogden.spunkycharts.traditionalchart.TraditionalChartPreferences;

public class PriceByVolumeChartFragment extends BaseChartFragmentA {
    /* our important inner views */
    private LinearLayout _yAxis;
    private LinearLayout _xAxis;
    private PriceByVolumeChartPanel _mainPanel;
    private LinearLayout _rightPanel;

    /* all the internal logical and view management*/
    private PriceByVolumeChartFragmentAdapter _chartAdapter;
    private PriceByVolumeChartFragment _this = this;
    private ViewGroup _myViewGroup = null;
    private String _symbol;

    /* private settings */
    private int _ySegHeight = ApplicationPreferences.getSegmentWidth();
    private float _segThickness = TraditionalChartPreferences.getDefaultSegThickness();
    private int _foreColor = TraditionalChartPreferences.getDefaultForegroundColor();
    private int _backColor = TraditionalChartPreferences.getDefaultBackgroundColor();

    /* cache these to avoid myriad getter calls */
    static boolean majorMarkFlag = ApplicationPreferences.getShowMajorMark();
    static boolean minorMarkFlag = ApplicationPreferences.getShowMinorMark();

    /* used to draw ALL axis marks of this chart type */
    public static final Paint axisMarkBrush = new Paint(Paint.ANTI_ALIAS_FLAG);
    static {
        axisMarkBrush.setStrokeWidth(ApplicationPreferences.getAxisMarkThickness());
        axisMarkBrush.setColor(ApplicationPreferences.getAxisMarkColor());
    }

    /*|--------->         begin PUBLIC interface         <--------|*/
    /* getter/setters for universal preferences*/
    static public void setAxisMarkColor(int colorId) {
        axisMarkBrush.setColor(colorId);
    }

    static public int getAxisMarktColor() {
        return axisMarkBrush.getColor();
    }

    static public void setAxisMarkThickness(int pixSize) {
        axisMarkBrush.setStrokeWidth(pixSize);
    }

    static public float getAxisMarkThickness() {
        return axisMarkBrush.getStrokeWidth();
    }

    static public void setShowMinorMark(boolean show) {
        minorMarkFlag = show;
    }

    static public boolean getShowMinorMark() {
        return minorMarkFlag;
    }

    static public void setShowMajorMark(boolean show) {
        majorMarkFlag = show;
    }

    static public boolean getShowMajorMark() {
        return majorMarkFlag;
    }

    @Override
    public void setAxisFontSize(int sp) {
        if (_chartAdapter != null)
            _chartAdapter.setAxisFontSize(sp);
    }

    @Override
    public void setAxisFontColor(int colorId) {
        if (_chartAdapter != null)
            _chartAdapter.setAxisFontColor(colorId);
    }

    @Override
    public void setXAxisPaddingSize(int pixSize) {
        // if(_chartAdapter != null)
        //  _chartAdapter.setXAxisPaddingSize(pixSize);
    }

    @Override
    public void setAxisTimeout(int value) {
        if (_chartAdapter != null)
            _chartAdapter.setAxisTimeout(value);
    }

    @Override
    public void setTimeoutIncrement(int value) {
        if (_chartAdapter != null)
            _chartAdapter.setTimeoutIncrement(value);
    }

    @Override
    public void refresh() {
        if (MainApplication.readyForData)
            DataContentService.load(_symbol, _chartAdapter);
        _chartAdapter.reset();
        //  _mainPanel.forceDraw();
    }

    //
    // TODO: local chart preferences   
    //

    @Override
    public void hide() {
        super.hide();
        _mainPanel.forceStop();
    }

    @Override
    public void show() {
        super.show();
        _mainPanel.forceStart();
    }

    /*|--------->         end PUBLIC interface         <--------|*/
    /*|--------->     begin PROTECTED interface     <--------|*/

    protected void setForegroundColor(int colorId) {
        _foreColor = colorId;
        _yAxis.setBackgroundColor(colorId);
        _xAxis.setBackgroundColor(colorId);
        super.setColor(colorId);
    }

    protected int getForegroundColor() {
        return _foreColor;
    }

    protected void setBackgroundColor(int colorId) {
        _backColor = colorId;
        _myViewGroup.setBackgroundColor(colorId);
    }

    protected int getBackgroundColor() {
        return _backColor;
    }

    protected void setSegmentThickness(float thickness) {
        _mainPanel.setSegmentThickness(thickness);
        _mainPanel.forceDraw();
        _segThickness = thickness;
    }

    protected float getSegmentThickness() {
        return _segThickness;
    }

    @Override
    protected void setRefreshRate(int refreshRate) {
        super.setRefreshRate(refreshRate);
    }

    @Override /* should be a mirror of onSavedState handler */
    protected void extractState(Bundle savedState) {
        if (savedState == null)
            return;
        if (_symbol == null)
            _symbol = savedState.getString("symbol");
        if (_symbol == null)
            throw new IllegalStateException("ChartFragment does not have valid symbol string");
        //
        //  TODO: sync with local preferences
        //
        super.extractState(savedState);
    }

    /* how to handle a held/long touch of the frag view */
    @Override
    protected void onHoldDown() {
        //
        //TODO: implement hold down behavior
        //
    }

    @Override
    protected void onStacked(BaseChartFragmentA base, BaseChartFragmentA oldTop, BaseChartFragmentA newTop) {
        //
        //TODO: implement stack behavior
        //
    }

    @Override
    protected void onDeStacked(BaseChartFragmentA base, BaseChartFragmentA oldTop, BaseChartFragmentA newTop) {
        //
        //TODO: implement de-stack behavior
        //
    }

    /*|--------->         end PROTECTED interface         <--------|*/
    /*|--------->         begin PRIVATE methods         <--------|*/

    private void _syncPanels() {
        final TextView tv = (TextView) _rightPanel.findViewById(R.id.xAxis_mirror);
        /* synch X-Axis heights */
        _xAxis.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                    int oldRight, int oldBottom) {
                tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        (int) (bottom - top)));
                _rightPanel.invalidate();
            }
        });
    }

    /*|--------->         end PRIVATE methods         <--------|*/
    /*|--------->     begin LIFE-CYCLE handlers     <--------|*/

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        _symbol = this.getArguments().getString("symbol");
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        super.onCreateView(inflater, container, savedState);
        _myViewGroup = (ViewGroup) inflater.inflate(R.layout.chart_fragment_pricebyvolume, ourViewGroup, true)
                .findViewById(R.id.pbv_chart_main);
        _yAxis = (LinearLayout) _myViewGroup.findViewById(R.id.label_yAxis);
        _xAxis = (LinearLayout) _myViewGroup.findViewById(R.id.label_xAxis);
        _mainPanel = (PriceByVolumeChartPanel) _myViewGroup.findViewById(R.id.main_lay);
        _rightPanel = (LinearLayout) _myViewGroup.findViewById(R.id.chart_right_panel);
        _syncPanels();
        setBackgroundColor(_backColor);
        setForegroundColor(_foreColor);
        return ourViewGroup;
    }

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);
        _chartAdapter = new PriceByVolumeChartFragmentAdapter(parentActivity, new Handler(),
                MainApplication.getLocalBroadcastManager(), _mainPanel, _xAxis, _yAxis, _myViewGroup, _ySegHeight,
                null, _symbol);
        DataContentService.addChannel(_symbol, _chartAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (MainApplication.readyForData)
            DataContentService.load(_symbol, _chartAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle savedState) {
        savedState.putString("symbol", _symbol);
        super.onSaveInstanceState(savedState);
        //
        //TODO: sync with local prefs
        //
    }

    @Override
    public void onStop() {
        super.onStop();
        _chartAdapter.cleanUpAndUpdate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        DataContentService.removeChannel(_symbol, _chartAdapter);
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}