From 41756802a1966bfba7deadd6b9dbe280771fbe73 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Mon, 1 Oct 2012 10:37:51 -0700 Subject: [PATCH] Move hasConnectivity() method into a helper method. It's now used in multiple places, so unify the logic into one place. --- src/com/fsck/k9/activity/MessageList.java | 11 +---------- src/com/fsck/k9/helper/Utility.java | 23 +++++++++++++++++++++++ src/com/fsck/k9/service/MailService.java | 22 ++++++++-------------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index a1254de62..ca5d3f261 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -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) { diff --git a/src/com/fsck/k9/helper/Utility.java b/src/com/fsck/k9/helper/Utility.java index 27ec953b0..113032d34 100644 --- a/src/com/fsck/k9/helper/Utility.java +++ b/src/com/fsck/k9/helper/Utility.java @@ -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; + } + } } diff --git a/src/com/fsck/k9/service/MailService.java b/src/com/fsck/k9/service/MailService.java index 289b5eb2e..bb3a84d3a 100644 --- a/src/com/fsck/k9/service/MailService.java +++ b/src/com/fsck/k9/service/MailService.java @@ -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);