that might be all you are interested in but here is the
AlarmDBAdapter, followed by the Alarm class, followed by the
PreferencesActivity layout for creating a new alarm, followed by the
associated class NewAlarm:

package net.esalazar.alarmsutta;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class AlarmDBAdapter {

        public static final String DATABASE_NAME = "alarms.db";
        public static final String DATABASE_TABLE = "alarmsTable";
        public static final int DATABASE_VERSION = 1;

        private SQLiteDatabase db;
        private alarmDBOpenHelper dbHelper;

        public static final String ID_KEY = "_id";
        public static final int ID_COL = 0;
        public static final String EXPIRY_KEY = "expiry";
        public static final int EXPIRY_COL = 1;
        public static final String TITLE_KEY = "title";
        public static final int TITLE_COL = 2;
        public static final String MESSAGE_KEY = "message";
        public static final int MESSAGE_COL = 3;
        public static final String ENABLED_KEY = "enabled";
        public static final int ENABLED_COL = 4;

        private static final String [] ALL_KEYS =
                new String[] 
{ID_KEY,EXPIRY_KEY,TITLE_KEY,MESSAGE_KEY,ENABLED_KEY};

        public AlarmDBAdapter(Context context) {
                dbHelper = new alarmDBOpenHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
        }

        public void close() {
                db.close();
        }

        public void open() {
                try {
                        db = dbHelper.getWritableDatabase();
                } catch (SQLiteException e) {
                        db = dbHelper.getReadableDatabase();
                }
        }

        private ContentValues setContentValuesFromAlarm(Alarm alarm) {
                ContentValues newAlarmValues = new ContentValues();
                newAlarmValues.put(EXPIRY_KEY,alarm.getExpiry().getTime());
                newAlarmValues.put(TITLE_KEY, alarm.getTitle());
                newAlarmValues.put(MESSAGE_KEY,alarm.getMessage());
                newAlarmValues.put(ENABLED_KEY, alarm.isEnabled()?1:0);

                return newAlarmValues;
        }

        // Methods for updating database
        public long insertAlarm(Alarm alarm) {
                return db.insert(DATABASE_TABLE, null, setContentValuesFromAlarm
(alarm));
        }

        public boolean removeAlarm(long id) {
                return db.delete(DATABASE_TABLE, ID_KEY + "=" + id, null) > 0;
        }

        public boolean updateAlarm(long id, Alarm alarm) {
                return db.update(DATABASE_TABLE, 
setContentValuesFromAlarm(alarm),
ID_KEY + "=" + id, null) > 0;
        }

        // Methods for querying database
        public Cursor getAllAlarmItemsCursor() {
                return db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, 
null,
null);
        }

        public Cursor setCursorToAlarmItem(long id) throws SQLException {
                Cursor result = db.query(DATABASE_TABLE, ALL_KEYS,
                                ID_KEY + "=" + id, null, null, null, null);

                if(result.getCount() == 0 || !result.moveToFirst()) {
                        throw new SQLException("No to do item found for row: " 
+ id);
                }
                return result;
        }

        public Alarm getAlarm(long id) throws SQLException {
                return getAlarmFromCursor(setCursorToAlarmItem(id));
        }

        public Alarm getAlarmFromCursor(Cursor cursor) {
                return new Alarm(
                                new Date(cursor.getLong(EXPIRY_COL)),
                                cursor.getString(TITLE_COL),
                                cursor.getString(MESSAGE_COL),
                                cursor.getInt(ENABLED_COL) == 1,
                                cursor.getInt(ID_COL)
                                );
        }

        private static class alarmDBOpenHelper extends SQLiteOpenHelper {

                public alarmDBOpenHelper(Context context, String name, 
CursorFactory
factory, int version) {
                        super(context,name,factory,version);
                }

                // create table alarmsTable (_id integer primary key 
autoincrement,
                //   expiry integer not null, message text not null, title text 
not
null,
                //   enabled integer not null)
                private static final String DATABASE_CREATE = "create table " +
DATABASE_TABLE +        " (" +
                ID_KEY + " integer primary key autoincrement, " + EXPIRY_KEY + "
integer not null, " +
                TITLE_KEY + " text not null, " + MESSAGE_KEY + " text not null, 
" +
                ENABLED_KEY + " integer not null)";

                @Override
                public void onCreate(SQLiteDatabase db) {
                        db.execSQL(DATABASE_CREATE);
                }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
                        Log.w("AlarmDBAdapter", "Upgrading from version " + 
oldVersion + "
to " +
                                        newVersion + ".  All old data 
destroyed.");
                        db.execSQL("drop table if exists " + DATABASE_TABLE);
                        onCreate(db);
                }

        }

}

