From 5f5d89eca4d228dcd0564462975b69d6fb7a8dee Mon Sep 17 00:00:00 2001 From: Chiu Yue Chun Date: Sun, 7 Jun 2015 11:03:41 +0800 Subject: [PATCH] move due checking to asynctask previously it fail to retreive any book from internet in android 4 also make exception handing more explicit and add log message --- app/src/main/AndroidManifest.xml | 4 +- .../loopbook/cuhk_loopbook/CheckSched.java | 4 ++ .../loopbook/cuhk_loopbook/DueChecker.java | 42 ++++++++++++++----- .../com/loopbook/cuhk_loopbook/LibConn.java | 20 +++++---- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2cdef1d..9b1740f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,7 +28,9 @@ android:value=".MainActivity" /> - + diff --git a/app/src/main/java/com/loopbook/cuhk_loopbook/CheckSched.java b/app/src/main/java/com/loopbook/cuhk_loopbook/CheckSched.java index 4ebde73..6662f26 100644 --- a/app/src/main/java/com/loopbook/cuhk_loopbook/CheckSched.java +++ b/app/src/main/java/com/loopbook/cuhk_loopbook/CheckSched.java @@ -8,6 +8,8 @@ import android.widget.Toast; import android.content.Context; +import android.util.Log; + public class CheckSched { public static void scheduleNotification(Context context) { Intent notificationIntent = new Intent(context, DueChecker.class); @@ -17,11 +19,13 @@ public static void scheduleNotification(Context context) { Calendar calendar = Calendar.getInstance(); if (BuildInfo.DEBUG) { + Log.e("CheckSched", "scheduling in debugmode"); calendar.setTimeInMillis(System.currentTimeMillis() + 5000); alarmMgr.setInexactRepeating( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent); } else { + Log.e("CheckSched", "scheduling in releasemode"); // Set the alarm to start at approximately ?:??. calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 15); diff --git a/app/src/main/java/com/loopbook/cuhk_loopbook/DueChecker.java b/app/src/main/java/com/loopbook/cuhk_loopbook/DueChecker.java index 673baf7..63b2215 100644 --- a/app/src/main/java/com/loopbook/cuhk_loopbook/DueChecker.java +++ b/app/src/main/java/com/loopbook/cuhk_loopbook/DueChecker.java @@ -6,27 +6,49 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.os.AsyncTask; import java.util.*; import java.text.SimpleDateFormat; import org.jsoup.nodes.Element; +import android.util.Log; public class DueChecker extends BroadcastReceiver { public static int notifyId = 1; public static int day_threshold = BuildInfo.DEBUG ? 20 : 2; - public void onReceive(Context context, Intent intent) { - boolean connectable = LibConn.isConnectable(); - Element elm = null; - try { + private class AsyncBookLoader extends AsyncTask { + private Exception caughtException = null; + private Context context; + + @Override + protected Element doInBackground(Context... context) { + Log.e("DueChecker", "doingInbackground"); + Element elm; + this.context = context[0]; + boolean connectable = LibConn.isConnectable(); elm = connectable ? - DataIO.refreshStoredData(context) : - DataIO.getStoredData(context); - } catch (RuntimeException e) { - return; + DataIO.refreshStoredData(this.context) : + DataIO.getStoredData(this.context); + + return elm; + } + + @Override + protected void onPostExecute(Element elm) { + Log.e("DueChecker", "postexecute"); + DueChecker.checker(context, elm); } + } + + public void onReceive(Context context, Intent intent) { + Log.e("DueChecker", "onReceive"); + AsyncBookLoader bookLoader = new AsyncBookLoader(); + bookLoader.execute(context); + } + private static void checker(Context context, Element elm) { SimpleDateFormat dateparser = new SimpleDateFormat("dd-MM-yy"); Calendar DaysLater = Calendar.getInstance(); DaysLater.add(Calendar.DATE, day_threshold); @@ -42,14 +64,14 @@ public void onReceive(Context context, Intent intent) { if (DaysLater.compareTo(dueDate) > 0) { NotificationManager nManager = (NotificationManager)context - .getSystemService(Context.NOTIFICATION_SERVICE); + .getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = getNotification(context, title); nManager.notify(notifyId++, builder.build()); } } } - private NotificationCompat.Builder getNotification(Context context, String content) { + public static NotificationCompat.Builder getNotification(Context context, String content) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("Book going to expire"); builder.setContentText(content); diff --git a/app/src/main/java/com/loopbook/cuhk_loopbook/LibConn.java b/app/src/main/java/com/loopbook/cuhk_loopbook/LibConn.java index 551f242..d7ecf7f 100644 --- a/app/src/main/java/com/loopbook/cuhk_loopbook/LibConn.java +++ b/app/src/main/java/com/loopbook/cuhk_loopbook/LibConn.java @@ -8,6 +8,7 @@ import java.util.*; import java.net.InetAddress; +import android.util.Log; public class LibConn { private final String name; @@ -23,8 +24,10 @@ public LibConn(String name, String passwd) { public static boolean isConnectable() { try { InetAddress ipAddr = InetAddress.getByName("google.com"); + Log.e("Libconn", "connectable"); return !ipAddr.equals(""); } catch (Exception e) { + Log.e("Libconn", "unconnectable"); return false; } @@ -40,7 +43,8 @@ public void login() { try { resp = conn.execute(); doc = resp.parse(); - } catch(Exception e) { + } catch(java.io.IOException e) { + Log.e("Libconn", "IOException"); throw new RuntimeException("Failed connection", e); } @@ -50,13 +54,13 @@ public void login() { } //Element name = doc.select("strong").first(); - try { - Element bookListLink = doc.select(".patroninfoList a").first(); - this.bookhref = bookListLink.attr("abs:href"); - this.cookies = resp.cookies(); - } catch(Exception e) { - throw new RuntimeException("Cannnot get books after login", e); - } + Element bookListLink = doc.select(".patroninfoList a").first(); + if (bookListLink == null) + throw new RuntimeException("Cannnot get books after login"); + this.bookhref = bookListLink.attr("abs:href"); + if (this.bookhref == "") + throw new RuntimeException("Cannnot get books after login"); + this.cookies = resp.cookies(); } public Element getBooksElement() {