project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Downloader/DownloadActivity.java
branchhedgeroid
changeset 5671 ba4c3a4c8b09
parent 5657 4dad72a8f514
equal deleted inserted replaced
5669:a806dbe25288 5671:ba4c3a4c8b09
    31 import android.os.Handler;
    31 import android.os.Handler;
    32 import android.os.IBinder;
    32 import android.os.IBinder;
    33 import android.os.Message;
    33 import android.os.Message;
    34 import android.os.Messenger;
    34 import android.os.Messenger;
    35 import android.os.RemoteException;
    35 import android.os.RemoteException;
       
    36 import android.preference.PreferenceManager;
    36 import android.view.View;
    37 import android.view.View;
    37 import android.view.View.OnClickListener;
    38 import android.view.View.OnClickListener;
    38 import android.widget.Button;
    39 import android.widget.Button;
    39 import android.widget.ProgressBar;
    40 import android.widget.ProgressBar;
    40 import android.widget.TextView;
    41 import android.widget.TextView;
       
    42 import android.widget.Toast;
    41 
    43 
    42 public class DownloadActivity extends Activity{
    44 public class DownloadActivity extends Activity{
    43 
       
    44 	private Messenger messageService;
    45 	private Messenger messageService;
    45 	private boolean boundToService = false;
    46 	private boolean boundToService = false;
    46 	
    47 	
    47 	private TextView progress_sub;
    48 	private TextView progress_sub;
    48 	private ProgressBar progress;
    49 	private ProgressBar progress;
    49 	private Button positive, negative;
    50 	private Button positive, negative;
    50 	
    51 	
    51 	public static final int MSG_START = 0;
    52 	public static final int MSG_START = 0;
    52 	public static final int MSG_UPDATE = 1;
    53 	public static final int MSG_UPDATE = 1;
    53 	public static final int MSG_DONE = 2;
    54 	public static final int MSG_DONE = 2;
       
    55 	public static final int MSG_FAILED = 3;
    54 	private Handler.Callback messageCallback = new Handler.Callback() {
    56 	private Handler.Callback messageCallback = new Handler.Callback() {
    55 		
    57 		
    56 		public boolean handleMessage(Message msg) {
    58 		public boolean handleMessage(Message msg) {
    57 			switch(msg.what){
    59 			switch(msg.what){
    58 			case MSG_START:
    60 			case MSG_START:
    59 				progress.setMax(msg.arg1);
    61 				progress.setMax(msg.arg1);
    60 				progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, 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);
    61 				break;
    67 				break;
    62 			case MSG_UPDATE:
    68 			case MSG_UPDATE:
    63 				progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj));
    69 				progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj));
    64 				progress.setProgress(msg.arg1);
    70 				progress.setProgress(msg.arg1);
    65 				break;
    71 				break;
    67 				progress.setProgress(progress.getMax());
    73 				progress.setProgress(progress.getMax());
    68 				progress_sub.setText(R.string.download_done);
    74 				progress_sub.setText(R.string.download_done);
    69 				
    75 				
    70 				positive.setText(R.string.download_back);
    76 				positive.setText(R.string.download_back);
    71 				positive.setOnClickListener(doneClicker);
    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);
    72 				break;
    89 				break;
    73 			}
    90 			}
    74 			return false;
    91 			return false;
    75 		}
    92 		}
    76 	};
    93 	};
   109 			finish();
   126 			finish();
   110 			startActivity(new Intent(getApplicationContext(), MainActivity.class));
   127 			startActivity(new Intent(getApplicationContext(), MainActivity.class));
   111 		}
   128 		}
   112 	};
   129 	};
   113 	
   130 	
       
   131 	private OnClickListener tryAgainClicker = new OnClickListener(){
       
   132 		public void onClick(View v){
       
   133 			bindToService(DownloadService.TASKID_RETRY);
       
   134 		}
       
   135 	};
       
   136 	
   114 	public void onStart(){
   137 	public void onStart(){
   115 		super.onStart();
   138 		super.onStart();
   116 		bindToService();
   139 		bindToService(DownloadService.TASKID_START);
   117 	}
   140 	}
   118 	
   141 	
   119 	public void onStop(){
   142 	public void onStop(){
   120 		super.onStop();
   143 		super.onStop();
   121 		unBindFromService();
   144 		unBindFromService();
   138 			messageService = null;
   161 			messageService = null;
   139 		}
   162 		}
   140 		
   163 		
   141 	};
   164 	};
   142 	
   165 	
   143 	private void bindToService(){
   166 	private void bindToService(int taskId){
   144 		Intent i = new Intent(getApplicationContext(), DownloadService.class);
   167 		Intent i = new Intent(getApplicationContext(), DownloadService.class);
   145 		i.putExtra("taskID", DownloadService.TASKID_START);
   168 		i.putExtra("taskID", taskId);
   146 		startService(i);
   169 		startService(i);
   147 		bindService(new Intent(getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
   170 		bindService(new Intent(getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
   148 		boundToService = true;
   171 		boundToService = true;
   149 	}
   172 	}
   150 	
   173