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 }; |
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 |