6350
|
1 |
package org.hedgewars.hedgeroid.Downloader;
|
|
2 |
|
|
3 |
import java.io.BufferedReader;
|
|
4 |
import java.io.IOException;
|
|
5 |
import java.io.InputStreamReader;
|
|
6 |
import java.net.HttpURLConnection;
|
|
7 |
import java.net.URL;
|
|
8 |
import java.util.ArrayList;
|
|
9 |
|
|
10 |
import org.xmlpull.v1.XmlPullParser;
|
|
11 |
import org.xmlpull.v1.XmlPullParserException;
|
|
12 |
import org.xmlpull.v1.XmlPullParserFactory;
|
|
13 |
|
|
14 |
import android.graphics.Color;
|
|
15 |
import android.os.Bundle;
|
|
16 |
import android.support.v4.app.DialogFragment;
|
|
17 |
import android.support.v4.app.Fragment;
|
|
18 |
import android.support.v4.app.FragmentTransaction;
|
|
19 |
import android.support.v4.app.ListFragment;
|
|
20 |
import android.util.Log;
|
|
21 |
import android.view.View;
|
|
22 |
import android.widget.AbsListView;
|
|
23 |
import android.widget.AbsListView.OnScrollListener;
|
|
24 |
import android.widget.AdapterView;
|
|
25 |
import android.widget.AdapterView.OnItemClickListener;
|
|
26 |
import android.widget.ArrayAdapter;
|
|
27 |
|
|
28 |
public class DownloadListFragment extends ListFragment implements OnItemClickListener, OnScrollListener, Runnable{
|
|
29 |
|
|
30 |
private View targetView = null;
|
|
31 |
private ArrayList<OnItemMovementListener> listeners = new ArrayList<OnItemMovementListener>();
|
|
32 |
|
|
33 |
public void onActivityCreated(Bundle savedInstanceState){
|
|
34 |
super.onActivityCreated(savedInstanceState);
|
|
35 |
|
|
36 |
ArrayList<DownloadPackage> tasks = new ArrayList<DownloadPackage>();
|
|
37 |
|
|
38 |
ArrayAdapter<DownloadPackage> adapter = new ArrayAdapter<DownloadPackage>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, tasks);
|
|
39 |
setListAdapter(adapter);
|
|
40 |
getListView().setOnItemClickListener(this);
|
|
41 |
getListView().setOnScrollListener(this);
|
|
42 |
|
|
43 |
if(getActivity() instanceof OnItemMovementListener){
|
|
44 |
listeners.add((OnItemMovementListener)getActivity());
|
|
45 |
}
|
|
46 |
|
|
47 |
getListView().setCacheColorHint(Color.TRANSPARENT);//To prevent a blackish background while scrolling
|
|
48 |
|
|
49 |
|
|
50 |
Thread t = new Thread(this, "DownloadListParser");
|
|
51 |
t.start();
|
|
52 |
}
|
|
53 |
|
|
54 |
public void run(){
|
|
55 |
XmlPullParserFactory xmlPullFactory;
|
|
56 |
try {
|
|
57 |
xmlPullFactory = XmlPullParserFactory.newInstance();
|
|
58 |
XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
|
|
59 |
|
|
60 |
URL url = new URL("http://www.xelification.com/tmp/downloads.xml");
|
|
61 |
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
62 |
|
|
63 |
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()), 1024);
|
|
64 |
xmlPuller.setInput(br);
|
|
65 |
|
|
66 |
final ArrayAdapter<DownloadPackage> adapter = (ArrayAdapter<DownloadPackage>) getListAdapter();
|
|
67 |
|
|
68 |
int eventType = xmlPuller.getEventType();
|
|
69 |
while(eventType != XmlPullParser.END_DOCUMENT){
|
|
70 |
|
|
71 |
if(eventType == XmlPullParser.START_TAG){
|
|
72 |
if(xmlPuller.getName().toLowerCase().equals("task")){
|
|
73 |
final DownloadPackage task = DownloadPackage.getTaskFromXML(getActivity(), xmlPuller);
|
|
74 |
|
|
75 |
getActivity().runOnUiThread(new Runnable(){
|
|
76 |
public void run() {
|
|
77 |
adapter.add(task);
|
|
78 |
}
|
|
79 |
});
|
|
80 |
}
|
|
81 |
}
|
|
82 |
eventType = xmlPuller.nextTag();
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
} catch (XmlPullParserException e) {
|
|
87 |
// TODO Auto-generated catch block
|
|
88 |
e.printStackTrace();
|
|
89 |
} catch (IOException e) {
|
|
90 |
// TODO Auto-generated catch block
|
|
91 |
e.printStackTrace();
|
|
92 |
}
|
|
93 |
|
|
94 |
|
|
95 |
}
|
|
96 |
|
|
97 |
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
|
|
98 |
FragmentTransaction ft = getFragmentManager().beginTransaction();
|
|
99 |
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
|
|
100 |
|
|
101 |
//remove any old dialogs
|
|
102 |
if(prev != null) ft.remove(prev);
|
|
103 |
ft.addToBackStack(null);
|
|
104 |
|
|
105 |
//create a new dialog based on this task
|
|
106 |
DownloadPackage task = (DownloadPackage)arg0.getAdapter().getItem(position);
|
|
107 |
DialogFragment newFragment = DownloadDialogFragment.newInstance(task);
|
|
108 |
|
|
109 |
//show it
|
|
110 |
//newFragment.show(ft, "dialog");
|
|
111 |
|
|
112 |
targetView = arg1;
|
|
113 |
|
|
114 |
//determine state
|
|
115 |
int min = arg0.getTop();
|
|
116 |
int max = arg0.getBottom();
|
|
117 |
int top = targetView.getTop();
|
|
118 |
|
|
119 |
previousXCoord = top;
|
|
120 |
for(OnItemMovementListener listener : listeners){
|
|
121 |
listener.onNewItemSelected(task, top, min, max, targetView.getHeight()*2);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
private int previousXCoord = 0;
|
|
126 |
|
|
127 |
/*
|
|
128 |
* (non-Javadoc)
|
|
129 |
* @see android.widget.AbsListView.OnScrollListener#onScroll(android.widget.AbsListView, int, int, int)
|
|
130 |
*
|
|
131 |
* Android renews the Items used in the view, so when a user scroll down the list,
|
|
132 |
* as soon as the item has dissapeard from the list it gets reused at the bottom.
|
|
133 |
*
|
|
134 |
* For this reason we cannot just keep sending the targetView.getTop() but we must
|
|
135 |
* remember if the view is supposed to be at the top or bottom of the list. We
|
|
136 |
* remember this with int state,
|
|
137 |
* - 0 means we've got accurate reading from getTop()
|
|
138 |
* - -1 means it's somewhere at the top of the list
|
|
139 |
* - 1 means it's at the bottom somewhere
|
|
140 |
*/
|
|
141 |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
142 |
if(targetView != null){
|
|
143 |
int top = targetView.getTop() + (targetView.getHeight()/2);
|
|
144 |
int min = view.getTop();
|
|
145 |
int max = view.getBottom();
|
|
146 |
|
|
147 |
if(Math.abs(previousXCoord - top) > targetView.getHeight()*2 ){
|
|
148 |
top = previousXCoord;
|
|
149 |
}
|
|
150 |
|
|
151 |
for(OnItemMovementListener listener : listeners){
|
|
152 |
listener.onViewMoved(top, min, max);
|
|
153 |
}
|
|
154 |
|
|
155 |
previousXCoord = top;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
public void onScrollStateChanged(AbsListView view, int scrollState) {
|
|
160 |
switch(scrollState){
|
|
161 |
case OnScrollListener.SCROLL_STATE_FLING: Log.d("tag", "fling"); break;
|
|
162 |
case OnScrollListener.SCROLL_STATE_IDLE:Log.d("tag", "idle"); break;
|
|
163 |
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:Log.d("tag", "scroll"); break;
|
|
164 |
|
|
165 |
}
|
|
166 |
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
interface OnItemMovementListener{
|
|
171 |
/**
|
|
172 |
* When I new item has been selected this method will be called
|
|
173 |
* @param task The task which goes with this item
|
|
174 |
* @param x The middle of this item
|
|
175 |
* @param minX The top of the parent of this item
|
|
176 |
* @param maxX The bottom of the parent of this item
|
|
177 |
*/
|
|
178 |
void onNewItemSelected(DownloadPackage task, int x, int minX, int maxX, int size);
|
|
179 |
|
|
180 |
/**
|
|
181 |
* When the item has been moved this method is called
|
|
182 |
* @param x The middle of this item
|
|
183 |
* @param minX The top of the parent of this item
|
|
184 |
* @param maxX The bottom of the parent of this item
|
|
185 |
*/
|
|
186 |
void onViewMoved(int x, int minX, int maxX);
|
|
187 |
}
|
|
188 |
|