Android Open Source - KnowledgeBase Main Activity






From Project

Back to project page KnowledgeBase.

License

The source code is released under:

MIT License

If you think the Android project KnowledgeBase listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.martin.knowledgebase;
// w  ww  . j a v  a 2  s . c  o m
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends Activity {

    private String mPassword;
    private LinearLayout container;
    private TextView mEntries;
    private ImageButton mFab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = getIntent();
        mPassword = i.getStringExtra("password");
        container = (LinearLayout) findViewById(R.id.container);
        mEntries = (TextView) findViewById(R.id.tvEntries);
        mFab = (ImageButton) findViewById(R.id.fab);
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, EditActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        PlainStorage store = PlainStorage.getInstance();
        final SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
        if (store.isNew() && prefs.contains("data")) {
            final ProgressDialog progress = ProgressDialog.show(this, "Reading", "Decrypting", true);
            new Thread() {

                @Override
                public void run() {
                    super.run();

                    String plainText = Util.uDecrypt(MainActivity.this, mPassword);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress.setMessage("Crunching data");
                        }
                    });
                    PlainStorage.getInstance().setmEntries(Util.listify(plainText));
                    displayData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress.dismiss();
                        }
                    });
                }

            }.start();
        } else {
            displayData();
        }

    }

    private void displayData() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                ArrayList<Entry> entries = PlainStorage.getInstance().getmEntries();
                String all = "";
                for (Entry e : entries) {
                    all += e.getTitle() + " - " + e.getDate() + "\n";
                    all += e.getText() + "\n\n";
                }
                final String text = all;
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mEntries.setText(text);
                    }
                });
            }
        }.run();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_save) {
            final SharedPreferences prefs = getSharedPreferences("KB", MODE_PRIVATE);
            final String salt = prefs.getString("salt", "Oh crap");
            final ProgressDialog progress = ProgressDialog.show(this, "Writing", "Encrypting", true);
            new Thread() {

                @Override
                public void run() {
                    super.run();
                    Util.uEncrypt(MainActivity.this, mPassword);
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            progress.dismiss();
                        }
                    });
                }

            }.start();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}




Java Source Code List

com.martin.knowledgebase.ConversionTest.java
com.martin.knowledgebase.EditActivity.java
com.martin.knowledgebase.EncryptionTest.java
com.martin.knowledgebase.EntryTest.java
com.martin.knowledgebase.Entry.java
com.martin.knowledgebase.LoginActivity.java
com.martin.knowledgebase.MainActivity.java
com.martin.knowledgebase.PlainStorage.java
com.martin.knowledgebase.Snackbar.java
com.martin.knowledgebase.Util.java
com.tozny.crypto.android.AesCbcWithIntegrity.java