author | Xeli |
Sun, 21 Aug 2011 22:16:55 +0200 | |
branch | hedgeroid |
changeset 5637 | fcc0e5b3d66b |
parent 5621 | ea796c83ea47 |
child 5671 | ba4c3a4c8b09 |
permissions | -rw-r--r-- |
5621 | 1 |
/* |
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
20 |
package org.hedgewars.mobile.Downloader; |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
21 |
|
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
22 |
import java.util.ArrayList; |
5397 | 23 |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
24 |
import org.hedgewars.mobile.MainActivity; |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
25 |
import org.hedgewars.mobile.R; |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
26 |
import org.hedgewars.mobile.Utils; |
5397 | 27 |
|
28 |
import android.app.Notification; |
|
29 |
import android.app.NotificationManager; |
|
30 |
import android.app.PendingIntent; |
|
31 |
import android.app.Service; |
|
32 |
import android.content.Intent; |
|
33 |
import android.os.Handler; |
|
34 |
import android.os.IBinder; |
|
35 |
import android.os.Message; |
|
36 |
import android.os.Messenger; |
|
37 |
import android.os.RemoteException; |
|
38 |
import android.util.Log; |
|
39 |
import android.widget.RemoteViews; |
|
40 |
||
41 |
public class DownloadService extends Service { |
|
42 |
||
5637 | 43 |
private final static String URL = "http://hedgewars.googlecode.com/files/data_5631.zip"; |
5397 | 44 |
public static final int MSG_CANCEL = 0; |
45 |
public static final int MSG_REGISTER_CLIENT = 1; |
|
46 |
public static final int MSG_UNREGISTER_CLIENT = 2; |
|
47 |
||
48 |
public static final int NOTIFICATION_PROCESSING = 0; |
|
49 |
public static final int NOTIFICATION_DONE = 1; |
|
50 |
||
51 |
private DownloadAsyncTask downloadTask; |
|
52 |
private final Messenger messenger = new Messenger(new DownloadHandler()); |
|
53 |
private NotificationManager nM; |
|
54 |
private RemoteViews contentView; |
|
55 |
private Notification notification; |
|
56 |
||
57 |
private ArrayList<Messenger> clientList = new ArrayList<Messenger>(); |
|
58 |
private Message onRegisterMessage = null; |
|
59 |
||
60 |
||
61 |
class DownloadHandler extends Handler{ |
|
62 |
||
63 |
public void handleMessage(Message msg){ |
|
64 |
switch(msg.what){ |
|
65 |
case MSG_CANCEL: |
|
66 |
downloadTask.cancel(false); |
|
67 |
break; |
|
68 |
case MSG_REGISTER_CLIENT: |
|
69 |
clientList.add(msg.replyTo); |
|
70 |
if(onRegisterMessage != null){ |
|
71 |
try { |
|
72 |
msg.replyTo.send(Message.obtain(onRegisterMessage)); |
|
73 |
} catch (RemoteException e) { |
|
74 |
e.printStackTrace(); |
|
75 |
} |
|
76 |
} |
|
77 |
break; |
|
78 |
case MSG_UNREGISTER_CLIENT: |
|
79 |
clientList.remove(msg.replyTo); |
|
80 |
break; |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
public final static int TASKID_START = 0; |
|
86 |
public final static int TASKID_CANCEL = 1; |
|
87 |
||
88 |
public int onStartCommand(Intent intent, int flags, int startId){ |
|
89 |
switch(intent.getIntExtra("taskID", TASKID_START)){ |
|
90 |
case TASKID_START: |
|
91 |
nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
|
92 |
||
93 |
notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); |
|
94 |
//notification.flags |= Notification.FLAG_ONGOING_EVENT;// | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE; |
|
95 |
notification.flags |= Notification.FLAG_ONGOING_EVENT; |
|
96 |
||
97 |
contentView = new RemoteViews(getPackageName(), R.layout.notification); |
|
98 |
contentView.setProgressBar(R.id.notification_progress, 100, 34, false); |
|
99 |
notification.contentView = contentView; |
|
100 |
||
101 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DownloadActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
102 |
notification.contentIntent = contentIntent; |
|
103 |
||
104 |
//nM.notify(NOTIFICATION_PROCESSING, notification); |
|
105 |
startForeground(NOTIFICATION_PROCESSING, notification); |
|
106 |
||
107 |
if(downloadTask == null){ |
|
108 |
downloadTask = new DownloadAsyncTask(this); |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
109 |
downloadTask.execute(Utils.getDownloadPath(this), URL); |
5397 | 110 |
} |
111 |
break; |
|
112 |
case TASKID_CANCEL: |
|
113 |
downloadTask.cancel(false); |
|
114 |
stopService(); |
|
115 |
break; |
|
116 |
} |
|
117 |
return 0; |
|
118 |
} |
|
119 |
||
120 |
public void onDestroy(){ |
|
121 |
Log.e("bla", "onDestroy"); |
|
122 |
downloadTask.cancel(false); |
|
123 |
} |
|
124 |
||
125 |
public IBinder onBind(Intent intent) { |
|
126 |
return messenger.getBinder(); |
|
127 |
} |
|
128 |
||
129 |
/* |
|
130 |
* Thread safe method to let clients know the processing is starting and will process int max kbytes |
|
131 |
* |
|
132 |
*/ |
|
133 |
public void start(int max){ |
|
134 |
onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1); |
|
135 |
sendMessageToClients(onRegisterMessage); |
|
136 |
} |
|
137 |
||
138 |
public void update(int progress, int max, String fileName){ |
|
139 |
progress = (progress/1024); |
|
140 |
updateNotification(progress, max, fileName); |
|
141 |
||
142 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName)); |
|
143 |
} |
|
144 |
public void done(boolean succesful){ |
|
145 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE)); |
|
146 |
stopService();//stopService clears all notifications and thus must be called before we show the ready notification |
|
147 |
showDoneNotification(); |
|
148 |
} |
|
149 |
||
150 |
private void stopService(){ |
|
151 |
nM.cancelAll(); |
|
152 |
stopForeground(true); |
|
153 |
stopSelf(); |
|
154 |
} |
|
155 |
||
156 |
private void updateNotification(int progress, int max, String fileName){ |
|
157 |
||
158 |
contentView.setProgressBar(R.id.notification_progress, max, progress, false); |
|
159 |
contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); |
|
160 |
nM.notify(NOTIFICATION_PROCESSING, notification); |
|
161 |
} |
|
162 |
||
163 |
private void showDoneNotification(){ |
|
164 |
nM.cancelAll(); |
|
165 |
stopForeground(true); |
|
166 |
||
167 |
String title = getString(R.string.notification_title); |
|
168 |
||
169 |
notification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); |
|
170 |
notification.flags |= Notification.FLAG_AUTO_CANCEL; |
|
171 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
172 |
notification.setLatestEventInfo(this, title, getString(R.string.notification_done), contentIntent); |
|
173 |
nM.notify(NOTIFICATION_DONE, notification); |
|
174 |
} |
|
175 |
private void sendMessageToClients(Message msg){ |
|
176 |
for(Messenger m : clientList){ |
|
177 |
try { |
|
178 |
m.send(Message.obtain(msg)); |
|
179 |
} catch (RemoteException e) {}//TODO should we catch this properly? |
|
180 |
} |
|
181 |
} |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
182 |
|
5397 | 183 |
} |