AlarmClockpublic class AlarmClock extends android.app.Activity
Fields Summary |
---|
static final String | PREFERENCES | static final int | SET_ALARM | static final String | PREF_CLOCK_FACE | static final String | PREF_SHOW_CLOCK | static final int | MAX_ALARM_COUNTCap alarm count at this number | static final boolean | DEBUGThis must be false for production. If true, turns on logging,
test code, etc. | private android.content.SharedPreferences | mPrefs | private android.view.LayoutInflater | mFactory | private android.view.ViewGroup | mClockLayout | private android.view.View | mClock | private android.view.MenuItem | mAddAlarmItem | private android.view.MenuItem | mToggleClockItem | private android.widget.ListView | mAlarmsList | private android.database.Cursor | mCursor | private int | mFaceWhich clock face to show | static final int[] | CLOCKS |
Methods Summary |
---|
private boolean | getClockVisibility()
return mClockLayout.getVisibility() == View.VISIBLE;
| protected void | inflateClock()
if (mClock != null) {
mClockLayout.removeView(mClock);
}
mClock = mFactory.inflate(CLOCKS[mFace], null);
mClockLayout.addView(mClock, 0);
| public boolean | onContextItemSelected(android.view.MenuItem item)
// Confirm that the alarm will be deleted.
new AlertDialog.Builder(this)
.setTitle(getString(R.string.delete_alarm))
.setMessage(getString(R.string.delete_alarm_confirm))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int w) {
Alarms.deleteAlarm(AlarmClock.this,
item.getItemId());
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
return true;
| protected void | onCreate(android.os.Bundle icicle)
super.onCreate(icicle);
// sanity check -- no database, no clock
if (getContentResolver() == null) {
new AlertDialog.Builder(this)
.setTitle(getString(R.string.error))
.setMessage(getString(R.string.dberror))
.setPositiveButton(
android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
finish();
}})
.setIcon(android.R.drawable.ic_dialog_alert)
.create().show();
return;
}
setContentView(R.layout.alarm_clock);
mFactory = LayoutInflater.from(this);
mPrefs = getSharedPreferences(PREFERENCES, 0);
mCursor = Alarms.getAlarmsCursor(getContentResolver());
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setItemsCanFocus(true);
mClockLayout = (ViewGroup) findViewById(R.id.clock_layout);
mClockLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(AlarmClock.this, ClockPicker.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
setClockVisibility(mPrefs.getBoolean(PREF_SHOW_CLOCK, true));
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
super.onCreateOptionsMenu(menu);
mAddAlarmItem = menu.add(0, 0, 0, R.string.add_alarm);
mAddAlarmItem.setIcon(android.R.drawable.ic_menu_add);
mToggleClockItem = menu.add(0, 0, 0, R.string.hide_clock);
mToggleClockItem.setIcon(R.drawable.ic_menu_clock_face);
MenuItem settingsItem = menu.add(0, 0, 0, R.string.settings);
settingsItem.setIcon(android.R.drawable.ic_menu_preferences);
settingsItem.setIntent(new Intent(this, SettingsActivity.class));
return true;
| protected void | onDestroy()
super.onDestroy();
ToastMaster.cancelToast();
mCursor.deactivate();
| public boolean | onOptionsItemSelected(android.view.MenuItem item)
if (item == mAddAlarmItem) {
Uri uri = Alarms.addAlarm(getContentResolver());
// FIXME: scroll to new item. mAlarmsList.requestChildRectangleOnScreen() ?
String segment = uri.getPathSegments().get(1);
int newId = Integer.parseInt(segment);
if (Log.LOGV) Log.v("In AlarmClock, new alarm id = " + newId);
Intent intent = new Intent(AlarmClock.this, SetAlarm.class);
intent.putExtra(Alarms.ID, newId);
startActivityForResult(intent, SET_ALARM);
return true;
} else if (item == mToggleClockItem) {
setClockVisibility(!getClockVisibility());
saveClockVisibility();
return true;
}
return false;
| public boolean | onPrepareOptionsMenu(android.view.Menu menu)Only allow user to add a new alarm if there are fewer than
MAX_ALARM_COUNT
super.onPrepareOptionsMenu(menu);
mAddAlarmItem.setVisible(mAlarmsList.getChildCount() < MAX_ALARM_COUNT);
mToggleClockItem.setTitle(getClockVisibility() ? R.string.hide_clock :
R.string.show_clock);
return true;
| protected void | onResume()
super.onResume();
int face = mPrefs.getInt(PREF_CLOCK_FACE, 0);
if (mFace != face) {
if (face < 0 || face >= AlarmClock.CLOCKS.length)
mFace = 0;
else
mFace = face;
inflateClock();
}
| private void | saveClockVisibility()
mPrefs.edit().putBoolean(PREF_SHOW_CLOCK, getClockVisibility()).commit();
| private void | setClockVisibility(boolean visible)
mClockLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
|
|