Android Open Source - SoundScheduler Edit Rule Activity






From Project

Back to project page SoundScheduler.

License

The source code is released under:

GNU General Public License

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

/*
 * Sound Scheduler//w  w  w  .ja  va2s .  co m
 * Copyright (C) 2013 Victor Kifer
 */

package com.victorkifer.SoundScheduler;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TimePicker;
import com.victorkifer.SoundScheduler.database.RulesDataSource;
import com.victorkifer.SoundScheduler.entities.Rule;

import java.util.Calendar;
import java.util.Date;

public class EditRuleActivity extends ActionBarActivity {
    private final String TAG = EditRuleActivity.class.getSimpleName();

    private RulesDataSource rulesDS;
    private Rule rule;

    private TimePicker startTime;
    private TimePicker endTime;

    private boolean[] checkedDays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newrule);

        startTime = (TimePicker) findViewById(R.id.tpStartTime);
        endTime = (TimePicker) findViewById(R.id.tpEndTime);

        startTime.setIs24HourView(true);
        endTime.setIs24HourView(true);

        rulesDS = new RulesDataSource(EditRuleActivity.this);
        rulesDS.open();

        long ruleId = getIntent().getLongExtra(MainActivity.EXTRA_RULE_ID, 0);
        rule = rulesDS.getRuleById(ruleId);

        if(rule == null) {
            rule = new Rule();
        }
        else {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.setTimeInMillis(c.getTimeInMillis() + rule.getStartTime());

            startTime.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
            startTime.setCurrentMinute(c.get(Calendar.MINUTE));

            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.setTimeInMillis(c.getTimeInMillis() + rule.getEndTime());

            endTime.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
            endTime.setCurrentMinute(c.get(Calendar.MINUTE));

            RadioGroup stateToApply = (RadioGroup) findViewById(R.id.rgStateToApply);
            switch (rule.getStateToApply()) {
                case Rule.STATE_SILENT: // Silent Mode
                    stateToApply.check(stateToApply.getChildAt(0).getId());
                    break;
                case Rule.STATE_VIBRATE: // Vibrate Mode
                    stateToApply.check(stateToApply.getChildAt(1).getId());
                    break;
                default:
                    break;
            }
        }
        checkedDays = rule.getDaysRepeatArray();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_menu_newrule, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_select_days:
                showSelectDaysDialog();
                break;
            case R.id.action_save:
                saveRule();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    private void saveRule() {
        rule.setStartTime((startTime.getCurrentHour()*60*60 + startTime.getCurrentMinute()*60)*1000);
        rule.setEndTime((endTime.getCurrentHour()*60*60+endTime.getCurrentMinute()*60)*1000);
        rule.setActive(true);

        RadioGroup stateToApply = (RadioGroup) findViewById(R.id.rgStateToApply);
        int radioButtonID = stateToApply.getCheckedRadioButtonId();
        if(radioButtonID != -1) {
            View radioButton = stateToApply.findViewById(radioButtonID);
            int idx = stateToApply.indexOfChild(radioButton);

            switch (idx) {
                case 0: // Silent Mode
                    rule.setStateToApply(Rule.STATE_SILENT);
                    break;
                case 1: // Vibrate Mode
                    rule.setStateToApply(Rule.STATE_VIBRATE);
                    break;
                default:
                    break;
            }
        }

        if(rule.getId() == 0) {
            rulesDS.addRule(rule);
        }
        else {
            rulesDS.updateRule(rule);
        }

        sendBroadcast(new Intent(MainActivity.ACTION_RULELIST_UPDATE));
        finish();
    }

    private void showSelectDaysDialog() {
        String title = getResources().getString(R.string.days_dialog_title);
        AlertDialog.Builder ad = new AlertDialog.Builder(EditRuleActivity.this);
        ad.setTitle(title);
        ad.setCancelable(true);
        ad.setMultiChoiceItems(getResources().getStringArray(R.array.days), checkedDays,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        checkedDays[which] = isChecked;
                    }
                });
        ad.setPositiveButton(R.string.dialog_ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        rule.setDaysRepeat(checkedDays);
                    }
                });

        ad.setNegativeButton(R.string.dialog_cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        ad.show();
    }

    @Override
    protected void onDestroy() {
        rulesDS.close();
        super.onDestroy();
    }
}




Java Source Code List

com.victorkifer.SoundScheduler.AboutActivity.java
com.victorkifer.SoundScheduler.EditRuleActivity.java
com.victorkifer.SoundScheduler.MainActivity.java
com.victorkifer.SoundScheduler.adapters.RuleListAdapter.java
com.victorkifer.SoundScheduler.database.RulesDataSource.java
com.victorkifer.SoundScheduler.database.RulesDatabaseHelper.java
com.victorkifer.SoundScheduler.entities.Rule.java
com.victorkifer.SoundScheduler.listeners.RuleDeleteListener.java
com.victorkifer.SoundScheduler.listeners.RuleItemStateListener.java
com.victorkifer.SoundScheduler.managers.SoundManager.java
com.victorkifer.SoundScheduler.receivers.BootCompleteReceiver.java
com.victorkifer.SoundScheduler.receivers.TimeChangeReceiver.java