project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/DownloadActivity.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 android.app.Activity;
       
     4 import android.content.ComponentName;
       
     5 import android.content.Context;
       
     6 import android.content.Intent;
       
     7 import android.content.ServiceConnection;
       
     8 import android.os.Bundle;
       
     9 import android.os.Handler;
       
    10 import android.os.IBinder;
       
    11 import android.os.Message;
       
    12 import android.os.Messenger;
       
    13 import android.os.RemoteException;
       
    14 import android.view.View;
       
    15 import android.view.View.OnClickListener;
       
    16 import android.widget.Button;
       
    17 import android.widget.ProgressBar;
       
    18 import android.widget.TextView;
       
    19 
       
    20 public class DownloadActivity extends Activity{
       
    21 
       
    22 	private Messenger messageService;
       
    23 	private boolean boundToService = false;
       
    24 	
       
    25 	private TextView progress_sub;
       
    26 	private ProgressBar progress;
       
    27 	private Button positive, negative;
       
    28 	
       
    29 	public static final int MSG_START = 0;
       
    30 	public static final int MSG_UPDATE = 1;
       
    31 	public static final int MSG_DONE = 2;
       
    32 	private Handler.Callback messageCallback = new Handler.Callback() {
       
    33 		
       
    34 		@Override
       
    35 		public boolean handleMessage(Message msg) {
       
    36 			switch(msg.what){
       
    37 			case MSG_START:
       
    38 				progress.setMax(msg.arg1);
       
    39 				progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, ""));
       
    40 				break;
       
    41 			case MSG_UPDATE:
       
    42 				progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj));
       
    43 				progress.setProgress(msg.arg1);
       
    44 				break;
       
    45 			case MSG_DONE:
       
    46 				progress.setProgress(progress.getMax());
       
    47 				progress_sub.setText(R.string.download_done);
       
    48 				
       
    49 				positive.setText(R.string.download_back);
       
    50 				positive.setOnClickListener(doneClicker);
       
    51 				break;
       
    52 			}
       
    53 			return false;
       
    54 		}
       
    55 	};
       
    56 	private Handler messageHandler = new Handler(messageCallback);
       
    57 	private Messenger messenger = new Messenger(messageHandler);
       
    58 	
       
    59 	public void onCreate(Bundle savedInstanceState){
       
    60 		super.onCreate(savedInstanceState);
       
    61 		
       
    62 		setContentView(R.layout.download);
       
    63 		
       
    64 		progress_sub = (TextView)findViewById(R.id.progressbar_sub);
       
    65 		progress = (ProgressBar)findViewById(R.id.progressbar);
       
    66 		
       
    67 		positive = (Button) findViewById(R.id.background);
       
    68 		negative = (Button) findViewById(R.id.cancelDownload);
       
    69 		positive.setOnClickListener(backgroundClicker);
       
    70 		negative.setOnClickListener(cancelClicker);
       
    71 		
       
    72 	}
       
    73 	
       
    74 	private OnClickListener backgroundClicker = new OnClickListener(){
       
    75 		public void onClick(View v){
       
    76 			finish();
       
    77 		}
       
    78 	};
       
    79 	private OnClickListener cancelClicker = new OnClickListener(){
       
    80 		public void onClick(View v){
       
    81 			Intent i = new Intent(getApplicationContext(), DownloadService.class);
       
    82 			i.putExtra("taskID", DownloadService.TASKID_CANCEL);
       
    83 			startService(i);
       
    84 			finish();
       
    85 		}
       
    86 	};
       
    87 	private OnClickListener doneClicker = new OnClickListener(){
       
    88 		public void onClick(View v){
       
    89 			finish();
       
    90 			startActivity(new Intent(getApplicationContext(), MainActivity.class));
       
    91 		}
       
    92 	};
       
    93 	
       
    94 	public void onStart(){
       
    95 		super.onStart();
       
    96 		bindToService();
       
    97 	}
       
    98 	
       
    99 	public void onStop(){
       
   100 		super.onStop();
       
   101 		unBindFromService();
       
   102 	}
       
   103 	
       
   104 	private ServiceConnection connection = new ServiceConnection(){
       
   105 
       
   106 		@Override
       
   107 		public void onServiceConnected(ComponentName name, IBinder service) {
       
   108 			messageService = new Messenger(service);
       
   109 			
       
   110 			try{
       
   111 				Message msg = Message.obtain(null, DownloadService.MSG_REGISTER_CLIENT);
       
   112 				msg.replyTo = messenger;
       
   113 				messageService.send(msg);
       
   114 				
       
   115 			}catch (RemoteException e){}
       
   116 		}
       
   117 
       
   118 		@Override
       
   119 		public void onServiceDisconnected(ComponentName name) {
       
   120 			messageService = null;
       
   121 		}
       
   122 		
       
   123 	};
       
   124 	
       
   125 	private void bindToService(){
       
   126 		Intent i = new Intent(getApplicationContext(), DownloadService.class);
       
   127 		i.putExtra("taskID", DownloadService.TASKID_START);
       
   128 		startService(i);
       
   129 		bindService(new Intent(getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
       
   130 		boundToService = true;
       
   131 	}
       
   132 	
       
   133 	private void unBindFromService(){
       
   134 		if(boundToService){
       
   135 			boundToService = false;
       
   136 			unbindService(connection);
       
   137 		}	
       
   138 	}
       
   139 }