Android Open Source - tracks-android Task List Item






From Project

Back to project page tracks-android.

License

The source code is released under:

Copyright (c) 2010 Adam Wolfe Gordon <awg@xvx.ca> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to...

If you think the Android project tracks-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.xvx.tracks;
// w w  w  . j a  v a 2  s .  c  om
import ca.xvx.tracks.preferences.PreferenceConstants;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

public class TaskListItem extends RelativeLayout {
  private TextView _name;
  private TextView _info;
  private CheckBox _done;
  private LinearLayout _deferBox;
  private Button _defer1;
  private Button _defer7;

  private Task _task;
  private TracksAction _doneAction;
  private Handler _changeHandler;
  private Handler _notifyHandler;

  private SharedPreferences _prefs;
  private boolean _defer;
  
  // Within one day is amber.
  private static final long AMBER_TIME = 24*3600*1000;
  // Within one week is orange.
  private static final long ORANGE_TIME = 7*24*3600*1000;

  public TaskListItem(Context c, Task t, Handler n) {
    super(c);
    Calendar now = Calendar.getInstance();
    Calendar cmp = Calendar.getInstance();
    long nowm, cmpm;

    _prefs = PreferenceManager.getDefaultSharedPreferences(c);
    _defer = _prefs.getBoolean(PreferenceConstants.DEFER, true);
    _changeHandler = TracksCommunicator.getHandler();
    _notifyHandler = n;

    if(t == null) {
      return;
    }
    _task = t;

    addView(inflate(c, R.layout.tasklist_item, null));
    _name = (TextView)findViewById(R.id.TLI_name);
    _info = (TextView)findViewById(R.id.TLI_info);
    _done = (CheckBox)findViewById(R.id.TLI_done);
    _deferBox = (LinearLayout)findViewById(R.id.TLI_defer_box);
    _defer1 = (Button)findViewById(R.id.TLI_defer_1);
    _defer7 = (Button)findViewById(R.id.TLI_defer_7);

    String name = _task.getDescription();
    if(_task.getProject() != null) {
      name += "  [" + t.getProject().getName() + "]";
    }
    _name.setText(name);
    
    String infos = "";
    if(t.getDue() != null) {
      infos += "Due: " + DateFormat.getDateFormat(c).format(t.getDue());

      // Set the text color according to due date
      cmp.setTime(t.getDue());
      cmp.set(Calendar.HOUR_OF_DAY, 0);
      cmp.set(Calendar.MINUTE, 0);
      cmp.set(Calendar.SECOND, 0);
      cmp.set(Calendar.MILLISECOND, 0);

      now.set(Calendar.HOUR_OF_DAY, 0);
      now.set(Calendar.MINUTE, 0);
      now.set(Calendar.SECOND, 0);
      now.set(Calendar.MILLISECOND, 0);

      nowm = now.getTimeInMillis();
      cmpm = cmp.getTimeInMillis();
      if(cmpm - nowm < 0) {
        _info.setTextColor(getResources().getColor(R.color.red));
      } else if(cmpm - nowm <= AMBER_TIME) {
        _info.setTextColor(getResources().getColor(R.color.amber));
      } else if(cmpm - nowm <= ORANGE_TIME) {
        _info.setTextColor(getResources().getColor(R.color.orange));
      } else {
        _info.setTextColor(getResources().getColor(R.color.green));
      }
    }
    _info.setText(infos);

    _done.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton b, boolean checked) {
          _task.setDone(checked);
          if(checked) {
            _doneAction = new TracksAction(TracksAction.ActionType.COMPLETE_TASK,
                             _task, _notifyHandler);
            Message m = _changeHandler.obtainMessage(0, _doneAction);
            _changeHandler.sendMessageDelayed(m, 500);
          } else {
            _changeHandler.removeMessages(0, _doneAction);
          }
        }
      });

    if(_defer) {
      _deferBox.setVisibility(View.VISIBLE);

      _defer1.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
            Calendar newdue = Calendar.getInstance();
            Date olddue = _task.getDue();
            if(olddue != null) {
              newdue.setTime(_task.getDue());
            }
            newdue.set(Calendar.HOUR_OF_DAY, 0);
            newdue.set(Calendar.MINUTE, 0);
            newdue.set(Calendar.SECOND, 0);
            newdue.set(Calendar.MILLISECOND, 0);
            newdue.add(Calendar.DAY_OF_YEAR, 1);

            _task.setDue(newdue.getTime());

            Message m = _changeHandler.obtainMessage(0, new TracksAction(TracksAction.ActionType.UPDATE_TASK,
                                           _task, _notifyHandler));
            _changeHandler.sendMessage(m);
          }
        });

      _defer7.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
            Calendar newdue = Calendar.getInstance();
            Date olddue = _task.getDue();
            if(olddue != null) {
              newdue.setTime(_task.getDue());
            }
            newdue.set(Calendar.HOUR_OF_DAY, 0);
            newdue.set(Calendar.MINUTE, 0);
            newdue.set(Calendar.SECOND, 0);
            newdue.set(Calendar.MILLISECOND, 0);
            newdue.add(Calendar.DAY_OF_YEAR, 7);

            _task.setDue(newdue.getTime());

            Message m = _changeHandler.obtainMessage(0, new TracksAction(TracksAction.ActionType.UPDATE_TASK,
                                           _task, _notifyHandler));
            _changeHandler.sendMessage(m);
          }
        });
    } else {
      _deferBox.setVisibility(View.GONE);
    }
  }

  protected Task getTask() {
    return _task;
  }
}




Java Source Code List

ca.xvx.tracks.ContextEditorActivity.java
ca.xvx.tracks.ContextXmlHandler.java
ca.xvx.tracks.DuplicateContextException.java
ca.xvx.tracks.DuplicateProjectException.java
ca.xvx.tracks.DuplicateTaskException.java
ca.xvx.tracks.ProjectEditorActivity.java
ca.xvx.tracks.ProjectXmlHandler.java
ca.xvx.tracks.Project.java
ca.xvx.tracks.SettingsActivity.java
ca.xvx.tracks.TaskEditorActivity.java
ca.xvx.tracks.TaskListActivity.java
ca.xvx.tracks.TaskListAdapter.java
ca.xvx.tracks.TaskListItem.java
ca.xvx.tracks.TaskXmlHandler.java
ca.xvx.tracks.Task.java
ca.xvx.tracks.TodoContext.java
ca.xvx.tracks.TracksAction.java
ca.xvx.tracks.TracksCommunicator.java
ca.xvx.tracks.preferences.PreferenceConstants.java
ca.xvx.tracks.preferences.PreferenceUtils.java
ca.xvx.tracks.util.HttpConnection.java