Merge remote-tracking branch 'origin/master'

# Conflicts:
#	app/build.gradle
This commit is contained in:
Aidan Follestad 2016-09-02 15:05:06 -05:00
commit 680f6b2931
9 changed files with 100 additions and 101 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

@ -4,7 +4,7 @@ android:
components:
- tools
- platform-tools
- build-tools-24.0.0
- build-tools-24.0.1
- android-24
- extra-android-support
- extra-android-m2repository

BIN
NockNock-0.1.3.0.apk Normal file

Binary file not shown.

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

@ -34,7 +34,9 @@ public class AddSiteActivity extends AppCompatActivity implements View.OnClickLi
private View rootLayout;
private Toolbar toolbar;
private TextInputLayout nameTiLayout;
private EditText inputName;
private TextInputLayout urlTiLayout;
private EditText inputUrl;
private EditText inputInterval;
private Spinner spinnerInterval;
@ -49,7 +51,9 @@ public class AddSiteActivity extends AppCompatActivity implements View.OnClickLi
setContentView(R.layout.activity_addsite);
rootLayout = findViewById(R.id.rootView);
nameTiLayout = (TextInputLayout) findViewById(R.id.nameTiLayout);
inputName = (EditText) findViewById(R.id.inputName);
urlTiLayout = (TextInputLayout) findViewById(R.id.urlTiLayout);
inputUrl = (EditText) findViewById(R.id.inputUrl);
textUrlWarning = (TextView) findViewById(R.id.textUrlWarning);
inputInterval = (EditText) findViewById(R.id.checkIntervalInput);
@ -183,22 +187,21 @@ public class AddSiteActivity extends AppCompatActivity implements View.OnClickLi
model.status = ServerStatus.WAITING;
if (model.name.isEmpty()) {
((TextInputLayout) inputName.getParent()).setError(getString(R.string.please_enter_name));
nameTiLayout.setError(getString(R.string.please_enter_name));
isClosing = false;
return;
} else {
((TextInputLayout) inputName.getParent()).setError(null);
nameTiLayout.setError(null);
}
if (model.url.isEmpty()) {
((TextInputLayout) inputUrl.getParent()).setError(getString(R.string.please_enter_url));
urlTiLayout.setError(getString(R.string.please_enter_url));
isClosing = false;
return;
} else {
final TextInputLayout urlTl = (TextInputLayout) inputUrl.getParent();
urlTl.setError(null);
urlTiLayout.setError(null);
if (!Patterns.WEB_URL.matcher(model.url).find()) {
urlTl.setError(getString(R.string.please_enter_valid_url));
urlTiLayout.setError(getString(R.string.please_enter_valid_url));
isClosing = false;
return;
} else {

View file

@ -29,6 +29,7 @@
android:paddingRight="@dimen/content_inset">
<android.support.design.widget.TextInputLayout
android:id="@+id/nameTiLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
@ -49,6 +50,7 @@
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/urlTiLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"

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