android-ibc-forum

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

commit cfae6a2a86425fbd4aa8c559b6c69c23e0bfc280
parent 0acbffb9dd321bad0b2a1555fba1418bc6fd8967
Author: Jan Dankert <devnull@localhost>
Date:   Wed,  8 Feb 2012 23:01:43 +0100

Cleanup: Keine RuntimeExceptions werfen, wenn Tapatalk-Fehler auftritt.

Diffstat:
AndroidManifest.xml | 1-
res/values/strings.xml | 1+
src/de/mtbnews/android/ForumActivity.java | 76+++++++++++++++++++++++++++-------------------------------------------------
src/de/mtbnews/android/MailActivity.java | 11+----------
src/de/mtbnews/android/MailboxActivity.java | 32++++++++++++--------------------
src/de/mtbnews/android/MessageActivity.java | 13+++----------
src/de/mtbnews/android/PostActivity.java | 83-------------------------------------------------------------------------------
src/de/mtbnews/android/ReplyMailActivity.java | 11++---------
src/de/mtbnews/android/ReplyPostActivity.java | 81+++++++++++++++++++++++++++++--------------------------------------------------
src/de/mtbnews/android/SearchActivity.java | 30+++++++++++++++---------------
src/de/mtbnews/android/TopicActivity.java | 52++++++++++++++--------------------------------------
src/de/mtbnews/android/service/SubscriptionService.java | 12++++++++++--
src/de/mtbnews/android/util/ExceptionUtils.java | 27---------------------------
src/de/mtbnews/android/util/IBCException.java | 35+++++++++++++++++++++++++++++++++++
src/de/mtbnews/android/util/ServerAsyncTask.java | 39+++++++++++++++++++++------------------
15 files changed, 171 insertions(+), 333 deletions(-)

