From 1fc24f1e375e363a9bed25892aeef381c63518f6 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 30 May 2017 13:59:55 +0300 Subject: [PATCH] try updating widget several times if no network connection detected --- .../fox/ttrss/widget/WidgetUpdateService.java | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java index d0f329ea..5329e744 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java @@ -4,6 +4,8 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.Build; import android.os.Handler; import android.os.IBinder; @@ -29,8 +31,6 @@ public class WidgetUpdateService extends Service { public static final int UPDATE_RESULT_ERROR_NEED_CONF = 3; public static final int UPDATE_IN_PROGRESS = 4; - private int m_updateFailures = 0; - @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); @@ -38,21 +38,49 @@ public class WidgetUpdateService extends Service { return null; } + protected boolean isNetworkAvailable() { + ConnectivityManager cm = (ConnectivityManager) + getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = cm.getActiveNetworkInfo(); + + // if no network is available networkInfo will be null + // otherwise check if we are connected + if (networkInfo != null && networkInfo.isConnected()) { + return true; + } + return false; + } + @Override public void onStart(Intent intent, int startId) { Log.d(TAG, "onStart"); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); - if (pm.isDeviceIdleMode()) { - Log.d(TAG, "device in idle mode, bailing out"); - return; - } - } - try { sendResultIntent(-1, UPDATE_IN_PROGRESS); + if (!isNetworkAvailable()) { + final int retryCount = intent.getIntExtra("retryCount", 0); + + Log.d(TAG, "service update requested but network is not available, try: " + retryCount); + + if (retryCount < 10) { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + Intent serviceIntent = new Intent(getApplicationContext(), WidgetUpdateService.class); + serviceIntent.putExtra("retryCount", retryCount + 1); + startService(serviceIntent); + + } + }, 3 * 1000); + } else { + sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); + } + + stopSelf(); + return; + } + m_prefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); @@ -77,7 +105,6 @@ public class WidgetUpdateService extends Service { JsonObject content = result.getAsJsonObject(); if (content != null) { - m_updateFailures = 0; int unread = content.get("unread").getAsInt(); sendResultIntent(unread, UPDATE_RESULT_OK); @@ -91,7 +118,6 @@ public class WidgetUpdateService extends Service { Log.d(TAG, "request failed: " + getErrorMessage()); } - ++m_updateFailures; sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); } }; @@ -113,7 +139,6 @@ public class WidgetUpdateService extends Service { protected void onLoginFailed(int requestId, ApiRequest ar) { Log.d(TAG, "login failed: " + getString(ar.getErrorMessage())); - ++m_updateFailures; sendResultIntent(-1, UPDATE_RESULT_ERROR_LOGIN); } @@ -124,21 +149,15 @@ public class WidgetUpdateService extends Service { } }; - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - String login = m_prefs.getString("login", "").trim(); - String password = m_prefs.getString("password", "").trim(); + String login = m_prefs.getString("login", "").trim(); + String password = m_prefs.getString("password", "").trim(); - loginManager.logIn(getApplicationContext(), 1, login, password); - } - }, 2 * 1000); + loginManager.logIn(getApplicationContext(), 1, login, password); } } catch (Exception e) { e.printStackTrace(); - ++m_updateFailures; sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); } @@ -152,7 +171,6 @@ public class WidgetUpdateService extends Service { intent.setAction(SmallWidgetProvider.ACTION_UPDATE_RESULT); intent.putExtra("resultCode", resultCode); intent.putExtra("unread", unread); - intent.putExtra("failures", m_updateFailures); sendBroadcast(intent);