Switched the service to an IntentService so checks are queued

This commit is contained in:
Aidan Follestad 2016-08-24 20:01:55 -05:00
parent 5e1253b13d
commit ec76b96a44
6 changed files with 91 additions and 97 deletions

2
.idea/.name generated
View file

@ -1 +1 @@
Nock Nock
nock-nock

2
.idea/modules.xml generated
View file

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/NockNock.iml" filepath="$PROJECT_DIR$/NockNock.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/nock-nock.iml" filepath="$PROJECT_DIR$/nock-nock.iml" />
</modules>
</component>
</project>

View file

@ -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

View file

@ -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<ServerModel, Integer> 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<ServerModel, Integer> 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 {

View file

@ -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

View file

@ -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