Skip to content

Commit

Permalink
move due checking to asynctask
Browse files Browse the repository at this point in the history
previously it fail to retreive any book from internet in android 4
also make exception handing more explicit
and add log message
  • Loading branch information
Chiu Yue Chun committed Jun 7, 2015
1 parent 6a056d4 commit 5f5d89e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
android:value=".MainActivity" />
</activity>

<receiver android:name=".DueChecker" />
<receiver android:name=".DueChecker"
android:permission="android.permission.INTERNET"
/>
</application>

</manifest>
4 changes: 4 additions & 0 deletions app/src/main/java/com/loopbook/cuhk_loopbook/CheckSched.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
42 changes: 32 additions & 10 deletions app/src/main/java/com/loopbook/cuhk_loopbook/DueChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context, Void, Element> {
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);
Expand All @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/com/loopbook/cuhk_loopbook/LibConn.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.*;
import java.net.InetAddress;

import android.util.Log;

public class LibConn {
private final String name;
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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() {
Expand Down

0 comments on commit 5f5d89e

Please sign in to comment.