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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package de.danoeh.antennapod.activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.mobeta.android.dslv.DragSortListView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.util.UndoBarController;
public class OrganizeQueueActivity extends ActionBarActivity implements
UndoBarController.UndoListener {
private static final String TAG = "OrganizeQueueActivity";
private static final int MENU_ID_ACCEPT = 2;
private OrganizeAdapter adapter;
private UndoBarController undoBarController;
private DragSortListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme());
super.onCreate(savedInstanceState);
setContentView(R.layout.organize_queue);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
listView = (DragSortListView) findViewById(android.R.id.list);
listView.setDropListener(dropListener);
listView.setRemoveListener(removeListener);
adapter = new OrganizeAdapter(this);
listView.setAdapter(adapter);
undoBarController = new UndoBarController(findViewById(R.id.undobar),
this);
}
@Override
protected void onPause() {
super.onPause();
EventDistributor.getInstance().unregister(contentUpdate);
}
@Override
protected void onStop() {
super.onStop();
FeedManager.getInstance().autodownloadUndownloadedItems(
getApplicationContext());
}
@Override
protected void onResume() {
super.onResume();
EventDistributor.getInstance().register(contentUpdate);
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
@Override
public void update(EventDistributor eventDistributor, Integer arg) {
if (((EventDistributor.QUEUE_UPDATE | EventDistributor.FEED_LIST_UPDATE) & arg) != 0) {
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
};
private DragSortListView.DropListener dropListener = new DragSortListView.DropListener() {
@Override
public void drop(int from, int to) {
FeedManager manager = FeedManager.getInstance();
manager.moveQueueItem(OrganizeQueueActivity.this, from, to, false);
adapter.notifyDataSetChanged();
}
};
private DragSortListView.RemoveListener removeListener = new DragSortListView.RemoveListener() {
@Override
public void remove(int which) {
FeedManager manager = FeedManager.getInstance();
FeedItem item = (FeedItem) listView.getAdapter().getItem(which);
manager.removeQueueItem(OrganizeQueueActivity.this, item, false);
undoBarController.showUndoBar(false,
getString(R.string.removed_from_queue), new UndoToken(item,
which));
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return false;
}
}
@Override
public void onUndo(Parcelable token) {
// Perform the undo
UndoToken undoToken = (UndoToken) token;
FeedItem feedItem = undoToken.getFeedItem();
int position = undoToken.getPosition();
FeedManager manager = FeedManager.getInstance();
manager.addQueueItemAt(OrganizeQueueActivity.this, feedItem, position,
false);
}
private static class OrganizeAdapter extends BaseAdapter {
private Context context;
private FeedManager manager = FeedManager.getInstance();
public OrganizeAdapter(Context context) {
super();
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
final FeedItem item = getItem(position);
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.organize_queue_listitem, null);
holder.title = (TextView) convertView
.findViewById(R.id.txtvTitle);
holder.feedTitle = (TextView) convertView
.findViewById(R.id.txtvFeedname);
holder.feedImage = (ImageView) convertView
.findViewById(R.id.imgvFeedimage);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.title.setText(item.getTitle());
holder.feedTitle.setText(item.getFeed().getTitle());
holder.feedImage.setTag(item.getImageLoaderCacheKey());
ImageLoader.getInstance().loadThumbnailBitmap(
item,
holder.feedImage,
(int) convertView.getResources().getDimension(
R.dimen.thumbnail_length));
return convertView;
}
static class Holder {
TextView title;
TextView feedTitle;
ImageView feedImage;
}
@Override
public int getCount() {
int queueSize = manager.getQueueSize(true);
return queueSize;
}
@Override
public FeedItem getItem(int position) {
return manager.getQueueItemAtIndex(position, true);
}
@Override
public long getItemId(int position) {
return position;
}
}
private static class UndoToken implements Parcelable {
private long itemId;
private long feedId;
private int position;
public UndoToken(FeedItem item, int position) {
FeedManager manager = FeedManager.getInstance();
this.itemId = item.getId();
this.feedId = item.getFeed().getId();
this.position = position;
}
private UndoToken(Parcel in) {
itemId = in.readLong();
feedId = in.readLong();
position = in.readInt();
}
public static final Parcelable.Creator<UndoToken> CREATOR = new Parcelable.Creator<UndoToken>() {
public UndoToken createFromParcel(Parcel in) {
return new UndoToken(in);
}
public UndoToken[] newArray(int size) {
return new UndoToken[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeLong(itemId);
out.writeLong(feedId);
out.writeInt(position);
}
public FeedItem getFeedItem() {
FeedManager manager = FeedManager.getInstance();
return manager.getFeedItem(itemId, feedId);
}
public int getPosition() {
return position;
}
}
}
|