project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java
changeset 6437 4ed58839b13b
parent 6350 41b0a9955c47
child 6446 a49d01b96185
equal deleted inserted replaced
6436:f1a42ba9cab2 6437:4ed58839b13b
    17  */
    17  */
    18 
    18 
    19 
    19 
    20 package org.hedgewars.hedgeroid.Downloader;
    20 package org.hedgewars.hedgeroid.Downloader;
    21 
    21 
    22 import java.util.ArrayList;
       
    23 import java.util.Deque;
    22 import java.util.Deque;
    24 import java.util.LinkedList;
    23 import java.util.LinkedList;
    25 import java.util.List;
    24 import java.util.List;
    26 
    25 
    27 import org.hedgewars.hedgeroid.MainActivity;
       
    28 import org.hedgewars.hedgeroid.R;
    26 import org.hedgewars.hedgeroid.R;
    29 
    27 
    30 import android.app.Notification;
    28 import android.app.Notification;
    31 import android.app.NotificationManager;
    29 import android.app.NotificationManager;
    32 import android.app.PendingIntent;
    30 import android.app.PendingIntent;
    33 import android.app.Service;
    31 import android.app.Service;
    34 import android.content.Intent;
    32 import android.content.Intent;
    35 import android.os.AsyncTask;
       
    36 import android.os.Handler;
    33 import android.os.Handler;
    37 import android.os.IBinder;
    34 import android.os.IBinder;
    38 import android.os.Message;
    35 import android.os.Message;
    39 import android.os.Messenger;
    36 import android.os.Messenger;
    40 import android.os.RemoteException;
    37 import android.os.RemoteException;
    59 
    56 
    60 	private NotificationManager nM;
    57 	private NotificationManager nM;
    61 	private RemoteViews contentView;
    58 	private RemoteViews contentView;
    62 
    59 
    63 	private Deque<DownloadTask> downloadTasks = new LinkedList<DownloadTask>();
    60 	private Deque<DownloadTask> downloadTasks = new LinkedList<DownloadTask>();
       
    61 	private DownloadTask currentTask = null;
    64 
    62 
    65 	public class DownloadHandler extends Handler{
    63 	public class DownloadHandler extends Handler{
    66 
    64 
    67 		public void handleMessage(Message msg){
    65 		public void handleMessage(Message msg){
    68 			if(msg.obj != null){
    66 			if(msg.obj != null){
    85 
    83 
    86 					task.addClient(replyToMessenger);
    84 					task.addClient(replyToMessenger);
    87 					runNextTask();
    85 					runNextTask();
    88 					return;
    86 					return;
    89 				case MSG_CANCEL:
    87 				case MSG_CANCEL:
    90 					if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.RUNNING){
    88 					if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.PENDING){
       
    89 						downloadTasks.remove(task);
       
    90 					}
       
    91 					if(currentTask.getPackage().equals(pack)){
    91 						asyncExecutor.cancel(false);
    92 						asyncExecutor.cancel(false);
    92 					}
    93 					}
    93 					return;
    94 					return;
    94 				case MSG_UNREGISTER_CLIENT:
    95 				case MSG_UNREGISTER_CLIENT:
    95 					if(task != null){
    96 					if(task != null){
   109 		return messenger.getBinder();
   110 		return messenger.getBinder();
   110 	}
   111 	}
   111 
   112 
   112 	private void runNextTask(){
   113 	private void runNextTask(){
   113 		if(asyncExecutor == null){//if (task isnt running right now) ...
   114 		if(asyncExecutor == null){//if (task isnt running right now) ...
   114 			DownloadTask task = downloadTasks.pollFirst();
   115 			currentTask = downloadTasks.pollFirst();
   115 			if(task != null){
   116 			if(currentTask != null){
   116 				asyncExecutor = new DownloadAsyncTask(task);
   117 				asyncExecutor = new DownloadAsyncTask(currentTask);
   117 				asyncExecutor.execute(task.getPackage());
   118 				asyncExecutor.execute(currentTask.getPackage());
   118 			}
   119 			}
   119 		}
   120 		}
   120 	}
   121 	}
   121 
   122 
   122 	public void onDestroy(){
   123 	public void onDestroy(){
   176 			progressNotification.contentView = contentView;
   177 			progressNotification.contentView = contentView;
   177 
   178 
   178 			PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadFragment.class), Intent.FLAG_ACTIVITY_NEW_TASK);
   179 			PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadFragment.class), Intent.FLAG_ACTIVITY_NEW_TASK);
   179 			progressNotification.contentIntent = contentIntent;
   180 			progressNotification.contentIntent = contentIntent;
   180 
   181 
   181 			startForeground(NOTIFICATION_PROCESSING, progressNotification);//TODO werkt het?
   182 			startForeground(NOTIFICATION_PROCESSING, progressNotification);
   182 
   183 
   183 			Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0);
   184 			Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0);
   184 			sendMessageToClients(msg);
   185 			sendMessageToClients(msg);
   185 		}
   186 		}
   186 
   187 
   194 
   195 
   195 			sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName));
   196 			sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName));
   196 		}
   197 		}
   197 
   198 
   198 		//Call back from the ASync task when the task has either run into an error or finished otherwise
   199 		//Call back from the ASync task when the task has either run into an error or finished otherwise
   199 		public void done(boolean succesful){
   200 		public void done(int result){
   200 			if(succesful){
   201 			switch(result){
   201 				sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE));
   202 			case DownloadAsyncTask.EXIT_SUCCESS: 	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break;
   202 			}else sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED));
   203 			case DownloadAsyncTask.EXIT_CONNERROR:  sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
       
   204 			case DownloadAsyncTask.EXIT_FNF:		sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
       
   205 			case DownloadAsyncTask.EXIT_MD5:		sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
       
   206 			case DownloadAsyncTask.EXIT_URLFAIL:	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED)); break;
       
   207 			case DownloadAsyncTask.EXIT_CANCELLED:	sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break;
       
   208 				
       
   209 				
       
   210 			}
       
   211 			
   203 			stopForeground(true);
   212 			stopForeground(true);
   204 			nM.cancel(NOTIFICATION_PROCESSING);
   213 			nM.cancel(NOTIFICATION_PROCESSING);
   205 
   214 
   206 			String title = getString(R.string.notification_title);
   215 			String title = getString(R.string.notification_title);
   207 
   216