diff options
Diffstat (limited to 'org.fox.ttrss')
4 files changed, 70 insertions, 111 deletions
diff --git a/org.fox.ttrss/src/main/AndroidManifest.xml b/org.fox.ttrss/src/main/AndroidManifest.xml index 9a62d97e..4b5510bd 100755 --- a/org.fox.ttrss/src/main/AndroidManifest.xml +++ b/org.fox.ttrss/src/main/AndroidManifest.xml @@ -227,7 +227,6 @@ <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="org.fox.ttrss.WIDGET_FORCE_UPDATE" /> - <action android:name="org.fox.ttrss.WIDGET_UPDATE_RESULT" /> </intent-filter> <meta-data @@ -237,7 +236,7 @@ <service android:name=".widget.WidgetUpdateService" - android:enabled="true" /> + android:permission="android.permission.BIND_JOB_SERVICE" /> <service android:name=".DashClock" android:icon="@drawable/dashclock" diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java index a7eb8fb2..958e53ca 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java @@ -12,6 +12,7 @@ import android.os.Handler; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; +import android.support.v4.app.JobIntentService; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; @@ -26,7 +27,7 @@ import org.fox.ttrss.types.Article; import org.fox.ttrss.types.ArticleList; import org.fox.ttrss.types.Feed; import org.fox.ttrss.types.FeedCategory; -import org.fox.ttrss.widget.SmallWidgetProvider; +import org.fox.ttrss.widget.WidgetUpdateService; import java.util.Date; import java.util.HashMap; @@ -490,8 +491,7 @@ public class MasterActivity extends OnlineActivity implements HeadlinesEventList if (isFinishing() || date.getTime() - m_lastWidgetRefresh > 60*1000) { m_lastWidgetRefresh = date.getTime(); - Intent updateWidgetIntent = new Intent(SmallWidgetProvider.ACTION_REQUEST_UPDATE); - sendBroadcast(updateWidgetIntent); + JobIntentService.enqueueWork(getApplicationContext(), WidgetUpdateService.class, 0, new Intent()); } } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java index e0daecad..95d66289 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java @@ -8,6 +8,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.support.v4.app.JobIntentService; import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -19,7 +20,6 @@ public class SmallWidgetProvider extends AppWidgetProvider { private final String TAG = this.getClass().getSimpleName(); public static final String ACTION_REQUEST_UPDATE = "org.fox.ttrss.WIDGET_FORCE_UPDATE"; - public static final String ACTION_UPDATE_RESULT = "org.fox.ttrss.WIDGET_UPDATE_RESULT"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { @@ -49,12 +49,9 @@ public class SmallWidgetProvider extends AppWidgetProvider { appWidgetManager.updateAppWidget(appWidgetIds, views); - Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetUpdateService.class); - context.startService(serviceIntent); - + JobIntentService.enqueueWork(context.getApplicationContext(), WidgetUpdateService.class, 0, new Intent()); } - @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive: " + intent); @@ -63,47 +60,7 @@ public class SmallWidgetProvider extends AppWidgetProvider { ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName()); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); - if (ACTION_REQUEST_UPDATE.equals(intent.getAction())) { - Log.d(TAG, "onReceive: got update request"); - - onUpdate(context, appWidgetManager, appWidgetIds); - - } else if (ACTION_UPDATE_RESULT.equals(intent.getAction())) { - int unread = intent.getIntExtra("unread", -1); - int resultCode = intent.getIntExtra("resultCode", WidgetUpdateService.UPDATE_RESULT_ERROR_OTHER); - - Log.d(TAG, "onReceive: got update result from service: " + unread + " " + resultCode); - - updateWidgetsText(context, appWidgetManager, appWidgetIds, unread, resultCode); - } else { - super.onReceive(context, intent); - } + onUpdate(context, appWidgetManager, appWidgetIds); } - private void updateWidgetsText(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, int unread, int resultCode) { - - Intent intent = new Intent(context, OnlineActivity.class); - PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); - - RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small); - views.setOnClickPendingIntent(R.id.widget_main, pendingIntent); - - String viewText; - - switch (resultCode) { - case WidgetUpdateService.UPDATE_RESULT_OK: - viewText = String.valueOf(unread); - break; - case WidgetUpdateService.UPDATE_IN_PROGRESS: - viewText = "..."; - break; - default: - viewText = "?"; - } - - views.setTextViewText(R.id.widget_unread_counter, viewText); - - appWidgetManager.updateAppWidget(appWidgetIds, views); - } - } 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 c4d01066..ca0b4090 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 @@ -1,7 +1,8 @@ package org.fox.ttrss.widget; -import android.app.Notification; -import android.app.Service; +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -9,22 +10,23 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.os.Handler; -import android.os.IBinder; import android.preference.PreferenceManager; -import android.support.v4.app.NotificationCompat; +import android.support.annotation.NonNull; +import android.support.v4.app.JobIntentService; import android.util.Log; +import android.widget.RemoteViews; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.fox.ttrss.ApiRequest; -import org.fox.ttrss.CommonActivity; +import org.fox.ttrss.OnlineActivity; import org.fox.ttrss.R; import org.fox.ttrss.util.SimpleLoginManager; import java.util.HashMap; -public class WidgetUpdateService extends Service { +public class WidgetUpdateService extends JobIntentService { private final String TAG = this.getClass().getSimpleName(); private SharedPreferences m_prefs; @@ -35,44 +37,12 @@ public class WidgetUpdateService extends Service { public static final int UPDATE_IN_PROGRESS = 4; @Override - public void onCreate() { - super.onCreate(); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationCompat.Builder nb = new NotificationCompat.Builder(getApplicationContext()) - .setOngoing(true) - .setSmallIcon(R.drawable.ic_launcher) - .setPriority(Notification.PRIORITY_MIN) - .setCategory(Notification.CATEGORY_SERVICE) - .setChannelId(CommonActivity.NOTIFICATION_CHANNEL_NORMAL); - - startForeground(1, nb.build()); - } - } - - @Override - public IBinder onBind(Intent intent) { - Log.d(TAG, "onBind"); + protected void onHandleWork(@NonNull Intent intent) { - 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 - return networkInfo != null && networkInfo.isConnected(); - } - - @Override - public void onStart(Intent intent, int startId) { - Log.d(TAG, "onStart"); + Log.d(TAG, "onHandleWork: " + intent); try { - sendResultIntent(-1, UPDATE_IN_PROGRESS); + updateWidgets(-1, UPDATE_IN_PROGRESS); if (!isNetworkAvailable()) { final int retryCount = intent.getIntExtra("retryCount", 0); @@ -95,7 +65,7 @@ public class WidgetUpdateService extends Service { } }, 3 * 1000); } else { - sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); + updateWidgets(-1, UPDATE_RESULT_ERROR_OTHER); } stopSelf(); @@ -107,12 +77,10 @@ public class WidgetUpdateService extends Service { if (m_prefs.getString("ttrss_url", "").trim().length() == 0) { - sendResultIntent(-1, UPDATE_RESULT_ERROR_NEED_CONF); + updateWidgets(-1, UPDATE_RESULT_ERROR_NEED_CONF); } else { - Log.d(TAG, "starting update..."); - final int feedId = m_prefs.getBoolean("widget_show_fresh", true) ? -3 : 0; final SimpleLoginManager loginManager = new SimpleLoginManager() { @@ -120,14 +88,10 @@ public class WidgetUpdateService extends Service { @Override protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) { - Log.d(TAG, "onLoginSuccess"); - ApiRequest aru = new ApiRequest(getApplicationContext()) { @Override protected void onPostExecute(JsonElement result) { - Log.d(TAG, "got result" + result); - if (result != null) { try { JsonObject content = result.getAsJsonObject(); @@ -135,7 +99,7 @@ public class WidgetUpdateService extends Service { if (content != null) { int unread = content.get("unread").getAsInt(); - sendResultIntent(unread, UPDATE_RESULT_OK); + updateWidgets(unread, UPDATE_RESULT_OK); return; } @@ -146,7 +110,7 @@ public class WidgetUpdateService extends Service { Log.d(TAG, "request failed: " + getErrorMessage()); } - sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); + updateWidgets(-1, UPDATE_RESULT_ERROR_OTHER); } }; @@ -167,7 +131,7 @@ public class WidgetUpdateService extends Service { protected void onLoginFailed(int requestId, ApiRequest ar) { Log.d(TAG, "login failed: " + getString(ar.getErrorMessage())); - sendResultIntent(-1, UPDATE_RESULT_ERROR_LOGIN); + updateWidgets(-1, UPDATE_RESULT_ERROR_LOGIN); } @Override @@ -186,22 +150,61 @@ public class WidgetUpdateService extends Service { } catch (Exception e) { e.printStackTrace(); - sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER); + updateWidgets(-1, UPDATE_RESULT_ERROR_OTHER); } stopSelf(); - super.onStart(intent, startId); } - public void sendResultIntent(int unread, int resultCode) { - Intent intent = new Intent(); - intent.setAction(SmallWidgetProvider.ACTION_UPDATE_RESULT); - intent.putExtra("resultCode", resultCode); - intent.putExtra("unread", unread); + 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 + return networkInfo != null && networkInfo.isConnected(); + } + + public void updateWidgets(int unread, int resultCode) { + Log.d(TAG, "updateWidgets:" + unread + " " + resultCode); + + Context context = getApplicationContext(); + + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName()); + int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); - sendBroadcast(intent); + updateWidgetsText(context, appWidgetManager, appWidgetIds, unread, resultCode); if (resultCode != UPDATE_IN_PROGRESS) stopSelf(); } + + private void updateWidgetsText(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, int unread, int resultCode) { + + Intent intent = new Intent(context, OnlineActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); + + RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small); + views.setOnClickPendingIntent(R.id.widget_main, pendingIntent); + + String viewText; + + switch (resultCode) { + case WidgetUpdateService.UPDATE_RESULT_OK: + viewText = String.valueOf(unread); + break; + case WidgetUpdateService.UPDATE_IN_PROGRESS: + viewText = "..."; + break; + default: + viewText = "?"; + } + + views.setTextViewText(R.id.widget_unread_counter, viewText); + + appWidgetManager.updateAppWidget(appWidgetIds, views); + } + } |