Android Open Source - Tacere Event Details Fragment






From Project

Back to project page Tacere.

License

The source code is released under:

MIT License

If you think the Android project Tacere 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

/*
 * Copyright (c) 2014 Jonathan Nelson//from  w  ww  .  j a  v  a 2 s  .  c  o  m
 * Released under the BSD license.  For details see the COPYING file.
 */

package org.ciasaboark.tacere.activity.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.ciasaboark.tacere.R;
import org.ciasaboark.tacere.billing.Authenticator;
import org.ciasaboark.tacere.database.DataSetManager;
import org.ciasaboark.tacere.database.DatabaseInterface;
import org.ciasaboark.tacere.database.NoSuchEventInstanceException;
import org.ciasaboark.tacere.event.EventInstance;
import org.ciasaboark.tacere.event.EventManager;
import org.ciasaboark.tacere.event.ringer.RingerSource;
import org.ciasaboark.tacere.event.ringer.RingerType;
import org.ciasaboark.tacere.prefs.Prefs;
import org.ciasaboark.tacere.service.ExtendEventService;

import java.util.HashMap;

public class EventDetailsFragment extends DialogFragment {
    public static final String TAG = "EventLongClickFragment";
    DataSetManager dataSetManager;
    Authenticator authenticator;
    private DatabaseInterface databaseInterface;
    private Prefs prefs;
    private EventInstance event;
    private int instanceId;
    private Context context;
    private View view;
    private Button positiveButton;

    public static EventDetailsFragment newInstance(EventInstance eventInstance) {
        EventDetailsFragment fragment = new EventDetailsFragment();
        Bundle args = new Bundle();
        args.putLong("instanceId", eventInstance.getId());
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog);

        long instanceId = getArguments().getLong("instanceId");
        context = getActivity();
        dataSetManager = new DataSetManager(this, context);
        databaseInterface = DatabaseInterface.getInstance(context);
        prefs = new Prefs(context);
        authenticator = new Authenticator(context);

        try {
            event = databaseInterface.getEvent(instanceId);
        } catch (NoSuchEventInstanceException e) {
            Log.e(TAG, "unable to find event with id " + instanceId);
            return null;
        }

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(
                new ContextThemeWrapper(getActivity(), R.style.Dialog)
        );
        view = getActivity().getLayoutInflater().inflate(R.layout.dialog_event_longclick, null);
        setupWidgetsForView();
        dialogBuilder.setView(view);

        //the clear button should only be visible if the event has a custom ringer or if the events
        //series has a custom ringer set
        EventManager eventManager = new EventManager(context, event);

