mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-08-04 15:18:40 +00:00
Switched the service to an IntentService so checks are queued
This commit is contained in:
parent
5e1253b13d
commit
ec76b96a44
6 changed files with 91 additions and 97 deletions
2
.idea/.name
generated
2
.idea/.name
generated
|
@ -1 +1 @@
|
||||||
Nock Nock
|
nock-nock
|
2
.idea/modules.xml
generated
2
.idea/modules.xml
generated
|
@ -2,8 +2,8 @@
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<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$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/nock-nock.iml" filepath="$PROJECT_DIR$/nock-nock.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -2,14 +2,14 @@ apply plugin: 'com.android.application'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 24
|
compileSdkVersion 24
|
||||||
buildToolsVersion "24.0.0"
|
buildToolsVersion "24.0.1"
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.afollestad.nocknock"
|
applicationId "com.afollestad.nocknock"
|
||||||
minSdkVersion 21
|
minSdkVersion 21
|
||||||
targetSdkVersion 24
|
targetSdkVersion 24
|
||||||
versionCode 12
|
versionCode 13
|
||||||
versionName "0.1.2.2"
|
versionName "0.1.3.0"
|
||||||
|
|
||||||
lintOptions {
|
lintOptions {
|
||||||
abortOnError false
|
abortOnError false
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.afollestad.nocknock.services;
|
package com.afollestad.nocknock.services;
|
||||||
|
|
||||||
|
import android.app.IntentService;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
@ -35,7 +36,7 @@ import java.util.Locale;
|
||||||
* @author Aidan Follestad (afollestad)
|
* @author Aidan Follestad (afollestad)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("CheckResult")
|
@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_CHECK_UPDATE = BuildConfig.APPLICATION_ID + ".CHECK_UPDATE";
|
||||||
public static String ACTION_RUNNING = BuildConfig.APPLICATION_ID + ".CHECK_RUNNING";
|
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 String ONLY_WAITING = "only_waiting";
|
||||||
public static int NOTI_ID = 3456;
|
public static int NOTI_ID = 3456;
|
||||||
|
|
||||||
|
public CheckService() {
|
||||||
|
super("NockNockCheckService");
|
||||||
|
}
|
||||||
|
|
||||||
private static void LOG(String msg, Object... format) {
|
private static void LOG(String msg, Object... format) {
|
||||||
if (format != null)
|
if (format != null)
|
||||||
msg = String.format(Locale.getDefault(), msg, format);
|
msg = String.format(Locale.getDefault(), msg, format);
|
||||||
|
@ -55,6 +60,83 @@ public class CheckService extends Service {
|
||||||
return null;
|
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) {
|
private void processError(BridgeException e, ServerModel site) {
|
||||||
site.status = ServerStatus.OK;
|
site.status = ServerStatus.OK;
|
||||||
site.reason = null;
|
site.reason = null;
|
||||||
|
@ -147,94 +229,6 @@ public class CheckService extends Service {
|
||||||
nm.notify(site.url, NOTI_ID, noti);
|
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
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -5,7 +5,7 @@ buildscript {
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
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
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,6 +1,6 @@
|
||||||
#Mon Dec 28 10:00:20 PST 2015
|
#Wed Aug 24 19:51:45 CDT 2016
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue