de.aw.monma.mainscreen.FragmentPassport.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.mainscreen.FragmentPassport.java

Source

/*
 * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen
 *
 * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany]
 *
 * 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/>.
 */
package de.aw.monma.mainscreen;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.android.databinding.library.baseAdapters.BR;

import java.io.File;

import de.aw.awlib.csvimportexport.AWCSVExporter;
import de.aw.awlib.database.AbstractDBHelper;
import de.aw.awlib.recyclerview.AWCursorRecyclerViewFragment;
import de.aw.awlib.recyclerview.AWLibViewHolder;
import de.aw.awlib.utils.AWUtils;
import de.aw.awlib.views.AWLoaderTextViewCurrencyValue;
import de.aw.monma.R;
import de.aw.monma.csvexport.CSVExporter;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.hbci.ActivityHBCIActions;
import de.aw.monma.hbci.FragmentMasterPassword;
import de.aw.monma.hbci.HBCIInterface;
import de.aw.monma.hbci.HBCIService;
import de.aw.monma.hbci.hbcigv.MonMaPassport;
import de.aw.monma.monmamain.MonMaInterface;

import static de.aw.awlib.AWResultCodes.RESULT_OK;

/**
 * Zeigt die Liste der bisher konfigurierten Passports fuer Online-Zugaenge
 */
public class FragmentPassport extends AWCursorRecyclerViewFragment
        implements MonMaInterface, HBCIInterface, View.OnClickListener, AWCSVExporter.ExportCursorListener {
    private static final DBDefinition tbd = DBDefinition.MonMaPassportView;
    private static int layout = R.layout.fragment_hbcipassport;
    private static int viewHolderLayout = R.layout.hbcipassport_item;
    private static String[] projection = new String[] { column_hbci_bankname, column_hbci_blz, column_hbci_userID,
            column_hbci_pintan_url, column_hbciversion, column_saldo, column_hbci_userPIN, _id };
    private CSVExporter mCSVExporter;
    private boolean zeigeDetails;

    @Override
    public void onBindViewHolder(AWLibViewHolder holder, Cursor cursor, int position) {
        MonMaPassport item = new MonMaPassport(cursor);
        holder.setVariable(BR.passport, item);
        holder.setHandler(BR.handler, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.imgNoUserPin:
            Toast.makeText(getContext(), "PIN fehlt!", Toast.LENGTH_LONG).show();
            break;
        default:
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_passports, menu);
        inflater.inflate(R.menu.item_hide_details, menu);
        menu.findItem(R.id.menu_item_hideDetails).setChecked(zeigeDetails);
    }

    @Override
    public void onFinishExport(int result) {
        switch (result) {
        case RESULT_OK:
            Snackbar snackbar = Snackbar
                    .make(getView(), de.aw.awlib.R.string.exportErfolgreich, Snackbar.LENGTH_LONG)
                    .setAction(de.aw.awlib.R.string.awlib_openFile, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            File file = new File(mCSVExporter.getFilename());
                            final Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), "text/csv");
                            getActivity().startActivity(intent);
                        }
                    });
            snackbar.show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        boolean consumed = false;
        switch (item.getItemId()) {
        case R.id.menu_item_showUmsaetze:
            intent = new Intent(getActivity(), ActivityHBCIActions.class);
            intent.putExtra(ACTION, (Parcelable) MonMaInterface.Action.ShowNeueOnlineUmsaetze);
            startActivity(intent);
            return true;
        case R.id.menu_item_exportDepots:
            // Export der Wertpapiere der Online-Depots
            DBDefinition tbd = DBDefinition.UmsatzView;
            String[] projection = new String[] { column_wpid, column_partnername, _id,
                    AbstractDBHelper.SQLSumItem(column_bestand) };
            String selection = " GROUP BY " + column_wpid;
            Cursor c = getActivity().getContentResolver().query(tbd.getUri(), projection, selection, null, null);
            if (mCSVExporter == null) {
                mCSVExporter = new CSVExporter(getContext(), this);
            }
            mCSVExporter.doExport(tbd, c);
            consumed = true;
            break;
        case R.id.menu_item_hideDetails:
            zeigeDetails = !zeigeDetails;
            item.setChecked(zeigeDetails);
            getAdapter().notifyDataSetChanged();
            prefs.edit().putBoolean(getString(R.string.cbDetailAnzeigeBank), zeigeDetails).apply();
            consumed = true;
            break;
        case R.id.menu_item_HBCIKontoRundruf:
            if (AWUtils.hasInternetConnection(getActivity())) {
                String key = prefs.getString(getString(R.string.key), null);
                if (key != null) {
                    intent = new Intent(getActivity(), HBCIService.class);
                    intent.putExtra(HBCIACTION, ExecuteHBCICall);
                    intent.putExtra(USERACTION, true);
                    getActivity().startService(intent);
                } else {
                    DialogFragment dlg = new FragmentMasterPassword();
                    dlg.show(getFragmentManager(), null);
                }
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle(R.string.NoInternetConnection);
                builder.setMessage(R.string.NoInternetConnection);
                builder.setPositiveButton(R.string.awlib_btnAccept, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                Dialog dlg = builder.create();
                dlg.show();
            }
            consumed = true;
            break;
        }
        if (!consumed) {
            consumed = super.onOptionsItemSelected(item);
        }
        return consumed;
    }

    @Override
    public void onStartExport() {
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        AWLoaderTextViewCurrencyValue ltv = view.findViewById(R.id.tvOnlineKtoGesamtSaldo);
        DBDefinition tbd = DBDefinition.AccountOnline;
        String projection = AbstractDBHelper.SQLSumItem(column_saldo);
        ltv.initialize(getLoaderManager(), tbd, projection, null, null);
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putInt(LAYOUT, layout);
        args.putParcelable(DBDEFINITION, tbd);
        args.putStringArray(PROJECTION, projection);
        args.putInt(VIEWHOLDERLAYOUT, viewHolderLayout);
        zeigeDetails = prefs.getBoolean(getString(R.string.cbDetailAnzeigeBank), true);
    }

    public boolean showDetails() {
        return zeigeDetails;
    }
}