        if (eventManager.getRingerSource() == RingerSource.EVENT_SERIES ||
                eventManager.getRingerSource() == RingerSource.INSTANCE) {
            dialogBuilder.setNegativeButton(R.string.clear, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    resetEvent();
                }
            });

        }

        dialogBuilder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //nothing to do here
            }
        });

        dialogBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                saveSettings();
            }
        });

        //the positive button should only be enabled if the selected ringer is not UNDEFINED
        AlertDialog dialog = dialogBuilder.create();
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    private void setupWidgetsForView() {
        TextView eventTitle = (TextView) view.findViewById(R.id.event_title);
        eventTitle.setText(event.getTitle());

        TextView calendarTitle = (TextView) view.findViewById(R.id.calendar_title);
        calendarTitle.setText(databaseInterface.getCalendarNameForId(event.getCalendarId()));

        LinearLayout eventRepetitonBox = (LinearLayout) view.findViewById(R.id.event_details_repetitions_box);
        if (databaseInterface.doesEventRepeat(event.getEventId())) {
            eventRepetitonBox.setVisibility(View.VISIBLE);
            TextView eventRepetitionText = (TextView) view.findViewById(R.id.event_details_repetition_text);
            long eventRepetitions = databaseInterface.getEventRepetitionCount(event.getEventId());
            eventRepetitionText.setText("Repeats " + eventRepetitions + " times");
        } else {
            eventRepetitonBox.setVisibility(View.GONE);
        }

        drawExtendMinutesWidgets();

        colorizeIcons();

        ImageView buttonNormal = (ImageView) view.findViewById(R.id.imageButtonNormal);
        ImageView buttonVibrate = (ImageView) view.findViewById(R.id.imageButtonVibrate);
        ImageView buttonSilent = (ImageView) view.findViewById(R.id.imageButtonSilent);
        ImageView buttonIgnore = (ImageView) view.findViewById(R.id.imageButtonIgnore);
        buttonNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRingerType(RingerType.NORMAL);
            }
        });

        buttonVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRingerType(RingerType.VIBRATE);
            }
        });

        buttonSilent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRingerType(RingerType.SILENT);
            }
        });

        buttonIgnore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRingerType(RingerType.IGNORE);
            }
        });
    }

    private void resetEvent() {
        boolean eventRepeats = databaseInterface.doesEventRepeat(event.getEventId());
        boolean eventSeriesRingerSet = prefs.getRingerForEventSeries(event.getEventId()) != RingerType.UNDEFINED;
        if (eventRepeats && eventSeriesRingerSet) {
            if (event.getRingerType() == RingerType.UNDEFINED) {
                //this event does not have an instance ringer set, the reset button should prompt that
                //resetting will reset the entire event series
                String message = getResources().getString(R.string.event_dialog_reset_event_series);
                //if this events calendar has a custom ringer then notify the user that this is what
                //we will drop back to, otherwize use the default ringer
                if (prefs.getRingerForCalendar(event.getCalendarId()) != RingerType.UNDEFINED) {
                    message = String.format(message, new String[]{"calendar"});
                } else {
                    message = String.format(message, new String[]{"default"});
                }

                Drawable icon = getResources().getDrawable(R.drawable.history);
                icon.mutate().setColorFilter(
                        getResources().getColor(R.color.dialog_icon_tint), PorterDuff.Mode.MULTIPLY);
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        new ContextThemeWrapper(getActivity(), R.style.Dialog)
                )
                        .setTitle(R.string.event_dialog_repeating_event_conformation_title)
                        .setMessage(message)
                        .setPositiveButton(R.string.event_dialog_reset_event_series_ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                resetAllEvents();
                            }
                        })
                        .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //nothing to do here
                            }
                        })
                        .setIcon(icon);
                AlertDialog dialog = builder.show();

            } else {
                //this event has instance and event series ringers set, prompt for which to reset
                long eventRepetions = databaseInterface.getEventRepetitionCount(event.getEventId());
                String message = getResources().getString(R.string.event_dialog_reset_all_instances_message);
                Drawable icon = getResources().getDrawable(R.drawable.history);
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        new ContextThemeWrapper(getActivity(), R.style.Dialog)
                )
                        .setTitle(R.string.event_dialog_repeating_event_conformation_title)
                        .setMessage(String.format(message, event.getTitle(), eventRepetions))
                        .setPositiveButton(R.string.event_dialog_save_all_instances, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                resetAllEvents();
                            }
                        })
                        .setNegativeButton(R.string.event_dialog_save_instance, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                resetEventInstance();
                            }
                        })
                        .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //nothing to do here
                            }
                        })
                        .setIcon(icon);
                AlertDialog dialog = builder.show();
            }

        } else {
            resetEventInstance();
        }
    }

    private void saveSettings() {
        if (databaseInterface.doesEventRepeat(event.getEventId())) {
            long eventRepetions = databaseInterface.getEventRepetitionCount(event.getEventId());
            String positiveButtonText = getResources().getString(R.string.event_dialog_save_all_instances_message);
            Drawable icon = getResources().getDrawable(R.drawable.history);
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(getActivity(), R.style.Dialog)
            )
                    .setTitle(R.string.event_dialog_repeating_event_conformation_title)
                    .setMessage(String.format(positiveButtonText, event.getTitle(), eventRepetions))
                    .setPositiveButton(R.string.event_dialog_save_all_instances, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            saveSettingsForAllEvents();
                        }
                    })
                    .setNegativeButton(R.string.event_dialog_save_instance, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            saveSettingsForEventInstance();
                        }
                    })
                    .setIcon(icon);
            AlertDialog dialog = builder.show();
        } else {
            saveSettingsForEventInstance();
        }
    }

    private void drawExtendMinutesWidgets() {
        final LinearLayout extendMinutesBox = (LinearLayout) view.findViewById(R.id.event_extend_box);
        if (event.getExtendMinutes() == 0) {
            extendMinutesBox.setVisibility(View.GONE);
        } else {
            extendMinutesBox.setVisibility(View.VISIBLE);
            TextView extendMinutesText = (TextView) view.findViewById(R.id.event_extend_text);
            extendMinutesText.setText("+" + event.getExtendMinutes() + " minutes");
            Button extendMinutesResetButton = (Button) view.findViewById(R.id.event_extend_button);
            extendMinutesResetButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(context, ExtendEventService.class);
                    i.putExtra(ExtendEventService.INSTANCE_ID, event.getId());
                    i.putExtra(ExtendEventService.NEW_EXTEND_LENGTH, 0);
                    context.startService(i);
                    extendMinutesBox.setVisibility(View.INVISIBLE);
                }
            });
        }
    }

    private void colorizeIcons() {
        HashMap<ImageView, RingerType> buttons = new HashMap<ImageView, RingerType>();
        buttons.put((ImageView) view.findViewById(R.id.imageButtonNormal), RingerType.NORMAL);
        buttons.put((ImageView) view.findViewById(R.id.imageButtonVibrate), RingerType.VIBRATE);
        buttons.put((ImageView) view.findViewById(R.id.imageButtonSilent), RingerType.SILENT);
        buttons.put((ImageView) view.findViewById(R.id.imageButtonIgnore), RingerType.IGNORE);

        for (ImageView thisButton : buttons.keySet()) {
            thisButton.setImageDrawable(getColorizedIcon(buttons.get(thisButton)));
            thisButton.invalidate();
        }


    }

    private void setRingerType(RingerType type) {
        positiveButton.setEnabled(true);
        event.setRingerType(type);
        colorizeIcons();
    }

    private void resetAllEvents() {
        prefs.unsetRingerTypeForEventSeries(event.getEventId());
        databaseInterface.setRingerForAllInstancesOfEvent(event.getEventId(),
                RingerType.UNDEFINED);
        dataSetManager.broadcastDataSetChangedMessage();
    }

    private void resetEventInstance() {
        databaseInterface.setRingerForInstance(event.getId(), RingerType.UNDEFINED);
        dataSetManager.broadcastDataSetChangedForId(event.getId());
    }

    private void saveSettingsForAllEvents() {
        if (authenticator.isAuthenticated()) {
            prefs.unsetRingerTypeForEventSeries(event.getEventId());
            databaseInterface.setRingerForAllInstancesOfEvent(event.getEventId(),
                    RingerType.UNDEFINED);

            prefs.setRingerForEventSeries(event.getEventId(), event.getRingerType());
            dataSetManager.broadcastDataSetChangedMessage();
        } else {
            authenticator.showUpgradeDialog();
        }
    }

    private void saveSettingsForEventInstance() {
        databaseInterface.setRingerForInstance(event.getId(), event.getRingerType());
        dataSetManager.broadcastDataSetChangedForId(event.getId());
    }

    private Drawable getColorizedIcon(RingerType ringerType) {
        Drawable colorizedIcon;
        switch (ringerType) {
            case NORMAL:
                colorizedIcon = getResources().getDrawable(R.drawable.ic_state_normal);
                break;
            case VIBRATE:
                colorizedIcon = getResources().getDrawable(R.drawable.ic_state_vibrate);
                break;
            case SILENT:
                colorizedIcon = getResources().getDrawable(R.drawable.ic_state_silent);
                break;
            default:
                colorizedIcon = getResources().getDrawable(R.drawable.ic_state_ignore);
        }

        int color = getResources().getColor(R.color.dialog_icon_tint);
        if (event.getRingerType() == ringerType) {
            color = getResources().getColor(R.color.accent);
        }

        colorizedIcon.mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        return colorizedIcon;
    }

    @Override
    public void onResume() {
        super.onResume();
        AlertDialog thisDialog = (AlertDialog) getDialog();
        positiveButton = thisDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        if (positiveButton != null) {
            positiveButton.setEnabled(event.getRingerType() == RingerType.UNDEFINED ? false : true);
        }
    }
}




