add 1x1 simple widget

This commit is contained in:
Andrew Dolgov 2013-05-21 16:26:47 +04:00
parent 4f49ae38dd
commit 287392e0db
7 changed files with 279 additions and 6 deletions

View File

@ -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>

View File

@ -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"

View File

@ -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>

8
res/xml/widget_small.xml Normal file
View File

@ -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>

View File

@ -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;
@ -302,10 +305,18 @@ public class OnlineActivity extends CommonActivity {
return false;
}
@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);
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}