diff -r 4feced261c68 -r de822cd3df3a project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java --- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java Tue Jan 21 22:38:13 2014 +0100 +++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadService.java Tue Jan 21 22:43:06 2014 +0100 @@ -37,194 +37,194 @@ import android.widget.RemoteViews; public class DownloadService extends Service { - public final static String INTENT_TASKID = "taskId"; - public final static String INTENT_TASK = "task"; + public final static String INTENT_TASKID = "taskId"; + public final static String INTENT_TASK = "task"; - public static final String PREF_DOWNLOADED = "downloaded"; - public static final int MSG_CANCEL = 0; - public static final int MSG_UNREGISTER_CLIENT = 2; - public final static int MSG_ADDTASK = 4; + public static final String PREF_DOWNLOADED = "downloaded"; + public static final int MSG_CANCEL = 0; + public static final int MSG_UNREGISTER_CLIENT = 2; + public final static int MSG_ADDTASK = 4; - public static final int NOTIFICATION_PROCESSING = 0; - public static final int NOTIFICATION_DONE = 1; + public static final int NOTIFICATION_PROCESSING = 0; + public static final int NOTIFICATION_DONE = 1; - private DownloadAsyncTask asyncExecutor; + private DownloadAsyncTask asyncExecutor; - private DownloadHandler handler = new DownloadHandler(); - private final Messenger messenger = new Messenger(handler); + private DownloadHandler handler = new DownloadHandler(); + private final Messenger messenger = new Messenger(handler); - private NotificationManager nM; - private RemoteViews contentView; + private NotificationManager nM; + private RemoteViews contentView; - private LinkedList downloadTasks = new LinkedList(); - private DownloadTask currentTask = null; + private LinkedList downloadTasks = new LinkedList(); + private DownloadTask currentTask = null; - public class DownloadHandler extends Handler{ + public class DownloadHandler extends Handler{ - public void handleMessage(Message msg){ - if(msg.obj != null){ - DownloadPackage pack = (DownloadPackage) msg.obj; - DownloadTask task = null; - Messenger replyToMessenger = msg.replyTo; - for(DownloadTask _task : downloadTasks){ - if(_task.getPackage().equals(pack)){ - task = _task; - break; - } - } + public void handleMessage(Message msg){ + if(msg.obj != null){ + DownloadPackage pack = (DownloadPackage) msg.obj; + DownloadTask task = null; + Messenger replyToMessenger = msg.replyTo; + for(DownloadTask _task : downloadTasks){ + if(_task.getPackage().equals(pack)){ + task = _task; + break; + } + } - switch(msg.what){ - case MSG_ADDTASK: - if(task == null){ - task = new DownloadTask(pack); - downloadTasks.add(task); - } + switch(msg.what){ + case MSG_ADDTASK: + if(task == null){ + task = new DownloadTask(pack); + downloadTasks.add(task); + } - task.addClient(replyToMessenger); - runNextTask(); - return; - case MSG_CANCEL: - if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.PENDING){ - downloadTasks.remove(task); - } - if(currentTask != null && currentTask.getPackage().equals(pack)){//TODO synchronization problem? - asyncExecutor.cancel(false); - } - return; - case MSG_UNREGISTER_CLIENT: - if(task != null){ - task.removeClient(replyToMessenger); - } - return; - } - } - } - } + task.addClient(replyToMessenger); + runNextTask(); + return; + case MSG_CANCEL: + if(task != null && task.getPackage().equals(pack) && task.getStatus() == TASK_STATE.PENDING){ + downloadTasks.remove(task); + } + if(currentTask != null && currentTask.getPackage().equals(pack)){//TODO synchronization problem? + asyncExecutor.cancel(false); + } + return; + case MSG_UNREGISTER_CLIENT: + if(task != null){ + task.removeClient(replyToMessenger); + } + return; + } + } + } + } - public void onCreate(){ - super.onCreate(); - nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - } - public IBinder onBind(Intent intent) { - return messenger.getBinder(); - } + public void onCreate(){ + super.onCreate(); + nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + } + public IBinder onBind(Intent intent) { + return messenger.getBinder(); + } - private void runNextTask(){ - if(asyncExecutor == null){//if (task isnt running right now) ... - currentTask = downloadTasks.poll(); - if(currentTask != null){ - asyncExecutor = new DownloadAsyncTask(currentTask); - asyncExecutor.execute(currentTask.getPackage()); - } - } - } + private void runNextTask(){ + if(asyncExecutor == null){//if (task isnt running right now) ... + currentTask = downloadTasks.poll(); + if(currentTask != null){ + asyncExecutor = new DownloadAsyncTask(currentTask); + asyncExecutor.execute(currentTask.getPackage()); + } + } + } - public void onDestroy(){ - super.onDestroy(); - asyncExecutor.cancel(false); - } + public void onDestroy(){ + super.onDestroy(); + asyncExecutor.cancel(false); + } - class DownloadTask { - private final DownloadPackage pack; - private TASK_STATE status = TASK_STATE.PENDING; - private Notification progressNotification, doneNotification; + class DownloadTask { + private final DownloadPackage pack; + private TASK_STATE status = TASK_STATE.PENDING; + private Notification progressNotification, doneNotification; - //I expect little to no removeClient calls that's why we go for a list rather than a map - private final List clients; + //I expect little to no removeClient calls that's why we go for a list rather than a map + private final List clients; - public DownloadTask(DownloadPackage _pack){ - pack = _pack; - clients = new LinkedList(); - } + public DownloadTask(DownloadPackage _pack){ + pack = _pack; + clients = new LinkedList(); + } - public void addClient(Messenger messenger){ - clients.add(messenger); - } - public void removeClient(Messenger messenger){ - clients.remove(messenger); - } + public void addClient(Messenger messenger){ + clients.add(messenger); + } + public void removeClient(Messenger messenger){ + clients.remove(messenger); + } - public DownloadPackage getPackage(){ - return pack; - } + public DownloadPackage getPackage(){ + return pack; + } - public TASK_STATE getStatus(){ - return status; - } + public TASK_STATE getStatus(){ + return status; + } - public void sendMessageToClients(Message msg){ - for(Messenger messenger : clients){ - try { - messenger.send(msg); - } catch (RemoteException e) { - e.printStackTrace(); - } - } - } + public void sendMessageToClients(Message msg){ + for(Messenger messenger : clients){ + try { + messenger.send(msg); + } catch (RemoteException e) { + e.printStackTrace(); + } + } + } - /* - * Callbacks called from the async tasks - */ + /* + * Callbacks called from the async tasks + */ - //Thread safe method to let clients know the processing is starting and will process int max kbytes - public void start(int max){ - progressNotification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); - progressNotification.flags |= Notification.FLAG_ONGOING_EVENT; + //Thread safe method to let clients know the processing is starting and will process int max kbytes + public void start(int max){ + progressNotification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis()); + progressNotification.flags |= Notification.FLAG_ONGOING_EVENT; - contentView = new RemoteViews(getPackageName(), R.layout.notification); - contentView.setProgressBar(R.id.notification_progress, 100, 34, false); - progressNotification.contentView = contentView; + contentView = new RemoteViews(getPackageName(), R.layout.notification); + contentView.setProgressBar(R.id.notification_progress, 100, 34, false); + progressNotification.contentView = contentView; - PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); - progressNotification.contentIntent = contentIntent; + PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); + progressNotification.contentIntent = contentIntent; - startForeground(NOTIFICATION_PROCESSING, progressNotification); + startForeground(NOTIFICATION_PROCESSING, progressNotification); - Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0); - sendMessageToClients(msg); - } + Message msg = Message.obtain(null, DownloadFragment.MSG_START, max, 0); + sendMessageToClients(msg); + } - //periodically gets called by the ASyncTask, we can't tell for sure when it's called - public void update(int progress, int max, String fileName){ - progress = (progress/1024); + //periodically gets called by the ASyncTask, we can't tell for sure when it's called + public void update(int progress, int max, String fileName){ + progress = (progress/1024); - contentView.setProgressBar(R.id.notification_progress, max, progress, false); - contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); - nM.notify(NOTIFICATION_PROCESSING, progressNotification); + contentView.setProgressBar(R.id.notification_progress, max, progress, false); + contentView.setTextViewText(R.id.progressbar_sub, String.format("%dkb/%dkb (Compressed sizes)", progress, max)); + nM.notify(NOTIFICATION_PROCESSING, progressNotification); - sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName)); - } + sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_UPDATE, progress, max, fileName)); + } - //Call back from the ASync task when the task has either run into an error or finished otherwise - public void done(int result){ - switch(result){ - case DownloadAsyncTask.EXIT_SUCCESS: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; - case DownloadAsyncTask.EXIT_CONNERROR: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_CONNERROR, 0)); break; - case DownloadAsyncTask.EXIT_FNF: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_FNF, 0)); break; - case DownloadAsyncTask.EXIT_MD5: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_MD5, 0)); break; - case DownloadAsyncTask.EXIT_URLFAIL: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_URLFAIL, 0)); break; - case DownloadAsyncTask.EXIT_CANCELLED: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; - } - - stopForeground(true); - nM.cancel(NOTIFICATION_PROCESSING); + //Call back from the ASync task when the task has either run into an error or finished otherwise + public void done(int result){ + switch(result){ + case DownloadAsyncTask.EXIT_SUCCESS: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; + case DownloadAsyncTask.EXIT_CONNERROR: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_CONNERROR, 0)); break; + case DownloadAsyncTask.EXIT_FNF: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_FNF, 0)); break; + case DownloadAsyncTask.EXIT_MD5: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_MD5, 0)); break; + case DownloadAsyncTask.EXIT_URLFAIL: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_FAILED, DownloadAsyncTask.EXIT_URLFAIL, 0)); break; + case DownloadAsyncTask.EXIT_CANCELLED: sendMessageToClients(Message.obtain(handler, DownloadFragment.MSG_DONE)); break; + } - String title = getString(R.string.notification_title); + stopForeground(true); + nM.cancel(NOTIFICATION_PROCESSING); + + String title = getString(R.string.notification_title); - doneNotification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); - doneNotification.flags |= Notification.FLAG_AUTO_CANCEL; - PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); - doneNotification.setLatestEventInfo(DownloadService.this, title, getString(R.string.notification_done) + pack, contentIntent); - nM.notify(pack.getId(), doneNotification); + doneNotification = new Notification(R.drawable.icon, title, System.currentTimeMillis()); + doneNotification.flags |= Notification.FLAG_AUTO_CANCEL; + PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, new Intent(DownloadService.this, DownloadListActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK); + doneNotification.setLatestEventInfo(DownloadService.this, title, getString(R.string.notification_done) + pack, contentIntent); + nM.notify(pack.getId(), doneNotification); - asyncExecutor = null; - runNextTask();//see if there are more tasks - } + asyncExecutor = null; + runNextTask();//see if there are more tasks + } - } + } - enum TASK_STATE{ - RUNNING, FINISHED, PENDING; - } + enum TASK_STATE{ + RUNNING, FINISHED, PENDING; + } }