1 | /* |
2 | * Tomdroid |
3 | * Tomboy on Android |
4 | * http://www.launchpad.net/tomdroid |
5 | * |
6 | * Copyright 2010, Matthew Stevenson <saturnreturn@gmail.com> |
7 | * |
8 | * This file is part of Tomdroid. |
9 | * |
10 | * Tomdroid is free software: you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation, either version 3 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * Tomdroid is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
22 | */ |
23 | package org.tomdroid.util; |
24 | |
25 | import java.text.DateFormat; |
26 | import java.util.Date; |
27 | |
28 | import org.tomdroid.Note; |
29 | import org.tomdroid.R; |
30 | |
31 | import android.content.Context; |
32 | import android.database.Cursor; |
33 | import android.text.format.DateUtils; |
34 | import android.text.format.Time; |
35 | import android.view.LayoutInflater; |
36 | import android.view.View; |
37 | import android.view.ViewGroup; |
38 | import android.widget.SimpleCursorAdapter; |
39 | import android.widget.TextView; |
40 | |
41 | /* Provides a custom ListView layout for Note List */ |
42 | |
43 | public class NoteListCursorAdapter extends SimpleCursorAdapter { |
44 | |
45 | private int layout; |
46 | private Context context; |
47 | |
48 | private DateFormat localeDateFormat; |
49 | private DateFormat localeTimeFormat; |
50 | |
51 | public NoteListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { |
52 | super(context, layout, c, from, to); |
53 | this.layout = layout; |
54 | this.context = context; |
55 | localeDateFormat = android.text.format.DateFormat.getDateFormat(context); |
56 | localeTimeFormat = android.text.format.DateFormat.getTimeFormat(context); |
57 | } |
58 | |
59 | |
60 | @Override |
61 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
62 | |
63 | Cursor c = getCursor(); |
64 | |
65 | final LayoutInflater inflater = LayoutInflater.from(context); |
66 | View v = inflater.inflate(layout, parent, false); |
67 | |
68 | populateFields(v, c); |
69 | |
70 | return v; |
71 | } |
72 | |
73 | @Override |
74 | public void bindView(View v, Context context, Cursor c) { |
75 | |
76 | populateFields(v, c); |
77 | } |
78 | |
79 | @Override |
80 | public View getView(int position, View convertView, ViewGroup parent) { |
81 | View view = super.getView(position, convertView, parent); |
82 | return view; |
83 | } |
84 | |
85 | private void populateFields(View v, Cursor c){ |
86 | |
87 | int nameCol = c.getColumnIndex(Note.TITLE); |
88 | int modifiedCol = c.getColumnIndex(Note.MODIFIED_DATE); |
89 | |
90 | String title = c.getString(nameCol); |
91 | |
92 | //Format last modified dates to be similar to desktop Tomboy |
93 | //TODO this is messy - must be a better way than having 3 separate date types |
94 | Time lastModified = new Time(); |
95 | lastModified.parse3339(c.getString(modifiedCol)); |
96 | Long lastModifiedMillis = lastModified.toMillis(false); |
97 | Date lastModifiedDate = new Date(lastModifiedMillis); |
98 | |
99 | String strModified = this.context.getString(R.string.textModified)+" "; |
100 | //TODO this is very inefficient |
101 | if (DateUtils.isToday(lastModifiedMillis)){ |
102 | strModified += this.context.getString(R.string.textToday) +", " + localeTimeFormat.format(lastModifiedDate); |
103 | } else { |
104 | // Add a day to the last modified date - if the date is now today, it means the note was edited yesterday |
105 | Time yesterdayTest = lastModified; |
106 | yesterdayTest.monthDay += 1; |
107 | if (DateUtils.isToday(yesterdayTest.toMillis(false))){ |
108 | strModified += this.context.getString(R.string.textYexterday) +", " + localeTimeFormat.format(lastModifiedDate); |
109 | } else { |
110 | strModified += localeDateFormat.format(lastModifiedDate) + ", " + localeTimeFormat.format(lastModifiedDate); |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Next set the name of the entry. |
116 | */ |
117 | TextView note_title = (TextView) v.findViewById(R.id.note_title); |
118 | if (note_title != null) { |
119 | note_title.setText(title); |
120 | } |
121 | TextView note_modified = (TextView) v.findViewById(R.id.note_date); |
122 | if (note_modified != null) { |
123 | note_modified.setText(strModified); |
124 | } |
125 | } |
126 | |
127 | } |