Android Open Source - DailySelfie Activity Main






From Project

Back to project page DailySelfie.

License

The source code is released under:

Apache License

If you think the Android project DailySelfie 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 it.lusio.android.dailyselfie.ui;
//w ww .  j a  v  a 2s  .c  om
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import it.lusio.android.dailyselfie.alarm.AlarmSetter;
import it.lusio.android.dailyselfie.Constants;
import it.lusio.android.dailyselfie.R;


public class ActivityMain extends Activity {

    private static final int TAKE_SELFIE_REQUEST_CODE = 1;
    private static final String LAST_PHOTO_PATH = "mLastPhotoPath";
    private static final String LAST_PHOTO_DATE = "mLastPhotoDate";
    private AdapterGridView mAdapterGridView;
    private String mLastPhotoPath = "";
    private long mLastSelfieTime = new Date().getTime();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, getString(R.string.no_external_storage), Toast.LENGTH_SHORT).show();
            finish();
        }

        if (savedInstanceState != null) {
            mLastPhotoPath = savedInstanceState.getString(LAST_PHOTO_PATH);
            mLastSelfieTime = savedInstanceState.getLong(LAST_PHOTO_DATE);
        }

        setContentView(R.layout.activity_main);

        GridView gridView = (GridView) findViewById(R.id.gridview);
        if (gridView != null) {
            mAdapterGridView = new AdapterGridView(this);
            gridView.setAdapter(mAdapterGridView);
        }

        mAdapterGridView.notifyDataSetChanged();
    }


    @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.
        switch (item.getItemId()) {
            case (R.id.action_camera):
                Log.i(Constants.TAG, "Action Camera Clicked");
                takeNewSelfie();
                return true;
            case (R.id.action_delete_all):
                Log.i(Constants.TAG, "Delete all Clicked");
                new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle(getString(R.string.dialog_delete_all_title))
                        .setMessage(getString(R.string.dialog_delete_all_text))
                        .setPositiveButton(getString(R.string.positive_answer), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mAdapterGridView.deleteAll();
                            }

                        })
                        .setNegativeButton(getString(R.string.negative_answer), null)
                        .show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void takeNewSelfie() {
        mLastSelfieTime = new Date().getTime();

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

        String imageFileName = "s_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(mLastSelfieTime);
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                Constants.ALBUM_NAME);
        storageDir.mkdirs();
        File photoFile = new File(storageDir.getAbsolutePath() + File.separator + imageFileName + ".jpg");

        mLastPhotoPath = photoFile.getAbsolutePath();
        // Continue only if the File was successfully created
        if (photoFile != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(intent, TAKE_SELFIE_REQUEST_CODE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == TAKE_SELFIE_REQUEST_CODE) {
            Selfie selfie = new Selfie(mLastSelfieTime, Uri.fromFile(new File(mLastPhotoPath)));
            mAdapterGridView.add(selfie);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Save the user's current game state
        savedInstanceState.putString(LAST_PHOTO_PATH, mLastPhotoPath);
        savedInstanceState.putLong(LAST_PHOTO_DATE, mLastSelfieTime);

        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }

    public void showViewer(Uri uri) {
        Intent intent = new Intent(this, ActivityImageViewer.class);
        Bundle b = new Bundle();
        b.putParcelable("URI", uri);
        intent.putExtras(b);
        startActivity(intent);
    }
}




Java Source Code List

it.lusio.android.dailyselfie.ApplicationDailySelfie.java
it.lusio.android.dailyselfie.ApplicationTest.java
it.lusio.android.dailyselfie.Constants.java
it.lusio.android.dailyselfie.alarm.AlarmReceiver.java
it.lusio.android.dailyselfie.alarm.AlarmSetter.java
it.lusio.android.dailyselfie.ui.ActivityImageViewer.java
it.lusio.android.dailyselfie.ui.ActivityMain.java
it.lusio.android.dailyselfie.ui.AdapterGridView.java
it.lusio.android.dailyselfie.ui.Selfie.java
it.lusio.android.dailyselfie.ui.SquareImageView.java