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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.luhmer.owncloudnewsreader.widget;
import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import de.luhmer.owncloudnewsreader.Constants;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.RssItem;
public class WidgetNewsViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private static final String TAG = WidgetNewsViewsFactory.class.getCanonicalName();
private DatabaseConnectionOrm dbConn;
private List<RssItem> rssItems;
private Context context;
private int appWidgetId;
public WidgetNewsViewsFactory(Context context, Intent intent) {
this.context = context;
appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if(Constants.debugModeWidget)
Log.d(TAG, "CONSTRUCTOR CALLED - " + appWidgetId);
}
@Override
public void onCreate() {
if(Constants.debugModeWidget)
Log.d(TAG, "onCreate");
dbConn = new DatabaseConnectionOrm(context);
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
Log.v(TAG, "getCount");
return rssItems.size();
}
// Given the position (index) of a WidgetItem in the array, use the item's text value in
// combination with the app widget item XML file to construct a RemoteViews object.
@SuppressLint("SimpleDateFormat")
public RemoteViews getViewAt(int position) {
if(Constants.debugModeWidget) {
Log.d(TAG, "getViewAt: " + position);
}
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_item);
try {
RssItem rssItem = rssItems.get(position);
String header = rssItem.getFeed().getFeedTitle();
String colorString = rssItem.getFeed().getAvgColour();
String authorOfArticle = rssItem.getAuthor();
header += authorOfArticle == null ? "" : " - " + authorOfArticle.trim();
String title = Html.fromHtml(rssItem.getTitle()).toString();
long id = rssItem.getId();
Date date = rssItem.getPubDate();
String dateString = "";
if(date != null)
{
SimpleDateFormat formater = new SimpleDateFormat();
dateString = formater.format(date);
}
rv.setTextViewText(R.id.feed_datetime, dateString);
rv.setTextViewText(R.id.feed_author_source, header);
rv.setTextViewText(R.id.feed_title, title);
int resId = rssItem.getRead_temp() ? R.drawable.ic_check_box_white : R.drawable.ic_check_box_outline_blank_white;
int contentDescriptionId = rssItem.getRead_temp() ? R.string.content_desc_mark_as_unread : R.string.content_desc_mark_as_read;
rv.setInt(R.id.cb_lv_item_read, "setBackgroundResource", resId);
rv.setContentDescription(R.id.cb_lv_item_read, context.getString(contentDescriptionId));
if(colorString != null) {
rv.setInt(R.id.color_line_feed, "setBackgroundColor", Integer.parseInt(colorString));
}
//Get a fresh new intent
Intent rowIntent = new Intent();
//Load it with whatever extra you want
rowIntent.putExtra(WidgetProvider.RSS_ITEM_ID, id);
//Set it on the list remote view
rv.setOnClickFillInIntent(R.id.widget_row_layout, rowIntent);
//Get a fresh new intent
Intent ei = new Intent();
//Load it with whatever extra you want
ei.putExtra(WidgetProvider.RSS_ITEM_ID, id);
//Set it on the list remote view
rv.setOnClickFillInIntent(R.id.cb_lv_item_read_wrapper, ei);
//Get a fresh new intent
Intent iCheck = new Intent();
//Load it with whatever extra you want
iCheck.putExtra(WidgetProvider.RSS_ITEM_ID, id);
iCheck.putExtra(WidgetProvider.ACTION_CHECKED_CLICK, true);
rv.setOnClickFillInIntent(R.id.cb_lv_item_read, iCheck);
} catch(Exception ex) {
Log.e(TAG, "Error while getting view for widget at position: " + position, ex);
}
// Return the RemoteViews object.
return rv;
}
@Override
public RemoteViews getLoadingView() {
Log.v(TAG, "getLoadingView");
return(null);
}
@Override
public int getViewTypeCount() {
return(1);
}
@Override
public long getItemId(int position) {
//Log.v(TAG, "getItemId: " + position);
return(position);
}
@Override
public boolean hasStableIds() {
Log.v(TAG, "hasStableIds: " + appWidgetId);
return(true);
}
@SuppressWarnings("deprecation")
@Override
public void onDataSetChanged() {
Log.v(TAG, "DataSetChanged - WidgetID: " + appWidgetId);
rssItems = dbConn.getAllUnreadRssItemsForWidget();
Log.v(TAG, "DataSetChanged finished!");
}
}
|