-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetDataProvider.java
More file actions
119 lines (88 loc) · 3.04 KB
/
WidgetDataProvider.java
File metadata and controls
119 lines (88 loc) · 3.04 KB
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
package com.example.spoonpoint.spoonpoint.widget;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.example.spoonpoint.spoonpoint.R;
import com.example.spoonpoint.spoonpoint.Utils.RestaurantUtils;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* WidgetDataProvider acts as the adapter for the collection view widget,
* providing RemoteViews to the widget in the getViewAt method.
*/
public class WidgetDataProvider implements RemoteViewsService.RemoteViewsFactory {
private static final String TAG = "WidgetDataProvider";
private static List<RestaurantUtils> mCollection = new ArrayList<>();
private List<RestaurantUtils> mWidgetCollect = new ArrayList<>();
Context mContext = null;
public WidgetDataProvider(Context context, Intent intent) {
mContext = context;
}
@Override
public void onCreate() {
initData();
}
@Override
public void onDataSetChanged() {
initData();
}
@Override
public void onDestroy() {
}
@Override
public int getCount() {
if (mWidgetCollect.size() > 0) {
return mWidgetCollect.size();
} else {
return 0;
}
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews view = new RemoteViews(mContext.getPackageName(),
R.layout.list_remote_view);
Bitmap featured_poster;
/*Check for empty featured image url (the server returns an empty string for restaurants without featured image)
* substitute empty featured image by a local placeholder image.
*/
try {
if (!((mWidgetCollect.get(position).featured_image).isEmpty())) {
featured_poster = Picasso.with(mContext).load(mWidgetCollect.get(position).featured_image).resize(50, 50).get();
} else {
/* keep in mind not to load vector images to the remote view of the collection widget (picasso does not support loading vector images on to the widget - might result in a crash) */
featured_poster = Picasso.with(mContext).load(R.drawable.restaurant).resize(50, 50).get();
}
view.setImageViewBitmap(R.id.widget_image, featured_poster);
} catch (IOException j) {
j.printStackTrace();
}
view.setTextViewText(R.id.widget_text, mWidgetCollect.get(position).name);
return view;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
public static void recommendationData(ArrayList<RestaurantUtils> data) {
mCollection = data;
}
private void initData() {
mWidgetCollect = mCollection;
}
}