de.incoherent.osaac.activities.EditAlarmActivity.java Source code

Java tutorial

Introduction

Here is the source code for de.incoherent.osaac.activities.EditAlarmActivity.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Matt Barringer <matt@incoherent.de>
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/

package de.incoherent.osaac.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.format.DateFormat;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.incoherent.osaac.R;
import de.incoherent.osaac.database.Database;
import de.incoherent.osaac.fragments.SettingsFragment;
import de.incoherent.osaac.fragments.TimeScrollerFragment;
import de.incoherent.osaac.models.Alarm;
import de.incoherent.osaac.models.Settings;

public class EditAlarmActivity extends SherlockFragmentActivity {
    private EditText mNameEdit;
    private SettingsFragment mSettingsFragment;
    private TimeScrollerFragment mTimeScrollerFragment;
    private Alarm mAlarm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_alarm);
        mNameEdit = (EditText) findViewById(R.id.nameTextEdit);
        Bundle extras = getIntent().getExtras();
        long id = extras.getLong("id");
        Database db = new Database(this);
        db.open();
        mAlarm = db.getAlarm(id);
        int holidayCount = db.getAlarmHolidaysCount(id);
        mAlarm.set24HourFormat(DateFormat.is24HourFormat(this));
        db.close();
        if (mAlarm == null) {
            setResult(RESULT_CANCELED);
            finish();
        }
        mTimeScrollerFragment = new TimeScrollerFragment(mAlarm.getHour(), mAlarm.getMinute(),
                DateFormat.is24HourFormat(this));
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.timeScrollerLayout, mTimeScrollerFragment);

        mNameEdit.setText(mAlarm.getName());
        Bundle args = new Bundle();
        args.putLong("exemptionId", mAlarm.getId());
        args.putInt("exemptionCount", holidayCount);
        args.putString("ringtoneUri", mAlarm.getRingtoneUri());
        args.putString("ringtoneTitle", mAlarm.getRingtoneTitle());
        args.putBoolean("snoozeChecked", mAlarm.snooze());
        args.putBoolean("challengeChecked", false);
        args.putBoolean("vibrateChecked", mAlarm.vibrate());
        args.putInt("volume", mAlarm.getVolume());
        args.putInt("weekMask", mAlarm.getRepeatWeekBitmask());
        mSettingsFragment = new SettingsFragment(args);
        ft.add(R.id.settingsLayout, mSettingsFragment);
        ft.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, R.id.menu_item_cancel, Menu.NONE, getString(R.string.cancel))
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, R.id.menu_item_save, Menu.NONE, getString(R.string.save))
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem menuItem) {
        switch (menuItem.getItemId()) {
        case R.id.menu_item_cancel:
            setResult(RESULT_CANCELED);
            finish();
            return true;
        case R.id.menu_item_save:
            int newHour = 0, newMinute = 0;
            if (mTimeScrollerFragment != null) {
                newHour = mTimeScrollerFragment.getHour();
                newMinute = mTimeScrollerFragment.getMinute();
            }

            if (mSettingsFragment != null) {
                Bundle settingsBundle = mSettingsFragment.getSettings();
                mAlarm.setName(mNameEdit.getText().toString());
                mAlarm.setSnooze(settingsBundle.getBoolean("snoozeChecked"));
                mAlarm.setVolume(settingsBundle.getInt("volume"));
                //            mAlarm.setChallengeEnabled(settingsBundle.getBoolean("challengeChecked"));
                mAlarm.setVibrate(settingsBundle.getBoolean("vibrateChecked"));
                mAlarm.setRingtoneUri(settingsBundle.getString("ringtoneUri"));
                mAlarm.setRingtoneTitle(settingsBundle.getString("ringtoneTitle"));
                mAlarm.setRepeatWeekBitmask(settingsBundle.getInt("weekMask"));
            }
            mAlarm.setTime(newHour, newMinute);
            Database db = new Database(this);
            db.open();
            db.updateAlarm(mAlarm);
            db.close();
            setResult(RESULT_OK);
            finish();
            return true;
        }

        return super.onOptionsItemSelected(menuItem);
    }

}