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