author | Medo <smaxein@googlemail.com> |
Mon, 20 Aug 2012 21:05:57 +0200 | |
changeset 7584 | 7831c84cc644 |
parent 6485 | 7586c266b52e |
child 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
1 |
/* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
3 |
* Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
4 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
9 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
13 |
* GNU General Public License for more details. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
14 |
* |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
18 |
*/ |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6485
diff
changeset
|
19 |
|
6350 | 20 |
package org.hedgewars.hedgeroid.Downloader; |
21 |
||
22 |
import java.io.BufferedReader; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.InputStreamReader; |
|
25 |
import java.net.HttpURLConnection; |
|
26 |
import java.net.URL; |
|
27 |
import java.util.ArrayList; |
|
28 |
||
29 |
import org.xmlpull.v1.XmlPullParser; |
|
30 |
import org.xmlpull.v1.XmlPullParserException; |
|
31 |
import org.xmlpull.v1.XmlPullParserFactory; |
|
32 |
||
33 |
import android.graphics.Color; |
|
34 |
import android.os.Bundle; |
|
35 |
import android.support.v4.app.ListFragment; |
|
36 |
import android.util.Log; |
|
37 |
import android.view.View; |
|
38 |
import android.widget.AbsListView; |
|
39 |
import android.widget.AbsListView.OnScrollListener; |
|
40 |
import android.widget.AdapterView; |
|
41 |
import android.widget.AdapterView.OnItemClickListener; |
|
42 |
import android.widget.ArrayAdapter; |
|
43 |
||
44 |
public class DownloadListFragment extends ListFragment implements OnItemClickListener, OnScrollListener, Runnable{ |
|
45 |
||
46 |
private View targetView = null; |
|
47 |
private ArrayList<OnItemMovementListener> listeners = new ArrayList<OnItemMovementListener>(); |
|
48 |
||
49 |
public void onActivityCreated(Bundle savedInstanceState){ |
|
50 |
super.onActivityCreated(savedInstanceState); |
|
51 |
||
52 |
ArrayList<DownloadPackage> tasks = new ArrayList<DownloadPackage>(); |
|
53 |
||
54 |
ArrayAdapter<DownloadPackage> adapter = new ArrayAdapter<DownloadPackage>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, tasks); |
|
55 |
setListAdapter(adapter); |
|
56 |
getListView().setOnItemClickListener(this); |
|
57 |
getListView().setOnScrollListener(this); |
|
58 |
||
59 |
if(getActivity() instanceof OnItemMovementListener){ |
|
60 |
listeners.add((OnItemMovementListener)getActivity()); |
|
61 |
} |
|
62 |
||
63 |
getListView().setCacheColorHint(Color.TRANSPARENT);//To prevent a blackish background while scrolling |
|
64 |
||
65 |
||
66 |
Thread t = new Thread(this, "DownloadListParser"); |
|
67 |
t.start(); |
|
68 |
} |
|
69 |
||
70 |
public void run(){ |
|
71 |
XmlPullParserFactory xmlPullFactory; |
|
72 |
try { |
|
73 |
xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
74 |
XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
75 |
||
76 |
URL url = new URL("http://www.xelification.com/tmp/downloads.xml"); |
|
77 |
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
|
78 |
||
79 |
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()), 1024); |
|
80 |
xmlPuller.setInput(br); |
|
81 |
||
82 |
final ArrayAdapter<DownloadPackage> adapter = (ArrayAdapter<DownloadPackage>) getListAdapter(); |
|
83 |
||
84 |
int eventType = xmlPuller.getEventType(); |
|
85 |
while(eventType != XmlPullParser.END_DOCUMENT){ |
|
86 |
||
87 |
if(eventType == XmlPullParser.START_TAG){ |
|
88 |
if(xmlPuller.getName().toLowerCase().equals("task")){ |
|
89 |
final DownloadPackage task = DownloadPackage.getTaskFromXML(getActivity(), xmlPuller); |
|
90 |
||
91 |
getActivity().runOnUiThread(new Runnable(){ |
|
92 |
public void run() { |
|
93 |
adapter.add(task); |
|
94 |
} |
|
95 |
}); |
|
96 |
} |
|
97 |
} |
|
6446
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
98 |
eventType = getEventType(xmlPuller); |
6350 | 99 |
} |
100 |
||
101 |
||
102 |
} catch (XmlPullParserException e) { |
|
103 |
e.printStackTrace(); |
|
104 |
} catch (IOException e) { |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
6446
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
107 |
} |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
108 |
|
6350 | 109 |
|
6446
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
110 |
/** |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
111 |
* Skips whitespaces.. |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
112 |
*/ |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
113 |
private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
114 |
int eventType = xmlPuller.next(); |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
115 |
while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
116 |
eventType = xmlPuller.next(); |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
117 |
} |
a49d01b96185
android: cleaned up the code a bit and removed a bug with the parsing of the xml
Xeli
parents:
6350
diff
changeset
|
118 |
return eventType; |
6350 | 119 |
} |
120 |
||
121 |
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { |
|
6485
7586c266b52e
android: hopefully prevents a FC, I can't reproduce it though..
Xeli
parents:
6446
diff
changeset
|
122 |
DownloadPackage task = (DownloadPackage)arg0.getAdapter().getItem(position); |
7586c266b52e
android: hopefully prevents a FC, I can't reproduce it though..
Xeli
parents:
6446
diff
changeset
|
123 |
/* |
6350 | 124 |
FragmentTransaction ft = getFragmentManager().beginTransaction(); |
125 |
Fragment prev = getFragmentManager().findFragmentByTag("dialog"); |
|
126 |
||
127 |
//remove any old dialogs |
|
128 |
if(prev != null) ft.remove(prev); |
|
129 |
ft.addToBackStack(null); |
|
130 |
||
131 |
//create a new dialog based on this task |
|
6485
7586c266b52e
android: hopefully prevents a FC, I can't reproduce it though..
Xeli
parents:
6446
diff
changeset
|
132 |
|
6350 | 133 |
DialogFragment newFragment = DownloadDialogFragment.newInstance(task); |
134 |
||
135 |
//show it |
|
6485
7586c266b52e
android: hopefully prevents a FC, I can't reproduce it though..
Xeli
parents:
6446
diff
changeset
|
136 |
newFragment.show(ft, "dialog"); |
7586c266b52e
android: hopefully prevents a FC, I can't reproduce it though..
Xeli
parents:
6446
diff
changeset
|
137 |
*/ |
6350 | 138 |
targetView = arg1; |
139 |
||
140 |
//determine state |
|
141 |
int min = arg0.getTop(); |
|
142 |
int max = arg0.getBottom(); |
|
143 |
int top = targetView.getTop(); |
|
144 |
||
145 |
previousXCoord = top; |
|
146 |
for(OnItemMovementListener listener : listeners){ |
|
147 |
listener.onNewItemSelected(task, top, min, max, targetView.getHeight()*2); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
private int previousXCoord = 0; |
|
152 |
||
153 |
/* |
|
154 |
* (non-Javadoc) |
|
155 |
* @see android.widget.AbsListView.OnScrollListener#onScroll(android.widget.AbsListView, int, int, int) |
|
156 |
* |
|
157 |
* Android renews the Items used in the view, so when a user scroll down the list, |
|
158 |
* as soon as the item has dissapeard from the list it gets reused at the bottom. |
|
159 |
* |
|
160 |
* For this reason we cannot just keep sending the targetView.getTop() but we must |
|
161 |
* remember if the view is supposed to be at the top or bottom of the list. We |
|
162 |
* remember this with int state, |
|
163 |
* - 0 means we've got accurate reading from getTop() |
|
164 |
* - -1 means it's somewhere at the top of the list |
|
165 |
* - 1 means it's at the bottom somewhere |
|
166 |
*/ |
|
167 |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { |
|
168 |
if(targetView != null){ |
|
169 |
int top = targetView.getTop() + (targetView.getHeight()/2); |
|
170 |
int min = view.getTop(); |
|
171 |
int max = view.getBottom(); |
|
172 |
||
173 |
if(Math.abs(previousXCoord - top) > targetView.getHeight()*2 ){ |
|
174 |
top = previousXCoord; |
|
175 |
} |
|
176 |
||
177 |
for(OnItemMovementListener listener : listeners){ |
|
178 |
listener.onViewMoved(top, min, max); |
|
179 |
} |
|
180 |
||
181 |
previousXCoord = top; |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public void onScrollStateChanged(AbsListView view, int scrollState) { |
|
186 |
switch(scrollState){ |
|
187 |
case OnScrollListener.SCROLL_STATE_FLING: Log.d("tag", "fling"); break; |
|
188 |
case OnScrollListener.SCROLL_STATE_IDLE:Log.d("tag", "idle"); break; |
|
189 |
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:Log.d("tag", "scroll"); break; |
|
190 |
||
191 |
} |
|
192 |
||
193 |
} |
|
194 |
} |
|
195 |
||
196 |
interface OnItemMovementListener{ |
|
197 |
/** |
|
198 |
* When I new item has been selected this method will be called |
|
199 |
* @param task The task which goes with this item |
|
200 |
* @param x The middle of this item |
|
201 |
* @param minX The top of the parent of this item |
|
202 |
* @param maxX The bottom of the parent of this item |
|
203 |
*/ |
|
204 |
void onNewItemSelected(DownloadPackage task, int x, int minX, int maxX, int size); |
|
205 |
||
206 |
/** |
|
207 |
* When the item has been moved this method is called |
|
208 |
* @param x The middle of this item |
|
209 |
* @param minX The top of the parent of this item |
|
210 |
* @param maxX The bottom of the parent of this item |
|
211 |
*/ |
|
212 |
void onViewMoved(int x, int minX, int maxX); |
|
213 |
} |
|
214 |