summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/widget/WidgetUpdateService.java
blob: 00c8002f4ec23e75855d61844259fd8d5839892e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.fox.ttrss.widget;

import java.util.HashMap;

import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.R;
import org.fox.ttrss.util.SimpleLoginManager;

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);
    	
    	final ComponentName thisWidget = new ComponentName(this, SmallWidgetProvider.class);
    	final AppWidgetManager manager = AppWidgetManager.getInstance(this);

    	try {
        	view.setTextViewText(R.id.counter, String.valueOf(""));
        	view.setViewVisibility(R.id.progress, View.VISIBLE);

	        manager.updateAppWidget(thisWidget, view);
	        
	        final SharedPreferences m_prefs = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());
	   	
	   		if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
	    			
	    			// Toast: need configure
	    			
	   		} else {
	
	   			SimpleLoginManager loginManager = new SimpleLoginManager() {
					
					@Override
					protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {
					
						ApiRequest aru = new ApiRequest(getApplicationContext()) {
								@Override
								protected void onPostExecute(JsonElement result) {
									if (result != null) {
										try {
   										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;
   										}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}	   										
								
									view.setViewVisibility(R.id.progress, View.GONE);
									view.setTextViewText(R.id.counter, "?");
									manager.updateAppWidget(thisWidget, view);
								}
						};
						
						final String fSessionId = sessionId;
						
						HashMap<String, String> umap = new HashMap<String, String>() {
				   				{
				   					put("op", "getUnread");
				   					put("sid", fSessionId);
				   				}
				   			};

							aru.execute(umap);
					}
					
					@Override
					protected void onLoginFailed(int requestId, ApiRequest ar) {
						
						view.setViewVisibility(R.id.progress, View.GONE);
	   			    	view.setTextViewText(R.id.counter, "?");
	   			        manager.updateAppWidget(thisWidget, view);
					}
					
					@Override
					protected void onLoggingIn(int requestId) {
						
						
					}
				};

				String login = m_prefs.getString("login", "").trim();
				String password = m_prefs.getString("password", "").trim();
				
				loginManager.logIn(getApplicationContext(), 1, login, password);
	   		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		
	    	view.setViewVisibility(R.id.progress, View.GONE);
	    	view.setTextViewText(R.id.counter, getString(R.string.app_name));
	        manager.updateAppWidget(thisWidget, view);  					
	
    	}
    }
}