android-ibc-forum

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

commit 8a51af01b13fb60d7dcf4489f08d3a9dcceee597
parent 637bf9e923210b04e3fae8fed581e5f4170d1d06
Author: Jan Dankert <devnull@localhost>
Date:   Sun,  5 Feb 2012 20:55:21 +0100

Abonnements für Themen und Foren speichern.

Diffstat:
res/menu/forum.xml | 4++++
res/menu/topic.xml | 7+++++--
res/values/strings.xml | 29++++++++++++++++++++---------
src/de/mtbnews/android/ForumActivity.java | 56++++++++++++++++++++++++++++++++++++++++++++++++++------
src/de/mtbnews/android/TopicActivity.java | 44++++++++++++++++++++++++++++++++++++++++++++
src/de/mtbnews/android/tapatalk/TapatalkClient.java | 121++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
6 files changed, 215 insertions(+), 46 deletions(-)

diff --git a/res/menu/forum.xml b/res/menu/forum.xml @@ -6,8 +6,12 @@ <item android:id="@+id/menu_search" android:title="@string/search" android:icon="@android:drawable/ic_menu_search"></item> + <item android:id="@+id/menu_subscribe" android:title="@string/subscribe_forum" + android:icon="@android:drawable/ic_input_get"></item> +<!-- <item android:id="@+id/menu_mailbox" android:title="@string/mailbox" android:icon="@android:drawable/ic_menu_send"></item> + --> <item android:id="@+id/menu_subscribed_forums" android:title="@string/subscription_forums" android:icon="@android:drawable/ic_input_get"></item> diff --git a/res/menu/topic.xml b/res/menu/topic.xml @@ -1,10 +1,13 @@ <menu xmlns:android="http://schemas.android.com/apk/res/android"> - <item android:id="@+id/menu_reply" android:title="@string/reply" - android:icon="@android:drawable/ic_menu_gallery"></item> + <item android:id="@+id/menu_subscribe" android:title="@string/subscribe_topic" + android:icon="@android:drawable/ic_input_get"></item> <item android:id="@+id/menu_mark_read" android:title="@string/mark_topic_read" android:icon="@android:drawable/ic_menu_agenda"></item> + <item android:id="@+id/menu_reply" android:title="@string/reply" + android:icon="@android:drawable/ic_menu_gallery"></item> + </menu> \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml @@ -51,8 +51,8 @@ <string name="waitingforsave">Speichern</string> <string name="waitingfor_loadmore">Lade weitere Einträge</string> <string name="waitingfor_mailbox">Nachrichten werden geladen</string> - <string name="waitingfor_subscription_forums">Abbonierte Foren werden geladen</string> - <string name="waitingfor_subscription_topics">Abbonierte Themen werden geladen</string> + <string name="waitingfor_subscription_forums">Abonierte Foren werden geladen</string> + <string name="waitingfor_subscription_topics">Abonierte Themen werden geladen</string> <string name="name">Name</string> @@ -134,18 +134,21 @@ </string> <string name="subscription_service">Abo-Dienst</string> <string name="subscription_service_started">Abo-Dienst gestartet</string> + <string name="subscription_saved">Abonnement wurdeaktualisiert</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="subscription_forums">Abbonierte Foren</string> - <string name="subscription_topics">Abbonierte Themen</string> - + <string name="subscription_service_desc">Informiert bei neuen Nachrichten und ungelesenen + Beiträgen in abonnierten Foren und Themen</string> + <string name="subscription_forums">Abonierte Foren</string> + <string name="subscription_topics">Abonierte Themen</string> + <string name="mark_all_forum_read">Alle Foren als gelesen markieren</string> <string name="mark_forum_read">Forum als gelesen markieren</string> <string name="mark_topic_read">Thema als gelesen markieren</string> <string name="mark_unread">Als ungelesen markieren</string> - <string name="subscribe_forum">Forum abbonieren</string> - <string name="subscribe_topic">Thema abbonieren</string> - + <string name="subscribe_forum">Forum abonieren</string> + <string name="subscribe_topic">Thema abonieren</string> + + <string name="interval">Intervall</string> <string-array name="interval_list"> <item>1</item> @@ -170,4 +173,12 @@ <item>2 Tage</item> </string-array> + <string-array name="subscription_modes"> + <item>Kein Abo</item> + <item>Ohne E-Mail</item> + <item>Sofortige E-Mail</item> + <item>Tägliche E-Mail</item> + <item>Wöchentliche E-Mail</item> + </string-array> + </resources> diff --git a/src/de/mtbnews/android/ForumActivity.java b/src/de/mtbnews/android/ForumActivity.java @@ -4,8 +4,9 @@ package de.mtbnews.android; import java.io.IOException; -import java.util.Map; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -37,7 +38,7 @@ import de.mtbnews.android.util.ServerAsyncTask; public class ForumActivity extends EndlessListActivity<Topic> { public static final String FORUM_ID = "forum_id"; - + private SharedPreferences prefs; private int totalSize; private String forumId; @@ -144,8 +145,8 @@ public class ForumActivity extends EndlessListActivity<Topic> try { - client.login(prefs.getString( - "username", ""), prefs.getString("password", "")); + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); } catch (TapatalkException e) @@ -207,10 +208,10 @@ public class ForumActivity extends EndlessListActivity<Topic> case R.id.menu_mailbox: startActivity(new Intent(this, MailboxActivity.class)); return true; - + case R.id.menu_create_topic: Intent intent5 = new Intent(this, CreateTopicActivity.class); - intent5.putExtra(FORUM_ID,forumId); + intent5.putExtra(FORUM_ID, forumId); startActivity(intent5); return true; @@ -261,6 +262,49 @@ public class ForumActivity extends EndlessListActivity<Topic> } return true; + + case R.id.menu_subscribe: + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.subscribe_forum); + builder.setItems(R.array.subscription_modes, + new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, final int item) + { + + new ServerAsyncTask(ForumActivity.this,R.string.waitingfor_subscription_forums) + { + + @Override + protected void callServer() + throws IOException + { + TapatalkClient client = ((IBCApplication) getApplication()) + .getTapatalkClient(); + try + { + client.subscribeForum(forumId, + item - 1); + } + catch (TapatalkException e) + { + throw new RuntimeException(e); + } + } + + @Override + protected void doOnSuccess() + { + Toast.makeText(getApplicationContext(), + R.string.subscription_saved, + Toast.LENGTH_SHORT).show(); + } + }; + } + }); + builder.create().show(); + return true; } return false; } diff --git a/src/de/mtbnews/android/TopicActivity.java b/src/de/mtbnews/android/TopicActivity.java @@ -6,6 +6,8 @@ package de.mtbnews.android; import java.io.IOException; import java.util.List; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.Menu; @@ -152,6 +154,48 @@ public class TopicActivity extends EndlessListActivity<Post> intent.putExtra("subject", topicTitle); startActivity(intent); return true; + + case R.id.menu_subscribe: + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.subscribe_forum); + builder.setItems(R.array.subscription_modes, + new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, final int item) + { + new ServerAsyncTask(TopicActivity.this,R.string.waitingfor_subscription_forums) + { + + @Override + protected void callServer() + throws IOException + { + TapatalkClient client = ((IBCApplication) getApplication()) + .getTapatalkClient(); + try + { + client.subscribeForum(forumId, + item - 1); + } + catch (TapatalkException e) + { + throw new RuntimeException(e); + } + } + + @Override + protected void doOnSuccess() + { + Toast.makeText(getApplicationContext(), + R.string.subscription_saved, + Toast.LENGTH_SHORT).show(); + } + }; + } + }); + builder.create().show(); + return true; } return false; diff --git a/src/de/mtbnews/android/tapatalk/TapatalkClient.java b/src/de/mtbnews/android/tapatalk/TapatalkClient.java @@ -9,8 +9,6 @@ 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; import de.mtbnews.android.tapatalk.wrapper.ListHolder; import de.mtbnews.android.tapatalk.wrapper.Mailbox; @@ -20,14 +18,15 @@ import de.mtbnews.android.tapatalk.wrapper.Search; import de.mtbnews.android.tapatalk.wrapper.Topic; /** - * Tapatalk-kompatibler Client. + * Tapatalk-compatible client. * - * @author dankert + * @author Jan Dankert * */ public class TapatalkClient { private XMLRPCClient client; + public boolean loggedIn; public TapatalkClient(String connectorUrl) @@ -276,6 +275,76 @@ public class TapatalkClient } } + public final static int SUBSCRIBE_NONE = -1; + public final static int SUBSCRIBE_WITHOUT_EMAIL = 0; + public final static int SUBSCRIBE_INSTANT_EMAIL = 1; + public final static int SUBSCRIBE_DAILY_EMAIL = 2; + public final static int SUBSCRIBE_WEEKLY_EMAIL = 3; + + /** + * Subscribes to a forum. + * + * @param forumId + * The forum-Id + * @param mode + * One of the SUBSCRIBE_*-constants + * @throws TapatalkException + */ + public void subscribeForum(String forumId, int mode) + throws TapatalkException + { + try + { + if (mode == SUBSCRIBE_NONE) + { + Object[] params = new Object[] { forumId }; + toMap(client.callEx("unsubscribe_forum", params)); + } + else + { + Object[] params = new Object[] { forumId, mode }; + toMap(client.callEx("subscribe_forum", params)); + } + + } + catch (XMLRPCException e) + { + throw new TapatalkException("Could not load subscribe topics", e); + } + } + + /** + * Subscribes to a topic. + * + * @param topicId + * The topic-id + * @param mode + * One of the SUBSCRIBE_*-constants + * @throws TapatalkException + */ + public void subscribeTopic(String topicId, int mode) + throws TapatalkException + { + try + { + if (mode == SUBSCRIBE_NONE) + { + Object[] params = new Object[] { topicId }; + toMap(client.callEx("unsubscribe_topic", params)); + } + else + { + Object[] params = new Object[] { topicId, mode }; + toMap(client.callEx("subscribe_topic", params)); + } + + } + 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; @@ -458,7 +527,8 @@ public class TapatalkClient { final Object[] params = new Object[] { messageId, boxId }; - Map mapMap = toMap(client.callEx("get_message", params)); + Map<String, Object> mapMap = toMap(client.callEx("get_message", + params)); Object[] objects = (Object[]) mapMap.get("msg_to"); String[] msgTo = new String[objects.length]; @@ -484,10 +554,9 @@ public class TapatalkClient /** * Create a topic * - * @param boxId - * @param messageId - * @param asHtml - * @return + * @param forumId + * @param subject + * @param content * @throws TapatalkException */ public void createTopic(String forumId, String subject, String content) @@ -498,15 +567,7 @@ public class TapatalkClient final Object[] params = new Object[] { forumId, subject.getBytes(), content.getBytes() }; - Object o = client.callEx("new_topic", params); - Map map = (Map) o; - - Object object = map.get("result"); - - boolean ok = (Boolean) object; - if (!ok) - throw new TapatalkException(byteArrayToString(map - .get("result_text"))); + Map<?, ?> map = toMap(client.callEx("new_topic", params)); @SuppressWarnings("unused") // the newly generated post ID for this new topic. @@ -535,15 +596,7 @@ public class TapatalkClient final Object[] params = new Object[] { forumId, topicId, subject.getBytes(), content.getBytes() }; - Object o = client.callEx("reply_post", params); - Map map = (Map) o; - - Object object = map.get("result"); - - boolean ok = (Boolean) object; - if (!ok) - throw new TapatalkException(byteArrayToString(map - .get("result_text"))); + Map<?, ?> map = toMap(client.callEx("reply_post", params)); @SuppressWarnings("unused") String msgId = (String) map.get("topic_id"); @@ -612,14 +665,24 @@ public class TapatalkClient return i.intValue(); } - private Map toMap(Object o) throws TapatalkException + /** + * Converting the object to a map. If object is no Map, it throws a + * {@link TapatalkException}. If Map contains a key "result" and the value + * is not 'true', a {@link TapatalkException} is thrown. + * + * @param o + * @return + * @throws TapatalkException + */ + @SuppressWarnings("unchecked") + private Map<String, Object> toMap(Object o) throws TapatalkException { if (!(o instanceof Map)) { throw new TapatalkException("no map: " + o.toString() + " (" + o.getClass() + ")"); } - Map map = (Map) o; + Map<String, Object> map = (Map<String, Object>) o; Object object = map.get("result"); if (object == null)