diff options
author | Andrew Dolgov <[email protected]> | 2013-05-21 16:26:47 +0400 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2013-05-21 16:26:47 +0400 |
commit | 287392e0dbc7c6b016446721f10d0a9d47496e3e (patch) | |
tree | a3c643ead695b4a013b2a3a2b075e6fe0fce7d5e | |
parent | 4f49ae38dd06974ada031153569b5fff64e8afb3 (diff) |
add 1x1 simple widget
-rw-r--r-- | AndroidManifest.xml | 24 | ||||
-rw-r--r-- | res/layout/loading_fragment.xml | 2 | ||||
-rw-r--r-- | res/layout/widget_small.xml | 42 | ||||
-rw-r--r-- | res/xml/widget_small.xml | 8 | ||||
-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 |
7 files changed, 279 insertions, 6 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 3d6ebe8b..9f2ce335 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -34,8 +34,8 @@ </activity> <activity android:name=".FeedsActivity" - android:uiOptions="splitActionBarWhenNarrow" - android:label="@string/app_name" > + android:label="@string/app_name" + android:uiOptions="splitActionBarWhenNarrow" > </activity> <activity android:name=".HeadlinesActivity" @@ -51,8 +51,8 @@ </activity> <activity android:name=".offline.OfflineFeedsActivity" - android:uiOptions="splitActionBarWhenNarrow" - android:label="@string/app_name" > + android:label="@string/app_name" + android:uiOptions="splitActionBarWhenNarrow" > </activity> <activity android:name=".offline.OfflineHeadlinesActivity" @@ -188,6 +188,22 @@ <meta-data android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIwG6zsGB4qo6ZhjfwIJpm9WI7AqmWaoRXm6ZJnA" /> + + <receiver android:name=".widget.SmallWidgetProvider" > + <intent-filter> + <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> + <action android:name="org.fox.ttrss.WIDGET_FORCE_UPDATE" /> + </intent-filter> + + <meta-data + android:name="android.appwidget.provider" + android:resource="@xml/widget_small" /> + </receiver> + + <service + android:name=".widget.WidgetUpdateService" + android:enabled="true" /> + </application> </manifest>
\ No newline at end of file diff --git a/res/layout/loading_fragment.xml b/res/layout/loading_fragment.xml index 352662e6..bc1fd10d 100644 --- a/res/layout/loading_fragment.xml +++ b/res/layout/loading_fragment.xml @@ -5,7 +5,7 @@ <ProgressBar
- android:id="@+id/progressBar1"
+ android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
diff --git a/res/layout/widget_small.xml b/res/layout/widget_small.xml new file mode 100644 index 00000000..e0948a11 --- /dev/null +++ b/res/layout/widget_small.xml @@ -0,0 +1,42 @@ +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/widget_main"
+ android:layout_width="match_parent"
+ android:layout_height="fill_parent" >
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" >
+
+ <ImageView
+ android:id="@+id/imageView1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:paddingTop="4dp"
+ android:src="@drawable/icon" />
+
+ <TextView
+ android:id="@+id/counter"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:gravity="center"
+ android:text="-1"
+ android:shadowColor="#cc000000"
+ android:shadowDx="0"
+ android:shadowDy="3"
+ android:shadowRadius="3"
+ android:textAppearance="?android:attr/textAppearanceSmall"
+ android:textColor="@android:color/primary_text_dark" />
+
+ </LinearLayout>
+
+ <ProgressBar
+ android:id="@+id/progress"
+ style="?android:attr/progressBarStyleLarge"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:visibility="gone" />
+
+</FrameLayout>
\ No newline at end of file diff --git a/res/xml/widget_small.xml b/res/xml/widget_small.xml new file mode 100644 index 00000000..2ad3a638 --- /dev/null +++ b/res/xml/widget_small.xml @@ -0,0 +1,8 @@ +<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:minWidth="40dp"
+ android:minHeight="40dp"
+ android:updatePeriodMillis="86400000"
+ android:initialLayout="@layout/widget_small"
+ android:resizeMode="horizontal|vertical"
+ android:widgetCategory="home_screen">
+</appwidget-provider>
\ No newline at end of file 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);
+ }
+
+ }
+}
|