com.example.machine2.MyDynamicWorkoutChartFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.example.machine2.MyDynamicWorkoutChartFragment.java

Source

/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.machine2;

import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import org.achartengine.GraphicalView;

public class MyDynamicWorkoutChartFragment extends Fragment {

    private static float chartRefreshSeconds = 1;
    GraphicalView newView;
    private static Thread thread;
    WorkoutSession workoutSession = WorkoutSession.getInstance();
    private MyChart line = new MyChart(workoutSession.getChartData());

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

        drawChart();

        Log.i("MyDynamicWorkoutChar", "..... End of call to drawChart .....");
        thread = new Thread() {
            public void run() {

                while (WorkoutSession.realSession) {

                    try {
                        Thread.sleep((int) (chartRefreshSeconds * 1200));
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    line.resetViewAxis();
                    newView.postInvalidate();
                }
            }
        };
        thread.start();
        newView.postInvalidate();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.my_workout_chart, container, false);

        return rootView;
    }

    public void drawChart() {

        Log.i("MyDynamicWorkoutChar", "..... Start of drawChart .....");

        newView = line.getView(getActivity());
        LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.my_workout_chart_layout);
        layout.addView(newView, new LayoutParams(LayoutParams.MATCH_PARENT));

        Log.i("MyDynamicWorkoutChar", "..... End of drawChart .....");
    }

}