author | nemo |
Tue, 24 Oct 2017 18:55:19 -0400 | |
changeset 12764 | df9d9d19406a |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
1 |
/* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
6700 | 3 |
* Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
4 |
* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
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:
6700
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:
6700
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:
6700
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
9 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
14 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
18 |
*/ |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
19 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid.Downloader; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
21 |
|
6343 | 22 |
import java.util.LinkedList; |
6350 | 23 |
import java.util.List; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
24 |
|
6049
7bc38086d771
wops missed this one: removed a 'fix' eclipse put in there
Xeli
parents:
6047
diff
changeset
|
25 |
import org.hedgewars.hedgeroid.R; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
26 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
27 |
import android.app.Notification; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
28 |
import android.app.NotificationManager; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
29 |
import android.app.PendingIntent; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
30 |
import android.app.Service; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
31 |
import android.content.Intent; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
32 |
import android.os.Handler; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
33 |
import android.os.IBinder; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
34 |
import android.os.Message; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
35 |
import android.os.Messenger; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
36 |
import android.os.RemoteException; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
37 |
import android.widget.RemoteViews; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
38 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
39 |
public class DownloadService extends Service { |
10017 | 40 |
public final static String INTENT_TASKID = "taskId"; |
41 |
public final static String INTENT_TASK = "task"; |
|
6343 | 42 |
|
10017 | 43 |
public static final String PREF_DOWNLOADED = "downloaded"; |
44 |
public static final int MSG_CANCEL = 0; |
|
45 |
public static final int MSG_UNREGISTER_CLIENT = 2; |
|
46 |
public final static int MSG_ADDTASK = 4; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
47 |
|
10017 | 48 |
public static final int NOTIFICATION_PROCESSING = 0; |
49 |
public static final int NOTIFICATION_DONE = 1; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
50 |
|
10017 | 51 |
private DownloadAsyncTask asyncExecutor; |
6350 | 52 |
|
10017 | 53 |
private DownloadHandler handler = new DownloadHandler(); |
54 |
private final Messenger messenger = new Messenger(handler); |
|
6350 | 55 |
|
10017 | 56 |
private NotificationManager nM; |
57 |
private RemoteViews contentView; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
58 |
|
10017 | 59 |
private LinkedList<DownloadTask> downloadTasks = new LinkedList<DownloadTask>(); |
60 |
private DownloadTask currentTask = null; |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
61 |
|
10017 | 62 |
public class DownloadHandler extends Handler{ |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
63 |
|
10017 | 64 |
public void handleMessage(Message msg){ |
65 |
if(msg.obj != null){ |
|
66 |
DownloadPackage pack = (DownloadPackage) msg.obj; |
|
67 |
DownloadTask task = null; |
|
68 |
Messenger replyToMessenger = msg.replyTo; |
|
69 |
for(DownloadTask _task : downloadTasks){ |
|
70 |
if(_task.getPackage().equals(pack)){ |
|
71 |
task = _task; |
|
72 |
break; |
|
73 |
} |
|
74 |
} |
|
6350 | 75 |
|
10017 | 76 |
switch(msg.what){ |
77 |
case MSG_ADDTASK: |
|
78 |
if(task == null){ |
|
79 |
task = new DownloadTask(pack); |
|
80 |
downloadTasks.add(task); |
|
81 |
} |
|
6350 | 82 |
|
10017 | 83 |
task.addClient(replyToMessenger); |
84 |
runNextTask(); |
|
85 |
return; |
|
86 |
case MSG_CANCEL: |
|
87 |
if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.PENDING){ |
|
88 |
downloadTasks.remove(task); |
|
89 |
} |
|
90 |
if(currentTask != null && currentTask.getPackage().equals(pack)){//TODO synchronization problem? |
|
91 |
asyncExecutor.cancel(false); |
|
92 |
} |
|
93 |
return; |
|
94 |
case MSG_UNREGISTER_CLIENT: |
|
95 |
if(task != null){ |
|
96 |
task.removeClient(replyToMessenger); |
|
97 |
} |
|
98 |
return; |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
6350 | 103 |
|
10017 | 104 |
public void onCreate(){ |
105 |
super.onCreate(); |
|
106 |
nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
|
107 |
} |
|
108 |
public IBinder onBind(Intent intent) { |
|
109 |
return messenger.getBinder(); |
|
110 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
111 |
|
10017 | 112 |
private void runNextTask(){ |
113 |
if(asyncExecutor == null){//if (task isnt running right now) ... |
|
114 |
currentTask = downloadTasks.poll(); |
|
115 |
if(currentTask != null){ |
|
116 |
asyncExecutor = new DownloadAsyncTask(currentTask); |
|
117 |
asyncExecutor.execute(currentTask.getPackage()); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
121 |
|
10017 | 122 |
public void onDestroy(){ |
123 |
super.onDestroy(); |
|
124 |
asyncExecutor.cancel(false); |
|
125 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
126 |
|
10017 | 127 |
class DownloadTask { |
128 |
private final DownloadPackage pack; |
|
129 |
private TASK_STATE status = TASK_STATE.PENDING; |
|
130 |
private Notification progressNotification, doneNotification; |
|
6350 | 131 |
|
10017 | 132 |
//I expect little to no removeClient calls that's why we go for a list rather than a map |
133 |
private final List<Messenger> clients; |
|
6350 | 134 |
|
10017 | 135 |
public DownloadTask(DownloadPackage _pack){ |
136 |
pack = _pack; |
|
137 |
clients = new LinkedList<Messenger>(); |
|
138 |
} |
|
6350 | 139 |
|
10017 | 140 |
public void addClient(Messenger messenger){ |
141 |
clients.add(messenger); |
|
142 |
} |
|
143 |
public void removeClient(Messenger messenger){ |
|
144 |
clients.remove(messenger); |
|
145 |
} |
|
6350 | 146 |
|
10017 | 147 |
public DownloadPackage getPackage(){ |
148 |
return pack; |
|
149 |
} |
|
6350 | 150 |
|
10017 | 151 |
public TASK_STATE getStatus(){ |
152 |
return status; |
|
153 |
} |
|
6350 | 154 |
|
10017 | 155 |
public void sendMessageToClients(Message msg){ |
156 |
for(Messenger messenger : clients){ |
|
157 |
try { |
|
158 |
messenger.send(msg); |
|
159 |
} catch (RemoteException e) { |
|
160 |
e.printStackTrace(); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
6350 | 164 |
|
10017 | 165 |
/* |
166 |
* Callbacks called from the async tasks |
|
167 |
*/ |
|
6343 | 168 |
|
10017 | 169 |
//Thread safe method to let clients know the processing is starting and will process int max kbytes |
170 |
public void start(int max){ |
|
171 |
progressNotification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); |
|
172 |
progressNotification.flags |= Notification.FLAG_ONGOING_EVENT; |
|
6350 | 173 |
|
10017 | 174 |
contentView = new RemoteViews(getPackageName(), R.layout.notification); |
175 |
contentView.setProgressBar(R.id.notification_progress, 100, 34, false); |
|
176 |
progressNotification.contentView = contentView; |
|
6350 | 177 |
|
10017 | 178 |
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
179 |
progressNotification.contentIntent = contentIntent; |
|
6350 | 180 |
|
10017 | 181 |
startForeground(NOTIFICATION_PROCESSING, progressNotification); |
6350 | 182 |
|
10017 | 183 |
Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0); |
184 |
sendMessageToClients(msg); |
|
185 |
} |
|
6350 | 186 |
|
10017 | 187 |
//periodically gets called by the ASyncTask, we can't tell for sure when it's called |
188 |
public void update(int progress, int max, String fileName){ |
|
189 |
progress = (progress/1024); |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
190 |
|
10017 | 191 |
contentView.setProgressBar(R.id.notification_progress, max, progress, false); |
192 |
contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); |
|
193 |
nM.notify(NOTIFICATION_PROCESSING, progressNotification); |
|
6350 | 194 |
|
10017 | 195 |
sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName)); |
196 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
197 |
|
10017 | 198 |
//Call back from the ASync task when the task has either run into an error or finished otherwise |
199 |
public void done(int result){ |
|
200 |
switch(result){ |
|
201 |
case DownloadAsyncTask.EXIT_SUCCESS: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; |
|
202 |
case DownloadAsyncTask.EXIT_CONNERROR: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_CONNERROR, 0)); break; |
|
203 |
case DownloadAsyncTask.EXIT_FNF: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_FNF, 0)); break; |
|
204 |
case DownloadAsyncTask.EXIT_MD5: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_MD5, 0)); break; |
|
205 |
case DownloadAsyncTask.EXIT_URLFAIL: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_URLFAIL, 0)); break; |
|
206 |
case DownloadAsyncTask.EXIT_CANCELLED: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; |
|
207 |
} |
|
6350 | 208 |
|
10017 | 209 |
stopForeground(true); |
210 |
nM.cancel(NOTIFICATION_PROCESSING); |
|
211 |
||
212 |
String title = getString(R.string.notification_title); |
|
6350 | 213 |
|
10017 | 214 |
doneNotification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); |
215 |
doneNotification.flags |= Notification.FLAG_AUTO_CANCEL; |
|
216 |
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
217 |
doneNotification.setLatestEventInfo(DownloadService.this, title, getString(R.string.notification_done) + pack, contentIntent); |
|
218 |
nM.notify(pack.getId(), doneNotification); |
|
6350 | 219 |
|
10017 | 220 |
asyncExecutor = null; |
221 |
runNextTask();//see if there are more tasks |
|
222 |
} |
|
6350 | 223 |
|
10017 | 224 |
} |
6343 | 225 |
|
10017 | 226 |
enum TASK_STATE{ |
227 |
RUNNING, FINISHED, PENDING; |
|
228 |
} |
|
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
229 |
|
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
230 |
} |