Android Open Source - Tacere Main Settings 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/* w ww.  j a  va  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.Activity;
import android.app.AlertDialog;
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.v7.widget.SwitchCompat;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.ciasaboark.tacere.R;
import org.ciasaboark.tacere.activity.AdvancedSettingsActivity;
import org.ciasaboark.tacere.billing.Authenticator;
import org.ciasaboark.tacere.event.ringer.RingerType;
import org.ciasaboark.tacere.prefs.Prefs;

import java.util.ArrayList;

public class MainSettingsFragment extends android.support.v4.app.Fragment {
    public static final String SHOW_ADVANCED_SETTINGS_LINK = "showAdvancedSettings";
    public static final String TAG = "MainSettingsFragment";
    private static final int layout = R.layout.fragment_main_settings;
    private Prefs prefs;
    private Context context;
    private View rootView;
    private boolean showAdvancedSettingsLink = true;
    private OnSelectCalendarsListener mListener;

    public MainSettingsFragment() {
        // Required empty public constructor
    }

    public static MainSettingsFragment newInstance(boolean showAdvancedSettingsLink) {
        MainSettingsFragment fragment = new MainSettingsFragment();
        Bundle args = new Bundle();
        args.putBoolean(SHOW_ADVANCED_SETTINGS_LINK, showAdvancedSettingsLink);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnSelectCalendarsListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement " + OnSelectCalendarsListener.class + " to embed this fragment");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();
        if (context == null) {
            throw new IllegalStateException("Fragment " + TAG + " was not able to find its context");
        }

        if (getArguments() != null) {
            Bundle args = getArguments();
            showAdvancedSettingsLink = args.getBoolean(SHOW_ADVANCED_SETTINGS_LINK, true);
        }

        rootView = inflater.inflate(layout, container, false);
        prefs = new Prefs(getActivity());
        drawAllWidgets();

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void drawAllWidgets() {
        drawCalendarWidgets();
        drawRingerWidgets();
        drawMediaWidgets();
        drawAlarmWidgets();
        drawAdvancedSettingsWidget();
    }

    private void drawAdvancedSettingsWidget() {
        LinearLayout advancedSettingsContainer = (LinearLayout) rootView.findViewById(R.id.settings_advanced_settings_container);
        if (showAdvancedSettingsLink) {
            advancedSettingsContainer.setVisibility(View.VISIBLE);
            LinearLayout advancedSettingsBox = (LinearLayout) rootView.findViewById(R.id.settings_advanced_settings_box);
            advancedSettingsBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(context, AdvancedSettingsActivity.class));
                }
            });
        } else {
            advancedSettingsContainer.setVisibility(View.GONE);
        }
    }

    private void drawCalendarWidgets() {
        ImageView calendarIcon = (ImageView) rootView.findViewById(R.id.calendar_icon);
        TextView calendarTV = (TextView) rootView.findViewById(R.id.calendar_text);
        RelativeLayout calendarBox = (RelativeLayout) rootView.findViewById(R.id.select_calendar_box);

        calendarBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Authenticator authenticator = new Authenticator(context);
                if (authenticator.isAuthenticated()) {
                    mListener.onSelectCalendarsSelectedListener();
                } else {
                    authenticator.showUpgradeDialog();
                }

            }
        });
        calendarBox.setClickable(true);

    }

    private void drawRingerWidgets() {
        RelativeLayout ringerBox = (RelativeLayout) rootView.findViewById(R.id.settings_ringerBox);
        ringerBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] ringerTypes = RingerType.names();
                //we don't want ringer type UNDEFINED to be an option
                ArrayList<String> ringerList = new ArrayList<String>();
                for (String type : ringerTypes) {
                    if (!type.equalsIgnoreCase(RingerType.UNDEFINED.toString())) {
                        type = type.charAt(0) + type.substring(1).toLowerCase();
                        ringerList.add(type);
                    }
                }
                final String[] filteredRingerTypes = ringerList.toArray(new String[]{});
                int previouslySelectedRinger = ringerList.indexOf(prefs.getRingerType().toString());


                final AlertDialog alert = new AlertDialog.Builder(
                        new ContextThemeWrapper(context, R.style.Dialog)
                )
                        .setTitle(R.string.settings_section_general_ringer)
                        .setSingleChoiceItems(filteredRingerTypes, previouslySelectedRinger,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        String selectedRingerString = filteredRingerTypes[which];
                                        int selectedRingerValue = RingerType.getIntForStringValue(selectedRingerString);
                                        RingerType selectedRinger = RingerType.getTypeForInt(selectedRingerValue);
                                        prefs.setRingerType(selectedRinger);
                                        drawRingerWidgets();
                                        dialog.dismiss();
                                    }
                                })
                        .create();

                alert.show();
            }
        });

        //the ringer type description
        TextView ringerDescriptionTV = (TextView) rootView.findViewById(R.id.ringerTypeDescription);
        TextView ringerTV = (TextView) rootView.findViewById(R.id.settings_ringerTitle);

        Drawable icon;
        switch (prefs.getRingerType()) {
            case NORMAL:
                ringerDescriptionTV.setText(R.string.settings_section_general_ringer_normal);
                icon = getResources().getDrawable(R.drawable.ic_state_normal);
                break;
            case VIBRATE:
                ringerDescriptionTV.setText(R.string.settings_section_general_ringer_vibrate);
                icon = getResources().getDrawable(R.drawable.ic_state_vibrate);
                break;
            case SILENT:
                ringerDescriptionTV.setText(R.string.settings_section_general_ringer_silent);
                icon = getResources().getDrawable(R.drawable.ic_state_silent);
                break;
            case IGNORE:
                ringerDescriptionTV.setText(R.string.settings_section_general_ringer_ignore);
                icon = getResources().getDrawable(R.drawable.ic_state_ignore);
                break;
            default:
                throw new IllegalStateException("Saved default ringer is of unknown type: "
                        + prefs.getRingerType());
        }


        int iconColor = getResources().getColor(R.color.icon_tint);
        icon.mutate().setColorFilter(iconColor, PorterDuff.Mode.MULTIPLY);
        ringerDescriptionTV.setTextColor(getResources().getColor(R.color.text_color));
        ringerTV.setTextColor(getResources().getColor(R.color.text_color));

        ImageView ringerIcon = (ImageView) rootView.findViewById(R.id.settings_ringerIcon);
        ringerIcon.setImageDrawable(icon);
    }

    private void drawMediaWidgets() {
        RelativeLayout mediaBox = (RelativeLayout) rootView.findViewById(R.id.settings_mediaBox);
        mediaBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prefs.setMediaVolumeShouldSilence(!prefs.shouldMediaVolumeBeSilenced());
                drawMediaWidgets();
            }
        });

        //the media volumes toggle
        SwitchCompat mediaSwitch = (SwitchCompat) rootView.findViewById(R.id.adjustMediaCheckBox);
        if (prefs.shouldMediaVolumeBeSilenced()) {
            mediaSwitch.setChecked(true);
        } else {
            mediaSwitch.setChecked(false);
        }
    }

    private void drawAlarmWidgets() {
        RelativeLayout alarmBox = (RelativeLayout) rootView.findViewById(R.id.settings_alarmBox);
        alarmBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prefs.setAlarmVolumeShouldSilence(!prefs.shouldAlarmVolumeBeSilenced());
                drawAlarmWidgets();
            }
        });

        //the alarm volumes toggle
        SwitchCompat alarmSwitch = (SwitchCompat) rootView.findViewById(R.id.adjustAlarmCheckBox);
        alarmSwitch.setChecked(prefs.shouldAlarmVolumeBeSilenced());
    }


    public void onClickAdvancedSettings(View v) {
        Intent i = new Intent(context, AdvancedSettingsActivity.class);
        startActivity(i);
    }


    public interface OnSelectCalendarsListener {
        // TODO: Update argument type and name
        public void onSelectCalendarsSelectedListener();
    }
}




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