From 9cc761c03fc2da437a596df5c6cbec3cd01fcf2a Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Sat, 4 Feb 2012 01:19:20 +0400 Subject: [PATCH] SayWhen a separate class --- res/values/strings.xml | 13 ++++----- src/org/average/whereami/LastLocation.java | 33 +++++----------------- src/org/average/whereami/PhoneLog.java | 11 +++++--- src/org/average/whereami/SayWhen.java | 33 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 src/org/average/whereami/SayWhen.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 11b7827..32d38e3 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9,21 +9,18 @@ Неудачная попытка Нет календаря с именем Нет записей за последнюю неделю - Необходимо авторизоваться (через меню) + Ошибка авторизации Женя - отметка + отмечено Последний звонок к Жене от Жени - длительностью + длиной минуту минуты минут - больше суток назад - меньше часа назад - час с лишним назад - часа назад - часов назад + в + вчера Выход Авторизация Конфигурация diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java index 4b82564..76e8ef2 100644 --- a/src/org/average/whereami/LastLocation.java +++ b/src/org/average/whereami/LastLocation.java @@ -1,6 +1,7 @@ package org.average.whereami; import org.average.whereami.PersistentStore; +import org.average.whereami.SayWhen; import java.lang.Math; import java.io.IOException; @@ -27,12 +28,8 @@ public final class LastLocation extends Oracle { private String[] loc_lons; private String[] loc_dists; private String myname; - private String timestamp; - private String longago; - private String lessthanhour; - private String onehour; - private String hours2to4; - private String hours5up; + private String registered; + private SayWhen sayWhen; public LastLocation(final Resources res, final PersistentStore store) { super(res, store); @@ -41,12 +38,8 @@ public final class LastLocation extends Oracle { loc_lons = res.getStringArray(R.array.loc_lons); loc_dists = res.getStringArray(R.array.loc_dists); myname = res.getString(R.string.myname); - timestamp = res.getString(R.string.timestamp); - longago = res.getString(R.string.longago); - lessthanhour = res.getString(R.string.lessthanhour); - onehour = res.getString(R.string.onehour); - hours2to4 = res.getString(R.string.hours2to4); - hours5up = res.getString(R.string.hours5up); + registered = res.getString(R.string.registered); + sayWhen = new SayWhen(res); latitude = Latitude.builder(httpTransport, jsonFactory) .setHttpRequestInitializer(accessProtectedResource) @@ -101,20 +94,8 @@ public final class LastLocation extends Oracle { break; } } - Long ago = (System.currentTimeMillis() - tsm) / 3600000; - if (ago < 1) { - agomsg = lessthanhour; - } else if (ago < 2) { - agomsg = onehour; - } else if (ago < 5) { - agomsg = "" + ago + " " + hours2to4; - } else if (ago < 24) { - agomsg = "" + ago + " " + hours5up; - } else { - agomsg = longago; - } - return myname + " " + locmsg + ", " + timestamp + - " " + agomsg; + return myname + " " + locmsg + ", " + registered + + " " + sayWhen.say(tsm); } private Double toRad(Double deg) { diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index 708b88e..fb319a4 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -1,6 +1,7 @@ package org.average.whereami; import org.average.whereami.PersistentStore; +import org.average.whereami.SayWhen; import java.io.IOException; @@ -39,6 +40,7 @@ public final class PhoneLog extends Oracle { private String minutes1; private String minutes2to4; private String minutes5up; + private SayWhen sayWhen; public PhoneLog(final Resources res, final PersistentStore store) { super(res, store); @@ -52,6 +54,7 @@ public final class PhoneLog extends Oracle { minutes1 = res.getString(R.string.minutes1); minutes2to4 = res.getString(R.string.minutes2to4); minutes5up = res.getString(R.string.minutes5up); + sayWhen = new SayWhen(res); calendar_name = store.get("calendar_name"); peer_name = store.get("peer_name"); calendar_id = store.get("calendar_id"); @@ -192,9 +195,9 @@ public final class PhoneLog extends Oracle { } else { minutes = minutes5up; } - return last_call + " " + - (latestWasOutgoing?out_call:in_call) + - " " + duration + " " + dur + " " + minutes + - " " + stime.format("%H:%M"); + return last_call + + " " + (latestWasOutgoing?out_call:in_call) + + " " + sayWhen.say(latestStartTime) + + " " + duration + " " + dur + " " + minutes; } } diff --git a/src/org/average/whereami/SayWhen.java b/src/org/average/whereami/SayWhen.java new file mode 100644 index 0000000..21196ff --- /dev/null +++ b/src/org/average/whereami/SayWhen.java @@ -0,0 +1,33 @@ +package org.average.whereami; + +import android.content.res.Resources; +import android.text.format.Time; +import android.util.Log; + +public class SayWhen { + + private String s_at; + private String yesterday; + + public SayWhen(final Resources res) { + s_at = res.getString(R.string.s_at); + yesterday = res.getString(R.string.yesterday); + } + + public String say(Long when) { + Long now = System.currentTimeMillis(); + Time tnow = new Time(); + tnow.set(now); + Time twhen = new Time(); + twhen.set(when); + Boolean datechange = tnow.monthDay != twhen.monthDay; + Long hoursago = (System.currentTimeMillis() - when) / 3600000; + if (hoursago > 24) { + return s_at + twhen.format(" %H:%M %d/%m/%Y"); + } else if ((tnow.hour > 4 || hoursago > 12) && datechange) { + return yesterday + " " + s_at + twhen.format(" %H:%M"); + } else { + return s_at + twhen.format(" %H:%M"); + } + } +} -- 2.43.0