commit 575156facb0161f99be86f1ec3a9905352532fe5
parent 7e7f623fa245d2c634f85ab1b0a7343899c9d6d2
Author: Jan Dankert <devnull@localhost>
Date: Sat, 4 Feb 2012 23:09:28 +0100
Der SubscriptionService kann auch ungelesene Nachrichten ermitteln und benutzt das Intervall aus der Konfiguration.
Diffstat:
5 files changed, 193 insertions(+), 57 deletions(-)
diff --git a/res/values/strings.xml b/res/values/strings.xml
@@ -99,6 +99,7 @@
<string name="unread_topic">Ungelesenes Thema</string>
<string name="unread_forum">Ungelesenes Forum</string>
+ <string name="unread_messages">Ungelesene Nachrichten</string>
<string name="forum">Forum</string>
<string name="news">News</string>
<string name="photos">Fotos</string>
@@ -133,6 +134,7 @@
<string name="subscription_service_desc">Informiert bei ungelesenen, abonnierten Foren oder Themen</string>
<string name="interval">Intervall</string>
<string-array name="interval_list">
+ <item>1</item>
<item>5</item>
<item>15</item>
<item>30</item>
@@ -143,6 +145,7 @@
<item>2880</item>
</string-array>
<string-array name="interval_list_label">
+ <item>1 Minute</item>
<item>5 Minuten</item>
<item>15 Minuten</item>
<item>30 Minuten</item>
diff --git a/src/de/mtbnews/android/ForumActivity.java b/src/de/mtbnews/android/ForumActivity.java
@@ -142,7 +142,7 @@ public class ForumActivity extends EndlessListActivity<Topic>
try
{
- Map<String, Object> map = client.login(prefs.getString(
+ client.login(prefs.getString(
"username", ""), prefs.getString("password", ""));
}
diff --git a/src/de/mtbnews/android/ForumOverviewActivity.java b/src/de/mtbnews/android/ForumOverviewActivity.java
@@ -70,8 +70,8 @@ public class ForumOverviewActivity extends ExpandableListActivity
try
{
- Map<String, Object> map = client.login(prefs.getString(
- "username", ""), prefs.getString("password", ""));
+ client.login(prefs.getString("username", ""), prefs
+ .getString("password", ""));
}
catch (TapatalkException e)
@@ -98,7 +98,8 @@ public class ForumOverviewActivity extends ExpandableListActivity
{
final List<Forum> newSubForen = new ArrayList<Forum>();
- // Wenn Forum Themen enthalten kann, dann mit aufnehmen in die 2. Hierarchie.
+ // Wenn Forum Themen enthalten kann, dann mit aufnehmen in die 2.
+ // Hierarchie.
if (!forum.subOnly)
newSubForen.add(forum);
diff --git a/src/de/mtbnews/android/service/SubscriptionService.java b/src/de/mtbnews/android/service/SubscriptionService.java
@@ -3,8 +3,11 @@
*/
package de.mtbnews.android.service;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@@ -15,16 +18,22 @@ import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.graphics.Paint.Join;
import android.os.IBinder;
import android.preference.PreferenceManager;
+import android.text.TextUtils;
import android.util.Log;
import de.mtbnews.android.ForumActivity;
import de.mtbnews.android.IBCApplication;
+import de.mtbnews.android.MailboxActivity;
import de.mtbnews.android.R;
+import de.mtbnews.android.SubscriptionForenActivity;
+import de.mtbnews.android.SubscriptionTopicsActivity;
import de.mtbnews.android.TopicActivity;
import de.mtbnews.android.tapatalk.TapatalkClient;
import de.mtbnews.android.tapatalk.TapatalkException;
import de.mtbnews.android.tapatalk.wrapper.Forum;
+import de.mtbnews.android.tapatalk.wrapper.Mailbox;
import de.mtbnews.android.tapatalk.wrapper.Topic;
/**
@@ -33,55 +42,110 @@ import de.mtbnews.android.tapatalk.wrapper.Topic;
*/
public class SubscriptionService extends Service
{
- public SubscriptionService()
- {
- }
-
private IBCApplication ibcApp;
private static Timer timer;
private SharedPreferences prefs;
- private Date startDate;
- private static final int NOTIFICATION_TOPIC = 1;
- private static final int NOTIFICATION_FORUM = 2;
-
+ private static final int NOTIFICATION_ERROR = 1;
+ private static final int NOTIFICATION_TOPIC = 2;
+ private static final int NOTIFICATION_FORUM = 3;
+ private static final int NOTIFICATION_MESSAGES = 4;
+
public IBinder onBind(Intent arg0)
{
return null;
}
+ /**
+ * Erzeugt für diese Serviceinstanz einen Timer, der in regelmäßigen
+ * Abständen auf neue Themen und Nachrichten prüft.
+ *
+ * @see android.app.Service#onCreate()
+ */
public void onCreate()
{
Log.d(this.getClass().getSimpleName(), "Starting service");
super.onCreate();
ibcApp = (IBCApplication) getApplication();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
- startDate = new Date();
+
+ // Intervall in Minuten (Default = 3 Stunden)
+ int intervalInMinutes = Integer.parseInt(prefs.getString(
+ "subscription_service_interval", "180"));
+
timer = new Timer();
- timer.scheduleAtFixedRate(new TestSubscriptionTask(), 1000, 60000);
+ timer.scheduleAtFixedRate(new SubscriptionTask(), 0,
+ intervalInMinutes * 60 * 1000);
}
- private class TestSubscriptionTask extends TimerTask
+ /**
+ * Der Timer, der auf ungelesene Nachrichten und ungelesene Themen prüft.
+ * Falls gefunden, wird dies über den NotificationService gemeldet.
+ *
+ * @author dankert
+ *
+ */
+ private class SubscriptionTask extends TimerTask
{
-
-
public void run()
{
- long currentTimeMillis = System.currentTimeMillis();
Log.d(this.getClass().getSimpleName(),
- "Testing for unread subscriptions");
+ "now testing for unread topics and messages");
+
+ final long actualTime = System.currentTimeMillis();
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ final TapatalkClient client = ibcApp.getTapatalkClient();
+
+ if (!client.loggedIn)
+ {
+ try
+ {
+ client.login(prefs.getString("username", ""), prefs
+ .getString("password", ""));
+ }
+ catch (TapatalkException e)
+ {
+ final Notification notification = new Notification(
+ R.drawable.ibc_logo, e.getMessage(), actualTime);
+ notification.setLatestEventInfo(getApplicationContext(),
+ getResources().getString(R.string.login_failed),
+ "", null);
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
+ nm.notify(NOTIFICATION_ERROR, notification);
- TapatalkClient client = ibcApp.getTapatalkClient();
+ return;
+ }
+ }
+
+ if (!client.loggedIn)
+ {
+ final Notification notification = new Notification(
+ R.drawable.ibc_logo, "", actualTime);
+ notification.setLatestEventInfo(getApplicationContext(),
+ getResources().getString(R.string.login_failed), "",
+ null);
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
+ nm.notify(NOTIFICATION_ERROR, notification);
+
+ return;
+ }
try
{
List<Forum> subscribedForum = client.getSubscribedForum(true);
+
+ final List<String> forumNameList = new ArrayList<String>();
for (Forum forum : subscribedForum)
{
+ forumNameList.add(forum.getTitle());
+ }
+ if (!forumNameList.isEmpty())
+ {
+ String forumNames = TextUtils.join(", ", forumNameList);
+
final Intent notificationIntent = new Intent(
- SubscriptionService.this, ForumActivity.class);
- notificationIntent.putExtra("forum_id", forum.getId());
+ SubscriptionService.this,
+ SubscriptionForenActivity.class);
final PendingIntent contentIntent = PendingIntent
.getActivity(SubscriptionService.this, 0,
notificationIntent, 0);
@@ -90,18 +154,15 @@ public class SubscriptionService extends Service
R.string.unread_forum);
final Notification notification = new Notification(
- R.drawable.ibc_logo, tickerText, System
- .currentTimeMillis());
+ R.drawable.ibc_logo, tickerText, actualTime);
notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.unread_forum)+ " ("+subscribedForum.size()+")",
- forum.getTitle(), contentIntent);
+ getResources().getString(R.string.unread_forum)
+ + " (" + subscribedForum.size() + ")",
+ forumNames, contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
// notification.flags = Notification.FLAG_ONGOING_EVENT
// | Notification.FLAG_NO_CLEAR;
nm.notify(NOTIFICATION_FORUM, notification);
-
- Log.d(this.getClass().getName(), "forum unread: "
- + forum.getName() + forum.getId());
}
}
catch (TapatalkException e)
@@ -119,30 +180,91 @@ public class SubscriptionService extends Service
{
List<Topic> subscribedTopic = client.getSubscribedTopics(0, 10,
true);
+ final List<String> topicNameList = new ArrayList<String>();
for (Topic topic : subscribedTopic)
{
+ topicNameList.add(topic.getTitle());
+ }
+ if (!topicNameList.isEmpty())
+ {
+
+ String topicNames = TextUtils.join(", ", topicNameList);
+
final Intent notificationIntent = new Intent(
- SubscriptionService.this, TopicActivity.class);
- notificationIntent.putExtra("topic_id", topic.getId());
+ SubscriptionService.this,
+ SubscriptionTopicsActivity.class);
final PendingIntent contentIntent = PendingIntent
.getActivity(SubscriptionService.this, 0,
notificationIntent, 0);
final String tickerText = getResources().getString(
- R.string.unread_topic);
+ R.string.unread_topic)
+ + ": " + topicNames;
final Notification notification = new Notification(
- R.drawable.ibc_logo, tickerText, currentTimeMillis);
+ R.drawable.ibc_logo, tickerText, actualTime);
notification.setLatestEventInfo(getApplicationContext(),
- getResources().getString(R.string.unread_topic)+ " ("+subscribedTopic.size()+")",
- topic.getTitle(), contentIntent);
+ getResources().getString(R.string.unread_topic)
+ + " (" + subscribedTopic.size() + ")",
+ topicNames, contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
// notification.flags = Notification.FLAG_ONGOING_EVENT
// | Notification.FLAG_NO_CLEAR;
nm.notify(NOTIFICATION_TOPIC, notification);
+ }
+ }
+ catch (TapatalkException e)
+ {
+ Log.w(this.getClass().getSimpleName(), e);
+ throw new RuntimeException(e);
+ }
+ catch (Exception e)
+ {
+ Log.w("Laufzeitfehler", e);
+ throw new RuntimeException(e);
+ }
+
+ try
+ {
+ List<Mailbox> mailboxList = client.getMailbox();
+ int unreadCount = 0;
- Log.d(this.getClass().getSimpleName(), "topic unread: "
- + topic.getName() + topic.getId());
+ final List<String> unreadBoxNames = new ArrayList<String>();
+
+ for (Mailbox mailbox : mailboxList)
+ {
+ if (mailbox.countUnread > 0)
+ {
+
+ unreadBoxNames.add(mailbox.getTitle());
+ unreadCount += mailbox.countUnread;
+ }
+ }
+ final String unreadBoxes = TextUtils.join(", ", unreadBoxNames);
+
+ if (unreadCount > 0)
+ {
+ final Intent notificationIntent = new Intent(
+ SubscriptionService.this, MailboxActivity.class);
+ final PendingIntent contentIntent = PendingIntent
+ .getActivity(SubscriptionService.this, 0,
+ notificationIntent, 0);
+
+ final String tickerText = getResources().getString(
+ R.string.unread_messages)
+ + unreadBoxes;
+
+ final Notification notification = new Notification(
+ R.drawable.ibc_logo, tickerText, actualTime);
+ notification.setLatestEventInfo(getApplicationContext(),
+ getResources().getString(R.string.unread_messages)
+ + " (" + unreadCount + ")", unreadBoxes,
+ contentIntent);
+
+ notification.flags = Notification.FLAG_AUTO_CANCEL;
+ // notification.flags = Notification.FLAG_ONGOING_EVENT
+ // | Notification.FLAG_NO_CLEAR;
+ nm.notify(NOTIFICATION_MESSAGES, notification);
}
}
catch (TapatalkException e)
@@ -158,9 +280,17 @@ public class SubscriptionService extends Service
}
}
+ /**
+ * Wird vom System automatisch aufgerufen bevor der Service entfernt wird.
+ * Hier räumen wir vor allem den Timer weg, damit keine weiteren Ereignisse
+ * ausgelöst werden.
+ *
+ * @see android.app.Service#onDestroy()
+ */
public void onDestroy()
{
- timer.cancel();
+
+ timer.cancel(); // Alle Timer-Ereignisse stoppen.
Log.d(this.getClass().getSimpleName(), "Destroying service");
super.onDestroy();
diff --git a/src/de/mtbnews/android/tapatalk/TapatalkClient.java b/src/de/mtbnews/android/tapatalk/TapatalkClient.java
@@ -8,6 +8,7 @@ import java.util.Map;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
+import android.text.TextUtils;
import android.util.Log;
import de.mtbnews.android.tapatalk.wrapper.Forum;
@@ -33,23 +34,26 @@ public class TapatalkClient
this.client = new XMLRPCClient(connectorUrl);
}
- @SuppressWarnings("unchecked")
- public Map<String, Object> login(String username, String password)
+ public void login(String username, String password)
throws TapatalkException
{
- try
- {
-
- final Map<String, Object> map = (Map<String, Object>) toMap(getXMLRPCClient()
- .call("get_config"));
- Log.i("IBC Server Config", map.toString());
- for (String key : map.keySet())
- Log.d("IBC Server Config", key + "=" + map.get(key));
- }
- catch (XMLRPCException e)
- {
- throw new TapatalkException("Load Config failed", e);
- }
+ if (TextUtils.isEmpty(username))
+ throw new TapatalkException("Username empty");
+
+ // try
+ // {
+ //
+ // final Map<String, Object> map = (Map<String, Object>)
+ // toMap(getXMLRPCClient()
+ // .call("get_config"));
+ // Log.i("IBC Server Config", map.toString());
+ // for (String key : map.keySet())
+ // Log.d("IBC Server Config", key + "=" + map.get(key));
+ // }
+ // catch (XMLRPCException e)
+ // {
+ // throw new TapatalkException("Load Config failed", e);
+ // }
final Object[] params = new Object[] { username.getBytes(),
password.getBytes() };
@@ -57,10 +61,8 @@ public class TapatalkClient
try
{
- final Map<String, Object> map = (Map<String, Object>) toMap(getXMLRPCClient()
- .callEx("login", params));
+ toMap(client.callEx("login", params));
this.loggedIn = true;
- return map;
}
catch (XMLRPCException e)
{
@@ -75,7 +77,7 @@ public class TapatalkClient
try
{
@SuppressWarnings("unused")
- Object result = getXMLRPCClient().call("logout_user");
+ Object result = client.call("logout_user");
this.loggedIn = false;
}
catch (XMLRPCException e)