/
FileListController.java
275 lines (239 loc) · 9.64 KB
/
FileListController.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (C) 2005-2009 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC Remote; see the file license. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
package org.xbmc.android.remote.presentation.controller;
import java.util.ArrayList;
import java.util.HashMap;
import org.xbmc.android.remote.R;
import org.xbmc.android.remote.business.ManagerFactory;
import org.xbmc.android.remote.presentation.activity.ListActivity;
import org.xbmc.android.remote.presentation.activity.NowPlayingActivity;
import org.xbmc.android.remote.presentation.widget.OneLabelItemView;
import org.xbmc.api.business.DataResponse;
import org.xbmc.api.business.IControlManager;
import org.xbmc.api.business.IInfoManager;
import org.xbmc.api.object.FileLocation;
import org.xbmc.api.type.MediaType;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
public class FileListController extends ListController implements IController {
public static final int MESSAGE_HANDLE_DATA = 1;
public static final int MESSAGE_CONNECTION_ERROR = 2;
private static final int ITEM_CONTEXT_QUEUE = 0;
private static final int ITEM_CONTEXT_PLAY = 0;
private HashMap<String, FileLocation> mFileItems;
private volatile String mGettingUrl;
private int mMediaType = MediaType.UNKNOWN;
// from ListActivity.java
protected ListAdapter mAdapter;
private IInfoManager mInfoManager;
private IControlManager mControlManager;
public FileListController() {}
public FileListController(int mediaType) {
mMediaType = mediaType;
}
public void onCreate(Activity activity, AbsListView list) {
mInfoManager = ManagerFactory.getInfoManager(this);
mControlManager = ManagerFactory.getControlManager(this);
if (!isCreated()) {
super.onCreate(activity, list);
if (mMediaType == MediaType.UNKNOWN) {
mMediaType = mActivity.getIntent().getIntExtra(EXTRA_SHARE_TYPE, MediaType.MUSIC);
}
mFallbackBitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon_file);
final String path = mActivity.getIntent().getStringExtra(EXTRA_PATH);
final String displayPath = mActivity.getIntent().getStringExtra(EXTRA_DISPLAY_PATH);
fillUp(path == null ? "" : path, displayPath);
activity.registerForContextMenu(mList);
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mFileItems == null)
return;
FileLocation item = mFileItems.get(((FileLocation)parent.getAdapter().getItem(position)).name);
if (item.isDirectory) {
Intent nextActivity = new Intent(mActivity, ListActivity.class);
nextActivity.putExtra(ListController.EXTRA_LIST_CONTROLLER, new FileListController());
nextActivity.putExtra(ListController.EXTRA_SHARE_TYPE, mMediaType);
nextActivity.putExtra(ListController.EXTRA_PATH, item.path);
nextActivity.putExtra(ListController.EXTRA_DISPLAY_PATH, item.displayPath);
mActivity.startActivity(nextActivity);
} else {
switch(item.mediaType) {
case 0:
break;
case MediaType.PICTURES:
mControlManager.showPicture(new DataResponse<Boolean>() {
public void run() {
if (value) {
mActivity.startActivity(new Intent(mActivity, NowPlayingActivity.class));
}
}
}, item.path, mActivity.getApplicationContext());
break;
default:
mControlManager.playFile(new DataResponse<Boolean>() {
public void run() {
if (value) {
mActivity.startActivity(new Intent(mActivity, NowPlayingActivity.class));
}
}
}, item.path, mActivity.getApplicationContext());
}
}
}
});
mList.setOnKeyListener(new ListControllerOnKeyListener<FileLocation>());
}
}
private class FileItemAdapter extends ArrayAdapter<FileLocation> {
FileItemAdapter(Activity activity, ArrayList<FileLocation> items) {
super(activity, 0, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
final OneLabelItemView view;
if (convertView == null) {
view = new OneLabelItemView(mActivity, parent.getWidth(), mFallbackBitmap, mList.getSelector());
} else {
view = (OneLabelItemView)convertView;
}
final FileLocation fileItem = this.getItem(position);
view.reset();
view.position = position;
view.title = fileItem.name;
final Resources res = mActivity.getResources();
if (fileItem.isArchive) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_zip));
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_zip));
} else if (fileItem.isDirectory) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_folder));
} else {
final String ext = fileItem.name.substring(fileItem.name.lastIndexOf(".") + 1).toLowerCase();
if (ext.equals("mp3") || ext.equals("ogg") || ext.equals("flac") || ext.equals("m4a")) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_song));
} else if (ext.equals("avi") || ext.equals("mov") || ext.equals("flv") || ext.equals("mkv") || ext.equals("wmv") || ext.equals("mp4")) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_video));
} else if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("bmp") || ext.equals("gif") || ext.equals("png") || ext.equals("tbn")) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_picture));
} else if (ext.equals("m3u")) {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_playing));
} else {
view.setCover(BitmapFactory.decodeResource(res, R.drawable.icon_file));
}
}
return view;
}
}
private void fillUp(final String url, final String displayPath) {
if (mGettingUrl != null)
return;
mGettingUrl = url;
mFileItems = null;
mList.setTextFilterEnabled(false);
setTitle("Loading...");
showOnLoading();
DataResponse<ArrayList<FileLocation>> mediaListHandler = new DataResponse<ArrayList<FileLocation>>() {
public void run() {
setTitle(url.equals("") ? "/" : displayPath);
if (value.size() > 0) {
mFileItems = new HashMap<String, FileLocation>();
for (FileLocation item : value) {
mFileItems.put(item.name, item);
}
setListAdapter(new FileItemAdapter(mActivity, value));
} else {
setNoDataMessage("No files found.", R.drawable.icon_folder_dark);
}
}
};
if (mGettingUrl.length() == 0) {
mInfoManager.getShares(mediaListHandler, mMediaType, mActivity.getApplicationContext());
} else {
mInfoManager.getDirectory(mediaListHandler, mGettingUrl, mActivity.getApplicationContext());
}
}
/**
* Provide the cursor for the list view.
*/
public void setListAdapter(ListAdapter adapter) {
synchronized (this) {
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
@Override
public void onContextItemSelected(MenuItem item) {
// be aware that this must be explicitly called by your activity!
final FileLocation loc = (FileLocation) mList.getAdapter().getItem(((OneLabelItemView)((AdapterContextMenuInfo)item.getMenuInfo()).targetView).position);
switch(item.getItemId()) {
case ITEM_CONTEXT_QUEUE:
mControlManager.getPlaylistId(new DataResponse<Integer>() {
@Override
public void run() {
}
}, mActivity);
// mControlManager.setPlaylistId(response, id, context)
mControlManager.addToPlaylist(new DataResponse<Boolean>(), loc.path,
mActivity);
break;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// be aware that this must be explicitly called by your activity!
Log.d("FileListController", "Create Context Menu");
final OneLabelItemView view = (OneLabelItemView)((AdapterContextMenuInfo)menuInfo).targetView;
menu.setHeaderTitle(((FileLocation)mList.getItemAtPosition(view.getPosition())).name);
menu.add(0, ITEM_CONTEXT_QUEUE, 1, "Queue Folder");
menu.add(0, ITEM_CONTEXT_PLAY, 2, "Play Folder");
}
public void onActivityPause() {
if (mInfoManager != null) {
mInfoManager.setController(null);
}
if (mControlManager != null) {
mControlManager.setController(null);
}
super.onActivityPause();
}
public void onActivityResume(Activity activity) {
if (mInfoManager != null) {
mInfoManager.setController(this);
}
if (mControlManager != null) {
mControlManager.setController(this);
}
super.onActivityResume(activity);
}
private static final long serialVersionUID = -3883887349523448733L;
}