Stay alive unless closed from menu
[WhereAmI.git] / src / org / average / whereami / SayWhen.java
1 package org.average.whereami;
2
3 import android.content.res.Resources;
4 import android.text.format.Time;
5 import android.util.Log;
6
7 public class SayWhen {
8
9         final String TAG = getClass().getName();
10
11         private String s_at;
12         private String yesterday;
13
14         public SayWhen(final Resources res) {
15                 s_at = res.getString(R.string.s_at);
16                 yesterday = res.getString(R.string.yesterday);
17         }
18
19         public String say(Long when) {
20                 Long now = System.currentTimeMillis();
21                 Time tnow = new Time();
22                 tnow.set(now);
23                 Time twhen = new Time();
24                 twhen.set(when);
25                 Boolean datechange = tnow.monthDay != twhen.monthDay;
26                 Long hoursago = (System.currentTimeMillis() - when) / 3600000;
27                 Log.v(TAG, "datechange=" + datechange +
28                         " hoursago=" + hoursago);
29                 if (hoursago > 24) {
30                         return s_at + twhen.format(" %H:%M %d/%m/%Y");
31                 } else if ((tnow.hour > 4 || hoursago > 12) && datechange) {
32                         return yesterday + " " + s_at + twhen.format(" %H:%M");
33                 } else {
34                         return s_at + twhen.format(" %H:%M");
35                 }
36         }
37 }