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