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

Java tutorial

Introduction

Here is the source code for de.aw.monma.mainscreen.ActivityMainScreen.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.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import java.util.Date;

import de.aw.awlib.activities.AWActivityMainScreen;
import de.aw.awlib.activities.AWWebViewActivity;
import de.aw.awlib.adapters.AWViewPagerAdapter;
import de.aw.awlib.recyclerview.AWBaseRecyclerViewListener;
import de.aw.awlib.utils.AWUtils;
import de.aw.monma.R;
import de.aw.monma.actions.ActivityActions;
import de.aw.monma.actions.dialoge.DialogFragmentKontoEdit;
import de.aw.monma.cash.ActivityCash;
import de.aw.monma.database.DBDefinition;
import de.aw.monma.database.DBHelper;
import de.aw.monma.gv.Account;
import de.aw.monma.gv.GeschaeftsObjekt;
import de.aw.monma.hbci.ActivityHBCIActions;
import de.aw.monma.hbci.HBCIInterface;
import de.aw.monma.hbci.hbcigv.MonMaPassport;
import de.aw.monma.monmamain.ConvertWPIncomeTask;
import de.aw.monma.monmamain.MonMaApplication;
import de.aw.monma.monmamain.MonMaFragment;
import de.aw.monma.monmamain.MonMaInterface;
import de.aw.monma.monmamain.preferences.MonMaPreferenceActivity;
import de.aw.monma.reports.ActivityReports;
import de.aw.monma.reports.DialogReportDaten;
import de.aw.monma.wertpapier.ActivityWertpapiere;

