summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java
blob: 6162abab8142655fa949de37e4b5aa7aea4f8c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
		}
	} 
	
}