Java Source Code List

.EventInstanceTest.java
com.viewpagerindicator.BuildConfig.java
com.viewpagerindicator.BuildConfig.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.ciasaboark.tacere.ApplicationTest.java
org.ciasaboark.tacere.Tacere.java
org.ciasaboark.tacere.activity.AboutActivity.java
org.ciasaboark.tacere.activity.AboutLicenseActivity.java
org.ciasaboark.tacere.activity.AdvancedSettingsActivity.java
org.ciasaboark.tacere.activity.BetaReleaseActivity.java
org.ciasaboark.tacere.activity.BetaSettingsActivity.java
org.ciasaboark.tacere.activity.BugReportActivity.java
org.ciasaboark.tacere.activity.DonationActivityTest.java
org.ciasaboark.tacere.activity.LicenseCheckWrapper.java
org.ciasaboark.tacere.activity.MainActivity.java
org.ciasaboark.tacere.activity.ProUpgradeActivity.java
org.ciasaboark.tacere.activity.SelectCalendarsActivity.java
org.ciasaboark.tacere.activity.SettingsActivity.java
org.ciasaboark.tacere.activity.ShowUpdatesActivity.java
org.ciasaboark.tacere.activity.TutorialActivity.java
org.ciasaboark.tacere.activity.fragment.AdvancedSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.BetaSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.EventDetailsFragment.java
org.ciasaboark.tacere.activity.fragment.MainSettingsFragment.java
org.ciasaboark.tacere.activity.fragment.SelectCalendarsFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialCrashReporterFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialEndFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialEventListFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialProVersionFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialRingerPriorityFragment.java
org.ciasaboark.tacere.activity.fragment.TutorialWelcomeFragment.java
org.ciasaboark.tacere.adapter.EventCursorAdapter.java
org.ciasaboark.tacere.billing.Authenticator.java
org.ciasaboark.tacere.billing.google.Base64DecoderException.java
org.ciasaboark.tacere.billing.google.Base64.java
org.ciasaboark.tacere.billing.google.IabException.java
org.ciasaboark.tacere.billing.google.IabHelper.java
org.ciasaboark.tacere.billing.google.IabResult.java
org.ciasaboark.tacere.billing.google.Inventory.java
org.ciasaboark.tacere.billing.google.Purchase.java
org.ciasaboark.tacere.billing.google.Security.java
org.ciasaboark.tacere.billing.google.SkuDetails.java
org.ciasaboark.tacere.bug.CrashReportManager.java
org.ciasaboark.tacere.converter.DateConverterTest.java
org.ciasaboark.tacere.converter.DateConverter.java
org.ciasaboark.tacere.converter.TimeConverter.java
org.ciasaboark.tacere.database.Columns.java
org.ciasaboark.tacere.database.DataSetManager.java
org.ciasaboark.tacere.database.DatabaseInterface.java
org.ciasaboark.tacere.database.EventDatabaseOpenHelper.java
org.ciasaboark.tacere.database.NoSuchEventInstanceException.java
org.ciasaboark.tacere.database.TooltipManager.java
org.ciasaboark.tacere.event.Calendar.java
org.ciasaboark.tacere.event.EventInstance.java
org.ciasaboark.tacere.event.EventManager.java
org.ciasaboark.tacere.event.ringer.Intervals.java
org.ciasaboark.tacere.event.ringer.RingerSource.java
org.ciasaboark.tacere.event.ringer.RingerType.java
org.ciasaboark.tacere.key.ApplicationTest.java
org.ciasaboark.tacere.key.MainActivity.java
org.ciasaboark.tacere.manager.AlarmManagerWrapper.java
org.ciasaboark.tacere.manager.NotificationManagerWrapper.java
org.ciasaboark.tacere.manager.RingerStateManager.java
org.ciasaboark.tacere.manager.ServiceStateManager.java
org.ciasaboark.tacere.manager.VolumesManager.java
org.ciasaboark.tacere.notifier.WidgetNotifier.java
org.ciasaboark.tacere.prefs.BetaPrefs.java
org.ciasaboark.tacere.prefs.DefaultPrefs.java
org.ciasaboark.tacere.prefs.PrefsTest.java
org.ciasaboark.tacere.prefs.Prefs.java
org.ciasaboark.tacere.prefs.Updates.java
org.ciasaboark.tacere.receiver.BootCompletedReceiver.java
org.ciasaboark.tacere.receiver.ProviderChangedReceiver.java
org.ciasaboark.tacere.service.EventSilencerService.java
org.ciasaboark.tacere.service.ExtendEventService.java
org.ciasaboark.tacere.service.ExtendQuicksilenceService.java
org.ciasaboark.tacere.service.RequestTypes.java
org.ciasaboark.tacere.service.ResetEventServiceTest.java
org.ciasaboark.tacere.service.ResetEventService.java
org.ciasaboark.tacere.service.SkipEventService.java
org.ciasaboark.tacere.versioning.Versioning.java
org.ciasaboark.tacere.view.EventListItem.java
org.ciasaboark.tacere.widget.ActiveEventWidgetProvider.java
org.ciasaboark.tacere.widget.EventListWidgetProvider.java
org.ciasaboark.tacere.widget.QuickSilenceTinyWidgetProvider.java
org.ciasaboark.tacere.widget.QuickSilenceWidgetProvider.java
org.ciasaboark.tacere.widget.activity.WidgetPopupRinger.java
org.ciasaboark.tacere.widget.adapter.WidgetEventListAdapter.java
org.ciasaboark.tacere.widget.service.WidgetService.java