diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/org/fox/ttrss/OnlineActivity.java | 13 | ||||
-rw-r--r-- | src/org/fox/ttrss/widget/SmallWidgetProvider.java | 65 | ||||
-rw-r--r-- | src/org/fox/ttrss/widget/WidgetUpdateService.java | 131 |
3 files changed, 208 insertions, 1 deletions
diff --git a/src/org/fox/ttrss/OnlineActivity.java b/src/org/fox/ttrss/OnlineActivity.java index c3696d02..033b119d 100644 --- a/src/org/fox/ttrss/OnlineActivity.java +++ b/src/org/fox/ttrss/OnlineActivity.java @@ -12,12 +12,15 @@ import org.fox.ttrss.types.Article; import org.fox.ttrss.types.ArticleList; import org.fox.ttrss.types.Feed; import org.fox.ttrss.types.Label; +import org.fox.ttrss.widget.SmallWidgetProvider; import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.Dialog; +import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; @@ -303,9 +306,17 @@ public class OnlineActivity extends CommonActivity { } @Override + public void onStop() { + super.onStop(); + + Intent initialUpdateIntent = new Intent(SmallWidgetProvider.FORCE_UPDATE_ACTION); + sendBroadcast(initialUpdateIntent); + } + + @Override public void onPause() { super.onPause(); - + unregisterReceiver(m_broadcastReceiver); } diff --git a/src/org/fox/ttrss/widget/SmallWidgetProvider.java b/src/org/fox/ttrss/widget/SmallWidgetProvider.java new file mode 100644 index 00000000..dbbe718c --- /dev/null +++ b/src/org/fox/ttrss/widget/SmallWidgetProvider.java @@ -0,0 +1,65 @@ +package org.fox.ttrss.widget;
+
+import org.fox.ttrss.R;
+
+import android.app.PendingIntent;
+import android.app.PendingIntent.CanceledException;
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+import android.widget.RemoteViews;
+
+public class SmallWidgetProvider extends AppWidgetProvider {
+ private final String TAG = this.getClass().getSimpleName();
+
+ public static final String FORCE_UPDATE_ACTION = "org.fox.ttrss.WIDGET_FORCE_UPDATE";
+
+ @Override
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
+ //RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small);
+
+ final int N = appWidgetIds.length;
+
+ for (int i=0; i < N; i++) {
+ int appWidgetId = appWidgetIds[i];
+
+ Intent updateIntent = new Intent(context, org.fox.ttrss.widget.WidgetUpdateService.class);
+ PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, updateIntent, 0);
+
+ Intent intent = new Intent(context, org.fox.ttrss.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);
+
+ appWidgetManager.updateAppWidget(appWidgetId, views);
+
+ try {
+ updatePendingIntent.send();
+ } catch (CanceledException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ super.onReceive(context, intent);
+
+ if (FORCE_UPDATE_ACTION.equals(intent.getAction())) {
+
+ AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
+ ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName());
+ int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
+
+ onUpdate(context, appWidgetManager, appWidgetIds);
+ }
+ }
+
+}
diff --git a/src/org/fox/ttrss/widget/WidgetUpdateService.java b/src/org/fox/ttrss/widget/WidgetUpdateService.java new file mode 100644 index 00000000..68a61e4d --- /dev/null +++ b/src/org/fox/ttrss/widget/WidgetUpdateService.java @@ -0,0 +1,131 @@ +package org.fox.ttrss.widget;
+
+import java.util.HashMap;
+
+import org.fox.ttrss.ApiRequest;
+import org.fox.ttrss.R;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.IBinder;
+import android.preference.PreferenceManager;
+import android.util.Log;
+import android.view.View;
+import android.widget.RemoteViews;
+
+public class WidgetUpdateService extends Service {
+ private final String TAG = this.getClass().getSimpleName();
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ Log.d(TAG, "onBind");
+
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /* @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ Log.d(TAG, "onStartCommand");
+
+ return super.onStartCommand(intent, flags, startId);
+ } */
+
+ public void update() {
+
+
+ }
+
+ @Override
+ public void onStart(Intent intent, int startId) {
+ final RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_small);
+
+ view.setTextViewText(R.id.counter, String.valueOf(""));
+ view.setViewVisibility(R.id.progress, View.VISIBLE);
+
+ final ComponentName thisWidget = new ComponentName(this, SmallWidgetProvider.class);
+ final AppWidgetManager manager = AppWidgetManager.getInstance(this);
+
+ manager.updateAppWidget(thisWidget, view);
+
+ final SharedPreferences m_prefs = PreferenceManager
+ .getDefaultSharedPreferences(getApplicationContext());
+
+ if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
+
+ // Toast: need configure
+
+ } else {
+
+ ApiRequest ar = new ApiRequest(getApplicationContext()) {
+ @SuppressWarnings({ "unchecked", "serial" })
+ @Override
+ protected void onPostExecute(JsonElement result) {
+ if (result != null) {
+ JsonObject content = result.getAsJsonObject();
+
+ if (content != null) {
+ final String sessionId = content.get("session_id").getAsString();
+
+ ApiRequest aru = new ApiRequest(getApplicationContext()) {
+ @Override
+ protected void onPostExecute(JsonElement result) {
+ if (result != null) {
+ JsonObject content = result.getAsJsonObject();
+
+ if (content != null) {
+ int unread = content.get("unread").getAsInt();
+
+ view.setViewVisibility(R.id.progress, View.GONE);
+ view.setTextViewText(R.id.counter, String.valueOf(unread));
+ manager.updateAppWidget(thisWidget, view);
+
+ return;
+ }
+ }
+
+ view.setViewVisibility(R.id.progress, View.GONE);
+ view.setTextViewText(R.id.counter, getString(R.string.app_name));
+ manager.updateAppWidget(thisWidget, view);
+ }
+ };
+
+ HashMap<String, String> umap = new HashMap<String, String>() {
+ {
+ put("op", "getUnread");
+ put("sid", sessionId);
+ }
+ };
+
+ aru.execute(umap);
+ return;
+ }
+ }
+
+ // Toast: login failed
+
+ view.setViewVisibility(R.id.progress, View.GONE);
+ view.setTextViewText(R.id.counter, getString(R.string.app_name));
+ manager.updateAppWidget(thisWidget, view);
+ };
+ };
+
+ HashMap<String, String> map = new HashMap<String, String>() {
+ {
+ put("op", "login");
+ put("user", m_prefs.getString("login", "").trim());
+ put("password", m_prefs.getString("password", "").trim());
+ }
+ };
+
+ ar.execute(map);
+ }
+
+ }
+}
|