Android Open Source - Tacere Select Calendars 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/*  ww w.j  a  va  2 s . co  m*/
 * Released under the BSD license.  For details see the COPYING file.
 */

package org.ciasaboark.tacere.activity.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.ciasaboark.tacere.R;
import org.ciasaboark.tacere.database.DatabaseInterface;
import org.ciasaboark.tacere.event.Calendar;
import org.ciasaboark.tacere.event.ringer.RingerType;
import org.ciasaboark.tacere.prefs.Prefs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SelectCalendarsFragment extends Fragment {
    @SuppressWarnings("unused")
    private final String TAG = "SelectCalendarsFragment";
    private int layout = R.layout.fragment_select_calendars;
    private Prefs prefs;
    private List<Calendar> calendars;
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = (ViewGroup) inflater.inflate(
                layout, container, false);

        prefs = new Prefs(getActivity());
        buildSimpleCalendarList();
        final RelativeLayout syncAllCalendarsBox = (RelativeLayout) rootView.findViewById(R.id.sync_all_calendars);
        syncAllCalendarsBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                prefs.setSyncAllCalendars(!prefs.shouldAllCalendarsBeSynced());
                buildSimpleCalendarList();
                drawSyncBoxSwitch();
                drawDialogBody();
            }
        });

        drawSyncBoxSwitch();
        drawDialogBodyOrError();

        return rootView;
    }

    private void buildSimpleCalendarList() {
        DatabaseInterface databaseInterface = DatabaseInterface.getInstance(getActivity());
        calendars = databaseInterface.getCalendarIdList();
        List<Long> calendarsToSync = prefs.getSelectedCalendarsIds();
        for (Calendar c : calendars) {
            if (calendarsToSync.contains(c.getId())) {
                c.setSelected(true);
            }
        }
    }

    private void drawSyncBoxSwitch() {
        final SwitchCompat syncAllCalendarsSwitch = (SwitchCompat) rootView.findViewById(R.id.sync_all_calendars_switch);
        syncAllCalendarsSwitch.setChecked(prefs.shouldAllCalendarsBeSynced());
    }

    private void drawDialogBodyOrError() {
        if (calendars.isEmpty()) {
            drawError();
        } else {
            drawDialogBody();
        }
    }

    private void drawError() {
        hideDialogBody();
        LinearLayout error = (LinearLayout) rootView.findViewById(R.id.error_box);
        error.setVisibility(View.VISIBLE);

    }

    private void hideDialogBody() {
        LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.calendars_box);
        layout.setVisibility(View.GONE);
    }

    private void drawDialogBody() {
        hideError();
        drawSyncBoxSwitch();
        drawListView();
    }

    private void drawListView() {
        final ListView lv = (ListView) rootView.findViewById(R.id.calendar_listview);
        final SimpleCalendarListAdapter listAdapter = new SimpleCalendarListAdapter(getActivity(), R.layout.list_item_calendar, calendars);
        lv.setAdapter(listAdapter);

        //the calendars should only be clickable if we arent syncing all calendars
        if (!prefs.shouldAllCalendarsBeSynced()) {
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Calendar calendar = calendars.get(position);
                    calendar.setSelected(!calendar.isSelected());
                    toggleSyncCalendar(calendar);
                    listAdapter.notifyDataSetChanged();
                }
            });
            lv.setClickable(true);
        } else {
            lv.setClickable(false);
        }

        boolean syncAllCalendars = prefs.shouldAllCalendarsBeSynced();
        lv.setClickable(!syncAllCalendars);
    }

    private void toggleSyncCalendar(Calendar calendar) {
        if (calendar.isSelected()) {
            addCalendarToSyncList(calendar);
        } else {
            removeCalendarFromSyncList(calendar);
        }
    }

    private void removeCalendarFromSyncList(Calendar calendar) {
        List<Long> calendarsToSync = prefs.getSelectedCalendarsIds();
        long calendarId = calendar.getId();
        calendarsToSync.remove(calendarId);
        prefs.setSelectedCalendars(calendarsToSync);
    }

    private void addCalendarToSyncList(Calendar calendar) {
        List<Long> calendarsToSync = prefs.getSelectedCalendarsIds();
        long calendarId = calendar.getId();
        if (!calendarsToSync.contains(calendarId)) {
            calendarsToSync.add(calendarId);
        }
        prefs.setSelectedCalendars(calendarsToSync);
    }

    private void hideError() {
        LinearLayout error = (LinearLayout) rootView.findViewById(R.id.error_box);
        error.setVisibility(View.GONE);
    }

    private class SimpleCalendarListAdapter extends ArrayAdapter<Calendar> {
        private static final String TAG = "CalendarListAdapter";
        private final Context context;
        private final List<Calendar> calendarList;
        private final SimpleCalendarListAdapter thisAdapter = this;
        private View row;
        private Calendar calendar;

        public SimpleCalendarListAdapter(Context ctx, int resourceId, List<Calendar> calendars) {
            super(ctx, resourceId, calendars);
            this.context = ctx;
            this.calendarList = calendars;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            row = convertView;
            if (row == null) {
                LayoutInflater inflator = ((Activity) context).getLayoutInflater();
                row = inflator.inflate(R.layout.list_item_calendar, parent, false);
            }

            try {
                calendar = calendarList.get(position);
                List<Long> selectedCalendarIds = prefs.getSelectedCalendarsIds();

                if (selectedCalendarIds.contains(calendar.getId())) {
                    calendar.setSelected(true);
                } else {
                    calendar.setSelected(false);
                }

                drawSidebar();
                colorizeText();
                drawRingerIcon();
                drawSettingsButton(position);
                drawCheckBox();


            } catch (IndexOutOfBoundsException e) {
                Log.e(TAG, "error getting calendar at position " + position);
                Log.e(TAG, e.getMessage());
                TextView calendarName = (TextView) row.findViewById(R.id.calendar_name);
                calendarName.setText("Error getting calendar for this position, check logcat");
            }

            return row;
        }

        private void drawSidebar() {
            ImageView calendarSidebar = (ImageView) row.findViewById(R.id.calendar_sidebar);
            Drawable sideBarImage = (Drawable) getResources().getDrawable(R.drawable.sidebar).mutate();

            sideBarImage.setColorFilter(calendar.getColor(), PorterDuff.Mode.MULTIPLY);
            calendarSidebar.setBackgroundDrawable(sideBarImage); //TODO deprecated method use
        }

        private void colorizeText() {
            TextView calendarName = (TextView) row.findViewById(R.id.calendar_name);
            TextView calendarAccountName = (TextView) row.findViewById(R.id.calendar_account);
            calendarName.setText(calendar.getDisplayName());
            calendarAccountName.setText(calendar.getAccountName());

            int textColor;
            if (!calendar.isSelected() || prefs.shouldAllCalendarsBeSynced()) {
                textColor = getResources().getColor(R.color.text_color_disabled);
            } else {
                textColor = getResources().getColor(R.color.text_color);
            }

            calendarName.setTextColor(textColor);
            calendarAccountName.setTextColor(textColor);
        }

        private void drawRingerIcon() {
            ImageView calendarIcon = (ImageView) row.findViewById(R.id.configure_calendar_icon);
            int iconColor;
            if (calendar.isSelected() || prefs.shouldAllCalendarsBeSynced()) {
                iconColor = getResources().getColor(R.color.icon_tint);
            } else {
                iconColor = getResources().getColor(R.color.icon_disabled);
            }

            Drawable calendarIconDrawable = null;
            RingerType calendarRinger = prefs.getRingerForCalendar(calendar.getId());
            int visibility = View.VISIBLE;
            switch (calendarRinger) {
                case NORMAL:
                    calendarIconDrawable = getResources().getDrawable(R.drawable.ic_state_normal);
                    break;
                case IGNORE:
                    calendarIconDrawable = getResources().getDrawable(R.drawable.ic_state_ignore);
                    break;
                case VIBRATE:
                    calendarIconDrawable = getResources().getDrawable(R.drawable.ic_state_vibrate);
                    break;
                case SILENT:
                    calendarIconDrawable = getResources().getDrawable(R.drawable.ic_state_silent);
                    break;
                default:
                    calendarIconDrawable = getResources().getDrawable(R.drawable.blank);
                    visibility = View.GONE;
            }
            calendarIconDrawable.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
            calendarIcon.setImageDrawable(calendarIconDrawable);
            calendarIcon.setVisibility(visibility);
        }

        private void drawSettingsButton(final int position) {

            ImageView calendarSettingsButton = (ImageView) row.findViewById(R.id.configure_calendar_button);
            Drawable settingsIcon = getResources().getDrawable(R.drawable.action_settings);
            int iconColor;
            if (calendar.isSelected() || prefs.shouldAllCalendarsBeSynced()) {
                iconColor = getResources().getColor(R.color.icon_tint);
            } else {
                iconColor = getResources().getColor(R.color.icon_disabled);
            }
            settingsIcon.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
            calendarSettingsButton.setImageDrawable(settingsIcon);


            calendarSettingsButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    final Calendar selectedCalendar = calendars.get(position);
                    if (selectedCalendar.isSelected() || prefs.shouldAllCalendarsBeSynced()) {
                        final ArrayList<String> ringerTypes = new ArrayList<String>(Arrays.asList(RingerType.names()));
                        String undefinedString = RingerType.UNDEFINED.toString();
                        ringerTypes.remove(undefinedString);
                        for (int i = 0; i < ringerTypes.size(); i++) {
                            String type = ringerTypes.get(i);
                            Character firstChar = type.charAt(0);
                            type = firstChar.toString().toUpperCase() + type.substring(1).toLowerCase();
                            ringerTypes.set(i, type);
                        }

                        final String[] options = ringerTypes.toArray(new String[]{});

                        //set the pre-selected option to the value previously selected
                        int selectedRinger = -1;
                        try {
                            RingerType storedRinger = prefs.getRingerForCalendar(selectedCalendar.getId());
                            if (storedRinger != RingerType.UNDEFINED) {
                                selectedRinger = ringerTypes.indexOf(storedRinger.toString());
                            }
                        } catch (IllegalArgumentException e) {
                            //nothing to do here
                        }

                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                new ContextThemeWrapper(context, R.style.Dialog)
                        );
                        builder.setTitle("Configure calendar");
                        builder.setSingleChoiceItems(options, selectedRinger, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String selectedRinger = options[i];
                                int selectedRingerInt = RingerType.getIntForStringValue(selectedRinger);
                                prefs.setRingerForCalendar(selectedCalendar.getId(), selectedRingerInt);
                                thisAdapter.notifyDataSetChanged();
                            }
                        });
                        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //nothing to do here
                            }
                        });
                        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //nothing to do here
                            }
                        });

                        //the clear button should only be visible if a ringer has been set
                        builder.setNeutralButton(R.string.clear, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                prefs.unsetRingerTypeForCalendar(selectedCalendar.getId());
                                //TODO stringify
                                Toast.makeText(context, "Unset ringer for calendar " + selectedCalendar.getDisplayName(), Toast.LENGTH_SHORT).show();
                                thisAdapter.notifyDataSetChanged();
                            }
                        });

                        builder.show();
                    }
                }
            });
        }

        private void drawCheckBox() {
            CheckBox calendarCheckBox = (CheckBox) row.findViewById(R.id.calendar_checkbox);
            if (calendar.isSelected() || prefs.shouldAllCalendarsBeSynced()) {
                calendarCheckBox.setChecked(true);
            } else {
                calendarCheckBox.setChecked(false);
            }

            if (prefs.shouldAllCalendarsBeSynced()) {
                calendarCheckBox.setEnabled(false);
            }
        }

    }

}




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