summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java
blob: ddc602cd411d2132d2dcded21f82175dfcee2201 (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
66
67
68
69
70
71
72
package org.fox.ttrss.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
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;

import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.R;

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

    @Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate");

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

        SharedPreferences prefs  = PreferenceManager.getDefaultSharedPreferences(context);
        String widgetBackground = prefs.getString("widget_background", "WB_LIGHT");

        Log.d(TAG, "widget bg: " + widgetBackground);

        if ("WB_LIGHT".equals(widgetBackground)) {
            views.setViewVisibility(R.id.widget_dark, View.INVISIBLE);
            views.setViewVisibility(R.id.widget_light, View.VISIBLE);
        } else if ("WB_DARK".equals(widgetBackground)) {
            views.setViewVisibility(R.id.widget_dark, View.VISIBLE);
            views.setViewVisibility(R.id.widget_light, View.INVISIBLE);
        } else {
            views.setViewVisibility(R.id.widget_dark, View.INVISIBLE);
            views.setViewVisibility(R.id.widget_light, View.INVISIBLE);
        }

        appWidgetManager.updateAppWidget(appWidgetIds, views);
    }

	@Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: " + intent);

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName());
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

	    onUpdate(context, appWidgetManager, appWidgetIds);

	    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction()) ||
                ACTION_REQUEST_UPDATE.equals(intent.getAction())) {

	        Log.d(TAG, "sheduling widget update...");

            JobIntentService.enqueueWork(context.getApplicationContext(), WidgetUpdateService.class, 0, new Intent());
        }
	}

}