author | Xeli |
Thu, 14 Jul 2011 15:41:26 +0200 | |
branch | hedgeroid |
changeset 5412 | ab055114c788 |
parent 5406 | project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/DownloadService.java@a3f98a8a0b80 |
child 5514 | 294c92eea729 |
permissions | -rw-r--r-- |
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
1 |
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
|
2 |
|
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
3 |
import java.util.ArrayList; |
5397 | 4 |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
5 |
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
|
6 |
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
|
7 |
import org.hedgewars.mobile.Utils; |
5397 | 8 |
|
9 |
import android.app.Notification; |
|
10 |
import android.app.NotificationManager; |
|
11 |
import android.app.PendingIntent; |
|
12 |
import android.app.Service; |
|
13 |
import android.content.Intent; |
|
14 |
import android.os.Handler; |
|
15 |
import android.os.IBinder; |
|
16 |
import android.os.Message; |
|
17 |
import android.os.Messenger; |
|
18 |
import android.os.RemoteException; |
|
19 |
import android.util.Log; |
|
20 |
import android.widget.RemoteViews; |
|
21 |
||
22 |
public class DownloadService extends Service { |
|
23 |
||
24 |
private final static String URL = "http://www.xelification.com/tmp/data.zip"; |
|
25 |
public static final int MSG_CANCEL = 0; |
|
26 |
public static final int MSG_REGISTER_CLIENT = 1; |
|
27 |
public static final int MSG_UNREGISTER_CLIENT = 2; |
|
28 |
||
29 |
public static final int NOTIFICATION_PROCESSING = 0; |
|
30 |
public static final int NOTIFICATION_DONE = 1; |
|
31 |
||
32 |
private DownloadAsyncTask downloadTask; |
|
33 |
private final Messenger messenger = new Messenger(new DownloadHandler()); |
|
34 |
private NotificationManager nM; |
|
35 |
private RemoteViews contentView; |
|
36 |
private Notification notification; |
|
37 |
||
38 |
private ArrayList<Messenger> clientList = new ArrayList<Messenger>(); |
|
39 |
private Message onRegisterMessage = null; |
|
40 |
||
41 |
||
42 |
class DownloadHandler extends Handler{ |
|
43 |
||
44 |
public void handleMessage(Message msg){ |
|
45 |
switch(msg.what){ |
|
46 |
case MSG_CANCEL: |
|
47 |
downloadTask.cancel(false); |
|
48 |
break; |
|
49 |
case MSG_REGISTER_CLIENT: |
|
50 |
clientList.add(msg.replyTo); |
|
51 |
if(onRegisterMessage != null){ |
|
52 |
try { |
|
53 |
msg.replyTo.send(Message.obtain(onRegisterMessage)); |
|
54 |
} catch (RemoteException e) { |
|
55 |
e.printStackTrace(); |
|
56 |
} |
|
57 |
} |
|
58 |
break; |
|
59 |
case MSG_UNREGISTER_CLIENT: |
|
60 |
clientList.remove(msg.replyTo); |
|
61 |
break; |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
public final static int TASKID_START = 0; |
|
67 |
public final static int TASKID_CANCEL = 1; |
|
68 |
||
69 |
public int onStartCommand(Intent intent, int flags, int startId){ |
|
70 |
switch(intent.getIntExtra("taskID", TASKID_START)){ |
|
71 |
case TASKID_START: |
|
72 |
nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
|
73 |
||
74 |
notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); |
|
75 |
//notification.flags |= Notification.FLAG_ONGOING_EVENT;// | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE; |
|
76 |
notification.flags |= Notification.FLAG_ONGOING_EVENT; |
|
77 |
||
78 |
contentView = new RemoteViews(getPackageName(), R.layout.notification); |
|
79 |
contentView.setProgressBar(R.id.notification_progress, 100, 34, false); |
|
80 |
notification.contentView = contentView; |
|
81 |
||
82 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DownloadActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
83 |
notification.contentIntent = contentIntent; |
|
84 |
||
85 |
//nM.notify(NOTIFICATION_PROCESSING, notification); |
|
86 |
startForeground(NOTIFICATION_PROCESSING, notification); |
|
87 |
||
88 |
if(downloadTask == null){ |
|
89 |
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
|
90 |
downloadTask.execute(Utils.getDownloadPath(this), URL); |
5397 | 91 |
} |
92 |
break; |
|
93 |
case TASKID_CANCEL: |
|
94 |
downloadTask.cancel(false); |
|
95 |
stopService(); |
|
96 |
break; |
|
97 |
} |
|
98 |
return 0; |
|
99 |
} |
|
100 |
||
101 |
public void onDestroy(){ |
|
102 |
Log.e("bla", "onDestroy"); |
|
103 |
downloadTask.cancel(false); |
|
104 |
} |
|
105 |
||
106 |
public IBinder onBind(Intent intent) { |
|
107 |
return messenger.getBinder(); |
|
108 |
} |
|
109 |
||
110 |
/* |
|
111 |
* Thread safe method to let clients know the processing is starting and will process int max kbytes |
|
112 |
* |
|
113 |
*/ |
|
114 |
public void start(int max){ |
|
115 |
onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1); |
|
116 |
sendMessageToClients(onRegisterMessage); |
|
117 |
} |
|
118 |
||
119 |
public void update(int progress, int max, String fileName){ |
|
120 |
progress = (progress/1024); |
|
121 |
updateNotification(progress, max, fileName); |
|
122 |
||
123 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName)); |
|
124 |
} |
|
125 |
public void done(boolean succesful){ |
|
126 |
sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE)); |
|
127 |
stopService();//stopService clears all notifications and thus must be called before we show the ready notification |
|
128 |
showDoneNotification(); |
|
129 |
} |
|
130 |
||
131 |
private void stopService(){ |
|
132 |
nM.cancelAll(); |
|
133 |
stopForeground(true); |
|
134 |
stopSelf(); |
|
135 |
} |
|
136 |
||
137 |
private void updateNotification(int progress, int max, String fileName){ |
|
138 |
||
139 |
contentView.setProgressBar(R.id.notification_progress, max, progress, false); |
|
140 |
contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); |
|
141 |
nM.notify(NOTIFICATION_PROCESSING, notification); |
|
142 |
} |
|
143 |
||
144 |
private void showDoneNotification(){ |
|
145 |
nM.cancelAll(); |
|
146 |
stopForeground(true); |
|
147 |
||
148 |
String title = getString(R.string.notification_title); |
|
149 |
||
150 |
notification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); |
|
151 |
notification.flags |= Notification.FLAG_AUTO_CANCEL; |
|
152 |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); |
|
153 |
notification.setLatestEventInfo(this, title, getString(R.string.notification_done), contentIntent); |
|
154 |
nM.notify(NOTIFICATION_DONE, notification); |
|
155 |
} |
|
156 |
private void sendMessageToClients(Message msg){ |
|
157 |
for(Messenger m : clientList){ |
|
158 |
try { |
|
159 |
m.send(Message.obtain(msg)); |
|
160 |
} catch (RemoteException e) {}//TODO should we catch this properly? |
|
161 |
} |
|
162 |
} |
|
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5406
diff
changeset
|
163 |
|
5397 | 164 |
} |