diff --git a/AndroidManifest.xml b/AndroidManifest.xml @@ -19,7 +19,6 @@ <activity android:name="ForumOverviewActivity" android:label="@string/forum"></activity> <activity android:name="ForumActivity" android:label="@string/forum"></activity> <activity android:name="TopicActivity" android:label="@string/forum"></activity> - <activity android:name="PostActivity" android:label="@string/forum"></activity> <activity android:name=".SearchActivity"> <intent-filter> diff --git a/res/values/strings.xml b/res/values/strings.xml @@ -62,6 +62,7 @@ <string name="error_timeout">Zeitablauf</string> <string name="error_io">Verbindungsabbruch</string> <string name="error">Fehler</string> + <string name="error_tapatalk">Tut uns leid, aber diese Aktion war nicht erfolgreich</string> <string name="reason">Grund</string> <string name="areyousure">Sicher?</string> <string name="choosefile">Datei auswählen</string> diff --git a/src/de/mtbnews/android/ForumActivity.java b/src/de/mtbnews/android/ForumActivity.java @@ -29,6 +29,7 @@ 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; +import de.mtbnews.android.util.IBCException; import de.mtbnews.android.util.ServerAsyncTask; /** @@ -79,7 +80,7 @@ public class ForumActivity extends EndlessListActivity<Topic> { if (topicMode == TapatalkClient.TOPIC_ANNOUNCEMENT) return; - + MenuInflater menuInflater = new MenuInflater(getApplication()); menuInflater.inflate(R.menu.topic_context, menu); @@ -93,7 +94,7 @@ public class ForumActivity extends EndlessListActivity<Topic> { if (topicMode == TapatalkClient.TOPIC_ANNOUNCEMENT) return; - + // int aktPosition = displayFrom + position + 1; final Intent intent = new Intent(ForumActivity.this, TopicActivity.class); @@ -144,7 +145,8 @@ public class ForumActivity extends EndlessListActivity<Topic> { @Override - protected synchronized void callServer() throws IOException + protected synchronized void callServer() throws IOException, + IBCException { try @@ -155,8 +157,8 @@ public class ForumActivity extends EndlessListActivity<Topic> } catch (TapatalkException e) { - e.printStackTrace(); - throw new RuntimeException(e); + throw new IBCException(R.string.login_failed, e + .getMessage(), e); } } @@ -172,7 +174,8 @@ public class ForumActivity extends EndlessListActivity<Topic> { @Override - protected synchronized void callServer() throws IOException + protected synchronized void callServer() throws IOException, + IBCException { try @@ -182,8 +185,7 @@ public class ForumActivity extends EndlessListActivity<Topic> } catch (TapatalkException e) { - e.printStackTrace(); - throw new RuntimeException(e); + } } @@ -284,19 +286,14 @@ public class ForumActivity extends EndlessListActivity<Topic> @Override protected void callServer() - throws IOException + throws IOException, + TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - client.subscribeForum(forumId, - item - 1); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + client + .subscribeForum(forumId, + item - 1); } @Override @@ -318,18 +315,11 @@ public class ForumActivity extends EndlessListActivity<Topic> { @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - client.markForumAsRead(forumId); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + client.markForumAsRead(forumId); } @Override @@ -346,18 +336,11 @@ public class ForumActivity extends EndlessListActivity<Topic> R.string.mark_forum_read) { @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - client.markForumAsRead(null); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + client.markForumAsRead(null); } @Override @@ -407,29 +390,24 @@ public class ForumActivity extends EndlessListActivity<Topic> private Forum forum; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()).client; - try - { - this.forum = client.getForum(forumId, from, to, topicMode); - totalSize = this.forum.topicCount; - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + this.forum = client.getForum(forumId, from, to, topicMode); + totalSize = this.forum.topicCount; } protected void doOnSuccess() { ForumActivity.this.setTitle(forum.getTitle()); onListLoaded.listLoaded(this.forum.getTopics()); - + if (firstLoad) - Toast.makeText(ForumActivity.this, R.string.hint_press_long, - Toast.LENGTH_SHORT).show(); } + Toast.makeText(ForumActivity.this, + R.string.hint_press_long, Toast.LENGTH_SHORT) + .show(); + } }.execute(); diff --git a/src/de/mtbnews/android/MailActivity.java b/src/de/mtbnews/android/MailActivity.java @@ -124,22 +124,13 @@ public class MailActivity extends EndlessListActivity<Message> private Mailbox mailbox; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { - - try - { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); mailbox = client.getBoxContent(boxId, from, to); totalMessageCount = mailbox.countAll; - - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } } protected void doOnSuccess() diff --git a/src/de/mtbnews/android/MailboxActivity.java b/src/de/mtbnews/android/MailboxActivity.java @@ -34,32 +34,24 @@ public class MailboxActivity extends ListActivity super.onCreate(savedInstanceState); prefs = PreferenceManager.getDefaultSharedPreferences(this); - + new ServerAsyncTask(this, R.string.waitingforcontent) { @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { + TapatalkClient client = ((IBCApplication) getApplication()) + .getTapatalkClient(); + + // Login. + if (!((IBCApplication) getApplication()).getTapatalkClient().loggedIn + && prefs.getBoolean("auto_login", false)) + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); + + mailboxList = client.getMailbox(); - try - { - TapatalkClient client = ((IBCApplication) getApplication()) - .getTapatalkClient(); - - // Login. - if (!((IBCApplication) getApplication()).getTapatalkClient().loggedIn - && prefs.getBoolean("auto_login", false)) - client.login(prefs.getString("username", ""), prefs - .getString("password", "")); - - mailboxList = client.getMailbox(); - - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } } protected void doOnSuccess() diff --git a/src/de/mtbnews/android/MessageActivity.java b/src/de/mtbnews/android/MessageActivity.java @@ -36,25 +36,18 @@ public class MessageActivity extends Activity messageId = getIntent().getStringExtra("message_id"); findViewById(R.id.item_button).setVisibility(View.INVISIBLE); - + new ServerAsyncTask(this, R.string.waitingforcontent) { private TapatalkClient client; private Message message; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - message = client.getMessage(boxId, messageId); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + message = client.getMessage(boxId, messageId); } protected void doOnSuccess() diff --git a/src/de/mtbnews/android/PostActivity.java b/src/de/mtbnews/android/PostActivity.java @@ -1,83 +0,0 @@ -/** - * - */ -package de.mtbnews.android; - -import java.io.IOException; -import java.util.Map; - -import org.xmlrpc.android.XMLRPCClient; -import org.xmlrpc.android.XMLRPCException; - -import android.app.Activity; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceManager; -import de.mtbnews.android.util.IBC; -import de.mtbnews.android.util.ServerAsyncTask; - -/** - * @author dankert - * - */ -public class PostActivity extends Activity -{ - public static final String ID = "id"; - public static final String CLIENT = "client"; - private String objectid; - - Map<String, String> data; - - @Override - protected void onCreate(Bundle savedInstanceState) - { - final SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(this); - - super.onCreate(savedInstanceState); - - setContentView(R.layout.listing); - - new ServerAsyncTask(this, R.string.waitingforcontent) - { - - @Override - protected void callServer() throws IOException - { - - XMLRPCClient client = new XMLRPCClient(IBC.IBC_FORUM_CONNECTOR_URL); - // add 2 to 4 - Object[] params = new Object[] { - prefs.getString("username", "").getBytes(), - prefs.getString("password", "").getBytes() }; - - try - { - Object sum = client.callEx("login", params); - System.out.println(sum.getClass()); - System.out.println(sum); - - Object l = client.call("get_inbox_stat"); - System.out.println(l.toString() ); - - Object i = client.call("get_box_info"); - System.out.println(i.toString() ); - - } - catch (XMLRPCException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } - - } - - protected void doOnSuccess() - { - } - - }.execute(); - - } - -} diff --git a/src/de/mtbnews/android/ReplyMailActivity.java b/src/de/mtbnews/android/ReplyMailActivity.java @@ -41,18 +41,11 @@ public class ReplyMailActivity extends Activity private TapatalkClient client; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - message = client.getMessage(boxId, messageId); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + message = client.getMessage(boxId, messageId); } protected void doOnSuccess() diff --git a/src/de/mtbnews/android/ReplyPostActivity.java b/src/de/mtbnews/android/ReplyPostActivity.java @@ -32,68 +32,47 @@ public class ReplyPostActivity extends Activity subject = getIntent().getStringExtra("subject"); quote = getIntent().getStringExtra("quote"); - new ServerAsyncTask(this, R.string.waitingforcontent) - { - private TapatalkClient client; + ReplyPostActivity.this.setTitle(R.string.reply); - @Override - protected void callServer() throws IOException - { - client = ((IBCApplication) getApplication()) - .getTapatalkClient(); - // try - // { - // message = client.getMessage(boxId, messageId); - // } - // catch (TapatalkException e) - // { - // throw new RuntimeException(e); - // } - } + final TextView recipient = (TextView) findViewById(R.id.recipient); + recipient.setVisibility(View.INVISIBLE); - protected void doOnSuccess() - { - ReplyPostActivity.this.setTitle(R.string.reply); - - final TextView recipient = (TextView) findViewById(R.id.recipient); - recipient.setVisibility(View.INVISIBLE); + final TextView subject = (TextView) findViewById(R.id.subject); + subject + .setText(ReplyPostActivity.this.subject != null ? ReplyPostActivity.this.subject + : ""); - final TextView subject = (TextView) findViewById(R.id.subject); - subject - .setText(ReplyPostActivity.this.subject != null ? ReplyPostActivity.this.subject - : ""); + final TextView text = (TextView) findViewById(R.id.content); + text.setText(quote != null ? "[quote]" + quote + "[/quote]" : ""); - final TextView text = (TextView) findViewById(R.id.content); - text.setText(quote != null ? "[quote]"+quote+"[/quote]" : ""); + Button button = (Button) findViewById(R.id.send); + button.setOnClickListener(new OnClickListener() + { - Button button = (Button) findViewById(R.id.send); - button.setOnClickListener(new OnClickListener() + @Override + public void onClick(View v) + { + new ServerAsyncTask(ReplyPostActivity.this, R.string.send) { @Override - public void onClick(View v) + protected void callServer() throws TapatalkException { - try - { - client.createReply(forumId, topicId, subject - .getText().toString(), text.getText() - .toString()); - Toast.makeText(ReplyPostActivity.this, - R.string.sent_ok, Toast.LENGTH_LONG); - ReplyPostActivity.this.finish(); - } - catch (TapatalkException e) - { - new AlertDialog.Builder(ReplyPostActivity.this) - .setTitle(R.string.sent_fail).setMessage( - e.getMessage()).show(); - } + TapatalkClient client = ((IBCApplication) getApplication()) + .getTapatalkClient(); + + client.createReply(forumId, topicId, subject.getText() + .toString(), text.getText().toString()); } - }); + protected void doOnSuccess() + { + Toast.makeText(ReplyPostActivity.this, + R.string.sent_ok, Toast.LENGTH_LONG); + ReplyPostActivity.this.finish(); + } + }.execute(); } - }.execute(); - + }); } - } diff --git a/src/de/mtbnews/android/SearchActivity.java b/src/de/mtbnews/android/SearchActivity.java @@ -10,6 +10,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; +import android.util.Log; import android.view.ContextMenu; import android.view.MenuInflater; import android.view.MenuItem; @@ -25,6 +26,7 @@ import de.mtbnews.android.tapatalk.TapatalkClient; import de.mtbnews.android.tapatalk.TapatalkException; import de.mtbnews.android.tapatalk.wrapper.Search; import de.mtbnews.android.tapatalk.wrapper.Topic; +import de.mtbnews.android.util.IBC; import de.mtbnews.android.util.ServerAsyncTask; /** @@ -78,6 +80,11 @@ public class SearchActivity extends EndlessListActivity<Topic> } else { + // Diese Activity wurde mit einer unbekannten Action aufgerufen. + Log.w(IBC.TAG, "Unknown search action: " + intent.getAction()); + + // Das kann nur ein Programmfehler sein, daher RTE werfen und damit + // hoffen, dass der Benutzer einen Bericht sendet. throw new RuntimeException("Unknown search action: " + intent.getAction()); } @@ -136,8 +143,8 @@ public class SearchActivity extends EndlessListActivity<Topic> final Intent intent = new Intent(SearchActivity.this, TopicActivity.class); - intent.putExtra(TopicActivity.TOPIC_ID, super.entries - .get(menuInfo.position).getId()); + intent.putExtra(TopicActivity.TOPIC_ID, super.entries.get( + menuInfo.position).getId()); intent.putExtra("first_post", true); startActivity(intent); return true; @@ -165,22 +172,15 @@ public class SearchActivity extends EndlessListActivity<Topic> private Search search; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()).client; - try - { - search = client.searchTopics(searchType, query, username, - from, to, searchId); - - totalSize = search.topicCount; - searchId = search.searchId; - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + search = client.searchTopics(searchType, query, username, from, + to, searchId); + + totalSize = search.topicCount; + searchId = search.searchId; } protected void doOnSuccess() diff --git a/src/de/mtbnews/android/TopicActivity.java b/src/de/mtbnews/android/TopicActivity.java @@ -97,23 +97,16 @@ public class TopicActivity extends EndlessListActivity<Post> private Topic topic; @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()).client; - try - { - topic = client.getTopic(topicId, from, to); - - forumId = topic.forumId; - topicTitle = topic.getTitle(); - totalSize = topic.getPostCount(); - this.posts = topic.getPosts(); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + topic = client.getTopic(topicId, from, to); + + forumId = topic.forumId; + topicTitle = topic.getTitle(); + totalSize = topic.getPostCount(); + this.posts = topic.getPosts(); } protected void doOnSuccess() @@ -164,19 +157,13 @@ public class TopicActivity extends EndlessListActivity<Post> @Override protected void callServer() - throws IOException + throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - client.subscribeTopic(topicId, - item - 1); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } + client + .subscribeTopic(topicId, + item - 1); } @Override @@ -194,28 +181,17 @@ public class TopicActivity extends EndlessListActivity<Post> case R.id.menu_mark_read: + // TODO: In Tapatalk-API-Version 3 nicht verfügbar! new ServerAsyncTask(TopicActivity.this, R.string.mark_topic_read) { @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { TapatalkClient client = ((IBCApplication) getApplication()) .getTapatalkClient(); - try - { - client.markTopicAsRead(topicId); - } - catch (TapatalkException e) - { - throw new RuntimeException(e); - } - } - - @Override - protected void doOnSuccess() - { + client.markTopicAsRead(topicId); } }.execute(); return true; diff --git a/src/de/mtbnews/android/service/SubscriptionService.java b/src/de/mtbnews/android/service/SubscriptionService.java @@ -107,14 +107,15 @@ public class SubscriptionService extends Service public void run() { Log.d(IBC.TAG, "timer event fired"); - + final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); try { if (!client.loggedIn) { - Log.d(IBC.TAG, "Login for: " + prefs.getString("username", "")); + Log.d(IBC.TAG, "Login for: " + + prefs.getString("username", "")); client.login(prefs.getString("username", ""), prefs .getString("password", "")); } @@ -225,6 +226,13 @@ public class SubscriptionService extends Service } catch (Exception e) { + // Das sollte eigentlich nicht vorkommen. Hier scheint etwas + // schlimmeres kaputt zu sein, daher werfen wir hier eine + // RuntimeException weiter. Android wird dann den Service + // beenden und das Senden eines Berichtes ermöglichen. Das ist + // das beste, was wir hier noch tun können ;) Würden wir den + // Fehler nicht weiterwerfen, würde der Service eh nichts mehr + // machen und niemandem wäre geholfen. Log.w(IBC.TAG, e); throw new RuntimeException(e); } diff --git a/src/de/mtbnews/android/util/ExceptionUtils.java b/src/de/mtbnews/android/util/ExceptionUtils.java @@ -1,27 +0,0 @@ -/** - * - */ -package de.mtbnews.android.util; - -import java.io.IOException; -import java.net.SocketTimeoutException; - -import de.mtbnews.android.R; - -/** - * @author dankert - * - */ -public class ExceptionUtils -{ - - public static int getResourceStringId( Throwable throwable) { - - if ( throwable instanceof SocketTimeoutException ) - return R.string.error_timeout; - else if ( throwable instanceof IOException ) - return R.string.error_io; - else - return R.string.error_io; - } -} diff --git a/src/de/mtbnews/android/util/IBCException.java b/src/de/mtbnews/android/util/IBCException.java @@ -0,0 +1,35 @@ +/** + * + */ +package de.mtbnews.android.util; + +/** + * + * @author dankert + * + */ +public class IBCException extends Exception +{ + + private int errorResId; + + /** + * @param errorResId Resource-Id der Fehlermeldung + * @param detailMessage + * @param cause + */ + public IBCException(int errorResId, String detailMessage, Throwable cause) + { + super(detailMessage, cause); + this.errorResId = errorResId; + + } + + /** + * @return + */ + public int getErrorResId() + { + return errorResId; + } +} diff --git a/src/de/mtbnews/android/util/ServerAsyncTask.java b/src/de/mtbnews/android/util/ServerAsyncTask.java @@ -4,7 +4,7 @@ package de.mtbnews.android.util; import java.io.IOException; -import java.util.concurrent.locks.Lock; +import java.net.SocketTimeoutException; import android.app.AlertDialog; import android.app.ProgressDialog; @@ -13,6 +13,7 @@ import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; import android.util.Log; +import de.mtbnews.android.R; import de.mtbnews.android.tapatalk.TapatalkClient; import de.mtbnews.android.tapatalk.TapatalkException; @@ -39,7 +40,7 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> private ProgressDialog progressDialog; private Context context; private AlertDialog alertDialog; - private Exception error; + private IBCException error; // private static final Semaphore lock2 = new Semaphore(1,true); @@ -102,25 +103,13 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> * @param error * Exception, die aufgetreten ist. */ - protected void doOnError(Exception error) + protected void doOnError(IBCException error) { final Builder builder = new AlertDialog.Builder(this.context); alertDialog = builder.setCancelable(true).create(); - final int causeRId = ExceptionUtils.getResourceStringId(error); - String msg = // this.context.getResources().getString(R.string.reason) - // + ":\n\n" + - error.getMessage(); - Throwable t = error; - while (t.getCause() != null) - { - t = t.getCause(); - msg += ": " + t.getMessage(); - } - - alertDialog.setTitle(causeRId); alertDialog.setIcon(android.R.drawable.ic_menu_close_clear_cancel); - alertDialog.setMessage(msg); + alertDialog.setMessage(context.getText(error.getErrorResId())); alertDialog.show(); } @@ -151,11 +140,24 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> catch (IOException e) { Log.w(this.getClass().getName(), e.getMessage(), e); - error = e; + + final int resId; + + if (e instanceof SocketTimeoutException) + resId = R.string.error_timeout; + else + resId = R.string.error_io; + + error = new IBCException(resId, e.getMessage(), e); } catch (TapatalkException e) { Log.w(this.getClass().getName(), e.getMessage(), e); + error = new IBCException(R.string.error_tapatalk, e.getMessage(), e); + } + catch (IBCException e) + { + Log.w(this.getClass().getName(), e.getMessage(), e); error = e; } catch (RuntimeException e) @@ -187,7 +189,8 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> * @throws IOException * Vom Server erzeugte Fehler */ - protected abstract void callServer() throws IOException, TapatalkException; + protected abstract void callServer() throws IOException, TapatalkException, + IBCException; /** *