Android Open Source - volumescheduler Volume Rule Fragment






From Project

Back to project page volumescheduler.

License

The source code is released under:

GNU General Public License

If you think the Android project volumescheduler 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 RuneCasters IT Solutions.
 *// w  ww.  j av  a2s  .  c o m
 * This file is part of VolumeScheduler.
 *
 * VolumeScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VolumeScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VolumeScheduler.  If not, see <http://www.gnu.org/licenses/>.
 */

package au.com.runecasters.volumescheduler.app;

import android.app.Fragment;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import au.com.runecasters.volumescheduler.R;
import au.com.runecasters.volumescheduler.model.SchedulerRule;

public class VolumeRuleFragment extends Fragment implements RuleActivity.ScheduleRuleSetter {
    private CheckBox mCheckRingtoneVol;
    private CheckBox mCheckVibrate;
    private CheckBox mCheckNotificationVol;
    private CheckBox mCheckMatchRingtoneNotificationVol;
    private CheckBox mCheckMediaVol;
    private CheckBox mCheckAlarmVol;
    private SeekBar mSeekBarRingtoneVol;
    private SeekBar mSeekBarNotificationVol;
    private SeekBar mSeekBarMediaVol;
    private SeekBar mSeekBarAlarmVol;
    private EditText mTextRuleName;
    private SchedulerRule mSchedulerRule;
    private boolean mFragmentLoaded;

