project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/DownloadService.java
author Xeli
Thu, 14 Jul 2011 15:59:29 +0200
branchhedgeroid
changeset 5437 19a7b798f73a
parent 5406 a3f98a8a0b80
permissions -rw-r--r--
Removed double icons introduced in 5411:9b82f4f9fba6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     2
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     3
import java.io.File;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     4
import java.util.ArrayList;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     5
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     6
import android.app.Notification;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     7
import android.app.NotificationManager;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     8
import android.app.PendingIntent;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
     9
import android.app.Service;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    10
import android.content.Context;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    11
import android.content.Intent;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    12
import android.os.Handler;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    13
import android.os.IBinder;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    14
import android.os.Message;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    15
import android.os.Messenger;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    16
import android.os.RemoteException;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    17
import android.util.Log;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    18
import android.widget.RemoteViews;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    19
import android.widget.Toast;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    20
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    21
public class DownloadService extends Service {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    22
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    23
	private final static String URL = "http://www.xelification.com/tmp/data.zip";
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    24
	public static final int MSG_CANCEL = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    25
	public static final int MSG_REGISTER_CLIENT = 1;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    26
	public static final int MSG_UNREGISTER_CLIENT = 2;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    27
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    28
	public static final int NOTIFICATION_PROCESSING = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    29
	public static final int NOTIFICATION_DONE = 1;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    30
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    31
	private DownloadAsyncTask downloadTask;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    32
	private final Messenger messenger = new Messenger(new DownloadHandler());
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    33
	private NotificationManager nM;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    34
	private RemoteViews contentView;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    35
	private Notification notification;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    36
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    37
	private ArrayList<Messenger> clientList = new ArrayList<Messenger>();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    38
	private Message onRegisterMessage = null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    39
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
	class DownloadHandler extends Handler{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    42
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    43
		public void handleMessage(Message msg){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    44
			switch(msg.what){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    45
			case MSG_CANCEL:
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    46
				downloadTask.cancel(false);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    47
				break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    48
			case MSG_REGISTER_CLIENT:
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    49
				clientList.add(msg.replyTo);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    50
				if(onRegisterMessage != null){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    51
					try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    52
						msg.replyTo.send(Message.obtain(onRegisterMessage));
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    53
					} catch (RemoteException e) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    54
						e.printStackTrace();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    55
					}
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
				break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    58
			case MSG_UNREGISTER_CLIENT:
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    59
				clientList.remove(msg.replyTo);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    60
				break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    61
			}
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
	public final static int TASKID_START = 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    66
	public final static int TASKID_CANCEL = 1;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    67
	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    68
	public int onStartCommand(Intent intent, int flags, int startId){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    69
		switch(intent.getIntExtra("taskID", TASKID_START)){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    70
		case TASKID_START:
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    71
			nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    72
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    73
			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
    74
			//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
    75
			notification.flags |= Notification.FLAG_ONGOING_EVENT;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    76
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    77
			contentView = new RemoteViews(getPackageName(), R.layout.notification);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    78
			contentView.setProgressBar(R.id.notification_progress, 100, 34, false);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    79
			notification.contentView = contentView;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    80
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    81
			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
    82
			notification.contentIntent = contentIntent;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    83
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    84
			//nM.notify(NOTIFICATION_PROCESSING, notification);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    85
			startForeground(NOTIFICATION_PROCESSING, notification);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    86
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    87
			if(downloadTask == null){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    88
				downloadTask = new DownloadAsyncTask(this);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    89
				downloadTask.execute(getDownloadPath(this), URL);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    90
			}	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    91
			break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    92
		case TASKID_CANCEL:
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    93
			downloadTask.cancel(false);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    94
			stopService();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    95
			break;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    96
		}		
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    97
		return 0;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
    98
	}
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
	public void onDestroy(){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   101
		Log.e("bla", "onDestroy");
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   102
		downloadTask.cancel(false);	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   103
	}
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
	public IBinder onBind(Intent intent) {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   106
		return messenger.getBinder();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   107
	}
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
	 * 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
   111
	 * 
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
	public void start(int max){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   114
		onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   115
		sendMessageToClients(onRegisterMessage);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   116
	}
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
	public void update(int progress, int max, String fileName){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   119
		progress = (progress/1024);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   120
		updateNotification(progress, max, fileName);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   121
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   122
		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName));
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   123
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   124
	public void done(boolean succesful){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   125
		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE));
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   126
		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
   127
		showDoneNotification();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   128
	}
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
	private void stopService(){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   131
		nM.cancelAll();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   132
		stopForeground(true);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   133
		stopSelf();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   134
	}
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
	private void updateNotification(int progress, int max, String fileName){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   137
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   138
		contentView.setProgressBar(R.id.notification_progress, max, progress, false);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   139
		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
   140
		nM.notify(NOTIFICATION_PROCESSING, notification);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   141
	}
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
	private void showDoneNotification(){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   144
		nM.cancelAll();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   145
		stopForeground(true);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   146
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   147
		String title = getString(R.string.notification_title);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   148
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   149
		notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   150
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   151
		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
   152
		notification.setLatestEventInfo(this, title, getString(R.string.notification_done), contentIntent);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   153
		nM.notify(NOTIFICATION_DONE, notification);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   154
	}	
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   155
	private void sendMessageToClients(Message msg){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   156
		for(Messenger m : clientList){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   157
			try {
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   158
				m.send(Message.obtain(msg));
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   159
			} catch (RemoteException e) {}//TODO should we catch this properly?
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   160
		}
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
	public static String getDownloadPath(Context c){
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   163
		File f =  c.getExternalCacheDir();
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   164
		if(f != null){
5406
a3f98a8a0b80 fixed Data path
Xeli
parents: 5397
diff changeset
   165
			return f.getAbsolutePath() + '/';
5397
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   166
		}else{
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   167
			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG);
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   168
			return null;
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   169
		}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   170
	}
4ae1b082e4ba Added download functionality and changed some icons
Xeli
parents:
diff changeset
   171
}