summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/DashClock.java
blob: 26c1ecbbcf48cae534f7f76912f67c33e7af6fd9 (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
package org.fox.ttrss;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class DashClock extends DashClockExtension {
  
	private final String TAG = this.getClass().getSimpleName();
	
	protected SharedPreferences m_prefs;
	
	@Override
	protected void onInitialize(boolean isReconnect) {
		super.onInitialize(isReconnect);
		setUpdateWhenScreenOn(true);
		
		m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
	}

	@Override
	protected void onUpdateData(int reason) {
		
		UnreadRequest req = new UnreadRequest(getApplicationContext());
				
		@SuppressWarnings("serial")
		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());
			}
		};

		req.execute(map);
	}
	
	protected class UnreadRequest extends ApiRequest {
		private String m_sessionId;
		
		private int m_unreadCount;
		
		public UnreadRequest(Context context) {
			super(context);
		}

		@SuppressWarnings("unchecked")
		protected void onPostExecute(JsonElement result) {
			if (result != null) {
				try {
					JsonObject content = result.getAsJsonObject();
					if (content != null) {
						m_sessionId = content.get("session_id").getAsString();

						Log.d(TAG, "Authenticated!");
						
						ApiRequest req = new ApiRequest(m_context) {
							protected void onPostExecute(JsonElement result) {
								m_unreadCount = 0;

								if (result != null) {
									try {
										JsonElement unreadCount = result.getAsJsonObject().get("unread");
										
										if (unreadCount != null) {
											m_unreadCount = unreadCount.getAsInt();
										} else {
											m_unreadCount = -1;
										}
										
										ExtensionData updatedData = null; // when null DashClock hides the widget
										if (m_unreadCount > 0) {
											updatedData = new ExtensionData();
											updatedData.visible(true);
						
											updatedData.icon(R.drawable.dashclock);
											updatedData.status(String.valueOf(m_unreadCount));
						
											updatedData.expandedTitle(m_unreadCount + " unread articles");
											//updatedData.expandedBody(getString(R.string.app_name));
						
											updatedData.clickIntent(new Intent().setClassName("org.fox.ttrss",
													"org.fox.ttrss.OnlineActivity"));
										}
										publishUpdate(updatedData);				
										
									} catch (Exception e) {
										e.printStackTrace();
									}
								}

								//Log.d(TAG, "unread count is: " + m_unreadCount);
							}
						};

						@SuppressWarnings("serial")
						HashMap<String, String> map = new HashMap<String, String>() {
							{
								put("sid", m_sessionId);
								put("op", "getUnread");
							}
						};

						req.execute(map);

						return;
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			m_sessionId = null;
		}
	}
}