project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java
author Xeli
Mon, 14 Nov 2011 18:03:31 +0100
branchhedgeroid
changeset 6343 9df5a486f41e
parent 6049 7bc38086d771
child 6350 41b0a9955c47
permissions -rw-r--r--
first part of the new downloader implementation
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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    22
import java.util.ArrayList;
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    23
import java.util.LinkedList;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    24
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    25
import org.hedgewars.hedgeroid.MainActivity;
6049
7bc38086d771 wops missed this one: removed a 'fix' eclipse put in there
Xeli
parents: 6047
diff changeset
    26
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
    27
import org.hedgewars.hedgeroid.Utils;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    28
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.Notification;
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.NotificationManager;
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.app.PendingIntent;
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.app.Service;
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.content.Intent;
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    34
import android.os.AsyncTask;
6047
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.Handler;
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.IBinder;
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.os.Message;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
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
    39
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
    40
import android.preference.PreferenceManager;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    41
import android.util.Log;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
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
    43
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
public class DownloadService extends Service {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    46
	public final static int TASKID_SETUP = 0;
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    47
	public final static int TASKID_CANCEL = 1;
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    48
	public final static int TASKID_ADDTASK = 2;
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    49
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    50
	public final static String INTENT_TASKID = "taskId";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    51
	public final static String INTENT_TASK = "task";
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    52
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    53
	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
    54
	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
    55
	public static final int MSG_REGISTER_CLIENT = 1;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    56
	public static final int MSG_UNREGISTER_CLIENT = 2;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    57
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    58
	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
    59
	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
    60
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    61
	private DownloadAsyncTask asyncExecutor;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    62
	private final Messenger messenger = new Messenger(new DownloadHandler());
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    63
	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
    64
	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
    65
	private Notification notification;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    66
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    67
	private LinkedList<DownloadTask> downloadTasks = new LinkedList<DownloadTask>();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    68
	private ArrayList<Messenger> clientList = new ArrayList<Messenger>();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    69
	private Message onRegisterMessage = null;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    70
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    71
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    72
	class DownloadHandler extends Handler{
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
		public void handleMessage(Message msg){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    75
			switch(msg.what){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    76
			case MSG_CANCEL:
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    77
				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
    78
				break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    79
			case MSG_REGISTER_CLIENT:
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    80
				clientList.add(msg.replyTo);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    81
				if(onRegisterMessage != null){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    82
					try {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    83
						msg.replyTo.send(Message.obtain(onRegisterMessage));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    84
					} catch (RemoteException e) {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    85
						e.printStackTrace();
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    86
					}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    87
				}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    88
				break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    89
			case MSG_UNREGISTER_CLIENT:
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    90
				clientList.remove(msg.replyTo);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    91
				break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    92
			}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    93
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
	}
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    95
	public IBinder onBind(Intent intent) {
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    96
		return messenger.getBinder();
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    97
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    98
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    99
	/**
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   100
	 * This is the main method which controls how DownloadService and DownloadAsyncTask are running
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   101
	 */
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   102
	public int onStartCommand(Intent intent, int flags, int startId){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   103
		switch(intent.getIntExtra("taskID", TASKID_SETUP)){
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   104
		case TASKID_SETUP:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   105
			nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   106
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   107
			notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis());
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   108
			notification.flags |= Notification.FLAG_ONGOING_EVENT;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   109
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   110
			contentView = new RemoteViews(getPackageName(), R.layout.notification);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   111
			contentView.setProgressBar(R.id.notification_progress, 100, 34, false);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   112
			notification.contentView = contentView;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   113
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   114
			PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DownloadActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   115
			notification.contentIntent = contentIntent;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   116
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   117
			asyncExecutor = new DownloadAsyncTask(this);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   118
			break;
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   119
		case TASKID_ADDTASK:
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   120
			//Add downloadtask to the queue
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   121
			DownloadTask task = intent.getParcelableExtra(DownloadService.INTENT_TASK);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   122
			downloadTasks.offer(task);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   123
			runNextTask();
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   124
			break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   125
		case TASKID_CANCEL:
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   126
			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
   127
			break;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   128
		}		
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   129
		return 0;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   130
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   131
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   132
	private synchronized void runNextTask(){
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   133
		if(!asyncExecutor.getStatus().equals(AsyncTask.Status.RUNNING)){//if the task isnt running right now...
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   134
			DownloadTask task = downloadTasks.poll();
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   135
			if(task == null){
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   136
				startForeground(NOTIFICATION_PROCESSING, notification);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   137
				asyncExecutor.execute(task);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   138
			}
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   139
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   140
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   141
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   142
	public void onDestroy(){
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   143
		super.onDestroy();
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   144
		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
   145
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   146
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   147
	/*
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   148
	 * Callbacks called from the async tasks
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   149
	 */
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   150
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   151
	//Thread safe method to let clients know the processing is starting and will process int max kbytes
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   152
	public void start(int max){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   153
		onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   154
		sendMessageToClients(onRegisterMessage);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   155
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   156
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   157
	//periodically gets called by the ASyncTask, we can't tell for sure when it's called
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   158
	public void update(int progress, int max, String fileName){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   159
		progress = (progress/1024);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   160
		updateNotification(progress, max, fileName);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   161
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   162
		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   163
	}
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   164
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   165
	//Call back from the ASync task when the task has either run into an error or finished otherwise
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   166
	public void done(boolean succesful){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   167
		if(succesful){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   168
			sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   169
		}else sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_FAILED));
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   170
		nM.cancel(NOTIFICATION_PROCESSING);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   171
		stopForeground(true);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   172
		showDoneNotification();
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   173
		runNextTask();//see if there are more tasks
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   174
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   175
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   176
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   177
	private void updateNotification(int progress, int max, String fileName){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   178
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   179
		contentView.setProgressBar(R.id.notification_progress, max, progress, false);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   180
		contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   181
		nM.notify(NOTIFICATION_PROCESSING, notification);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   182
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   183
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   184
	private void showDoneNotification(){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   185
		String title = getString(R.string.notification_title);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   186
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   187
		notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   188
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   189
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   190
		notification.setLatestEventInfo(this, title, getString(R.string.notification_done), contentIntent);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   191
		nM.notify(NOTIFICATION_DONE, notification);
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   192
	}
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   193
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   194
	private void sendMessageToClients(Message msg){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   195
		for(Messenger m : clientList){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   196
			try {
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   197
				m.send(Message.obtain(msg));
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   198
			} catch (RemoteException e) {}//TODO should we catch this properly?
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   199
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   200
	}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   201
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   202
}