klawisch.jimdo.statistics.statisticsdemo.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for klawisch.jimdo.statistics.statisticsdemo.MainActivity.java

Source

/*
The MIT License (MIT)
    
Copyright (c) 2015 Justus Klawisch
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */

package klawisch.jimdo.statistics.statisticsdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;

import klawisch.jimdo.statistics.statisticsdemo.model.DayStatistics;
import klawisch.jimdo.statistics.statisticsdemo.pages.ClicksPerHourFragment;
import klawisch.jimdo.statistics.statisticsdemo.pages.LineGraphFragment;
import klawisch.jimdo.statistics.statisticsdemo.pages.TopPagesFragment;
import klawisch.jimdo.statistics.statisticsdemo.view.CustomProgressBar;

public class MainActivity extends FragmentActivity implements DownloaderThread.Callback {
    private static final String TAG = MainActivity.class.getSimpleName();
    private CustomProgressBar customProgressBar;
    private RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
         * Register the pages you want to display in the viewpager
         * You can register as much pages as you want, as long as they extend APagerFragment!
         */
        PagerCatalog.register(initLineGraph(7), "Last seven days");
        PagerCatalog.register(initLineGraph(30), "Last thirty days");
        PagerCatalog.register(new TopPagesFragment(), "Top pages");
        PagerCatalog.register(new ClicksPerHourFragment(), "Clicks per interval");

        initProgressBar();

        /*
         * Start the download process
         */
        DownloaderThread.startDownload(this, "website_statistics.json");
    }

    /**
     * Creates a LineGraph displaying fragment
     *
     * @param daysToDisplay Set the amount of days you want the graph to display
     * @return The PagerFragment
     */
    private LineGraphFragment initLineGraph(int daysToDisplay) {
        LineGraphFragment lineGraph = new LineGraphFragment();
        Bundle b2 = new Bundle();
        b2.putInt("daysToDisplay", daysToDisplay);
        lineGraph.setArguments(b2);
        return lineGraph;
    }

    private void initProgressBar() {
        layout = (RelativeLayout) findViewById(R.id.relativeLayout);
        customProgressBar = new CustomProgressBar(this);
        customProgressBar.setColor(Color.parseColor("#0097df"));
        customProgressBar.setStrokeWidth(20);
        customProgressBar.setDiameter(500);
        //        customProgressBar.setLogProgress(true);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        customProgressBar.setLayoutParams(params);
    }

    /**
     * Called by DownloaderThread when the download has started
     */
    @Override
    public void downloadStarted() {
        setLoadingscreenEnabled(true);
    }

    /**
     * Called by DownloaderThread when the download has finished
     * Initializes the view pager and its adapter
     *
     * @param statistics The downloaded website statistics
     */
    @Override
    public void downloadFinished(ArrayList<DayStatistics> statistics) {
        setLoadingscreenEnabled(false);

        // Transmit the downloaded content to all registered pages
        PagerCatalog.transmitStatisticsData(statistics);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
                PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
                viewPager.setAdapter(adapter);
            }
        });
    }

    /**
     * Called by DownloadedThread. Used to update the progressbar
     *
     * @param downloaded Number of downloaded items at calling time
     * @param total      Total number of items to download
     */
    @Override
    public void updateProgress(int downloaded, int total) {

        float factor = 100f / total;
        final float currentPercent = downloaded * factor;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                customProgressBar.setProgress(Math.round(currentPercent));
            }
        });
    }

    /**
     * Enables and disables the progressbar
     *
     * @param enabled
     */
    private void setLoadingscreenEnabled(final boolean enabled) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (enabled) {
                    layout.addView(customProgressBar);
                } else {
                    layout.removeView(customProgressBar);
                }
            }
        });
    }
}