project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadService.java
branchhedgeroid
changeset 5671 ba4c3a4c8b09
parent 5637 fcc0e5b3d66b
equal deleted inserted replaced
5669:a806dbe25288 5671:ba4c3a4c8b09
    33 import android.os.Handler;
    33 import android.os.Handler;
    34 import android.os.IBinder;
    34 import android.os.IBinder;
    35 import android.os.Message;
    35 import android.os.Message;
    36 import android.os.Messenger;
    36 import android.os.Messenger;
    37 import android.os.RemoteException;
    37 import android.os.RemoteException;
       
    38 import android.preference.PreferenceManager;
    38 import android.util.Log;
    39 import android.util.Log;
    39 import android.widget.RemoteViews;
    40 import android.widget.RemoteViews;
    40 
    41 
    41 public class DownloadService extends Service {
    42 public class DownloadService extends Service {
    42 
    43 
    43 	private final static String URL = "http://hedgewars.googlecode.com/files/data_5631.zip";
    44 	public static final String PREF_DOWNLOADED = "downloaded";
    44 	public static final int MSG_CANCEL = 0;
    45 	public static final int MSG_CANCEL = 0;
    45 	public static final int MSG_REGISTER_CLIENT = 1;
    46 	public static final int MSG_REGISTER_CLIENT = 1;
    46 	public static final int MSG_UNREGISTER_CLIENT = 2;
    47 	public static final int MSG_UNREGISTER_CLIENT = 2;
    47 
    48 
    48 	public static final int NOTIFICATION_PROCESSING = 0;
    49 	public static final int NOTIFICATION_PROCESSING = 0;
    82 		}
    83 		}
    83 	}
    84 	}
    84 
    85 
    85 	public final static int TASKID_START = 0;
    86 	public final static int TASKID_START = 0;
    86 	public final static int TASKID_CANCEL = 1;
    87 	public final static int TASKID_CANCEL = 1;
       
    88 	public final static int TASKID_RETRY = 2;
    87 	
    89 	
    88 	public int onStartCommand(Intent intent, int flags, int startId){
    90 	public int onStartCommand(Intent intent, int flags, int startId){
    89 		switch(intent.getIntExtra("taskID", TASKID_START)){
    91 		switch(intent.getIntExtra("taskID", TASKID_START)){
       
    92 		case TASKID_RETRY:
       
    93 			if(downloadTask != null){
       
    94 				downloadTask.cancel(false);
       
    95 				downloadTask = null;
       
    96 			}
    90 		case TASKID_START:
    97 		case TASKID_START:
    91 			nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    98 			nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    92 
    99 
    93 			notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis());
   100 			notification = new Notification(R.drawable.statusbar, getString(R.string.notification_title), System.currentTimeMillis());
    94 			//notification.flags |= Notification.FLAG_ONGOING_EVENT;// | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
   101 			//notification.flags |= Notification.FLAG_ONGOING_EVENT;// | Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
   104 			//nM.notify(NOTIFICATION_PROCESSING, notification);
   111 			//nM.notify(NOTIFICATION_PROCESSING, notification);
   105 			startForeground(NOTIFICATION_PROCESSING, notification);
   112 			startForeground(NOTIFICATION_PROCESSING, notification);
   106 
   113 
   107 			if(downloadTask == null){
   114 			if(downloadTask == null){
   108 				downloadTask = new DownloadAsyncTask(this);
   115 				downloadTask = new DownloadAsyncTask(this);
   109 				downloadTask.execute(Utils.getDownloadPath(this), URL);
   116 				downloadTask.execute(Utils.getDownloadPath(this));
   110 			}	
   117 			}	
   111 			break;
   118 			break;
   112 		case TASKID_CANCEL:
   119 		case TASKID_CANCEL:
   113 			downloadTask.cancel(false);
   120 			downloadTask.cancel(false);
   114 			stopService();
   121 			stopService();
   126 		return messenger.getBinder();
   133 		return messenger.getBinder();
   127 	}
   134 	}
   128 
   135 
   129 	/*
   136 	/*
   130 	 * Thread safe method to let clients know the processing is starting and will process int max kbytes
   137 	 * Thread safe method to let clients know the processing is starting and will process int max kbytes
   131 	 * 
       
   132 	 */
   138 	 */
   133 	public void start(int max){
   139 	public void start(int max){
   134 		onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1);
   140 		onRegisterMessage = Message.obtain(null, DownloadActivity.MSG_START, max, -1);
   135 		sendMessageToClients(onRegisterMessage);
   141 		sendMessageToClients(onRegisterMessage);
   136 	}
   142 	}
   137 
   143 
       
   144 	/*
       
   145 	 * periodically gets called by the ASyncTask, we can't tell for sure when it's called
       
   146 	 */
   138 	public void update(int progress, int max, String fileName){
   147 	public void update(int progress, int max, String fileName){
   139 		progress = (progress/1024);
   148 		progress = (progress/1024);
   140 		updateNotification(progress, max, fileName);
   149 		updateNotification(progress, max, fileName);
   141 
   150 
   142 		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName));
   151 		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_UPDATE, progress, max, fileName));
   143 	}
   152 	}
       
   153 	
       
   154 	/*
       
   155 	 * Call back from the ASync task when the task has either run into an error or finished otherwise
       
   156 	 */
   144 	public void done(boolean succesful){
   157 	public void done(boolean succesful){
   145 		sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE));
   158 		if(succesful){
       
   159 			PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean(DownloadService.PREF_DOWNLOADED, true).commit();
       
   160 			sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_DONE));
       
   161 		}else sendMessageToClients(Message.obtain(null, DownloadActivity.MSG_FAILED));
   146 		stopService();//stopService clears all notifications and thus must be called before we show the ready notification
   162 		stopService();//stopService clears all notifications and thus must be called before we show the ready notification
   147 		showDoneNotification();
   163 		showDoneNotification();
   148 	}
   164 	}
   149 
   165 
   150 	private void stopService(){
   166 	private void stopService(){