@Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { // Inflate the menu from xml. getMenuInflater().inflate(R.menu.time_schedule_context_menu, menu); // Use the current item to create a custom view for the header. final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; final Cursor c = (Cursor) mScheduleList.getAdapter().getItem((int) info.position); final TimeSchedule schedule = new TimeSchedule(c); // Construct the Calendar to compute the time. final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, schedule.hour); cal.set(Calendar.MINUTE, schedule.minutes); final String time = TimeSchedule.formatTime(this, cal); // Inflate the custom view and set each TextView's text. final View v = mInflater.inflate(R.layout.context_menu_header, null); TextView textView = (TextView) v.findViewById(R.id.header_time); textView.setText(time); textView = (TextView) v.findViewById(R.id.header_mode); textView.setText(OBS.getInstance().getModeDbAdapter().getModeNameStr(schedule.modeId)); // Set the custom view on the menu. menu.setHeaderView(v); // Change the text based on the state of the schedule. if (schedule.enabled) { menu.findItem(R.id.enable_schedule).setTitle(R.string.disable_schedule); } }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This // sample only has one Loader, so we don't care about the ID. // First, pick the base URI to use depending on whether we are // currently filtering. return TimeSchedule.getSchedulesCursorLoader(this); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mInflater = LayoutInflater.from(this); // mPrefs = getSharedPreferences(PREFERENCES, 0); mCursor = TimeSchedule.getTimeSchedulesCursor(getContentResolver()); updateLayout(); }
@Override public void bindView(View view, Context context, Cursor cursor) { final TimeSchedule schedule = new TimeSchedule(cursor); ToggleButton scheduleOnOff = (ToggleButton) view.findViewById(R.id.schedule_onOff); scheduleOnOff.setChecked(schedule.enabled); final Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, schedule.hour); c.set(Calendar.MINUTE, schedule.minutes); TextView timeDisplay = (TextView) view.findViewById(R.id.timeDisplay); TextView amPm = (TextView) view.findViewById(R.id.am_pm); // format date time // if date time is 12M show am_pm text view String format = TimeSchedule.get24HourMode(TimeScheduleActivity.this) ? TimeSchedule.M24 : M12; CharSequence time = DateFormat.format(format, c); timeDisplay.setText(time); String[] ampm = new DateFormatSymbols().getAmPmStrings(); amPm.setVisibility(format == M12 ? View.VISIBLE : View.GONE); // if is morning, text = "AM", if not is "PM" amPm.setText(c.get(Calendar.AM_PM) == 0 ? ampm[0] : ampm[1]); timeDisplay.setTypeface(Typeface.DEFAULT); TextView modeToChange = (TextView) view.findViewById(R.id.text2); final String modeNameStr = OBS.getInstance().getModeDbAdapter().getModeNameStr(schedule.modeId); modeToChange.setText(getString(R.string.mode_to_change, modeNameStr)); TextView daysOfWeekView = (TextView) view.findViewById(R.id.text3); final String daysOfWeekStr = schedule.daysOfWeek.toString(TimeScheduleActivity.this, false); if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) { daysOfWeekView.setText(daysOfWeekStr); daysOfWeekView.setVisibility(View.VISIBLE); } else { daysOfWeekView.setVisibility(View.GONE); } }
@Override public boolean onContextItemSelected(MenuItem item) { final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); final int id = (int) info.id; // Error check just in case. if (id == -1) { return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.delete_schedule: // Confirm that the schedule will be deleted. new AlertDialog.Builder(this) .setTitle(getString(R.string.delete_schedule)) .setMessage(getString(R.string.delete_schedule_confirm)) .setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int w) { TimeSchedule.deleteTimeSchedule(TimeScheduleActivity.this, id); } }) .setNegativeButton(android.R.string.cancel, null) .show(); return true; case R.id.enable_schedule: final Cursor c = (Cursor) mScheduleList.getAdapter().getItem(info.position); final TimeSchedule schedule = new TimeSchedule(c); TimeSchedule.enableTimeSchedule(this, schedule.id, !schedule.enabled); if (!schedule.enabled) {} return true; case R.id.edit_schedule: Intent intent = new Intent(this, SetTimeScheduleActivity.class); intent.putExtra(TimeSchedule.EXTRA_ID, id); startActivity(intent); return true; default: break; } return super.onContextItemSelected(item); }