android-ibc-forum

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

commit d3b1d330aa17725bea33cf5922915eb763d288e6
parent 1580b0753ac8b21117f607fd10d72c054b182fb9
Author: Jan Dankert <devnull@localhost>
Date:   Mon,  6 Feb 2012 23:27:46 +0100

Verbessertes Fehlerhandling: Laufzeitfehler im SubscriptionService fangen, bei TaptatalkException in AlertDialog anzeigen.

Diffstat:
src/de/mtbnews/android/ForumOverviewActivity.java | 60+++++++++++++++++-------------------------------------------
src/de/mtbnews/android/service/SubscriptionService.java | 77+++++++++++++++++++++++------------------------------------------------------
src/de/mtbnews/android/util/ServerAsyncTask.java | 29+++++++++++++++--------------
3 files changed, 55 insertions(+), 111 deletions(-)

diff --git a/src/de/mtbnews/android/ForumOverviewActivity.java b/src/de/mtbnews/android/ForumOverviewActivity.java @@ -91,26 +91,16 @@ public class ForumOverviewActivity extends ExpandableListActivity { @Override - protected void callServer() throws IOException + protected void callServer() throws TapatalkException { + // Login. + if (!((IBCApplication) getApplication()).getTapatalkClient().loggedIn + && prefs.getBoolean("auto_login", false)) + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); - try - { - // Login. - if (!((IBCApplication) getApplication()) - .getTapatalkClient().loggedIn - && prefs.getBoolean("auto_login", false)) - client.login(prefs.getString("username", ""), prefs - .getString("password", "")); - - forumList = client.getAllForum(); - unterforenFlachkloppen(); - } - catch (TapatalkException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } + forumList = client.getAllForum(); + unterforenFlachkloppen(); } @@ -266,19 +256,11 @@ public class ForumOverviewActivity extends ExpandableListActivity { @Override - protected void callServer() throws IOException + protected void callServer() throws IOException, + TapatalkException { - try - { - client.login(prefs.getString("username", ""), - prefs.getString("password", "")); - - } - catch (TapatalkException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); } @@ -287,7 +269,8 @@ public class ForumOverviewActivity extends ExpandableListActivity { Log.d("IBC", "login success"); Toast.makeText(ForumOverviewActivity.this, - R.string.login_success, Toast.LENGTH_SHORT).show(); + R.string.login_success, Toast.LENGTH_SHORT) + .show(); } }.executeSynchronized(); @@ -313,19 +296,10 @@ public class ForumOverviewActivity extends ExpandableListActivity { @Override - protected synchronized void callServer() throws IOException + protected synchronized void callServer() throws IOException, + TapatalkException { - - try - { - client.logout(); - } - catch (TapatalkException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } - + client.logout(); } @Override diff --git a/src/de/mtbnews/android/service/SubscriptionService.java b/src/de/mtbnews/android/service/SubscriptionService.java @@ -24,6 +24,7 @@ import android.preference.PreferenceManager; import android.text.TextUtils; import android.util.Log; import de.mtbnews.android.ForumActivity; +import de.mtbnews.android.IBCActivity; import de.mtbnews.android.IBCApplication; import de.mtbnews.android.MailboxActivity; import de.mtbnews.android.R; @@ -90,24 +91,26 @@ public class SubscriptionService extends Service { public void run() { - Log.d(this.getClass().getSimpleName(), - "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 { - try + Log.d(this.getClass().getSimpleName(), + "now testing for unread topics and messages"); + + final TapatalkClient client = ibcApp.getTapatalkClient(); + + if (!client.loggedIn) { client.login(prefs.getString("username", ""), prefs .getString("password", "")); } - catch (TapatalkException e) + + if (!client.loggedIn) { final Notification notification = new Notification( - R.drawable.ibc_logo, e.getMessage(), actualTime); + R.drawable.ibc_logo, "", actualTime); notification.setLatestEventInfo(getApplicationContext(), getResources().getString(R.string.login_failed), "", null); @@ -116,23 +119,7 @@ public class SubscriptionService extends Service 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>(); @@ -159,20 +146,6 @@ public class SubscriptionService extends Service .join(", ", forumNameList), contentIntent); nm.notify(NOTIFICATION_FORUM, 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 - { ListHolder<Topic> subscribedTopic = client.getSubscribedTopics( 0, 10, true); final List<String> topicNameList = new ArrayList<String>(); @@ -202,20 +175,7 @@ public class SubscriptionService extends Service 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; @@ -252,12 +212,21 @@ public class SubscriptionService extends Service } catch (TapatalkException e) { - Log.w(this.getClass().getSimpleName(), e); - throw new RuntimeException(e); + final Intent notificationIntent = new Intent( + SubscriptionService.this, IBCActivity.class); + final PendingIntent contentIntent = PendingIntent.getActivity( + SubscriptionService.this, 0, notificationIntent, 0); + + final Notification notification = createNotification(e + .getMessage(), R.string.error, null, e.getMessage(), + contentIntent); + + notification.flags = Notification.FLAG_AUTO_CANCEL; + nm.notify(NOTIFICATION_ERROR, notification); } catch (Exception e) { - Log.w("Laufzeitfehler", e); + Log.w("IBC-Service", e); throw new RuntimeException(e); } } diff --git a/src/de/mtbnews/android/util/ServerAsyncTask.java b/src/de/mtbnews/android/util/ServerAsyncTask.java @@ -4,8 +4,11 @@ package de.mtbnews.android.util; import java.io.IOException; +import java.util.concurrent.Semaphore; import java.util.concurrent.locks.ReentrantLock; +import de.mtbnews.android.tapatalk.TapatalkException; + import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; @@ -14,7 +17,7 @@ import android.content.DialogInterface; import android.os.AsyncTask; import android.util.Log; -/** +/** * Ein asynchroner Task für den Zugriff auf den OpenRat-CMS-Server. Der Aufruf * des Servers muss in der zu überschreibenden Methode {@link #callServer()} * durchgeführt werden.<br> @@ -38,7 +41,8 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> private Context context; private AlertDialog alertDialog; private Exception error; - private ReentrantLock lock; + + // private static final Semaphore lock2 = new Semaphore(1,true); /** * @param context @@ -58,9 +62,6 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> @Override final protected void onPreExecute() { - lock = new ReentrantLock(); - lock.lock(); - progressDialog.setCancelable(true); progressDialog .setOnCancelListener(new DialogInterface.OnCancelListener() @@ -84,7 +85,6 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> final protected void onPostExecute(Void result) { progressDialog.dismiss(); - lock.unlock(); if (error != null) { @@ -106,8 +106,7 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> protected void doOnError(Exception error) { progressDialog.dismiss(); - lock.unlock(); - + final Builder builder = new AlertDialog.Builder(this.context); alertDialog = builder.setCancelable(true).create(); final int causeRId = ExceptionUtils.getResourceStringId(error); @@ -149,7 +148,12 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> { callServer(); } - catch (Exception e) + catch (IOException e) + { + Log.e(this.getClass().getName(), e.getMessage(), e); + error = e; + } + catch (TapatalkException e) { Log.e(this.getClass().getName(), e.getMessage(), e); error = e; @@ -165,7 +169,7 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> * @throws IOException * Vom Server erzeugte Fehler */ - protected abstract void callServer() throws IOException; + protected abstract void callServer() throws IOException,TapatalkException; /** * @@ -173,10 +177,7 @@ public abstract class ServerAsyncTask extends AsyncTask<Void, Void, Void> */ public final void executeSynchronized(Void... params) { - synchronized (ServerAsyncTask.class) - { - super.execute(params); - } + super.execute(params); } }