Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11bcbd3b authored by Marten Gajda's avatar Marten Gajda
Browse files

improve list redraw code

improve relative date & time string building
revert widget to use absolute times
parent 51126d03
Loading
Loading
Loading
Loading
+21 −24
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@@ -171,8 +170,6 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
		}
	};

	private Handler mListViewRedrawHandler;

	/**
	 * A callback interface that all activities containing this fragment must implement. This mechanism allows activities to be notified of item selections.
	 */
@@ -190,6 +187,21 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
		public void onAddNewTask();
	}

	/**
	 * A runnable that periodically updates the list. We need that to update relative dates & times. TODO: we probably should move that to the adapter to update
	 * only the date & times fields, not the entire list.
	 */
	private Runnable mListRedrawRunnable = new Runnable()
	{

		@Override
		public void run()
		{
			mExpandableListView.invalidateViews();
			mHandler.postDelayed(this, INTERVAL_LISTVIEW_REDRAW);
		}
	};


	public static TaskListFragment newInstance(int instancePosition, boolean twoPaneLayout)
	{
@@ -286,7 +298,7 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
	{
		super.onResume();
		mExpandableListView.invalidateViews();
		startAutomaticRedraw(INTERVAL_LISTVIEW_REDRAW);
		startAutomaticRedraw();
	}


@@ -737,27 +749,12 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
	 * @param interval
	 *            The interval for the redraw in milliseconds.
	 */
	public void startAutomaticRedraw(final long interval)
	{
		mListViewRedrawHandler = new Handler();

		// calculate delay to next minute
		Time nextMinute = new Time();
		nextMinute.setToNow();
		nextMinute.second = 0;
		nextMinute.minute += 1;

		mListViewRedrawHandler.postDelayed(new Runnable()
		{

			@Override
			public void run()
	public void startAutomaticRedraw()
	{
		long now = System.currentTimeMillis();
		long millisToMinute = 60000 - (now % 60000);

				mExpandableListView.invalidateViews();
				mListViewRedrawHandler.postDelayed(this, interval);
			}
		}, nextMinute.toMillis(true) - System.currentTimeMillis());
		mHandler.postDelayed(mListRedrawRunnable, millisToMinute);
	}


@@ -767,7 +764,7 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
	 */
	public void stopAutomaticRedraw()
	{
		mListViewRedrawHandler.removeCallbacksAndMessages(null);
		mHandler.removeCallbacks(mListRedrawRunnable);
	}


+18 −10
Original line number Diff line number Diff line
@@ -97,13 +97,7 @@ public class DateFormatter
		 * 
		 * Currently this inherits the default short format.
		 */
		WIDGET_VIEW {
			@Override
			public boolean useRelative(Time now, Time date)
			{
				return Math.abs(date.toMillis(false) - now.toMillis(false)) < 7 * 24 * 3600 * 1000;
			}
		},
		WIDGET_VIEW,

		/**
		 * The date format in the dash clock. This shows a time only.
@@ -199,21 +193,35 @@ public class DateFormatter

		if (dateContext.useRelative(mNow, date))
		{
			long delta = Math.abs(mNow.toMillis(false) - date.toMillis(false));

			if (date.allDay)
			{
				Time allDayNow = new Time("UTC");
				allDayNow.set(mNow.monthDay, mNow.month, mNow.year);
				return DateUtils.getRelativeTimeSpanString(date.toMillis(false), allDayNow.toMillis(false), DateUtils.DAY_IN_MILLIS).toString();
			}
			else if (Math.abs(mNow.toMillis(false) - date.toMillis(false)) < 60 * 1000)
			else if (delta < 60 * 1000)
			{
				// the date is within this minute
				// the time is within this minute, show "now"
				return mContext.getString(R.string.now);
			}
			else
			else if (delta < 60 * 60 * 1000)
			{
				// time is within this hour, show number of minutes left
				return DateUtils.getRelativeTimeSpanString(date.toMillis(false), mNow.toMillis(false), DateUtils.MINUTE_IN_MILLIS).toString();
			}
			else if (delta < 24 * 60 * 60 * 1000)
			{
				// time is within 24 hours, show relative string with time
				// FIXME: instead of using a fixed 24 hour interval this should be aligned to midnight tomorrow and yesterday
				return DateUtils.getRelativeDateTimeString(mContext, date.toMillis(false), DateUtils.DAY_IN_MILLIS, DateUtils.WEEK_IN_MILLIS,
					dateContext.getDateUtilsFlags(mNow, date)).toString();
			}
			else
			{
				return DateUtils.getRelativeTimeSpanString(date.toMillis(false), mNow.toMillis(false), DateUtils.DAY_IN_MILLIS).toString();
			}
		}

		return date.allDay ? formatAllDay(date, dateContext) : formatNonAllDay(date, dateContext);