import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
+import android.database.DataSetObserver;
+import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
+import android.widget.Button;
+import android.widget.CheckBox;
import android.widget.CursorAdapter;
+import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
-import android.widget.CheckBox;
import java.util.Calendar;
-import java.text.DateFormatSymbols;
/**
* AlarmClock application.
public class AlarmClock extends Activity implements OnItemClickListener {
final static String PREFERENCES = "AlarmClock";
- final static String PREF_CLOCK_FACE = "face";
- final static String PREF_SHOW_CLOCK = "show_clock";
/** Cap alarm count at this number */
final static int MAX_ALARM_COUNT = 12;
private SharedPreferences mPrefs;
private LayoutInflater mFactory;
- private ViewGroup mClockLayout;
- private View mClock = null;
private ListView mAlarmsList;
private Cursor mCursor;
- private String mAm, mPm;
-
- /**
- * Which clock face to show
- */
- private int mFace = -1;
-
- /*
- * FIXME: it would be nice for this to live in an xml config file.
- */
- final static int[] CLOCKS = {
- R.layout.clock_basic_bw,
- R.layout.clock_googly,
- R.layout.clock_droid2,
- R.layout.clock_droids,
- R.layout.digital_clock
- };
+ private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
+ Alarm alarm) {
+ bar.setImageResource(enabled ? R.drawable.ic_indicator_on
+ : R.drawable.ic_indicator_off);
+ Alarms.enableAlarm(this, alarm.id, enabled);
+ if (enabled) {
+ SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
+ alarm.daysOfWeek);
+ }
+ }
private class AlarmTimeAdapter extends CursorAdapter {
public AlarmTimeAdapter(Context context, Cursor cursor) {
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
- ((TextView) ret.findViewById(R.id.am)).setText(mAm);
- ((TextView) ret.findViewById(R.id.pm)).setText(mPm);
-
DigitalClock digitalClock = (DigitalClock)ret.findViewById(R.id.digitalClock);
digitalClock.setLive(false);
if (Log.LOGV) Log.v("newView " + cursor.getPosition());
public void bindView(View view, Context context, Cursor cursor) {
final Alarm alarm = new Alarm(cursor);
- CheckBox onButton = (CheckBox)view.findViewById(R.id.alarmButton);
- onButton.setChecked(alarm.enabled);
- onButton.setOnClickListener(new OnClickListener() {
+ View indicator = view.findViewById(R.id.indicator);
+
+ // Set the initial resource for the bar image.
+ final ImageView barOnOff =
+ (ImageView) indicator.findViewById(R.id.bar_onoff);
+ barOnOff.setImageResource(alarm.enabled ?
+ R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
+
+ // Set the initial state of the clock "checkbox"
+ final CheckBox clockOnOff =
+ (CheckBox) indicator.findViewById(R.id.clock_onoff);
+ clockOnOff.setChecked(alarm.enabled);
+
+ // Handle the "checkbox" click and toggle the alarm.
+ clockOnOff.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean isChecked = ((CheckBox) v).isChecked();
- Alarms.enableAlarm(AlarmClock.this, alarm.id,
- isChecked);
- if (isChecked) {
- SetAlarm.popAlarmSetToast(AlarmClock.this,
- alarm.hour, alarm.minutes, alarm.daysOfWeek);
- }
+ updateIndicatorAndAlarm(isChecked, barOnOff, alarm);
+ }
+ });
+
+ // Clicking outside the "checkbox" should also change the state.
+ indicator.setOnClickListener(new OnClickListener() {
+ public void onClick(View v) {
+ clockOnOff.toggle();
+ updateIndicatorAndAlarm(clockOnOff.isChecked(),
+ barOnOff, alarm);
}
});
c.set(Calendar.HOUR_OF_DAY, alarm.hour);
c.set(Calendar.MINUTE, alarm.minutes);
digitalClock.updateTime(c);
+ digitalClock.setTypeface(Typeface.DEFAULT);
// Set the repeat text or leave it blank if it does not repeat.
TextView daysOfWeekView =
// Display the label
TextView labelView =
- (TextView) digitalClock.findViewById(R.id.label);
+ (TextView) view.findViewById(R.id.label);
if (alarm.label != null && alarm.label.length() != 0) {
labelView.setText(alarm.label);
labelView.setVisibility(View.VISIBLE);
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
- String[] ampm = new DateFormatSymbols().getAmPmStrings();
- mAm = ampm[0];
- mPm = ampm[1];
-
mFactory = LayoutInflater.from(this);
mPrefs = getSharedPreferences(PREFERENCES, 0);
mCursor = Alarms.getAlarmsCursor(getContentResolver());
updateLayout();
- setClockVisibility(mPrefs.getBoolean(PREF_SHOW_CLOCK, true));
- }
-
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- updateLayout();
- inflateClock();
}
private void updateLayout() {
setContentView(R.layout.alarm_clock);
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
- mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
+ AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
+ mAlarmsList.setAdapter(adapter);
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setOnItemClickListener(this);
mAlarmsList.setOnCreateContextMenuListener(this);
- mClockLayout = (ViewGroup) findViewById(R.id.clock_layout);
- mClockLayout.setOnClickListener(new View.OnClickListener() {
+ final Button addAlarm = (Button) findViewById(R.id.add_alarm);
+ addAlarm.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);
+ Uri uri = Alarms.addAlarm(getContentResolver());
+ // FIXME: scroll to new item?
+ 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.ALARM_ID, newId);
startActivity(intent);
}
});
- }
+ // Update the enabled state of the "Add alarm" menu item depending on
+ // how many alarms are set.
+ adapter.registerDataSetObserver(new DataSetObserver() {
+ public void onChanged() {
+ updateAddAlarmButton(addAlarm);
+ }
+ public void onInvalidate() {
+ updateAddAlarmButton(addAlarm);
+ }
+ });
- @Override
- protected void onResume() {
- super.onResume();
+ Button settings = (Button) findViewById(R.id.settings);
+ settings.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View v) {
+ startActivity(
+ new Intent(AlarmClock.this, SettingsActivity.class));
+ }
+ });
+ }
- 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 updateAddAlarmButton(Button b) {
+ b.setEnabled(mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT);
}
@Override
mCursor.deactivate();
}
- protected void inflateClock() {
- if (mClock != null) {
- mClockLayout.removeView(mClock);
- }
-
- LayoutInflater.from(this).inflate(CLOCKS[mFace], mClockLayout);
- mClock = findViewById(R.id.clock);
-
- TextView am = (TextView) findViewById(R.id.am);
- TextView pm = (TextView) findViewById(R.id.pm);
-
- if (am != null) {
- am.setText(mAm);
- }
- if (pm != null) {
- pm.setText(mPm);
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate our menu.
- getMenuInflater().inflate(R.menu.main_menu, menu);
-
- return super.onCreateOptionsMenu(menu);
- }
-
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
intent.putExtra(Alarms.ALARM_ID, (int) id);
startActivity(intent);
}
-
- /**
- * Only allow user to add a new alarm if there are fewer than
- * MAX_ALARM_COUNT
- */
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- menu.findItem(R.id.menu_add_alarm).setVisible(
- mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT);
- menu.findItem(R.id.menu_toggle_clock).setTitle(
- getClockVisibility() ? R.string.hide_clock
- : R.string.show_clock);
- return super.onPrepareOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_add_alarm:
- Uri uri = Alarms.addAlarm(getContentResolver());
- // FIXME: scroll to new item?
- 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(this, SetAlarm.class);
- intent.putExtra(Alarms.ALARM_ID, newId);
- startActivity(intent);
- return true;
-
- case R.id.menu_toggle_clock:
- setClockVisibility(!getClockVisibility());
- saveClockVisibility();
- return true;
-
- case R.id.menu_settings:
- startActivity(new Intent(this, SettingsActivity.class));
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
-
-
- private boolean getClockVisibility() {
- return mClockLayout.getVisibility() == View.VISIBLE;
- }
-
- private void setClockVisibility(boolean visible) {
- mClockLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
- }
-
- private void saveClockVisibility() {
- mPrefs.edit().putBoolean(PREF_SHOW_CLOCK, getClockVisibility()).commit();
- }
}