6350
|
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.hedgeroid.Downloader;
|
|
21 |
|
|
22 |
import org.hedgewars.hedgeroid.R;
|
|
23 |
|
|
24 |
import android.content.ComponentName;
|
|
25 |
import android.content.Context;
|
|
26 |
import android.content.Intent;
|
|
27 |
import android.content.ServiceConnection;
|
|
28 |
import android.os.Bundle;
|
|
29 |
import android.os.Handler;
|
|
30 |
import android.os.IBinder;
|
|
31 |
import android.os.Message;
|
|
32 |
import android.os.Messenger;
|
|
33 |
import android.os.RemoteException;
|
|
34 |
import android.support.v4.app.Fragment;
|
|
35 |
import android.view.LayoutInflater;
|
|
36 |
import android.view.View;
|
|
37 |
import android.view.View.OnClickListener;
|
|
38 |
import android.view.ViewGroup;
|
|
39 |
import android.widget.Button;
|
|
40 |
import android.widget.ProgressBar;
|
|
41 |
import android.widget.TextView;
|
|
42 |
|
|
43 |
public class DownloadFragment extends Fragment{
|
|
44 |
public static final String EXTRA_TASK = "task";
|
|
45 |
|
|
46 |
public static final int MSG_START = 0;
|
|
47 |
public static final int MSG_UPDATE = 1;
|
|
48 |
public static final int MSG_DONE = 2;
|
|
49 |
public static final int MSG_FAILED = 3;
|
|
50 |
|
|
51 |
private boolean boundToService = false;
|
|
52 |
|
|
53 |
private TextView progress_sub;
|
|
54 |
private ProgressBar progress;
|
|
55 |
private Button positive, negative;
|
|
56 |
|
|
57 |
private DownloadPackage pack;
|
|
58 |
|
|
59 |
private Handler messageHandler;
|
|
60 |
private Messenger messenger, messengerService;
|
|
61 |
|
|
62 |
public static DownloadFragment getInstance(DownloadPackage task){
|
|
63 |
DownloadFragment df = new DownloadFragment();
|
|
64 |
Bundle args = new Bundle();
|
|
65 |
args.putParcelable(DownloadFragment.EXTRA_TASK, task);
|
|
66 |
|
|
67 |
df.setArguments(args);
|
|
68 |
|
|
69 |
return df;
|
|
70 |
}
|
|
71 |
|
|
72 |
public void onActivityCreated(Bundle savedInstanceState){
|
|
73 |
super.onActivityCreated(savedInstanceState);
|
|
74 |
|
|
75 |
messageHandler = new Handler(messageCallback);
|
|
76 |
messenger = new Messenger(messageHandler);
|
|
77 |
Intent i = new Intent(getActivity(), DownloadService.class);
|
|
78 |
getActivity().startService(i);
|
|
79 |
getActivity().bindService(new Intent(getActivity(), DownloadService.class), connection, Context.BIND_AUTO_CREATE);
|
|
80 |
}
|
|
81 |
|
|
82 |
public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle savedInstanceState){
|
|
83 |
View v = inflater.inflate(R.layout.download_progress, viewgroup, false);
|
|
84 |
progress_sub = (TextView)v.findViewById(R.id.progressbar_sub);
|
|
85 |
progress = (ProgressBar)v.findViewById(R.id.progressbar);
|
|
86 |
|
|
87 |
positive = (Button) v.findViewById(R.id.background);
|
|
88 |
negative = (Button) v.findViewById(R.id.cancelDownload);
|
|
89 |
positive.setOnClickListener(backgroundClicker);
|
|
90 |
negative.setOnClickListener(cancelClicker);
|
|
91 |
|
|
92 |
pack = getArguments().getParcelable(DownloadFragment.EXTRA_TASK);
|
|
93 |
|
|
94 |
return v;
|
|
95 |
}
|
|
96 |
|
|
97 |
private OnClickListener backgroundClicker = new OnClickListener(){
|
|
98 |
public void onClick(View v){
|
|
99 |
getActivity().finish();
|
|
100 |
}
|
|
101 |
};
|
|
102 |
private OnClickListener cancelClicker = new OnClickListener(){
|
|
103 |
public void onClick(View v){
|
|
104 |
if(messenger != null){
|
|
105 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_CANCEL, pack);
|
|
106 |
try {
|
|
107 |
messengerService.send(message);
|
|
108 |
} catch (RemoteException e) {}
|
|
109 |
}
|
|
110 |
getActivity().finish();
|
|
111 |
}
|
|
112 |
};
|
|
113 |
private OnClickListener doneClicker = new OnClickListener(){
|
|
114 |
public void onClick(View v){
|
|
115 |
getActivity().finish();
|
|
116 |
}
|
|
117 |
};
|
|
118 |
|
|
119 |
private OnClickListener tryAgainClicker = new OnClickListener(){
|
|
120 |
public void onClick(View v){
|
|
121 |
if(messenger != null){
|
|
122 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack);
|
|
123 |
message.replyTo = messenger;
|
|
124 |
try {
|
|
125 |
messengerService.send(message);
|
|
126 |
} catch (RemoteException e) {
|
|
127 |
e.printStackTrace();
|
|
128 |
}
|
|
129 |
}
|
|
130 |
}
|
|
131 |
};
|
|
132 |
|
|
133 |
public void onStop(){
|
|
134 |
super.onStop();
|
|
135 |
unBindFromService();
|
|
136 |
}
|
|
137 |
|
|
138 |
private ServiceConnection connection = new ServiceConnection(){
|
|
139 |
|
|
140 |
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
141 |
messengerService = new Messenger(service);
|
|
142 |
|
|
143 |
try{
|
|
144 |
//give the service a task
|
|
145 |
if(messenger != null){
|
|
146 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack);
|
|
147 |
message.replyTo = messenger;
|
|
148 |
messengerService.send(message);
|
|
149 |
}
|
|
150 |
|
|
151 |
}catch (RemoteException e){}
|
|
152 |
}
|
|
153 |
|
|
154 |
public void onServiceDisconnected(ComponentName name) {
|
|
155 |
messengerService = null;
|
|
156 |
}
|
|
157 |
|
|
158 |
};
|
|
159 |
|
|
160 |
private void unBindFromService(){
|
|
161 |
if(boundToService){
|
|
162 |
if(messenger != null){
|
|
163 |
try {
|
|
164 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack);
|
|
165 |
message.replyTo = messenger;
|
|
166 |
messengerService.send(message);
|
|
167 |
} catch (RemoteException e) {
|
|
168 |
e.printStackTrace();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
boundToService = false;
|
|
173 |
getActivity().unbindService(connection);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
private Handler.Callback messageCallback = new Handler.Callback() {
|
|
178 |
|
|
179 |
public boolean handleMessage(Message msg) {
|
|
180 |
switch(msg.what){
|
|
181 |
case MSG_START:
|
|
182 |
progress.setMax(msg.arg1);
|
|
183 |
progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, ""));
|
|
184 |
positive.setText(R.string.download_background);
|
|
185 |
positive.setOnClickListener(backgroundClicker);
|
|
186 |
negative.setText(R.string.download_cancel);
|
|
187 |
negative.setOnClickListener(cancelClicker);
|
|
188 |
break;
|
|
189 |
case MSG_UPDATE:
|
|
190 |
progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj));
|
|
191 |
progress.setProgress(msg.arg1);
|
|
192 |
break;
|
|
193 |
case MSG_DONE:
|
|
194 |
progress.setProgress(progress.getMax());
|
|
195 |
progress_sub.setText(R.string.download_done);
|
|
196 |
|
|
197 |
positive.setText(R.string.download_back);
|
|
198 |
positive.setOnClickListener(doneClicker);
|
|
199 |
|
|
200 |
negative.setVisibility(View.INVISIBLE);
|
|
201 |
break;
|
|
202 |
case MSG_FAILED:
|
|
203 |
progress.setProgress(progress.getMax());
|
|
204 |
progress_sub.setText(R.string.download_failed);
|
|
205 |
positive.setText(R.string.download_back);
|
|
206 |
positive.setOnClickListener(doneClicker);
|
|
207 |
|
|
208 |
negative.setText(R.string.download_tryagain);
|
|
209 |
negative.setOnClickListener(tryAgainClicker);
|
|
210 |
break;
|
|
211 |
}
|
|
212 |
return false;
|
|
213 |
}
|
|
214 |
};
|
|
215 |
|
|
216 |
}
|