If there is no internet, app will wait until internet is connected to perform full checks.

This commit is contained in:
Aidan Follestad 2016-08-01 15:50:58 -05:00
parent 6f6c649241
commit 2e5303d31e
5 changed files with 75 additions and 18 deletions

View file

@ -43,12 +43,18 @@
android:enabled="true"
android:label="Site Check Service" />
<receiver android:name=".services.BootReceiver">
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>

View file

@ -1,4 +1,4 @@
package com.afollestad.nocknock.services;
package com.afollestad.nocknock.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;

View file

@ -0,0 +1,24 @@
package com.afollestad.nocknock.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.afollestad.nocknock.services.CheckService;
import com.afollestad.nocknock.util.NetworkUtil;
/**
* @author Aidan Follestad (afollestad)
*/
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final boolean hasInternet = NetworkUtil.hasInternet(context);
Log.v("ConnectivityReceiver", "Connectivity state changed... has internet? " + hasInternet);
if (hasInternet)
context.startService(new Intent(context, CheckService.class)
.putExtra(CheckService.ONLY_WAITING, true));
}
}

View file

@ -24,6 +24,7 @@ import com.afollestad.nocknock.api.ServerModel;
import com.afollestad.nocknock.api.ServerStatus;
import com.afollestad.nocknock.ui.MainActivity;
import com.afollestad.nocknock.ui.ViewSiteActivity;
import com.afollestad.nocknock.util.NetworkUtil;
import java.util.Locale;
@ -35,6 +36,7 @@ public class CheckService extends Service {
public static String ACTION_CHECK_UPDATE = BuildConfig.APPLICATION_ID + ".CHECK_UPDATE";
public static String ACTION_RUNNING = BuildConfig.APPLICATION_ID + ".CHECK_RUNNING";
public static String MODEL_ID = "model_id";
public static String ONLY_WAITING = "only_waiting";
public static int NOTI_ID = 3456;
private static void LOG(String msg, Object... format) {
@ -157,8 +159,11 @@ public class CheckService extends Service {
new Thread(() -> {
final Query<ServerModel, Integer> query = Inquiry.get()
.selectFrom(MainActivity.SITES_TABLE_NAME, ServerModel.class);
if (intent != null && intent.hasExtra(MODEL_ID))
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) {
@ -177,23 +182,27 @@ public class CheckService extends Service {
updateStatus(site);
}
for (ServerModel site : sites) {
LOG("Checking %s (%s)...", site.name, site.url);
site.status = ServerStatus.CHECKING;
site.lastCheck = System.currentTimeMillis();
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 {
Bridge.get(site.url)
.throwIfNotSuccess()
.cancellable(false)
.request();
site.reason = null;
site.status = ServerStatus.OK;
} catch (BridgeException e) {
processError(e, site);
try {
Bridge.get(site.url)
.throwIfNotSuccess()
.cancellable(false)
.request();
site.reason = null;
site.status = ServerStatus.OK;
} catch (BridgeException e) {
processError(e, site);
}
updateStatus(site);
}
updateStatus(site);
} else {
LOG("No internet connection, waiting.");
}
isRunning(false);

View file

@ -0,0 +1,18 @@
package com.afollestad.nocknock.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* @author Aidan Follestad (afollestad)
*/
public class NetworkUtil {
public static boolean hasInternet(Context context) {
final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
}