Alarm.java:

package net.esalazar.alarmsutta;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Alarm {

        private Date expiry;
        private String title;
        private String message;
        private boolean enabled;
        private int id;

        private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/
yyyy HH:mm");

        public Alarm( String eString, String title, String message, int id) {
                setExpiry(eString);
                this.title = title;
                this.message = message;
                this.enabled = true;
                this.id = id;
        }
        public Alarm(Date expiry, String title, String message, boolean
enabled, int id) {
                this.expiry = expiry;
                this.title = title;
                this.message = message;
                this.enabled = enabled;
                this.id = id;
        }
        public Alarm(Date expiry, String title, String message, int id) {
                this.expiry = expiry;
                this.title = title;
                this.message = message;
                this.enabled = true;
                this.id = id;
        }

        public Date getExpiry() { return expiry; }
        public String getTitle() { return title; }
        public String getMessage() { return message; }
        public boolean isEnabled() { return enabled; }
        public int getId() { return id; }

        public void setExpiry(Date expiry) { this.expiry = expiry; }
        public void setTitle(String title) { this.title = title; }
        public void setMessage(String message) { this.message = message; }
        public void setEnabled(boolean enabled) { this.enabled = enabled; }
        public void setID(int id) { this.id = id; }

        public String getExpiryString() {
                return sdf.format(expiry);
        }
        public void setExpiry(String expiryString) {

                expiry = Calendar.getInstance().getTime();
                try {
                        expiry = sdf.parse(expiryString);
                } catch (ParseException e) {
                        e.printStackTrace();
                }
        }

        @Override
        public String toString() {
                return title;
        }

}


layout for new activity using PreferencesActivity:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
android"
        android:title="Alarm Settings">

    <Preference android:key="expiry_time"
        android:title="Time"/>
    <Preference android:key="expiry_date"
        android:title="Date"/>
    <EditTextPreference android:key="title"
        android:title="Title"
        android:dialogTitle="Title" />
    <EditTextPreference android:key="message"
        android:title="Message"
        android:dialogTitle="Message" />
    <CheckBoxPreference android:key="enabled"
        android:title="Enabled"/>

</PreferenceScreen>


NewAlarm.java:

