eu.rubenrosado.tinypasswordmanager.AllRows.java Source code

Java tutorial

Introduction

Here is the source code for eu.rubenrosado.tinypasswordmanager.AllRows.java

Source

/*
* Tiny Password Manager
* Copyright (C) 2014 Ruben Rosado <rrosadoalba@gmail.com>
*
* 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 eu.rubenrosado.tinypasswordmanager;

import java.util.ArrayList;

import eu.rubenrosado.tinypasswordmanager.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;

public class AllRows extends Activity {
    private ArrayList<ObjectRows> spArray;
    private ArrayList<String> decryptedPasswords;
    private AdapterRows adapter;
    private ListView lv;
    private final static String HIDEPW = "********";

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

        selectAllRows();

        // _____LIST VIEW_____//
        lv = (ListView) findViewById(R.id.saved_passwords_row);
        if (spArray.isEmpty()) {
            toastMessage(getString(R.string.empty_list));
        }
        adapter = new AdapterRows(this, spArray);
        lv.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.all_rows_menu, menu);
        return true;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.new_password:
            onClickNew();
            return true;
        case R.id.password_generator:
            startActivity(new Intent(this, PasswordGenerator.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void selectAllRows() {
        DBHelper dbhelper;
        SQLiteDatabase sql;
        dbhelper = new DBHelper(this);
        sql = dbhelper.getWritableDatabase();
        spArray = new ArrayList<ObjectRows>();
        decryptedPasswords = new ArrayList<String>();
        String[] columnes = { DBHelper.COLID, DBHelper.COLSITE, DBHelper.COLUSER, DBHelper.COLPASS };
        Cursor curs = sql.query(DBHelper.TABLENAME, columnes, null, null, null, null, null);
        try {
            while (curs.moveToNext()) {
                spArray.add(
                        new ObjectRows(curs.getInt(0), Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(1)),
                                Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(2)), HIDEPW));
                decryptedPasswords.add(Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(3)));
            }
        } catch (Exception e) {
            toastMessage(getString(R.string.internal_error));
        } finally {
            sql.close();
            dbhelper.close();
        }
    }

    public void onClickNew() {
        Intent in = new Intent(this, AddEditRow.class);
        startActivityForResult(in, 0);
    }

    public void onClickEdit(View v) {
        int position = lv.getPositionForView(v);
        Intent in = new Intent(this, AddEditRow.class);
        in.putExtra("position", position);
        in.putExtra("id", spArray.get(position).getId());
        in.putExtra("site", spArray.get(position).getSite());
        in.putExtra("user", spArray.get(position).getUser());
        in.putExtra("password", decryptedPasswords.get(position).toString());
        startActivityForResult(in, 0);
    }

    public void onClickDelete(View v) {
        DBHelper dbhelper = new DBHelper(this);
        SQLiteDatabase sql = dbhelper.getWritableDatabase();

        int position = lv.getPositionForView(v);

        sql.delete(DBHelper.TABLENAME, DBHelper.COLID + "=" + String.valueOf(spArray.get(position).getId()), null);
        sql.close();
        dbhelper.close();

        spArray.remove(position);
        decryptedPasswords.remove(position);
        adapter.notifyDataSetChanged();
    }

    public void onClickShowHide(View v) {
        int position = lv.getPositionForView(v);
        if (!spArray.get(position).isShow()) {
            try {
                spArray.get(position).setPassword(decryptedPasswords.get(position));
                spArray.get(position).setShow(true);
            } catch (Exception e) {
                toastMessage(getString(R.string.internal_error));
            }
        } else {
            try {
                spArray.get(position).setPassword(HIDEPW);
                spArray.get(position).setShow(false);
            } catch (Exception e) {
                toastMessage(getString(R.string.internal_error));
            }
        }
        adapter.notifyDataSetChanged();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
                toastMessage(getString(R.string.saving));
                int position = data.getIntExtra("position", -1);
                int id = data.getIntExtra("id", -1);
                String site = data.getStringExtra("site");
                String user = data.getStringExtra("user");
                String password = data.getStringExtra("password");

                DBHelper dbhelper = new DBHelper(this);
                SQLiteDatabase sql = dbhelper.getWritableDatabase();

                try {
                    ObjectRows sp = new ObjectRows(id, Encryption.decrypt(Main.MASTERPASSWORD, site),
                            Encryption.decrypt(Main.MASTERPASSWORD, user), HIDEPW);
                    ContentValues cv = new ContentValues();
                    cv.put(DBHelper.COLSITE, site);
                    cv.put(DBHelper.COLUSER, user);
                    cv.put(DBHelper.COLPASS, password);
                    if (position != -1) {
                        sql.update(DBHelper.TABLENAME, cv, DBHelper.COLID + "=" + id, null);
                        spArray.set(position, sp);
                        decryptedPasswords.set(position, Encryption.decrypt(Main.MASTERPASSWORD, password));
                    } else {
                        sql.insert(DBHelper.TABLENAME, null, cv);
                        spArray.add(sp);
                        decryptedPasswords.add(Encryption.decrypt(Main.MASTERPASSWORD, password));
                    }
                    toastMessage(getString(R.string.saved));
                } catch (Exception e) {
                    toastMessage(getString(R.string.internal_error));
                } finally {
                    sql.close();
                    dbhelper.close();
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    public void toastMessage(String msg) {
        Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
        toast.show();
    }

}