1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-26 01:28:50 -05:00

Move hasConnectivity() method into a helper method.

It's now used in multiple places, so unify the logic into one place.
This commit is contained in:
Andrew Chen 2012-10-01 10:37:51 -07:00
parent 48faef808f
commit 41756802a1
3 changed files with 32 additions and 24 deletions

View File

@ -19,8 +19,6 @@ import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.text.Spannable;
@ -896,14 +894,7 @@ public class MessageList extends K9ListActivity implements OnItemClickListener,
// Check if we have connectivity. Cache the value.
if (mHasConnectivity == null) {
final ConnectivityManager connectivityManager =
(ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
mHasConnectivity = true;
} else {
mHasConnectivity = false;
}
mHasConnectivity = Utility.hasConnectivity(getApplication());
}
if (mQueryString == null) {

View File

@ -1,7 +1,11 @@
package com.fsck.k9.helper;
import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.Editable;
import android.util.Log;
import android.widget.EditText;
@ -643,4 +647,23 @@ public class Utility {
public static String sanitizeFilename(String filename) {
return filename.replaceAll(INVALID_CHARACTERS, REPLACEMENT_CHARACTER);
}
/**
* Check to see if we have network connectivity.
* @param app Current application (Hint: see if your base class has a getApplication() method.)
* @return true if we have connectivity, false otherwise.
*/
public static boolean hasConnectivity(final Application app) {
final ConnectivityManager connectivityManager =
(ConnectivityManager) app.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
}
final NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}
}

View File

@ -9,8 +9,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.IBinder;
import android.util.Log;
@ -19,6 +17,7 @@ import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.Account.FolderMode;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Pusher;
public class MailService extends CoreService {
@ -89,20 +88,17 @@ public class MailService extends CoreService {
boolean oldIsSyncDisabled = isSyncDisabled();
ConnectivityManager connectivityManager = (ConnectivityManager)getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
boolean doBackground = true;
boolean hasConnectivity = false;
boolean backgroundData = false;
final boolean hasConnectivity = Utility.hasConnectivity(getApplication());
if (connectivityManager != null) {
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
if (netInfo != null) {
State state = netInfo.getState();
hasConnectivity = state == State.CONNECTED;
}
boolean backgroundData = connectivityManager.getBackgroundDataSetting();
boolean autoSync = ContentResolver.getMasterSyncAutomatically();
backgroundData = connectivityManager.getBackgroundDataSetting();
}
boolean autoSync = ContentResolver.getMasterSyncAutomatically();
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
switch (bOps) {
switch (bOps) {
case NEVER:
doBackground = false;
break;
@ -115,8 +111,6 @@ public class MailService extends CoreService {
case WHEN_CHECKED_AUTO_SYNC:
doBackground = backgroundData & autoSync;
break;
}
}
syncBlocked = !(doBackground && hasConnectivity);