public class ActivityMainScreen extends AWActivityMainScreen
        implements MonMaInterface, HBCIInterface, AWBaseRecyclerViewListener,
        NavigationView.OnNavigationItemSelectedListener, PopupMenu.OnMenuItemClickListener {
    /**
     * Titel der Tabs
     */
    private static final int[] tableTitlesResIDs = new int[] { R.string.finanzUebersicht, R.string.Reports,
            R.string.tvDepotUebersicht, R.string.OnlineBanking };

    protected FragmentPagerAdapter getFragmentPagerAdapter() {
        return new ActivityMainViewPagerAdapter(this, getSupportFragmentManager(), tableTitlesResIDs);
    }

    @Override
    protected int getNavigationMenuID() {
        return R.menu.navigationdrawer;
    }

    @Override
    public int getNavigationTitel() {
        return R.string.finanzUebersichtTitel;
    }

    @Override
    public void onClick(View v) {
        MonMaFragment d;
        switch (v.getId()) {
        case R.id.awlib_defaultFAB:
            int currentItem = getPager().getCurrentItem();
            switch (currentItem) {
            case 0:
                d = DialogFragmentKontoEdit.newInstance(new Account());
                d.show(getSupportFragmentManager(), null);
                break;
            case 1:
                d = new DialogReportDaten();
                d.show(getSupportFragmentManager(), null);
                break;
            case 3:
                if (AWUtils.hasInternetConnection(this)) {
                    Intent intent = new Intent(this, ActivityHBCIActions.class);
                    intent.putExtra(ACTION, (Parcelable) Action.AddHBCIZugang);
                    startActivity(intent);
                } else {
                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
                    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();
                }
                break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getDefaultFAB().setOnClickListener(this);
        getDefaultFAB().setVisibility(View.VISIBLE);
        //noinspection ConstantConditions
        setTitle(R.string.finanzUebersichtTitel);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        if (prefs.getBoolean(getString(R.string.pref_OnlineUms), false)) {
            getMenuInflater().inflate(R.menu.item_hbci_ums_vorhanden, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Bundle extras = item.getIntent().getExtras();
        long id = extras.getLong(ID, NOID);
        switch (item.getItemId()) {
        case R.id.context_menu_item_deleteReport:
            DBDefinition reportTbd = DBDefinition.ReportBasisdaten;
            String selection = _id + " = " + id;
            DBHelper db = DBHelper.getInstance();
            db.delete(reportTbd, selection, null);
            return true;
        case R.id.context_menu_item_editReport:
            DialogReportDaten d = DialogReportDaten.newInstance(id);
            d.show(getSupportFragmentManager(), d.getTAG());
            return true;
        }
        return false;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        mDrawerLayout.closeDrawer(Gravity.START, false);
        Intent intent = null;
        switch (item.getItemId()) {
        case R.id.nav_Convert:
            new ConvertWPIncomeTask(this).execute();
            break;
        case R.id.nav_UnassignedSales:
            intent = new Intent();
            intent.setClass(this, ActivityCash.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowUnassignedSales);
            intent.putExtra(FROMDATE, new Date(0).getTime());
            intent.putExtra(TODATE, new Date().getTime());
            break;
        case R.id.nav_HBCILog:
            intent = new Intent();
            intent.setClass(this, ActivityHBCIActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowLogFile);
            intent.putExtra(LOGFILE, MonMaApplication.HBCILOGFilename);
            break;
        case R.id.nav_Uebersicht:
            intent = new Intent();
            intent.setClass(this, ActivityActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowUebersicht);
            break;
        case R.id.nav_Cats:
            intent = new Intent();
            intent.setClass(this, ActivityActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowCats);
            break;
        case R.id.nav_Payee:
            intent = new Intent();
            intent.setClass(this, ActivityActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowPayees);
            break;
        case R.id.nav_Wertpapiere:
            intent = new Intent();
            intent.setClass(this, ActivityWertpapiere.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowWPliste);
            break;
        case R.id.nav_Accounts:
            break;
        case R.id.nav_Settings:
            intent = new Intent();
            intent.setClass(this, MonMaPreferenceActivity.class);
            break;
        default:
            return super.onNavigationItemSelected(item);
        }
        if (intent != null) {
            startActivity(intent);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        boolean consumed;
        mDrawerLayout.closeDrawer(Gravity.START, false);
        switch (item.getItemId()) {
        case R.id.menu_item_hbci_ums_vorhanden:
            item.setVisible(false);
            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            prefs.edit().putBoolean(getString(R.string.pref_OnlineUms), false).apply();
            intent = new Intent(this, ActivityHBCIActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowNeueOnlineUmsaetze);
            startActivity(intent);
            consumed = true;
            break;
        case R.id.menu_item_Ertraege:
            intent = new Intent(this, ActivityActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.EditErtraege);
            startActivity(intent);
            consumed = true;
            break;
        case R.id.awlib_menu_item_hilfe:
            intent = new Intent(this, AWWebViewActivity.class);
            intent.putExtra(ID, "hilfe_finanzuebersicht.html");
            startActivity(intent);
            consumed = true;
            break;
        case R.id.menu_item_show_AllBuchungRegelm:
            intent = new Intent(this, ActivityCash.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowRegelmBuchungen);
            startActivity(intent);
            consumed = true;
            break;
        default:
            consumed = super.onOptionsItemSelected(item);
            break;
        }
        return consumed;
    }

    @Override
    public void onPageSelected(int position) {
        View view = findViewById(R.id.awlib_defaultFAB);
        switch (position) {
        case 2:
            // Kein FAB bei Depotuebersicht
            view.setVisibility(View.GONE);
            break;
        default:
            view.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRecyclerItemClick(RecyclerView parent, View view, int position, long id, int viewHolderLayoutID) {
        Intent intent = new Intent();
        int accountID = (int) id;
        switch (viewHolderLayoutID) {
        case R.layout.finanzuebersicht_item:
            if (id < 0) {
                return;
            }
            Account acc = new Account(accountID);
            intent.putExtra(ACTION, (Parcelable) Action.ShowBuchungen);
            intent.putExtra(ACCOUNT, acc);
            switch (acc.getAccountTyp()) {
            case Port:
                intent.setClass(this, ActivityWertpapiere.class);
                break;
            default:
                intent.setClass(this, ActivityCash.class);
                break;
            }
            startActivity(intent);
            break;
        case R.layout.hbcipassport_item:
            intent.setClass(this, ActivityHBCIActions.class);
            intent.putExtra(ACTION, (Parcelable) Action.ShowPassportAccounts);
            intent.putExtra(ID, id);
            startActivity(intent);
            break;
        case R.layout.reportlist_item:
            intent.setClass(this, ActivityReports.class);
            intent.putExtra(REPORTID, id);
            startActivity(intent);
            break;
        }
    }

    @Override
    public boolean onRecyclerItemLongClick(RecyclerView recyclerView, View view, int position, long id,
            int viewHolderLayoutID) {
        switch (viewHolderLayoutID) {
        case R.layout.reportlist_item:
            PopupMenu popUpMenu = new PopupMenu(this, view);
            Menu menu = popUpMenu.getMenu();
            popUpMenu.getMenuInflater().inflate(R.menu.context_fragment_report_selection, menu);
            for (int i = 0; i < menu.size(); i++) {
                Intent intent = new Intent();
                intent.putExtra(ID, id);
                menu.getItem(i).setIntent(intent);
            }
            popUpMenu.setOnMenuItemClickListener(this);
            popUpMenu.show();
            return true;
        case R.layout.finanzuebersicht_item:
            DialogFragmentKontoEdit d = DialogFragmentKontoEdit.newInstance(new Account((int) id));
            d.show(getSupportFragmentManager(), null);
            return true;
        case R.layout.hbcipassport_item:
            final long mID = id;
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.HBCIZugangDelete);
            builder.setMessage(R.string.HBCIZugangDeleteMsg);
            builder.setPositiveButton(getString(R.string.awlib_btnAccept), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        MonMaPassport passport = new MonMaPassport(mID);
                        passport.delete(DBHelper.getInstance());
                    } catch (GeschaeftsObjekt.LineNotFoundException e) {
                        //TODO Execption bearbeiten
                        e.printStackTrace();
                    }
                }
            });
            builder.setNegativeButton(getString(R.string.awlib_btnCancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.create().show();
            return true;
        default:
            return false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDrawerLayout.closeDrawers();
    }

    public class ActivityMainViewPagerAdapter extends AWViewPagerAdapter {
        ActivityMainViewPagerAdapter(Context context, FragmentManager fm, int[] tableTitlesResIDs) {
            super(context, fm, tableTitlesResIDs);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
            case 0:
                f = new FragmentFinanzen();
                break;
            case 1:
                f = new FragmentReportList();
                break;
            case 2:
                f = new FragmentDepots();
                break;
            case 3:
                f = new FragmentPassport();
                break;
            }
            return f;
        }
    }
}