package net.esalazar.alarmsutta;

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

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.KeyEvent;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class NewAlarm extends PreferenceActivity {

        // references to each preference thing
        private Preference timePref;
        private Preference datePref;
        private EditTextPreference titlePref;
        private EditTextPreference messagePref;
        private CheckBoxPreference enabledPref;

        private int mHour;
        private int mMinute;
        private int mYear;
        private int mMonth;
        private int mDay;

        private String mTitle;
        private String mMessage;
        private boolean mEnabled;

        private static final int DIALOG_TIMEPICKER = 0;
    private static final int DIALOG_DATEPICKER = 1;

        private class OnSetExpiryTime implements
TimePickerDialog.OnTimeSetListener {
                public void onTimeSet(TimePicker view, int hourOfDay, int 
minute) {
                        mHour = hourOfDay;
                        mMinute = minute;
                        updateTime();
                }
        }
        private class OnSetExpiryDate implements
DatePickerDialog.OnDateSetListener {
                public void onDateSet(DatePicker view, int year, int 
monthOfYear,
int dayOfMonth) {
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        updateDate();
                }
        }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.alarm_settings);

        timePref = findPreference("expiry_time");
        datePref = findPreference("expiry_date");
        titlePref = (EditTextPreference)findPreference("title");
        messagePref = (EditTextPreference)findPreference("message");
        enabledPref = (CheckBoxPreference)findPreference("enabled");

                Intent request = getIntent();

                long expiryTime = request.getLongExtra("expiryTime", (new Date
().getTime()) );
                Date expiry = new Date(expiryTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(expiry);

                mHour = calendar.get(Calendar.HOUR_OF_DAY);
                mMinute = calendar.get(Calendar.MINUTE);
                mYear = calendar.get(Calendar.YEAR);
                mMonth = calendar.get(Calendar.MONTH);
                mDay = calendar.get(Calendar.DAY_OF_MONTH);

                mTitle = request.getStringExtra("title");
                mMessage = request.getStringExtra("message");
                mEnabled = request.getBooleanExtra("enabled", true);

                // Set defaults for title and message.
                if(mTitle == null)
                        mTitle = "Alarm";
                if(mMessage == null)
                        mMessage = "";

        titlePref.setText(mTitle);
        titlePref.setSummary(mTitle);
        messagePref.setText(mMessage);
        messagePref.setSummary(mMessage);

        enabledPref.setChecked(mEnabled);
        updateTime();
        updateDate();

        titlePref.setOnPreferenceChangeListener(new
Preference.OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference, Object
newValue) {
                        mTitle = (String)newValue;
                        preference.setSummary(mTitle);
                        return true;
                }
        });
        messagePref.setOnPreferenceChangeListener(new
Preference.OnPreferenceChangeListener() {
                        public boolean onPreferenceChange(Preference 
preference, Object
newValue) {
                                mMessage = (String)newValue;
                                preference.setSummary(mMessage);
                        return false;
                        }
                });
        enabledPref.setOnPreferenceChangeListener(new
Preference.OnPreferenceChangeListener() {
                        public boolean onPreferenceChange(Preference 
preference, Object
newValue) {
                                mEnabled = (Boolean)newValue;
                                
((CheckBoxPreference)preference).setChecked(mEnabled);
                                return false;
                        }
                });

        }

    private void updateTime() {
        int twelveHour = mHour%12;
        if(twelveHour == 0)
                twelveHour = 12;

        String minuteString = (mMinute < 10) ? minuteString = "0" +
mMinute : Integer.toString(mMinute);
        String amPm = (mHour >= 12) ? "pm" : "am";

        timePref.setSummary(twelveHour + ":" + minuteString + amPm);
    }
    private void updateDate() {
        // Months are zero indexed in Calendar but 1 indexed irl
        datePref.setSummary( (mMonth+1) + "/" + mDay + "/" + mYear);
    }

        @Override
        public boolean onPreferenceTreeClick(PreferenceScreen
preferenceScreen,
                        Preference preference) {

                if(preference == timePref) {
                        showDialog(DIALOG_TIMEPICKER);
                } else if (preference == datePref) {
                        showDialog(DIALOG_DATEPICKER);
                }
                return super.onPreferenceTreeClick(preferenceScreen, 
preference);
        }

        @Override
        protected Dialog onCreateDialog(int id) {
                Dialog d;

                switch(id) {
                        case DIALOG_TIMEPICKER:
                                d = new TimePickerDialog(this, new 
OnSetExpiryTime(), mHour,
mMinute, false);
                                d.setTitle("Time");
                                break;
                        case DIALOG_DATEPICKER:
                                d = new DatePickerDialog(this, new 
OnSetExpiryDate(), mYear,
mMonth, mDay);
                                d.setTitle("Date");
                                break;
                        default:
                                d = null;
                }

                return d;
        }

        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
                switch(id) {
                        case DIALOG_TIMEPICKER:
                                TimePickerDialog timeDialog = 
(TimePickerDialog)dialog;
                                timeDialog.updateTime(mHour, mMinute);
                                break;
                        case DIALOG_DATEPICKER:
                                DatePickerDialog dateDialog = 
(DatePickerDialog)dialog;
                                dateDialog.updateDate(mYear, mMonth, mDay);
                                break;
                }
        }


        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {

                if(keyCode == KeyEvent.KEYCODE_BACK) {
                        exitWithResult();
                        return true;
                }

                return super.onKeyDown(keyCode, event);
        }

        protected void exitWithResult() {

                // If something nasty is hapenning, don't try
                // and save changes because they could be junk
                Calendar calendar = Calendar.getInstance();

                calendar.set(Calendar.HOUR_OF_DAY, mHour);
                calendar.set(Calendar.MINUTE, mMinute);
                calendar.set(Calendar.MONTH, mMonth );
                calendar.set(Calendar.DAY_OF_MONTH, mDay );
                calendar.set(Calendar.YEAR, mYear );

                long expiryTime = calendar.getTime().getTime();

                Intent result = new Intent();
                result.putExtra("title", mTitle);
                result.putExtra("message", mMessage);
                result.putExtra("expiryTime", expiryTime);
                result.putExtra("enabled", mEnabled);

                // We need to copy around the id so we can find our id in the db
later
                result.putExtra("id", getIntent().getIntExtra("id", -1));
                setResult(RESULT_OK,result);

                finish();

        }

}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to