project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadActivity.java
branchhedgeroid
changeset 6047 10011f051f9c
parent 6045 9a7cc0f29430
child 6049 7bc38086d771
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 
       
    20 package org.hedgewars.mobile.Downloader;
       
    21 
       
    22 import org.hedgewars.mobile.MainActivity;
       
    23 import org.hedgewars.mobile.R;
       
    24 
       
    25 import android.app.Activity;
       
    26 import android.content.ComponentName;
       
    27 import android.content.Context;
       
    28 import android.content.Intent;
       
    29 import android.content.ServiceConnection;
       
    30 import android.os.Bundle;
       
    31 import android.os.Handler;
       
    32 import android.os.IBinder;
       
    33 import android.os.Message;
       
    34 import android.os.Messenger;
       
    35 import android.os.RemoteException;
       
    36 import android.preference.PreferenceManager;
       
    37 import android.view.View;
       
    38 import android.view.View.OnClickListener;
       
    39 import android.widget.Button;
       
    40 import android.widget.ProgressBar;
       
    41 import android.widget.TextView;
       
    42 import android.widget.Toast;
       
    43 
       
    44 public class DownloadActivity extends Activity{
       
    45 	private Messenger messageService;
       
    46 	private boolean boundToService = false;
       
    47 	
       
    48 	private TextView progress_sub;
       
    49 	private ProgressBar progress;
       
    50 	private Button positive, negative;
       
    51 	
       
    52 	public static final int MSG_START = 0;
       
    53 	public static final int MSG_UPDATE = 1;
       
    54 	public static final int MSG_DONE = 2;
       
    55 	public static final int MSG_FAILED = 3;
       
    56 	private Handler.Callback messageCallback = new Handler.Callback() {
       
    57 		
       
    58 		public boolean handleMessage(Message msg) {
       
    59 			switch(msg.what){
       
    60 			case MSG_START:
       
    61 				progress.setMax(msg.arg1);
       
    62 				progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, ""));
       
    63 				positive.setText(R.string.download_background);
       
    64 				positive.setOnClickListener(backgroundClicker);
       
    65 				negative.setText(R.string.download_cancel);
       
    66 				negative.setOnClickListener(cancelClicker);
       
    67 				break;
       
    68 			case MSG_UPDATE:
       
    69 				progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj));
       
    70 				progress.setProgress(msg.arg1);
       
    71 				break;
       
    72 			case MSG_DONE:
       
    73 				progress.setProgress(progress.getMax());
       
    74 				progress_sub.setText(R.string.download_done);
       
    75 				
       
    76 				positive.setText(R.string.download_back);
       
    77 				positive.setOnClickListener(doneClicker);
       
    78 				
       
    79 				negative.setVisibility(View.INVISIBLE);
       
    80 				break;
       
    81 			case MSG_FAILED:
       
    82 				progress.setProgress(progress.getMax());
       
    83 				progress_sub.setText(R.string.download_failed);
       
    84 				positive.setText(R.string.download_back);
       
    85 				positive.setOnClickListener(doneClicker);
       
    86 				
       
    87 				negative.setText(R.string.download_tryagain);
       
    88 				negative.setOnClickListener(tryAgainClicker);
       
    89 				break;
       
    90 			}
       
    91 			return false;
       
    92 		}
       
    93 	};
       
    94 	private Handler messageHandler = new Handler(messageCallback);
       
    95 	private Messenger messenger = new Messenger(messageHandler);
       
    96 	
       
    97 	public void onCreate(Bundle savedInstanceState){
       
    98 		super.onCreate(savedInstanceState);
       
    99 		setContentView(R.layout.download);
       
   100 		
       
   101 		progress_sub = (TextView)findViewById(R.id.progressbar_sub);
       
   102 		progress = (ProgressBar)findViewById(R.id.progressbar);
       
   103 		
       
   104 		positive = (Button) findViewById(R.id.background);
       
   105 		negative = (Button) findViewById(R.id.cancelDownload);
       
   106 		positive.setOnClickListener(backgroundClicker);
       
   107 		negative.setOnClickListener(cancelClicker);
       
   108 		
       
   109 	}
       
   110 	
       
   111 	private OnClickListener backgroundClicker = new OnClickListener(){
       
   112 		public void onClick(View v){
       
   113 			finish();
       
   114 		}
       
   115 	};
       
   116 	private OnClickListener cancelClicker = new OnClickListener(){
       
   117 		public void onClick(View v){
       
   118 			Intent i = new Intent(getApplicationContext(), DownloadService.class);
       
   119 			i.putExtra("taskID", DownloadService.TASKID_CANCEL);
       
   120 			startService(i);
       
   121 			finish();
       
   122 		}
       
   123 	};
       
   124 	private OnClickListener doneClicker = new OnClickListener(){
       
   125 		public void onClick(View v){
       
   126 			finish();
       
   127 			startActivity(new Intent(getApplicationContext(), MainActivity.class));
       
   128 		}
       
   129 	};
       
   130 	
       
   131 	private OnClickListener tryAgainClicker = new OnClickListener(){
       
   132 		public void onClick(View v){
       
   133 			bindToService(DownloadService.TASKID_RETRY);
       
   134 		}
       
   135 	};
       
   136 	
       
   137 	public void onStart(){
       
   138 		super.onStart();
       
   139 		bindToService(DownloadService.TASKID_START);
       
   140 	}
       
   141 	
       
   142 	public void onStop(){
       
   143 		super.onStop();
       
   144 		unBindFromService();
       
   145 	}
       
   146 	
       
   147 	private ServiceConnection connection = new ServiceConnection(){
       
   148 
       
   149 		public void onServiceConnected(ComponentName name, IBinder service) {
       
   150 			messageService = new Messenger(service);
       
   151 			
       
   152 			try{
       
   153 				Message msg = Message.obtain(null, DownloadService.MSG_REGISTER_CLIENT);
       
   154 				msg.replyTo = messenger;
       
   155 				messageService.send(msg);
       
   156 				
       
   157 			}catch (RemoteException e){}
       
   158 		}
       
   159 
       
   160 		public void onServiceDisconnected(ComponentName name) {
       
   161 			messageService = null;
       
   162 		}
       
   163 		
       
   164 	};
       
   165 	
       
   166 	private void bindToService(int taskId){
       
   167 		Intent i = new Intent(getApplicationContext(), DownloadService.class);
       
   168 		i.putExtra("taskID", taskId);
       
   169 		startService(i);
       
   170 		bindService(new Intent(getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
       
   171 		boundToService = true;
       
   172 	}
       
   173 	
       
   174 	private void unBindFromService(){
       
   175 		if(boundToService){
       
   176 			boundToService = false;
       
   177 			unbindService(connection);
       
   178 		}	
       
   179 	}
       
   180 }