Android Open Source - SimpleNotes Main Activity






From Project

Back to project page SimpleNotes.

License

The source code is released under:

Apache License

If you think the Android project SimpleNotes 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.moysa.simplenotes.ui.activities;
/*from   ww w .  j a  v a2s  .c  o m*/
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.moysa.simplenotes.R;
import com.moysa.simplenotes.core.Note;
import com.moysa.simplenotes.core.NotesUtils;
import com.moysa.simplenotes.ui.application.NoteApplication;
import com.moysa.simplenotes.ui.fragments.NavigationDrawerFragment;
import com.moysa.simplenotes.ui.fragments.NoteFragment;

import java.util.List;

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    private List<Note> mNotes;

    private NoteApplication mNoteApplication;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNoteApplication = NoteApplication.getInstance();

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mNotes = mNoteApplication.getNotes();
    }

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

    private void sortNotes() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

        boolean byName = sp.getString("sort_list", "").equals("1");
        boolean asc = sp.getString("sort_type_list", "").equals("1");

        if (mNotes != null) {
            NotesUtils.sortNotes(mNotes, byName, asc);
            mNavigationDrawerFragment.getArrayAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        openNote(position);
    }

    @Override
    public void onNavigationDrawerOpened() {
        NoteFragment fragment = NoteFragment.getInstance();

        if (fragment != null)
            fragment.scipEdits();
    }

    private void openNote(int position) {

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (mNotes.isEmpty()) {
            fragmentManager.beginTransaction()
                    .remove(NoteFragment.getInstance())
                    .commit();

            mTitle = getString(R.string.app_name);
            restoreActionBar();

            return;
        }

        NoteFragment noteFragment = NoteFragment.newInstance();
        noteFragment.setNote(mNotes.get(position));

        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.setCustomAnimations(
                android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);

        ft.replace(R.id.container, noteFragment).commit();
    }

    public void onSectionAttached(String title) {
        mTitle = title;
        restoreActionBar();
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
//            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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();

        if (id == R.id.action_add) {
            addNote();
//            Toast.makeText(this, getString(R.string.added), Toast.LENGTH_SHORT).show();
            return true;
        }

        if (id == R.id.action_delete) {
            deleteNote();
//            Toast.makeText(this, getString(R.string.deleted), Toast.LENGTH_SHORT).show();
        }

        if (id == R.id.action_settings) {
            Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void addNote() {

        int count = mNotes.size() + 1;
        String noteCount = String.valueOf(count);

        Note newNote = new Note(count, "Note " + noteCount, noteCount);

        if (!mNavigationDrawerFragment.isSearch())
            mNavigationDrawerFragment.getArrayAdapter().add(newNote);
        else
            mNotes.add(0, newNote);

        mNoteApplication.getDataBaseHelper().putNote(newNote);

        openNote(mNotes.indexOf(newNote));
    }

    private void deleteNote() {
        Note note = NoteFragment.getInstance().getNote();
        int currentPosition = mNotes.indexOf(note);

        if (!mNavigationDrawerFragment.isSearch())
            mNavigationDrawerFragment.getArrayAdapter().remove(note);
        else {
            mNavigationDrawerFragment.getArrayAdapter().remove(note);
            mNotes.remove(note);
        }

        mNoteApplication.getDataBaseHelper().deleteNote(note);

        if (currentPosition > 0)
            openNote(currentPosition - 1);
        else
            openNote(currentPosition);
    }

    public NavigationDrawerFragment getNavigationDrawerFragment() {
        return mNavigationDrawerFragment;
    }
}




Java Source Code List

com.moysa.simplenotes.core.Note.java
com.moysa.simplenotes.core.NotesComparator.java
com.moysa.simplenotes.core.NotesUtils.java
com.moysa.simplenotes.db.NoteDataBaseHelper.java
com.moysa.simplenotes.ui.activities.MainActivity.java
com.moysa.simplenotes.ui.activities.SettingsActivity.java
com.moysa.simplenotes.ui.adapters.NotesAdapter.java
com.moysa.simplenotes.ui.application.NoteApplication.java
com.moysa.simplenotes.ui.fragments.NavigationDrawerFragment.java
com.moysa.simplenotes.ui.fragments.NoteFragment.java
com.moysa.simplenotes.ui.listeners.ListHeaderAnimationListener.java