author | sheepluva |
Sat, 07 Dec 2019 16:14:27 +0100 | |
branch | hedgeroid |
changeset 15516 | fb23e49b2d4e |
parent 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 |
||
10017 | 46 |
private View targetView = null; |
47 |
private ArrayList<OnItemMovementListener> listeners = new ArrayList<OnItemMovementListener>(); |
|
6350 | 48 |
|
10017 | 49 |
public void onActivityCreated(Bundle savedInstanceState){ |
50 |
super.onActivityCreated(savedInstanceState); |
|
6350 | 51 |
|
10017 | 52 |
ArrayList<DownloadPackage> tasks = new ArrayList<DownloadPackage>(); |
6350 | 53 |
|
10017 | 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); |
|
6350 | 58 |
|
10017 | 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 |
||
6350 | 65 |
|
10017 | 66 |
Thread t = new Thread(this, "DownloadListParser"); |
67 |
t.start(); |
|
68 |
} |
|
6350 | 69 |
|
10017 | 70 |
public void run(){ |
71 |
XmlPullParserFactory xmlPullFactory; |
|
72 |
try { |
|
73 |
xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
74 |
XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
6350 | 75 |
|
10017 | 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); |
|
6350 | 81 |
|
10017 | 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); |
|
6350 | 90 |
|
10017 | 91 |
getActivity().runOnUiThread(new Runnable(){ |
92 |
public void run() { |
|
93 |
adapter.add(task); |
|
94 |
} |
|
95 |
}); |
|
96 |
} |
|
97 |
} |
|
98 |
eventType = getEventType(xmlPuller); |
|
99 |
} |
|
100 |
||
101 |
||
102 |
} catch (XmlPullParserException e) { |
|
103 |
e.printStackTrace(); |
|
104 |
} catch (IOException e) { |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
107 |
} |
|
108 |
||
6350 | 109 |
|
10017 | 110 |
/** |
111 |
* Skips whitespaces.. |
|
112 |
*/ |
|
113 |
private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
|
114 |
int eventType = xmlPuller.next(); |
|
115 |
while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
|
116 |
eventType = xmlPuller.next(); |
|
117 |
} |
|
118 |
return eventType; |
|
119 |
} |
|
6350 | 120 |
|
10017 | 121 |
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { |
122 |
DownloadPackage task = (DownloadPackage)arg0.getAdapter().getItem(position); |
|
123 |
/* |
|
124 |
FragmentTransaction ft = getFragmentManager().beginTransaction(); |
|
125 |
Fragment prev = getFragmentManager().findFragmentByTag("dialog"); |
|
6350 | 126 |
|
10017 | 127 |
//remove any old dialogs |
128 |
if(prev != null) ft.remove(prev); |
|
129 |
ft.addToBackStack(null); |
|
6350 | 130 |
|
10017 | 131 |
//create a new dialog based on this task |
132 |
||
133 |
DialogFragment newFragment = DownloadDialogFragment.newInstance(task); |
|
6350 | 134 |
|
10017 | 135 |
//show it |
136 |
newFragment.show(ft, "dialog"); |
|
137 |
*/ |
|
138 |
targetView = arg1; |
|
6350 | 139 |
|
10017 | 140 |
//determine state |
141 |
int min = arg0.getTop(); |
|
142 |
int max = arg0.getBottom(); |
|
143 |
int top = targetView.getTop(); |
|
6350 | 144 |
|
10017 | 145 |
previousXCoord = top; |
146 |
for(OnItemMovementListener listener : listeners){ |
|
147 |
listener.onNewItemSelected(task, top, min, max, targetView.getHeight()*2); |
|
148 |
} |
|
149 |
} |
|
6350 | 150 |
|
10017 | 151 |
private int previousXCoord = 0; |
6350 | 152 |
|
10017 | 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(); |
|
6350 | 172 |
|
10017 | 173 |
if(Math.abs(previousXCoord - top) > targetView.getHeight()*2 ){ |
174 |
top = previousXCoord; |
|
175 |
} |
|
6350 | 176 |
|
10017 | 177 |
for(OnItemMovementListener listener : listeners){ |
178 |
listener.onViewMoved(top, min, max); |
|
179 |
} |
|
6350 | 180 |
|
10017 | 181 |
previousXCoord = top; |
182 |
} |
|
183 |
} |
|
6350 | 184 |
|
10017 | 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; |
|
6350 | 190 |
|
10017 | 191 |
} |
6350 | 192 |
|
10017 | 193 |
} |
6350 | 194 |
} |
195 |
||
196 |
interface OnItemMovementListener{ |
|
10017 | 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); |
|
6350 | 205 |
|
10017 | 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); |
|
6350 | 213 |
} |
214 |