From ec76b96a444f5af50802111940b73712cb124b79 Mon Sep 17 00:00:00 2001 From: Aidan Follestad Date: Wed, 24 Aug 2016 20:01:55 -0500 Subject: [PATCH] Switched the service to an IntentService so checks are queued --- .idea/.name | 2 +- .idea/modules.xml | 2 +- app/build.gradle | 6 +- .../nocknock/services/CheckService.java | 172 +++++++++--------- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 6 files changed, 91 insertions(+), 97 deletions(-) diff --git a/.idea/.name b/.idea/.name index fb8fe6b..f519041 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -Nock Nock \ No newline at end of file +nock-nock \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index bcb2a12..56aca0f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 7080715..23e53a1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,14 +2,14 @@ apply plugin: 'com.android.application' android { compileSdkVersion 24 - buildToolsVersion "24.0.0" + buildToolsVersion "24.0.1" defaultConfig { applicationId "com.afollestad.nocknock" minSdkVersion 21 targetSdkVersion 24 - versionCode 12 - versionName "0.1.2.2" + versionCode 13 + versionName "0.1.3.0" lintOptions { abortOnError false diff --git a/app/src/main/java/com/afollestad/nocknock/services/CheckService.java b/app/src/main/java/com/afollestad/nocknock/services/CheckService.java index d031ac1..2b9ab94 100644 --- a/app/src/main/java/com/afollestad/nocknock/services/CheckService.java +++ b/app/src/main/java/com/afollestad/nocknock/services/CheckService.java @@ -1,5 +1,6 @@ package com.afollestad.nocknock.services; +import android.app.IntentService; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; @@ -35,7 +36,7 @@ import java.util.Locale; * @author Aidan Follestad (afollestad) */ @SuppressWarnings("CheckResult") -public class CheckService extends Service { +public class CheckService extends IntentService { public static String ACTION_CHECK_UPDATE = BuildConfig.APPLICATION_ID + ".CHECK_UPDATE"; public static String ACTION_RUNNING = BuildConfig.APPLICATION_ID + ".CHECK_RUNNING"; @@ -43,6 +44,10 @@ public class CheckService extends Service { public static String ONLY_WAITING = "only_waiting"; public static int NOTI_ID = 3456; + public CheckService() { + super("NockNockCheckService"); + } + private static void LOG(String msg, Object... format) { if (format != null) msg = String.format(Locale.getDefault(), msg, format); @@ -55,6 +60,83 @@ public class CheckService extends Service { return null; } + @Override + protected void onHandleIntent(Intent intent) { + Inquiry.newInstance(this, MainActivity.DB_NAME).build(); + isRunning(true); + Bridge.config() + .defaultHeader("User-Agent", getString(R.string.app_name) + " (Android)"); + + final Query query = Inquiry.get(this) + .selectFrom(MainActivity.SITES_TABLE_NAME, ServerModel.class); + if (intent != null && intent.hasExtra(MODEL_ID)) { + query.where("_id = ?", intent.getLongExtra(MODEL_ID, -1)); + } else if (intent != null && intent.getBooleanExtra(ONLY_WAITING, false)) { + query.where("status = ?", ServerStatus.WAITING); + } + final ServerModel[] sites = query.all(); + + if (sites == null || sites.length == 0) { + LOG("No sites added to check, service will terminate."); + isRunning(false); + stopSelf(); + return; + } + + LOG("Checking %d sites...", sites.length); + sendBroadcast(new Intent(ACTION_RUNNING)); + + for (ServerModel site : sites) { + LOG("Updating %s (%s) status to WAITING...", site.name, site.url); + site.status = ServerStatus.WAITING; + updateStatus(site); + } + + if (NetworkUtil.hasInternet(this)) { + for (ServerModel site : sites) { + LOG("Checking %s (%s)...", site.name, site.url); + site.status = ServerStatus.CHECKING; + site.lastCheck = System.currentTimeMillis(); + updateStatus(site); + + try { + final Response response = Bridge.get(site.url) + .throwIfNotSuccess() + .cancellable(false) + .request() + .response(); + + site.reason = null; + site.status = ServerStatus.OK; + + if (site.validationMode == ValidationMode.TERM_SEARCH) { + final String body = response.asString(); + if (body == null || !body.contains(site.validationContent)) { + site.status = ServerStatus.ERROR; + site.reason = "Term \"" + site.validationContent + "\" not found in response body."; + } + } else if (site.validationMode == ValidationMode.JAVASCRIPT) { + final String body = response.asString(); + site.reason = JsUtil.exec(site.validationContent, body); + if (site.reason != null && !site.toString().isEmpty()) + site.status = ServerStatus.ERROR; + } + + if (site.status == ServerStatus.ERROR) + showNotification(this, site); + } catch (BridgeException e) { + processError(e, site); + } + updateStatus(site); + } + } else { + LOG("No internet connection, waiting."); + } + + isRunning(false); + LOG("Service is finished!"); + } + private void processError(BridgeException e, ServerModel site) { site.status = ServerStatus.OK; site.reason = null; @@ -147,94 +229,6 @@ public class CheckService extends Service { nm.notify(site.url, NOTI_ID, noti); } - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (isRunning(this)) { - Toast.makeText(this, R.string.already_checking_sites, Toast.LENGTH_SHORT).show(); - stopSelf(); - return START_NOT_STICKY; - } - - Inquiry.newInstance(this, MainActivity.DB_NAME).build(); - isRunning(true); - Bridge.config() - .defaultHeader("User-Agent", getString(R.string.app_name) + " (Android)"); - - new Thread(() -> { - final Query query = Inquiry.get(this) - .selectFrom(MainActivity.SITES_TABLE_NAME, ServerModel.class); - if (intent != null && intent.hasExtra(MODEL_ID)) { - query.where("_id = ?", intent.getLongExtra(MODEL_ID, -1)); - } else if (intent != null && intent.getBooleanExtra(ONLY_WAITING, false)) { - query.where("status = ?", ServerStatus.WAITING); - } - final ServerModel[] sites = query.all(); - - if (sites == null || sites.length == 0) { - LOG("No sites added to check, service will terminate."); - isRunning(false); - stopSelf(); - return; - } - - LOG("Checking %d sites...", sites.length); - sendBroadcast(new Intent(ACTION_RUNNING)); - - for (ServerModel site : sites) { - LOG("Updating %s (%s) status to WAITING...", site.name, site.url); - site.status = ServerStatus.WAITING; - updateStatus(site); - } - - if (NetworkUtil.hasInternet(this)) { - for (ServerModel site : sites) { - LOG("Checking %s (%s)...", site.name, site.url); - site.status = ServerStatus.CHECKING; - site.lastCheck = System.currentTimeMillis(); - updateStatus(site); - - try { - final Response response = Bridge.get(site.url) - .throwIfNotSuccess() - .cancellable(false) - .request() - .response(); - - site.reason = null; - site.status = ServerStatus.OK; - - if (site.validationMode == ValidationMode.TERM_SEARCH) { - final String body = response.asString(); - if (body == null || !body.contains(site.validationContent)) { - site.status = ServerStatus.ERROR; - site.reason = "Term \"" + site.validationContent + "\" not found in response body."; - } - } else if (site.validationMode == ValidationMode.JAVASCRIPT) { - final String body = response.asString(); - site.reason = JsUtil.exec(site.validationContent, body); - if (site.reason != null && !site.toString().isEmpty()) - site.status = ServerStatus.ERROR; - } - - if (site.status == ServerStatus.ERROR) - showNotification(this, site); - } catch (BridgeException e) { - processError(e, site); - } - updateStatus(site); - } - } else { - LOG("No internet connection, waiting."); - } - - isRunning(false); - LOG("Service is finished!"); - stopSelf(); - }).start(); - - return START_STICKY; - } - @Override public void onDestroy() { try { diff --git a/build.gradle b/build.gradle index 317f714..75c86f6 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:2.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 122a0dc..cf2d737 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Dec 28 10:00:20 PST 2015 +#Wed Aug 24 19:51:45 CDT 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip