author | Xeli |
Sun, 09 Oct 2011 20:41:17 +0200 | |
branch | hedgeroid |
changeset 6045 | 9a7cc0f29430 |
parent 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; |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
38 |
import android.preference.PreferenceManager; |
5397 | 39 |
import android.util.Log; |
40 |
import android.widget.RemoteViews; |
|
41 |
||
42 |
public class DownloadService extends Service { |
|
43 |
||
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
44 |
public static final String PREF_DOWNLOADED = "downloaded"; |
5397 | 45 |
public static final int MSG_CANCEL = 0; |
46 |
public static final int MSG_REGISTER_CLIENT = 1; |
|
47 |
public static final int MSG_UNREGISTER_CLIENT = 2; |
|
48 |
||
49 |
public static final int NOTIFICATION_PROCESSING = 0; |
|
50 |
public static final int NOTIFICATION_DONE = 1; |
|
51 |
||
52 |
private DownloadAsyncTask downloadTask; |
|
53 |
private final Messenger messenger = new Messenger(new DownloadHandler()); |
|
54 |
private NotificationManager nM; |
|
55 |
private RemoteViews contentView; |
|
56 |
private Notification notification; |
|
57 |
||
58 |
private ArrayList<Messenger> clientList = new ArrayList<Messenger>(); |
|
59 |
private Message onRegisterMessage = null; |
|
60 |
||
61 |
||
62 |
class DownloadHandler extends Handler{ |
|
63 |
||
64 |
public void handleMessage(Message msg){ |
|
65 |
switch(msg.what){ |
|
66 |
case MSG_CANCEL: |
|
67 |
downloadTask.cancel(false); |
|
68 |
break; |
|
69 |
case MSG_REGISTER_CLIENT: |
|
70 |
clientList.add(msg.replyTo); |
|
71 |
if(onRegisterMessage != null){ |
|
72 |
try { |
|
73 |
msg.replyTo.send(Message.obtain(onRegisterMessage)); |
|
74 |
} catch (RemoteException e) { |
|
75 |
e.printStackTrace(); |
|
76 |
} |
|
77 |
} |
|
78 |
break; |
|
79 |
case MSG_UNREGISTER_CLIENT: |
|
80 |
clientList.remove(msg.replyTo); |
|
81 |
break; |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
public final static int TASKID_START = 0; |
|
87 |
public final static int TASKID_CANCEL = 1; |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
88 |
public final static int TASKID_RETRY = 2; |
5397 | 89 |
|
90 |
public int onStartCommand(Intent intent, int flags, int startId){ |
|
91 |
switch(intent.getIntExtra("taskID", TASKID_START)){ |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
92 |
case TASKID_RETRY: |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
93 |
if(downloadTask != null){ |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
94 |
downloadTask.cancel(false); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
95 |
downloadTask = null; |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
96 |
} |
5397 | 97 |
case TASKID_START: |
98 |
nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
|
99 |
||
100 |
notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); |
|
101 |
//notification.flags |= Notification.FLAG_ONGOING_EVENT;// | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE; |
|
102 |
notification.flags |= Notification.FLAG_ONGOING_EVENT; |
|
103 |
||
104 |
contentView = new RemoteViews(getPackageName(), R.layout.notification); |
|
105 |
contentView.setProgressBar(R.id.notification_progress, 100, 34, false); |
|
106 |
notification.contentView = contentView; |
|
107 |
||
108 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DownloadActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
109 |
notification.contentIntent = contentIntent; |
|
110 |
||
111 |
//nM.notify(NOTIFICATION_PROCESSING, notification); |
|
112 |
startForeground(NOTIFICATION_PROCESSING, notification); |
|
113 |
||
114 |
if(downloadTask == null){ |
|
115 |
downloadTask = new DownloadAsyncTask(this); |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
116 |
downloadTask.execute(Utils.getDownloadPath(this)); |
5397 | 117 |
} |
118 |
break; |
|
119 |
case TASKID_CANCEL: |
|
120 |
downloadTask.cancel(false); |
|
121 |
stopService(); |
|
122 |
break; |
|
123 |
} |
|
124 |
return 0; |
|
125 |
} |
|
126 |
||
127 |
public void onDestroy(){ |
|
128 |
Log.e("bla", "onDestroy"); |
|
129 |
downloadTask.cancel(false); |
|
130 |
} |
|
131 |
||
132 |
public IBinder onBind(Intent intent) { |
|
133 |
return messenger.getBinder(); |
|
134 |
} |
|
135 |
||
136 |
/* |
|
137 |
* Thread safe method to let clients know the processing is starting and will process int max kbytes |
|
138 |
*/ |
|
139 |
public void start(int max){ |
|
140 |
onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1); |
|
141 |
sendMessageToClients(onRegisterMessage); |
|
142 |
} |
|
143 |
||
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
144 |
/* |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
145 |
* periodically gets called by the ASyncTask, we can't tell for sure when it's called |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
146 |
*/ |
5397 | 147 |
public void update(int progress, int max, String fileName){ |
148 |
progress = (progress/1024); |
|
149 |
updateNotification(progress, max, fileName); |
|
150 |
||
151 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName)); |
|
152 |
} |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
153 |
|
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
154 |
/* |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
155 |
* Call back from the ASync task when the task has either run into an error or finished otherwise |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
156 |
*/ |
5397 | 157 |
public void done(boolean succesful){ |
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
158 |
if(succesful){ |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
159 |
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean(DownloadService.PREF_DOWNLOADED, true).commit(); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
160 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE)); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5637
diff
changeset
|
161 |
}else sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_FAILED)); |
5397 | 162 |
stopService();//stopService clears all notifications and thus must be called before we show the ready notification |
163 |
showDoneNotification(); |
|
164 |
} |
|
165 |
||
166 |
private void stopService(){ |
|
167 |
nM.cancelAll(); |
|
168 |
stopForeground(true); |
|
169 |
stopSelf(); |
|
170 |
} |
|
171 |
||
172 |
private void updateNotification(int progress, int max, String fileName){ |
|
173 |
||
174 |
contentView.setProgressBar(R.id.notification_progress, max, progress, false); |
|
175 |
contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); |
|
176 |
nM.notify(NOTIFICATION_PROCESSING, notification); |
|
177 |
} |
|
178 |
||
179 |
private void showDoneNotification(){ |
|
180 |
nM.cancelAll(); |
|
181 |
stopForeground(true); |
|
182 |
||
183 |
String title = getString(R.string.notification_title); |
|
184 |
||
185 |
notification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); |
|
186 |
notification.flags |= Notification.FLAG_AUTO_CANCEL; |
|
187 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
188 |
notification.setLatestEventInfo(this, title, getString(R.string.notification_done), contentIntent); |
|
189 |
nM.notify(NOTIFICATION_DONE, notification); |
|
190 |
} |
|
191 |
private void sendMessageToClients(Message msg){ |
|
192 |
for(Messenger m : clientList){ |
|
193 |
try { |
|
194 |
m.send(Message.obtain(msg)); |
|
195 |
} catch (RemoteException e) {}//TODO should we catch this properly? |
|
196 |
} |
|
197 |
} |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
198 |
|
5397 | 199 |
} |