ua.southwall.screenpicker.app.Fragments.BarFragment.java Source code

Java tutorial

Introduction

Here is the source code for ua.southwall.screenpicker.app.Fragments.BarFragment.java

Source

/*
 *       Created by Daniel Nadeau
 *       daniel.nadeau01@gmail.com
 *       danielnadeau.blogspot.com
 * 
 *       Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you 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 ua.southwall.screenpicker.app.Fragments;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
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.TextView;
import android.widget.Toast;

import com.echo.holographlibrary.Bar;
import com.echo.holographlibrary.BarGraph;
import com.echo.holographlibrary.BarGraph.OnBarClickedListener;

import org.json.JSONException;

import java.util.ArrayList;

import ua.southwall.screenpicker.app.R;
import ua.southwall.screenpicker.app.ResultsActivity;

public class BarFragment extends Fragment {
    String[] deviceModel;
    String[] deviceHeight;
    String[] deviceWidth;
    String[] devicePpi;
    String[] deviceDiagonal;

    Bar[] d;
    Bar[] b;
    Bar[] c;
    Bar[] e;

    int percentage;
    float area;
    double tangens_average = 0.0001454441053584;//real instead
    double tangens_ideal = 0.0000872664628212405;//fake
    Double[] resolutionDistanceAverage;
    Double[] thirdGraph;
    Integer[] numberOfPixels;
    int[] colors = { R.color.tealColor, R.color.steelBlueColor, R.color.robinEggColor, R.color.pastelBlueColor,
            R.color.turquoiseColor, R.color.skyBlueColor, R.color.indigoColor, R.color.denimColor,
            R.color.blueberryColor, R.color.cornflowerColor, };
    int pixelsBasis;
    int numberOfDevices;
    int clickedFirst = -10;
    int clickedSecond = -10;
    int clickedThird = -10;
    int clickedForth = -10;
    String prefValue = "";

    public static final BarFragment newInstance(String paramString) {
        BarFragment f = new BarFragment();
        Bundle localBundle = new Bundle(1);
        localBundle.putString("EXTRA_MESSAGE", paramString);
        f.setArguments(localBundle);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_bargraph, container, false);
        final Resources resources = getResources();
        ArrayList<Bar> points = new ArrayList<Bar>();
        ArrayList<Bar> pointsNew = new ArrayList<Bar>();
        ArrayList<Bar> pointsNewer = new ArrayList<Bar>();
        ArrayList<Bar> pointsEvenNewer = new ArrayList<Bar>();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getActivity().getApplicationContext());
        prefValue = prefs.getString("prefSyncFrequency", prefValue);

        try {
            deviceModel = ResultsActivity.getJSON("deviceModel");
            deviceHeight = ResultsActivity.getJSON("height");
            deviceWidth = ResultsActivity.getJSON("width");
            devicePpi = ResultsActivity.getJSON("ppi");
            deviceDiagonal = ResultsActivity.getJSON("diagonal");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        numberOfDevices = deviceModel.length;
        // if(numberOfDevices>10) numberOfDevices=10;
        //T
        // for(int i=1;i<numberOfDevices;i++){
        //     if (devicePpi[i].equals("(None)")||devicePpi[i].equals("(None)"))

        // }

        //double resolutionDistanceIdeal = 1 / (2 * Float.parseFloat(devicePpi[1]) * tangens_ideal);
        resolutionDistanceAverage = new Double[deviceModel.length];
        //////Log.e(devicePpi)
        resolutionDistanceAverage[1] = 1 / (2 * Float.parseFloat(devicePpi[1]) * tangens_average);

        for (int i = 0; i < numberOfDevices; i++) {

        }
        d = new Bar[numberOfDevices];
        for (int i = 0; i < numberOfDevices; i++) {
            d[i] = new Bar();
        }
        b = new Bar[numberOfDevices];
        for (int i = 0; i < numberOfDevices; i++) {
            b[i] = new Bar();
        }
        c = new Bar[numberOfDevices];
        for (int i = 0; i < numberOfDevices; i++) {
            c[i] = new Bar();
        }
        e = new Bar[numberOfDevices];

        for (int i = 0; i < numberOfDevices; i++) {
            e[i] = new Bar();
        }
        //d[1].setColor(resources.getColor(colors[1]));
        //d[1].setSelectedColor(resources.getColor(colors[1]));
        //d[1].setName(deviceModel[1]);
        pixelsBasis = Integer.parseInt(deviceHeight[1]) * Integer.parseInt(deviceWidth[1]);

        //d[1].setValueString(String.valueOf(100));
        //points.add(d[1]);

        numberOfPixels = new Integer[numberOfDevices];
        for (int i = 1; i < numberOfDevices; i++) {
            d[i].setColor(resources.getColor(colors[i - 1]));
            d[i].setSelectedColor(resources.getColor(colors[i - 1]));
            d[i].setName(deviceModel[i]);
            numberOfPixels[i] = Integer.parseInt(deviceHeight[i]) * Integer.parseInt(deviceWidth[i]);
            d[i].setValue((float) numberOfPixels[i]);
            d[i].setValueString(String.valueOf(Math.round((numberOfPixels[i] / 1000.0f)) / 1000.0f));
            if (i != 0)
                points.add(d[i]);

        }
        BarGraph g = (BarGraph) v.findViewById(R.id.bargraph);
        g.setBars(points);
        g.setOnBarClickedListener(new OnBarClickedListener() {

            @Override
            public void onClick(int index) {
                Toast.makeText(getActivity(), deviceModel[index + 1] + " " + getString(R.string.bar_value) + " "
                        + numberOfPixels[index + 1], Toast.LENGTH_SHORT).show();
                //recalculate to view percentages
                if (clickedFirst != index) {
                    d[index + 1].setValueString("100%");
                    for (int i = 1; i < numberOfDevices; i++) {
                        if (i != index + 1)
                            d[i].setValueString(
                                    String.valueOf(Math.round(numberOfPixels[i] * 100 / numberOfPixels[index + 1]))
                                            + "%");

                    }
                    clickedFirst = index;
                } else {
                    for (int i = 1; i < numberOfDevices; i++) {
                        d[i].setValueString(String.valueOf(Math.round((numberOfPixels[i] / 1000.0f)) / 1000.0f));
                    }
                    clickedFirst = -10;
                }
            }
        });

        // b[1].setColor(resources.getColor(colors[1]));
        // b[1].setSelectedColor(resources.getColor(colors[1]));
        // b[1].setName(deviceModel[1]);
        //pixelsBasis=Integer.parseInt(deviceHeight[1]) * Integer.parseInt(deviceWidth[1]);

        // b[1].setValueString(String.valueOf(resolutionDistanceAverage) + "");

        for (int i = 1; i < numberOfDevices; i++) {
            b[i].setColor(resources.getColor(colors[i - 1]));
            b[i].setSelectedColor(resources.getColor(colors[i - 1]));
            b[i].setName(deviceModel[i]);
            percentage = Integer.parseInt(deviceHeight[i]) * Integer.parseInt(deviceWidth[i]) * 100 / pixelsBasis;
            resolutionDistanceAverage[i] = 1 / (2 * Float.parseFloat(devicePpi[i]) * tangens_average);
            if (prefValue.equals("cm"))
                resolutionDistanceAverage[i] = resolutionDistanceAverage[i] * 2.54f;
            resolutionDistanceAverage[i] = (Math.round(resolutionDistanceAverage[i] * 10.0)) / 10.0;

            b[i].setValue((float) (resolutionDistanceAverage[i] * 1.0f));
            b[i].setValueString(String.valueOf(resolutionDistanceAverage[i]));
            if (i != 0)
                pointsNew.add(b[i]);

        }

        BarGraph g1 = (BarGraph) v.findViewById(R.id.bargraph_distance);
        g1.setBars(pointsNew);
        g1.setOnBarClickedListener(new OnBarClickedListener() {

            @Override
            public void onClick(int index) {
                Toast.makeText(getActivity(), deviceModel[index + 1] + " " + getString(R.string.bar_value) + " "
                        + resolutionDistanceAverage[index + 1], Toast.LENGTH_SHORT).show();
                if (clickedSecond != index) {
                    b[index + 1].setValueString("100%");
                    for (int i = 1; i < numberOfDevices; i++) {
                        if (i != index + 1)
                            b[i].setValueString(String.valueOf(Math.round(
                                    resolutionDistanceAverage[i] * 100 / resolutionDistanceAverage[index + 1]))
                                    + "%");

                    }
                    clickedSecond = index;
                } else {
                    for (int i = 1; i < numberOfDevices; i++) {
                        b[i].setValueString(String.valueOf(resolutionDistanceAverage[i]));
                    }
                    clickedSecond = -10;

                }
            }

        });

        thirdGraph = new Double[numberOfDevices];
        for (int i = 1; i < numberOfDevices; i++) {
            c[i].setColor(resources.getColor(colors[i - 1]));
            c[i].setSelectedColor(resources.getColor(colors[i - 1]));
            c[i].setName(deviceModel[i]);
            //(float)Math.pow(1/Float.parseFloat(devicePpi[1]),2)
            thirdGraph[i] = Math.pow(1 / Float.parseFloat(devicePpi[i]), 2);
            if (prefValue.equals("cm"))
                thirdGraph[i] = thirdGraph[i] * 2.54 * 2.54;
            thirdGraph[i] = ((float) Math.round(thirdGraph[i] * 1000000 * 100)) / 100.0;

            c[i].setValue((float) (thirdGraph[i] * 1.0f));
            c[i].setValueString(String.valueOf(thirdGraph[i]));
            if (i != 0)
                pointsNewer.add(c[i]);

        }
        BarGraph g2 = (BarGraph) v.findViewById(R.id.bargraph_size);
        g2.setBars(pointsNewer);
        g2.setOnBarClickedListener(new OnBarClickedListener() {

            @Override
            public void onClick(int index) {
                Toast.makeText(getActivity(),
                        deviceModel[index + 1] + " " + getString(R.string.bar_value) + " " + thirdGraph[index + 1],
                        Toast.LENGTH_SHORT).show();

                if (clickedThird != index) {
                    c[index + 1].setValueString("100%");
                    for (int i = 1; i < numberOfDevices; i++) {
                        if (i != index + 1)
                            c[i].setValueString(
                                    String.valueOf(Math.round(thirdGraph[i] * 100 / thirdGraph[index + 1])) + "%");

                    }
                    clickedThird = index;
                } else {
                    for (int i = 1; i < numberOfDevices; i++) {
                        c[i].setValueString(String.valueOf(thirdGraph[i]));
                    }
                    clickedThird = -10;
                }
            }

        });

        final Double[] forthGraph = new Double[numberOfDevices];
        for (int i = 1; i < numberOfDevices; i++) {
            e[i].setColor(resources.getColor(colors[i - 1]));
            e[i].setSelectedColor(resources.getColor(colors[i - 1]));
            e[i].setName(deviceModel[i]);
            //(float)Math.pow(1/Float.parseFloat(devicePpi[1]),2)
            forthGraph[i] = (Double.valueOf(deviceHeight[i]) / Double.valueOf(devicePpi[i]))
                    * (Double.valueOf(deviceWidth[i]) / Double.valueOf(devicePpi[i]));
            if (prefValue.equals("cm"))
                forthGraph[i] = forthGraph[i] * 2.54 * 2.54;
            forthGraph[i] = ((float) Math.round(forthGraph[i] * 100)) / 100.0;

            e[i].setValue((float) (forthGraph[i] * 1.0f));
            e[i].setValueString(String.valueOf(forthGraph[i]));
            if (i != 0)
                pointsEvenNewer.add(e[i]);

        }
        BarGraph g3 = (BarGraph) v.findViewById(R.id.bargraph_area);
        g3.setBars(pointsEvenNewer);
        g3.setOnBarClickedListener(new OnBarClickedListener() {

            @Override
            public void onClick(int index) {
                Toast.makeText(getActivity(),
                        deviceModel[index + 1] + " " + getString(R.string.bar_value) + " " + forthGraph[index + 1],
                        Toast.LENGTH_SHORT).show();

                if (clickedForth != index) {
                    e[index + 1].setValueString("100%");
                    for (int i = 1; i < numberOfDevices; i++) {
                        if (i != index + 1)
                            e[i].setValueString(
                                    String.valueOf(Math.round(forthGraph[i] * 100 / forthGraph[index + 1])) + "%");

                    }
                    clickedForth = index;
                } else {
                    for (int i = 1; i < numberOfDevices; i++) {
                        e[i].setValueString(String.valueOf(forthGraph[i]));
                    }
                    clickedForth = -10;
                }
            }

        });

        Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");

        TextView firstBarView = (TextView) v.findViewById(R.id.bar_title_textview);
        TextView secondBarView = (TextView) v.findViewById(R.id.bar_title_textview_distance);
        TextView thirdBarView = (TextView) v.findViewById(R.id.bar_title_);
        TextView forthBarView = (TextView) v.findViewById(R.id.bar_title_textview_area);
        if (prefValue.equals("in")) {
            secondBarView.setText(R.string.distance_bargraph);
            thirdBarView.setText(R.string.textview_bargraph_size);
            forthBarView.setText(R.string.area_bargraph);
        } else if (prefValue.equals("cm")) {
            secondBarView.setText(R.string.distance_bargraph_cm);
            thirdBarView.setText(R.string.textview_bargraph_size_cm);
            forthBarView.setText(R.string.area_bargraph_cm);

        }

        firstBarView.setTypeface(typeface);
        secondBarView.setTypeface(typeface);
        thirdBarView.setTypeface(typeface);
        forthBarView.setTypeface(typeface);

        return v;
    }
}