Change date storage in DB to be a ms timestamp integral value.
Add a basic CursorAdapter to add more flexibility to views in list.
Create custom listview item that is pretty much the same, except
with a relative time string.

Change-Id: I9a1f956832f550b9a8192ea3967e732725dd6ba2
diff --git a/apps/Tag/res/layout/tag_list_item.xml b/apps/Tag/res/layout/tag_list_item.xml
new file mode 100644
index 0000000..cc984f5
--- /dev/null
+++ b/apps/Tag/res/layout/tag_list_item.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2010 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+  android:padding="4dip"
+  android:orientation="vertical"
+  android:layout_width="match_parent"
+  android:layout_height="wrap_content">
+
+  <TextView
+    android:id="@+id/title"
+    android:padding="4dip"
+    android:textAppearance="?android:attr/textAppearanceMedium"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    />
+
+  <TextView
+    android:id="@+id/date"
+    android:padding="4dip"
+    android:textAppearance="?android:attr/textAppearanceSmall"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    />
+</LinearLayout>
+
diff --git a/apps/Tag/src/com/android/apps/tag/TagCursorAdapter.java b/apps/Tag/src/com/android/apps/tag/TagCursorAdapter.java
new file mode 100644
index 0000000..a658268
--- /dev/null
+++ b/apps/Tag/src/com/android/apps/tag/TagCursorAdapter.java
@@ -0,0 +1,43 @@
+// Copyright 2010 Google Inc. All Rights Reserved.
+
+package com.android.apps.tag;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.text.format.DateUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Adapter;
+import android.widget.CursorAdapter;
+import android.widget.TextView;
+
+/**
+ * A custom {@link Adapter} that renders tag entries for a list.
+ */
+public class TagCursorAdapter extends CursorAdapter {
+
+    private final LayoutInflater mInflater;
+
+    public TagCursorAdapter(Context context, Cursor c) {
+        super(context, c);
+
+        mInflater = LayoutInflater.from(context);
+    }
+
+    @Override
+    public void bindView(View view, Context context, Cursor cursor) {
+        TextView mainLine = (TextView) view.findViewById(R.id.title);
+        TextView dateLine = (TextView) view.findViewById(R.id.date);
+
+        // TODO(benkomalo): either write a cursor abstraction, or use constants for column indices.
+        mainLine.setText(cursor.getString(cursor.getColumnIndex("bytes")));
+        dateLine.setText(DateUtils.getRelativeTimeSpanString(
+                context, cursor.getLong(cursor.getColumnIndex("date"))));
+    }
+
+    @Override
+    public View newView(Context context, Cursor cursor, ViewGroup parent) {
+        return mInflater.inflate(R.layout.tag_list_item, null);
+    }
+}
diff --git a/apps/Tag/src/com/android/apps/tag/TagDBHelper.java b/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
index 5135d50..06fa9e0 100644
--- a/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
+++ b/apps/Tag/src/com/android/apps/tag/TagDBHelper.java
@@ -17,14 +17,14 @@
 package com.android.apps.tag;
 
 import com.google.common.annotations.VisibleForTesting;
-import android.nfc.NdefMessage;
-import android.nfc.NdefRecord;
-import android.nfc.FormatException;
 
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.database.sqlite.SQLiteStatement;
+import android.nfc.FormatException;
+import android.nfc.NdefMessage;
+import android.nfc.NdefRecord;
 
 import java.net.URI;
 import java.util.Date;
@@ -39,7 +39,7 @@
     private static final String NDEF_MSG = "create table NdefMessage ("
             + "_id INTEGER NOT NULL, "
             + "bytes BLOB NOT NULL, "
-            + "date TEXT NOT NULL, "
+            + "date INTEGER NOT NULL, "
             + "saved TEXT NOT NULL default 0,"  // boolean
             + "PRIMARY KEY(_id)"
             + ")";
@@ -154,7 +154,7 @@
     private void insert(SQLiteDatabase db, NdefMessage msg, boolean isSaved) {
         SQLiteStatement stmt = db.compileStatement(INSERT);
         stmt.bindString(1, new String(msg.toByteArray())); // TODO: This should be a blob
-        stmt.bindString(2, new Date().toString());
+        stmt.bindLong(2, System.currentTimeMillis());
         String isSavedStr = isSaved ? "1" : "0";
         stmt.bindString(3, isSavedStr);
         stmt.executeInsert();
diff --git a/apps/Tag/src/com/android/apps/tag/TagList.java b/apps/Tag/src/com/android/apps/tag/TagList.java
index 5ca1a63..369ef65 100644
--- a/apps/Tag/src/com/android/apps/tag/TagList.java
+++ b/apps/Tag/src/com/android/apps/tag/TagList.java
@@ -43,19 +43,15 @@
         boolean showSavedOnly = getIntent().getBooleanExtra(SHOW_SAVED_ONLY, false);
         db = new TagDBHelper(getBaseContext()).getReadableDatabase();
         String selection = showSavedOnly ? "saved=1" : null;
+
+        // TODO: Use an AsyncQueryHandler so that DB queries are not done on UI thread.
         cursor = db.query(
                 "NdefMessage",
                 new String[] { "_id", "bytes", "date" },
                 selection,
                 null, null, null, null);
-        SimpleCursorAdapter sca =
-                new SimpleCursorAdapter(this,
-                        android.R.layout.two_line_list_item,
-                        cursor,
-                        new String[] { "bytes", "date" },
-                        new int[] { android.R.id.text1, android.R.id.text2 });
 
-        setListAdapter(sca);
+        setListAdapter(new TagCursorAdapter(this, cursor));
         registerForContextMenu(getListView());
     }