Android Open Source - Now_Manager Dialogs






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;
/*from   w  w  w .  ja v  a2 s  .c om*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaRecorder;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.collinguarino.nowmanager.EditTextBackEvent;
import com.collinguarino.nowmanager.TimeCardAdapter;
import com.collinguarino.nowmanager.AsyncGPSLog;
import com.collinguarino.nowmanager.R;
import com.collinguarino.nowmanager.TimeCardObject;
import com.collinguarino.nowmanager.provider.Contracts;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by collinux on 7/21/14.
 */
public class Dialogs extends ActivityMain {

    /**
     * Full view dialog for displaying an image bitmap with caption text
     */
    public static void displayPictureFullPreview(final Context context, TimeCardObject timeCard) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_image_view);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        Bitmap bMap = BitmapFactory.decodeFile(timeCard.getFilePath());
        ((ImageView) dialog.findViewById(R.id.imageView)).setImageBitmap(bMap);

        // if there's no description, hide the text
        final String descriptionText = timeCard.getEventNameInput();
        final TextView description = (TextView) dialog.findViewById(R.id.description);
        if (descriptionText.length() == 0) {
            description.setVisibility(View.INVISIBLE);
        } else {
            description.setText(descriptionText);
        }

        dialog.findViewById(R.id.activityLayout).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // toggle description visibility
                Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.abc_fade_in);
                Animation fadeOut = AnimationUtils.loadAnimation(context, R.anim.abc_fade_out);

                if (descriptionText.length() > 0) {
                    if (description.getVisibility() == View.GONE) {
                        description.startAnimation(fadeIn);
                        description.setVisibility(View.VISIBLE);
                    } else {
                        description.startAnimation(fadeOut);
                        description.setVisibility(View.GONE);
                    }
                }
            }
        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    /**
     * Small preview (onclick) of event text
     */
    public static void displayEventFullPreview(final Context context, final TimeCardObject timeCard) {
        Dialog eventPreviewDialog = new Dialog(context);
        eventPreviewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        eventPreviewDialog.setContentView(R.layout.dialog_event_preview);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(eventPreviewDialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        // setting event name
        ((TextView) eventPreviewDialog.findViewById(R.id.textInput)).setText(timeCard.getEventNameInput());

        // setting date and time
        final TextView dateText = (TextView) eventPreviewDialog.findViewById(R.id.dateText);
        final Calendar datetimeCalendar = Calendar.getInstance();
        datetimeCalendar.setTimeInMillis(timeCard.getTimestamp());
        final Date dateTime = datetimeCalendar.getTime();
        String timeText = "";
        if (!DateFormat.is24HourFormat(context)) {
            timeText = TimeCardAdapter.TIME_FORMAT_STANDARD.format(dateTime);
        } else if (DateFormat.is24HourFormat(context)) {
            timeText = TimeCardAdapter.TIME_FORMAT_MILITARY.format(dateTime);
        }
        dateText.setText(TimeCardAdapter.DATE_FORMAT.format(dateTime) + " " + timeText);

        eventPreviewDialog.show();
        eventPreviewDialog.getWindow().setAttributes(lp);
    }

    /**
     * Add a new event activity
     */
    public static void displayCreateNewEventDialog(final Context context) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_new_event);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        final EditTextBackEvent eventNameEdit = (EditTextBackEvent) dialog.findViewById(R.id.textInput);
        eventNameEdit.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener() {
            @Override
            public void onImeBack(EditTextBackEvent ctrl, String text) {
                eventNameEdit.setCursorVisible(false);
                eventNameEdit.clearComposingText(); // fixes underline bug
                eventNameEdit.setText(eventNameEdit.getText().toString());
            }
        });
        eventNameEdit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                eventNameEdit.setCursorVisible(true);
                return true;
            }
        });

        dialog.findViewById(R.id.createFinal).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (eventNameEdit.getText().toString().length() > 0) {
                    // insert the new card
                    final ContentValues values;
                    values = Contracts.TimeCards.getInsertValues(eventNameEdit.getText().toString(), false, false, null, false, "");
                    context.getContentResolver().insert(Contracts.TimeCards.CONTENT_URI, values);

                    dialog.dismiss();
                } else {
                    eventNameEdit.setHint("Enter 1+ letters or emojis");
                }

            }
        });

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(eventNameEdit, InputMethodManager.SHOW_IMPLICIT);
            }
        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);

    }

    /**
     * Displays after the user opens the app 5 times
     */
    public static void displayRateDialog(final Context context) {
        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
        builder1.setTitle("Do you enjoy using this app?");
        builder1.setMessage("Please rate it on the Google Play Store. All feedback is much appreciated :)");
        builder1.setCancelable(true);

        builder1.setPositiveButton("Rate", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                try {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.collinguarino.nowmanager")));
                } catch (android.content.ActivityNotFoundException anfe) {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.collinguarino.nowmanager")));
                }

                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                SharedPreferences.Editor preferenceEditor = preferences.edit();
                preferenceEditor.putBoolean("hasRated", true);
                preferenceEditor.commit();

                dialog.cancel();
            }
        });

        builder1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

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

    /**
     * Adding a new voice activity
     */
    static MediaRecorder recorder;
    public static void displayVoiceActivityDialog(final Context context) {
        final Dialog voiceLogDialog = new Dialog(context);
        voiceLogDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        voiceLogDialog.setContentView(R.layout.dialog_voice_record);
        voiceLogDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        voiceLogDialog.setCanceledOnTouchOutside(false);

        // recording variables
        File audioFile = new File(context.getFilesDir().getAbsolutePath(), "AudioRecorder"); // takes to ^/files/ dir

        if (!audioFile.exists()){
            audioFile.mkdirs();
        }

        String audioFileName = String.valueOf(System.currentTimeMillis());
        final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
        final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
        int currentFormat = 0;
        int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
        String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
        final String fileName = (context.getFilesDir().getAbsolutePath() + "/" + audioFileName + file_exts[currentFormat]);

        // start recording
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(output_formats[currentFormat]);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(fileName);

        try {
            recorder.prepare();
            recorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        final Button b1 = (Button) voiceLogDialog.findViewById(R.id.button1);
        final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
        animation.setDuration(1000); // duration - half a second
        animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
        animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
        animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
        b1.startAnimation(animation);

        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }catch(RuntimeException stopException) {

                }

                v.clearAnimation();

                // insert the card
                final ContentValues values;
                values = Contracts.TimeCards.getInsertValues("Tap to play.", false, false, fileName, true, "");
                context.getContentResolver().insert(Contracts.TimeCards.CONTENT_URI, values);

                voiceLogDialog.dismiss();
            }
        });

        voiceLogDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                // release media player
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
            }
        });

        voiceLogDialog.show();
    }

    /**
     * Adding a picture from intent
     * @param bMap from onActivityResult in ActivityMain
     */
    public static void displayPictureCaptionInput(final Context context, final Bitmap bMap) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_picture_caption);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        // dialog elements
        final EditTextBackEvent eventNameEdit = (EditTextBackEvent) dialog.findViewById(R.id.caption);
        eventNameEdit.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener() {
            @Override
            public void onImeBack(EditTextBackEvent ctrl, String text) {
                eventNameEdit.setCursorVisible(false);
                eventNameEdit.clearComposingText(); // fixes underline bug
                eventNameEdit.setText(eventNameEdit.getText().toString());
            }
        });
        eventNameEdit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                eventNameEdit.setCursorVisible(true);
                return true;
            }
        });

        ((ImageView)dialog.findViewById(R.id.imageView)).setImageBitmap(bMap);

        dialog.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // insert the new card
                final ContentValues values;
                values = Contracts.TimeCards.getInsertValues(eventNameEdit.getText().toString(), false, true, storeImage(context, bMap), false, "");
                context.getContentResolver().insert(Contracts.TimeCards.CONTENT_URI, values);

                dialog.dismiss();
            }
        });

        final InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                // show keyboard on dialog start
                keyboard.showSoftInput(eventNameEdit, InputMethodManager.SHOW_IMPLICIT);
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                // hide keyboard when dialog is gone
                keyboard.hideSoftInputFromWindow(eventNameEdit.getWindowToken(), 0);
            }
        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    /**
     * Stores the image in the user's gallery and links the file path
     * @return file path of image
     */
    public static String storeImage(Context context, Bitmap image) {
        String imageFileName = String.valueOf(System.currentTimeMillis() + ".JPEG");
        String imageFileFullPath = (context.getFilesDir().getAbsolutePath() + "/" + imageFileName);
        File imageFile = new File(context.getFilesDir().getAbsolutePath(), imageFileName);
        try {
            FileOutputStream fos = new FileOutputStream(imageFile);
            image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        }
        return imageFileFullPath;
    }
}




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