Android Open Source - Now_Manager Activity Main






From Project

Back to project page Now_Manager.

License

The source code is released under:

Apache License

If you think the Android project Now_Manager 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.collinguarino.nowmanager.ui;
/* w  w  w . jav  a 2s.c o m*/
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.collinguarino.nowmanager.AsyncGPSLog;
import com.collinguarino.nowmanager.SwipeDismissListViewTouchListener;
import com.collinguarino.nowmanager.TimeCardAdapter;
import com.collinguarino.nowmanager.R;
import com.collinguarino.nowmanager.TimeCardObject;
import com.collinguarino.nowmanager.floating.FloatingActionButton;
import com.collinguarino.nowmanager.floating.FloatingActionsMenu;
import com.collinguarino.nowmanager.provider.Contracts;
import com.collinguarino.nowmanager.provider.NowManagerProvider;

public class ActivityMain extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    // UI
    private TimeCardAdapter mAdapter;
    public ListView listView;
    private FloatingActionButton eventButton, voiceButton, pictureButton, locationButton;
    private FloatingActionsMenu mFam;

    // intent keys
    public static final int TAKE_PICTURE = 1;

    SharedPreferences preferences;
    SharedPreferences.Editor preferenceEditor;

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

        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new TimeCardAdapter(this, null);
        setListAdapter(mAdapter);

        preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        preferenceEditor = preferences.edit();
        listView = getListView();

        // Make the list dismissable by swipe.
        SwipeDismissListViewTouchListener swipeDismissListViewTouchListener = new SwipeDismissListViewTouchListener(listView, listDismissCallbacks);
        listView.setOnTouchListener(swipeDismissListViewTouchListener);
        listView.setOnScrollListener(swipeDismissListViewTouchListener.makeScrollListener());

        mFam = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
        mFam.attachToListView(listView);

        eventButton = (FloatingActionButton) findViewById(R.id.new_event);
        eventButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFam.collapse();
                Dialogs.displayCreateNewEventDialog(ActivityMain.this);
            }
        });

        voiceButton = (FloatingActionButton) findViewById(R.id.new_voice);
        voiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFam.collapse();
                Dialogs.displayVoiceActivityDialog(ActivityMain.this);
            }
        });

        pictureButton = (FloatingActionButton) findViewById(R.id.new_picture);
        pictureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFam.collapse();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, ActivityMain.TAKE_PICTURE);
            }
        });

        locationButton = (FloatingActionButton) findViewById(R.id.new_location);
        locationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AsyncGPSLog(ActivityMain.this, mFam).execute();
            }
        });

        // add to startup count- ask users to rate the app after x times opening the app
        int timesOpened = preferences.getInt("timesOpened", 0);
        boolean hasRated = preferences.getBoolean("hasRated", false);
        if (!hasRated && (timesOpened == 10 || timesOpened == 15 ||
                timesOpened == 20 || timesOpened == 25) || timesOpened == 30)
            Dialogs.displayRateDialog(this);
        preferenceEditor.putInt("timesOpened", timesOpened + 1);
        preferenceEditor.commit();

        // Prepare the loader:  either re-connect with an existing one or start a new one.
        getLoaderManager().initLoader(0, null, this);
    }

    /**
     * Callbacks for when list items are dismissed (by swipe).
     */
    private SwipeDismissListViewTouchListener.DismissCallbacks listDismissCallbacks = new SwipeDismissListViewTouchListener.DismissCallbacks() {
        @Override
        public boolean canDismiss(int position) {
            //Return false here if the item at the position should not be dissmissable
            return true;
        }

        @Override
        public void onDismiss(ListView listView, int[] reverseSortedPositions) {
            for (int position : reverseSortedPositions) {

                final TimeCardObject timeCard = ((TimeCardAdapter)mAdapter).getTimeCard(position);

                if (timeCard != null) {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(ActivityMain.this);
                    builder1.setTitle("Delete This Action?");
                    builder1.setMessage("This cannot be undone.");
                    builder1.setCancelable(true);

                    // delete
                    builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            getContentResolver().delete(Contracts.TimeCards.CONTENT_URI, Contracts.TimeCards._ID + " = " + timeCard.getId(), null);
                        }
                    });

                    // don't proceed
                    builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            dialog.cancel();

                        }
                    });

                    AlertDialog alert = builder1.create();
                    alert.show();
                }
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onPause() {
        mFam.collapse();
        super.onPause();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:
                Intent intent = new Intent(getApplicationContext(), ActivitySettings.class);
                startActivity(intent);
                return true;
        }
        return true;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK)
             if (requestCode == TAKE_PICTURE && data != null)
                    Dialogs.displayPictureCaptionInput(ActivityMain.this, (Bitmap) data.getExtras().get("data"));
    }

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

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {
        // This is called when a new Loader needs to be created.  This
        // sample only has one Loader, so we don't care about the ID.
        final Uri baseUri = Contracts.TimeCards.CONTENT_URI;

        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        switch(loaderId) {
            default: // select all
                return new CursorLoader(this, baseUri,
                        Contracts.TimeCards.SELECT_ALL_PROJECTION, null, null,
                        Contracts.TimeCards.C_TIMESTAMP + " DESC");
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no longer using it.
        mAdapter.swapCursor(null);
    }
}




Java Source Code List

com.collinguarino.nowmanager.AsyncGPSLog.java
com.collinguarino.nowmanager.EditTextBackEvent.java
com.collinguarino.nowmanager.EditTextMovement.java
com.collinguarino.nowmanager.SwipeDismissListViewTouchListener.java
com.collinguarino.nowmanager.TimeCardAdapter.java
com.collinguarino.nowmanager.TimeCardObject.java
com.collinguarino.nowmanager.floating.AddFloatingActionButton.java
com.collinguarino.nowmanager.floating.FloatingActionButton.java
com.collinguarino.nowmanager.floating.FloatingActionsMenu.java
com.collinguarino.nowmanager.provider.Contracts.java
com.collinguarino.nowmanager.provider.MainDatabaseHelper.java
com.collinguarino.nowmanager.provider.NowManagerProvider.java
com.collinguarino.nowmanager.ui.ActivityMain.java
com.collinguarino.nowmanager.ui.ActivitySettings.java
com.collinguarino.nowmanager.ui.Dialogs.java