    public VolumeRuleFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_rule_volume, container, false);
        mSchedulerRule = ((RuleActivity) getActivity()).mSchedulerRule;
        uiSetup(rootView);
        mFragmentLoaded = true;
        return rootView;
    }

    private void uiSetup(View rootView) {
        mCheckRingtoneVol = (CheckBox) rootView.findViewById(R.id.checkBoxRingtoneVol);
        mCheckVibrate = (CheckBox) rootView.findViewById(R.id.checkBoxVibrate);
        mCheckNotificationVol = (CheckBox) rootView.findViewById(R.id.checkBoxNotificationVol);
        mCheckMatchRingtoneNotificationVol = (CheckBox) rootView.findViewById(R.id.checkBoxMatchRingtoneNotificationVol);
        mCheckMediaVol = (CheckBox) rootView.findViewById(R.id.checkBoxMediaVol);
        mCheckAlarmVol = (CheckBox) rootView.findViewById(R.id.checkBoxAlarmVol);
        mSeekBarRingtoneVol = (SeekBar) rootView.findViewById(R.id.seekBarRingtoneVol);
        mSeekBarNotificationVol = (SeekBar) rootView.findViewById(R.id.seekBarNotificationVol);
        mSeekBarMediaVol = (SeekBar) rootView.findViewById(R.id.seekBarMediaVol);
        mSeekBarAlarmVol = (SeekBar) rootView.findViewById(R.id.seekBarAlarmVol);
        mTextRuleName = (EditText) rootView.findViewById(R.id.textRuleName);

        mSeekBarAlarmVol.setEnabled(false);
        mSeekBarNotificationVol.setEnabled(false);
        mSeekBarRingtoneVol.setEnabled(false);
        mSeekBarMediaVol.setEnabled(false);

        mCheckRingtoneVol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                mSeekBarRingtoneVol.setEnabled(isChecked);
                mCheckVibrate.setEnabled(isChecked);
                if (isChecked) {
                    if (mCheckVibrate.isChecked()) {
                        mCheckNotificationVol.setChecked(true);
                        mCheckNotificationVol.setEnabled(false);
                        mCheckMatchRingtoneNotificationVol.setChecked(true);
                        mCheckMatchRingtoneNotificationVol.setEnabled(false);
                    } else if (mCheckNotificationVol.isChecked()) {
                        mCheckMatchRingtoneNotificationVol.setEnabled(true);
                    }
                } else {
                    mCheckNotificationVol.setEnabled(true);
                    if (mCheckNotificationVol.isChecked()) {
                        mCheckMatchRingtoneNotificationVol.setChecked(false);
                        mCheckMatchRingtoneNotificationVol.setEnabled(false);
                    }
                }
                onChange();
            }
        });
        mSeekBarRingtoneVol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mCheckMatchRingtoneNotificationVol.isChecked()) {
                    mSeekBarNotificationVol.setProgress(progress);
                }
                if (progress == 0) {
                    mCheckVibrate.setVisibility(View.VISIBLE);
                    mCheckVibrate.setChecked(true);
                    if (mFragmentLoaded) {
                        ((Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(300);
                    }
                } else {
                    mCheckVibrate.setVisibility(View.GONE);
                    mCheckVibrate.setChecked(false);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        mCheckVibrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    if (mCheckRingtoneVol.isChecked()) {
                        mCheckNotificationVol.setChecked(true);
                        mCheckNotificationVol.setEnabled(false);
                        mCheckMatchRingtoneNotificationVol.setChecked(true);
                        mCheckMatchRingtoneNotificationVol.setEnabled(false);
                    }
                } else {
                    mCheckNotificationVol.setEnabled(true);
                    mCheckMatchRingtoneNotificationVol.setEnabled(true);
                }
            }
        });
        mCheckVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCheckVibrate.isChecked()) {
                    ((Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(300);
                }
            }
        });
        mCheckNotificationVol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked && mCheckRingtoneVol.isChecked()) {
                    mCheckMatchRingtoneNotificationVol.setEnabled(!mCheckVibrate.isChecked());
                } else if (isChecked) {
                    mCheckMatchRingtoneNotificationVol.setEnabled(false);
                    mCheckMatchRingtoneNotificationVol.setChecked(false);
                } else {
                    mCheckMatchRingtoneNotificationVol.setEnabled(false);
                    mCheckMatchRingtoneNotificationVol.setChecked(true);
                }
                onChange();
            }
        });
        mCheckMatchRingtoneNotificationVol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                mSeekBarNotificationVol.setEnabled(!isChecked);
                if (isChecked) {
                    mSeekBarNotificationVol.setVisibility(View.GONE);
                    mSeekBarNotificationVol.setProgress(mSeekBarRingtoneVol.getProgress());
                } else {
                    mSeekBarNotificationVol.setVisibility(View.VISIBLE);
                }
                onChange();
            }
        });
        mCheckMediaVol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                mSeekBarMediaVol.setEnabled(isChecked);
                onChange();
            }
        });
        mCheckAlarmVol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                mSeekBarAlarmVol.setEnabled(isChecked);
                onChange();
            }
        });
        mTextRuleName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                onChange();
            }
        });

        // Set volume sliders to current settings
        AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        mSeekBarRingtoneVol.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_RING));
        if (mSeekBarRingtoneVol.getProgress() == 0) {
            if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
                mCheckVibrate.setChecked(true);
            }
        }
        mSeekBarNotificationVol.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
        mSeekBarMediaVol.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        mSeekBarAlarmVol.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));

        // Prefill data if this is an edit of an existing rule
        if (mSchedulerRule.changingRingtone()) {
            mCheckRingtoneVol.setChecked(true);
            mSeekBarRingtoneVol.setProgress(mSchedulerRule.getVolRingtone());
            mCheckVibrate.setChecked(mSchedulerRule.isVibrate());
        }
        if (mSchedulerRule.changingNotification()) {
            mCheckNotificationVol.setChecked(true);
            mSeekBarNotificationVol.setProgress(mSchedulerRule.getVolNotification());
            if (mSchedulerRule.changingRingtone() && mSchedulerRule.getVolRingtone() != mSchedulerRule.getVolNotification()) {
                mCheckMatchRingtoneNotificationVol.setChecked(false);
            }
        }
        if (mSchedulerRule.changingMedia()) {
            mCheckMediaVol.setChecked(true);
            mSeekBarMediaVol.setProgress(mSchedulerRule.getVolMedia());
        }
        if (mSchedulerRule.changingAlarm()) {
            mCheckAlarmVol.setChecked(true);
            mSeekBarAlarmVol.setProgress(mSchedulerRule.getVolAlarm());
        }
        mTextRuleName.setText(mSchedulerRule.toString());
    }

    private void onChange() {
        if (mTextRuleName.length() > 0 &&
                (mCheckRingtoneVol.isChecked() ||
                        mCheckNotificationVol.isChecked() ||
                        mCheckMediaVol.isChecked() ||
                        mCheckAlarmVol.isChecked())) {
            ((RuleActivity) getActivity()).ruleUpdate(RuleActivity.VOLUME_RULE, true);
        } else {
            ((RuleActivity) getActivity()).ruleUpdate(RuleActivity.VOLUME_RULE, false);
        }
    }

    @Override
    public void setRules(SchedulerRule schedulerRule) {
        schedulerRule.changeRingtone(mCheckRingtoneVol.isChecked(), mSeekBarRingtoneVol.getProgress());
        schedulerRule.setVibrate(mCheckRingtoneVol.isChecked() && mCheckVibrate.isChecked());
        schedulerRule.changeNotification(mCheckNotificationVol.isChecked(), mSeekBarNotificationVol.getProgress());
        schedulerRule.changeMedia(mCheckMediaVol.isChecked(), mSeekBarMediaVol.getProgress());
        schedulerRule.changeAlarm(mCheckAlarmVol.isChecked(), mSeekBarAlarmVol.getProgress());
        schedulerRule.setRuleName(mTextRuleName.getText().toString());
    }
}




Java Source Code List

au.com.runecasters.volumescheduler.app.AboutActivity.java
au.com.runecasters.volumescheduler.app.AdFragment.java
au.com.runecasters.volumescheduler.app.ApplicationTest.java
au.com.runecasters.volumescheduler.app.CalendarRuleFragment.java
au.com.runecasters.volumescheduler.app.MainActivity.java
au.com.runecasters.volumescheduler.app.RuleActivity.java
au.com.runecasters.volumescheduler.app.TimeRuleFragment.java
au.com.runecasters.volumescheduler.app.VolumeRuleFragment.java
au.com.runecasters.volumescheduler.model.DatabaseHelper.java
au.com.runecasters.volumescheduler.model.SchedulerRule.java
au.com.runecasters.volumescheduler.service.VolumeSchedulerService.java
au.com.runecasters.volumescheduler.view.DialogFragments.java
au.com.runecasters.volumescheduler.view.RuleListAdapter.java