project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java
author Xeli
Mon, 28 Nov 2011 16:21:35 +0100
changeset 6458 eadb2db1ae83
parent 6446 a49d01b96185
child 6566 bc1214f66ee4
permissions -rw-r--r--
android: use linkedlist instead of the deque interface, Deque<> isn't supported until gingerbread
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     3
 * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     4
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     8
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * 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
    13
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
 * 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
    15
 * along with this program; if not, write to the Free Software
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    17
 */
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
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    22
import java.util.LinkedList;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    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 {
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    40
	public final static String INTENT_TASKID = "taskId";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    41
	public final static String INTENT_TASK = "task";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    42
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
	public static final String PREF_DOWNLOADED = "downloaded";
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
	public static final int MSG_CANCEL = 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
	public static final int MSG_UNREGISTER_CLIENT = 2;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    48
	public static final int NOTIFICATION_PROCESSING = 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
	public static final int NOTIFICATION_DONE = 1;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    50
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    51
	private DownloadAsyncTask asyncExecutor;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    52
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    53
	private DownloadHandler handler = new DownloadHandler();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    54
	private final Messenger messenger = new Messenger(handler);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    55
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    56
	private NotificationManager nM;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    57
	private RemoteViews contentView;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    58
6458
eadb2db1ae83 android: use linkedlist instead of the deque interface, Deque<> isn't supported until gingerbread
Xeli
parents: 6446
diff changeset
    59
	private LinkedList<DownloadTask> downloadTasks = new LinkedList<DownloadTask>();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    64
		public void handleMessage(Message msg){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    65
			if(msg.obj != null){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    66
				DownloadPackage pack = (DownloadPackage) msg.obj;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    67
				DownloadTask task = null;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    68
				Messenger replyToMessenger = msg.replyTo;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    69
				for(DownloadTask _task : downloadTasks){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    70
					if(_task.getPackage().equals(pack)){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    71
						task = _task;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    72
						break;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    73
					}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    74
				}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    75
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    76
				switch(msg.what){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    77
				case MSG_ADDTASK:
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    78
					if(task == null){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    79
						task = new DownloadTask(pack);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    80
						downloadTasks.add(task);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    81
					}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    82
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    83
					task.addClient(replyToMessenger);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    84
					runNextTask();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    85
					return;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    86
				case MSG_CANCEL:
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    87
					if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.PENDING){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    88
						downloadTasks.remove(task);
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    89
					}
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
    90
					if(currentTask.getPackage().equals(pack)){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    91
						asyncExecutor.cancel(false);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    92
					}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    93
					return;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    94
				case MSG_UNREGISTER_CLIENT:
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    95
					if(task != null){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    96
						task.removeClient(replyToMessenger);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    97
					}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    98
					return;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    99
				}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   100
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   102
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   103
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   104
	public void onCreate(){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   105
		super.onCreate();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   106
		nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   107
	}
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   108
	public IBinder onBind(Intent intent) {
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   109
		return messenger.getBinder();
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   112
	private void runNextTask(){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   113
		if(asyncExecutor == null){//if (task isnt running right now) ...
6458
eadb2db1ae83 android: use linkedlist instead of the deque interface, Deque<> isn't supported until gingerbread
Xeli
parents: 6446
diff changeset
   114
			currentTask = downloadTasks.poll();
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   115
			if(currentTask != null){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   116
				asyncExecutor = new DownloadAsyncTask(currentTask);
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   117
				asyncExecutor.execute(currentTask.getPackage());
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   118
			}
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   119
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   120
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   121
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   122
	public void onDestroy(){
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   123
		super.onDestroy();
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   124
		asyncExecutor.cancel(false);	
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   125
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   126
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   127
	class DownloadTask {
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   128
		private final DownloadPackage pack;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   129
		private TASK_STATE status = TASK_STATE.PENDING;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   130
		private Notification progressNotification, doneNotification;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   131
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   132
		//I expect little to no removeClient calls that's why we go for a list rather than a map
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   133
		private final List<Messenger> clients;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   134
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   135
		public DownloadTask(DownloadPackage _pack){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   136
			pack = _pack;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   137
			clients = new LinkedList<Messenger>();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   138
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   139
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   140
		public void addClient(Messenger messenger){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   141
			clients.add(messenger);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   142
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   143
		public void removeClient(Messenger messenger){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   144
			clients.remove(messenger);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   145
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   146
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   147
		public DownloadPackage getPackage(){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   148
			return pack;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   149
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   150
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   151
		public TASK_STATE getStatus(){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   152
			return status;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   153
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   154
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   155
		public void sendMessageToClients(Message msg){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   156
			for(Messenger messenger : clients){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   157
				try {
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   158
					messenger.send(msg);		
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   159
				} catch (RemoteException e) {
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   160
					e.printStackTrace();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   161
				}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   162
			}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   163
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   164
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   165
		/*
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   166
		 * Callbacks called from the async tasks
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   167
		 */
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   168
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   169
		//Thread safe method to let clients know the processing is starting and will process int max kbytes
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   170
		public void start(int max){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   171
			progressNotification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis());
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   172
			progressNotification.flags |= Notification.FLAG_ONGOING_EVENT;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   173
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   174
			contentView = new RemoteViews(getPackageName(), R.layout.notification);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   175
			contentView.setProgressBar(R.id.notification_progress, 100, 34, false);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   176
			progressNotification.contentView = contentView;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   177
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   178
			PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadFragment.class), Intent.FLAG_ACTIVITY_NEW_TASK);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   179
			progressNotification.contentIntent = contentIntent;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   180
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   181
			startForeground(NOTIFICATION_PROCESSING, progressNotification);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   182
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   183
			Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   184
			sendMessageToClients(msg);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   185
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   186
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   187
		//periodically gets called by the ASyncTask, we can't tell for sure when it's called
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   188
		public void update(int progress, int max, String fileName){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   191
			contentView.setProgressBar(R.id.notification_progress, max, progress, false);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   192
			contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max));
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   193
			nM.notify(NOTIFICATION_PROCESSING, progressNotification);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   194
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   195
			sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName));
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   198
		//Call back from the ASync task when the task has either run into an error or finished otherwise
6437
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   199
		public void done(int result){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   200
			switch(result){
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   201
			case DownloadAsyncTask.EXIT_SUCCESS: 	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   202
			case DownloadAsyncTask.EXIT_CONNERROR:  sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   203
			case DownloadAsyncTask.EXIT_FNF:		sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   204
			case DownloadAsyncTask.EXIT_MD5:		sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   205
			case DownloadAsyncTask.EXIT_URLFAIL:	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   206
			case DownloadAsyncTask.EXIT_CANCELLED:	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break;
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   207
			}
4ed58839b13b Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents: 6350
diff changeset
   208
			
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   209
			stopForeground(true);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   210
			nM.cancel(NOTIFICATION_PROCESSING);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   211
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   212
			String title = getString(R.string.notification_title);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   213
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   214
			doneNotification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   215
			doneNotification.flags |= Notification.FLAG_AUTO_CANCEL;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   216
			PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   217
			doneNotification.setLatestEventInfo(DownloadService.this, title, getString(R.string.notification_done) + pack, contentIntent);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   218
			nM.notify(pack.getId(), doneNotification);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   219
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   220
			asyncExecutor = null;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   221
			runNextTask();//see if there are more tasks
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   222
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   223
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   224
	}
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   225
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   226
	enum TASK_STATE{
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   227
		RUNNING, FINISHED, PENDING;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   228
	}
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
}