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