android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 9dfcd2809d0bd4b0695bd938e123d7b86da34571
parent 784f5450e6bf21b1b89da2b004008d52e50be2da
Author: Jan Dankert <devnull@localhost>
Date:   Sat,  4 Feb 2012 02:17:59 +0100

Neuer Abodienst, der abonnierte Foren/Themen prüft, ob es etwas ungelesenes gibt.

Diffstat:
AndroidManifest.xml | 2+-
res/values/strings.xml | 77++++++++++++++++++++++++++++++++++++++++++-----------------------------------
res/xml/preferences.xml | 20+++++++++++++++++---
src/de/mtbnews/android/Configuration.java | 40++++++++++++++++++++++++++++++++++++++++
src/de/mtbnews/android/IBCApplication.java | 8+++++++-
src/de/mtbnews/android/service/SubscriptionService.java | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
src/de/mtbnews/android/tapatalk/TapatalkClient.java | 84++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
7 files changed, 323 insertions(+), 108 deletions(-)

diff --git a/AndroidManifest.xml b/AndroidManifest.xml @@ -42,7 +42,7 @@ <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" /> - <service android:name=".service.UploadIntentService"></service> + <service android:name=".service.SubscriptionService"></service> </application> diff --git a/res/values/strings.xml b/res/values/strings.xml @@ -49,34 +49,7 @@ <string name="waitingforsave">Speichern</string> <string name="waitingfor_loadmore">Lade weitere Einträge</string> <string name="waitingfor_mailbox">Nachrichten werden geladen</string> - <string name="language">Sprache</string> - <string name="model">Variante</string> - <string name="new1">Neu</string> - <string name="new_folder">Neuer Unterordner</string> - <string name="new_page">Neue Seite</string> - <string name="upload">Hochladen</string> - <string name="upload_long">Die Datei wird jetzt hochladen</string> - <string name="upload_file">Datei hochladen</string> - <string name="upload_image">Bild hochladen</string> - <string name="upload_ok">Hochladevorgang abgeschlossen</string> - <string name="upload_ok_long">Die Datei wurde erfolgreich auf den Server geladen - </string> - <string name="upload_fail">Hochladevorgang fehlerhaft</string> - <string name="upload_fail_long">Die Datei konnte nicht auf den Server geladen - werden</string> - <string name="publish_long">Die Veröffentlichung wurde gestartet</string> - <string name="publish_ok">Wurde veröffentlicht</string> - <string name="publish_ok_long">Erfolgreich veröffentlicht</string> - <string name="publish_fail">Veröffentlichung nicht erfolgreich</string> - <string name="publish_fail_long">Leider konnte die Datei nicht veröffentlicht werden - </string> - <string name="delete">Löschen</string> <string name="name">Name</string> - <string name="filename">Dateiname</string> - <string name="template">Vorlage</string> - <string name="save">Speichern</string> - - <string name="description">Inhalt</string> @@ -101,14 +74,18 @@ <string name="nousername">Es ist noch kein Benutzername konfiguriert</string> <string name="www">Zur Webseite</string> <string name="scroll_down">Zum Ende springen</string> - <string name="scroll_down_desc">Beim Laden von Themen automatisch zum letzten Beitrag springen</string> + <string name="scroll_down_desc">Beim Laden von Themen automatisch zum letzten + Beitrag springen</string> <string name="use_ibc_theme">Das IBC-Theme benutzen</string> <string name="auto_login">Automatisch anmelden</string> - <string name="auto_login_desc">Beim Starten des Forums automatisch anmelden.</string> + <string name="auto_login_desc">Beim Starten des Forums automatisch anmelden. + </string> <string name="num_load">Anzahl Posts laden</string> - <string name="num_load_desc">Anzahl der Themen/Beiträge, die initial geladen werden</string> + <string name="num_load_desc">Anzahl der Themen/Beiträge, die initial geladen + werden</string> <string name="auto_load_next">Automatisch nachladen</string> - <string name="auto_load_next_desc">Beim Scrollen die nächsten Einträge automatisch laden</string> + <string name="auto_load_next_desc">Beim Scrollen die nächsten Einträge automatisch + laden</string> <string name="login">Anmelden</string> <string name="login_success">Benutzeranmeldung erfolgreich</string> <string name="login_failed">Benutzeranmeldung nicht erfolgreich</string> @@ -120,7 +97,8 @@ <item>50</item> </string-array> - + <string name="unread_topic">Ungelesenes Thema</string> + <string name="unread_forum">Ungelesenes Forum</string> <string name="forum">Forum</string> <string name="news">News</string> <string name="photos">Fotos</string> @@ -133,7 +111,8 @@ <string name="blog">Blog</string> <string name="links">Links</string> <string name="load_images">Bilder anzeigen</string> - <string name="load_images_desc">Automatisches Anzeigen von Bildern in Beiträgen. Dadurch wird die App langsamer und der Datenverbrauch steigt.</string> + <string name="load_images_desc">Automatisches Anzeigen von Bildern in Beiträgen. + Dadurch wird die App langsamer und der Datenverbrauch steigt.</string> <string name="goto_first_post">Zum ersten Beitrag</string> <string name="goto_last_post">Zum letzten Beitrag</string> <string name="goto_top">Nach oben</string> @@ -143,7 +122,35 @@ <string name="sent_fail">Nicht verschickt</string> <string name="new_topic">Neues Thema</string> <string name="parse_bbcode">BB-Code auswerten</string> - <string name="parse_bbcode_desc">In Forumbeiträgen den BB-Code auflösen. Kann die Anzeige von Beiträgen stark verlangsamen.</string> + <string name="parse_bbcode_desc">In Forumbeiträgen den BB-Code auflösen. Kann die + Anzeige von Beiträgen stark verlangsamen.</string> <string name="show_hints">Tipps anzeigen</string> - <string name="hint_press_long">Lange drücken um weitere Funktionen zu erhalten</string> + <string name="hint_press_long">Lange drücken um weitere Funktionen zu erhalten + </string> + <string name="subscription_service">Abo-Dienst</string> + <string name="subscription_service_started">Abo-Dienst gestartet</string> + <string name="subscription_service_stopped">Abo-Dienst gestoppt</string> + <string name="subscription_service_desc">Informiert bei ungelesenen, abonnierten Foren oder Themen</string> + <string name="interval">Intervall</string> + <string-array name="interval_list"> + <item>5</item> + <item>15</item> + <item>30</item> + <item>60</item> + <item>180</item> + <item>360</item> + <item>1440</item> + <item>2880</item> + </string-array> + <string-array name="interval_list_label"> + <item>5 Minuten</item> + <item>15 Minuten</item> + <item>30 Minuten</item> + <item>1 Stunde</item> + <item>3 Stunden</item> + <item>6 Stunden</item> + <item>1 Tag</item> + <item>2 Tage</item> + </string-array> + </resources> diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml @@ -3,7 +3,8 @@ android:title="@string/preferences"> <CheckBoxPreference android:defaultValue="true" - android:key="ibc_theme" android:title="@string/use_ibc_theme" android:enabled="false" /> + android:key="ibc_theme" android:title="@string/use_ibc_theme" + android:enabled="false" /> <CheckBoxPreference android:defaultValue="true" android:key="show_hints" android:title="@string/show_hints" /> @@ -17,8 +18,8 @@ <EditTextPreference android:key="server" android:title="@string/server"></EditTextPreference> --> - - + + <PreferenceCategory android:title="@string/username"> <EditTextPreference android:key="username" @@ -54,5 +55,18 @@ <CheckBoxPreference android:defaultValue="false" android:key="load_images" android:summary="@string/load_images_desc" android:title="@string/load_images" android:dependency="parse_bbcode" /> + + </PreferenceCategory> + + <PreferenceCategory android:title="@string/subscription_service"> + + <CheckBoxPreference android:defaultValue="true" + android:key="autostart_subscription_service" android:summary="@string/subscription_service_desc" + android:title="@string/subscription_service" /> + + <ListPreference android:defaultValue="60" + android:entries="@array/interval_list_label" android:entryValues="@array/interval_list" + android:key="subscription_service_interval" android:title="@string/interval" + android:dependency="autostart_subscription_service" /> </PreferenceCategory> </PreferenceScreen> diff --git a/src/de/mtbnews/android/Configuration.java b/src/de/mtbnews/android/Configuration.java @@ -1,14 +1,54 @@ package de.mtbnews.android; +import de.mtbnews.android.service.SubscriptionService; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; +import android.widget.Toast; public class Configuration extends PreferenceActivity { + private SharedPreferences prefs; @Override protected void onCreate(Bundle savedInstanceState) { + + prefs = PreferenceManager.getDefaultSharedPreferences(this); + OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() + { + public void onSharedPreferenceChanged(SharedPreferences prefs, + String key) + { + if (key.equals("autostart_subscription_service")) + { + if (prefs.getBoolean("autostart_subscription_service", + false)) + { + + startService(new Intent(getApplicationContext(), + SubscriptionService.class)); + Toast.makeText(Configuration.this, + R.string.subscription_service_started, + Toast.LENGTH_SHORT).show(); + } + else + { + stopService(new Intent(getApplicationContext(), + SubscriptionService.class)); + Toast.makeText(Configuration.this, + R.string.subscription_service_stopped, + Toast.LENGTH_SHORT).show(); + } + } + } + }; + + prefs.registerOnSharedPreferenceChangeListener(listener); + super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } diff --git a/src/de/mtbnews/android/IBCApplication.java b/src/de/mtbnews/android/IBCApplication.java @@ -6,8 +6,10 @@ package de.mtbnews.android; import org.mcsoxford.rss.RSSFeed; import android.app.Application; +import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import de.mtbnews.android.service.SubscriptionService; import de.mtbnews.android.tapatalk.TapatalkClient; import de.mtbnews.android.util.IBC; @@ -44,7 +46,11 @@ public class IBCApplication extends Application { client = new TapatalkClient(IBC.IBC_FORUM_CONNECTOR_URL); prefs = PreferenceManager.getDefaultSharedPreferences(this); - ibcTheme = prefs.getBoolean("ibc_theme",false); + ibcTheme = prefs.getBoolean("ibc_theme", false); + + if (prefs.getBoolean("autostart_subscription_service", false)) + startService(new Intent(getApplicationContext(), + SubscriptionService.class)); super.onCreate(); } diff --git a/src/de/mtbnews/android/service/SubscriptionService.java b/src/de/mtbnews/android/service/SubscriptionService.java @@ -3,95 +3,165 @@ */ package de.mtbnews.android.service; -import java.io.File; -import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; -import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.os.IBinder; +import android.preference.PreferenceManager; import android.util.Log; -import de.mtbnews.android.NewsActivity; +import de.mtbnews.android.ForumActivity; +import de.mtbnews.android.IBCApplication; import de.mtbnews.android.R; +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.Topic; /** * @author dankert * */ -public class SubscriptionService extends IntentService +public class SubscriptionService extends Service { + public SubscriptionService() + { + } - public static final String EXTRA_REQUEST = "request"; - public static final String EXTRA_FILENAME = "file"; + private IBCApplication ibcApp; + private static Timer timer; + private SharedPreferences prefs; private static final int NOTIFICATION_UPLOAD = 1; + private Date startDate; - public SubscriptionService() + public IBinder onBind(Intent arg0) { - super("UploadIntentService"); + return null; } - /** - * - * {@inheritDoc} - * - * @see android.app.IntentService#onHandleIntent(android.content.Intent) - */ - @Override - protected void onHandleIntent(Intent intent) + public void onCreate() { - final String filePath = intent.getStringExtra(EXTRA_FILENAME); - - final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - - final Intent notificationIntent = new Intent(this, NewsActivity.class); - final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - notificationIntent, 0); - - final File file = new File(filePath); - final String tickerText = getResources() - .getString(R.string.upload_long); - final Notification notification = new Notification( - R.drawable.ibc_logo, tickerText, System.currentTimeMillis()); - notification.setLatestEventInfo(getApplicationContext(), getResources() - .getString(R.string.upload), file.getName(), contentIntent); - notification.flags = Notification.FLAG_ONGOING_EVENT - | Notification.FLAG_NO_CLEAR; - nm.notify(NOTIFICATION_UPLOAD, notification); - - try - { + Log.d(this.getClass().getSimpleName(), "Starting service"); + super.onCreate(); + ibcApp = (IBCApplication) getApplication(); + prefs = PreferenceManager.getDefaultSharedPreferences(this); + startDate = new Date(); + timer = new Timer(); + timer.scheduleAtFixedRate(new TestSubscriptionTask(), 1000, 60000); + } - // Alles OK. - final String msgText = getResources().getString(R.string.upload_ok); - notification.tickerText = getResources().getString( - R.string.upload_ok_long); - notification.setLatestEventInfo(getApplicationContext(), msgText, - file.getName(), contentIntent); - notification.flags = Notification.FLAG_AUTO_CANCEL; - nm.notify(NOTIFICATION_UPLOAD, notification); - Log.d(this.getClass().getName(), msgText); - throw new IOException(); - } - catch (IOException e) - { - // Fehler ist aufgetreten. - final String msgText = getResources().getString( - R.string.upload_fail); - notification.tickerText = getResources().getString( - R.string.upload_fail_long); - notification.setLatestEventInfo(getApplicationContext(), msgText, e - .getMessage() - + ": " + file.getName(), contentIntent); - notification.flags = Notification.FLAG_AUTO_CANCEL; - nm.notify(NOTIFICATION_UPLOAD, notification); - - Log.e(this.getClass().getName(), msgText, e); - } - finally + private class TestSubscriptionTask extends TimerTask + { + int nId = 0; + + public void run() { + Log.d(this.getClass().getSimpleName(), + "Testing for unread subscriptions"); + final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + TapatalkClient client = ibcApp.getTapatalkClient(); + + try + { + List<Forum> subscribedForum = client.getSubscribedForum(true); + for (Forum forum : subscribedForum) + { + final Intent notificationIntent = new Intent( + SubscriptionService.this, ForumActivity.class); + notificationIntent.putExtra("forum_id", forum.getId()); + final PendingIntent contentIntent = PendingIntent + .getActivity(SubscriptionService.this, 0, + notificationIntent, 0); + + final String tickerText = getResources().getString( + R.string.unread_forum); + + final Notification notification = new Notification( + R.drawable.ibc_logo, tickerText, System + .currentTimeMillis()); + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.unread_forum), + forum.getTitle(), contentIntent); + notification.flags = Notification.FLAG_AUTO_CANCEL; + // notification.flags = Notification.FLAG_ONGOING_EVENT + // | Notification.FLAG_NO_CLEAR; + nm.notify(++nId, notification); + + Log.d(this.getClass().getName(), "forum unread: " + + forum.getName() + forum.getId()); + } + } + 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<Topic> subscribedTopic = client.getSubscribedTopics(0, 10, + true); + for (Topic topic : subscribedTopic) + { + final Intent notificationIntent = new Intent( + SubscriptionService.this, TopicActivity.class); + notificationIntent.putExtra("topic_id", topic.getId()); + final PendingIntent contentIntent = PendingIntent + .getActivity(SubscriptionService.this, 0, + notificationIntent, 0); + + final String tickerText = getResources().getString( + R.string.unread_topic); + + final Notification notification = new Notification( + R.drawable.ibc_logo, tickerText, System + .currentTimeMillis()); + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.unread_topic), + topic.getTitle(), contentIntent); + notification.flags = Notification.FLAG_AUTO_CANCEL; + // notification.flags = Notification.FLAG_ONGOING_EVENT + // | Notification.FLAG_NO_CLEAR; + nm.notify(++nId, notification); + + Log.d(this.getClass().getSimpleName(), "forum unread: " + + topic.getName() + topic.getId()); + } + } + catch (TapatalkException e) + { + Log.w(this.getClass().getSimpleName(), e); + throw new RuntimeException(e); + } + catch (Exception e) + { + Log.w("Laufzeitfehler", e); + throw new RuntimeException(e); + } } } + public void onDestroy() + { + timer.cancel(); + 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 @@ -194,6 +194,84 @@ public class TapatalkClient } } + @SuppressWarnings("unchecked") + public List<Topic> getSubscribedTopics(int from, int to, boolean onlyUnread) + throws TapatalkException + { + try + { + final Object[] params = new Object[] { from, to }; + Object o = client.callEx("get_subscribed_topic", params); + + Map map = (Map) o; + + @SuppressWarnings("unused") + int topicCount = (Integer) map.get("total_topic_num"); + + final List<Topic> topics = new ArrayList<Topic>(); + + for (Object o1 : (Object[]) map.get("topics")) + { + Map topicMap = (Map) o1; + if (!onlyUnread || (Boolean) topicMap.get("new_post")) + { + + Topic topic = new Topic((String) topicMap.get("topic_id"), + new ArrayList<Post>(), // + byteArrayToString(topicMap.get("topic_title")),// + (Date) topicMap.get("post_time"), // + new String((byte[]) topicMap.get("short_content")),// + new String((byte[]) topicMap + .get("post_author_name")), 0); + topics.add(topic); + } + } + + return topics; + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not load subscribe topics", e); + } + } + + @SuppressWarnings("unchecked") + public List<Forum> getSubscribedForum(boolean onlyUnread) + throws TapatalkException + { + try + { + Object o = client.call("get_subscribed_forum"); + + Map map = (Map) o; + + @SuppressWarnings("unused") + int forumCount = (Integer) map.get("total_forums_num"); + + final List<Forum> forums = new ArrayList<Forum>(); + + for (Object o1 : (Object[]) map.get("forums")) + { + Map map2 = (Map) o1; + if (!onlyUnread || (Boolean) map2.get("new_post")) + { + String id = (String) map2.get("forum_id"); + String name = byteArrayToString(map2.get("forum_name")); + Forum forum = new Forum(id, new ArrayList<Topic>(), name, + null, null); + + forums.add(forum); + } + } + + return forums; + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not load subscribe topics", e); + } + } + public static final int SEARCHTYPE_QUERY = 1; public static final int SEARCHTYPE_LATEST = 2; public static final int SEARCHTYPE_PARTICIPATED = 3; @@ -416,7 +494,7 @@ public class TapatalkClient final Object[] params = new Object[] { forumId, subject.getBytes(), content.getBytes() }; - Object o = client.callEx("new_topic", params); + Object o = client.callEx("new_topic", params); Map map = (Map) o; Object object = map.get("result"); @@ -425,7 +503,7 @@ public class TapatalkClient if (!ok) throw new TapatalkException(byteArrayToString(map .get("result_text"))); - + @SuppressWarnings("unused") // the newly generated post ID for this new topic. String msgId = (String) map.get("post_id"); @@ -490,7 +568,7 @@ public class TapatalkClient content.getBytes() }; Object o = client.callEx("create_message", params); - + Map map = (Map) o; Object object = map.get("result");