com.softminds.matrixcalculator.OperationFragments.TraceFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.softminds.matrixcalculator.OperationFragments.TraceFragment.java

Source

/*
 * Copyright (C) 2017 Ashar Khan <ashar786khan@gmail.com>
 *
 * This file is part of Matrix Calculator.
 *
 * Matrix Calculator 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.
 *
 * Matrix Calculator 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 Matrix Calculator.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.softminds.matrixcalculator.OperationFragments;

import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.softminds.matrixcalculator.MatrixV2;
import com.softminds.matrixcalculator.MatrixAdapter;
import com.softminds.matrixcalculator.R;
import com.softminds.matrixcalculator.GlobalValues;

import java.text.DecimalFormat;
import java.util.ArrayList;

@SuppressWarnings("ConstantConditions")
public class TraceFragment extends ListFragment {

    ArrayList<MatrixV2> SquareList;
    String TAG = this.getClass().getSimpleName();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SquareList = new ArrayList<>();
        for (int i = 0; i < ((GlobalValues) getActivity().getApplication()).GetCompleteList().size(); i++) {
            if (((GlobalValues) getActivity().getApplication()).GetCompleteList().get(i).isSquareMatrix())
                SquareList.add(((GlobalValues) getActivity().getApplication()).GetCompleteList().get(i));
        }

        MatrixAdapter adapter = new MatrixAdapter(getContext(), R.layout.list_layout_fragment, SquareList);
        getListView().setDividerHeight(1);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ProgressDialog progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage(getString(R.string.Calculating));
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setIndeterminate(true);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
        RunToGetTrace(position, progressDialog);
    }

    private void RunToGetTrace(int position, ProgressDialog progressDialog) {
        double result = ((GlobalValues) getActivity().getApplication()).GetCompleteList().get(position).getTrace();
        final String formatted = GetText(result);
        progressDialog.dismiss();
        String formatted2 = getString(R.string.trace_is) + " " + formatted;
        new AlertDialog.Builder(getContext()).setCancelable(true).setMessage(formatted2)
                .setTitle(R.string.trace_text)
                .setPositiveButton(R.string.copy, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ClipboardManager manager = (ClipboardManager) getContext()
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData data = ClipData.newPlainText("TRACE_RES", formatted);
                        manager.setPrimaryClip(data);
                        if (manager.hasPrimaryClip())
                            Toast.makeText(getContext(), R.string.CopyToClip, Toast.LENGTH_SHORT).show();
                        else
                            Log.e(TAG, "Cannot Put Data to Clip");
                        dialog.dismiss();
                    }
                }).setNeutralButton(R.string.Done, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private String GetText(double res) {

        if (!PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("DECIMAL_USE", true)) {
            DecimalFormat decimalFormat = new DecimalFormat("###############");
            return decimalFormat.format(res);
        } else {
            switch (Integer.parseInt(
                    PreferenceManager.getDefaultSharedPreferences(getContext()).getString("ROUNDIND_INFO", "0"))) {
            case 0:
                DecimalFormat zeroth = new DecimalFormat("########.######");
                return zeroth.format(res);
            case 1:
                DecimalFormat single = new DecimalFormat("########.#");
                return single.format(res);
            case 2:
                DecimalFormat Double = new DecimalFormat("########.##");
                return Double.format(res);
            case 3:
                DecimalFormat triple = new DecimalFormat("########.###");
                return triple.format(res);
            default:
                DecimalFormat fourth = new DecimalFormat("########.#####");
                return fourth.format(res);
            }
        }
    }
}