author | Wuzzy <Wuzzy2@mail.ru> |
Sat, 21 Oct 2017 18:38:28 +0200 | |
changeset 12734 | 4e0e59255856 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
6350 | 1 |
/* |
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
6350 | 4 |
* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
6350 | 9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
6700
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6350 | 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{ |
|
10017 | 44 |
public static final String EXTRA_TASK = "task"; |
6350 | 45 |
|
10017 | 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; |
|
6350 | 50 |
|
10017 | 51 |
private boolean boundToService = false; |
6350 | 52 |
|
10017 | 53 |
private TextView progress_sub; |
54 |
private ProgressBar progress; |
|
55 |
private Button /*positive,*/ negative; |
|
6350 | 56 |
|
10017 | 57 |
private DownloadPackage pack; |
6350 | 58 |
|
10017 | 59 |
private Handler messageHandler; |
60 |
private Messenger messenger, messengerService; |
|
6350 | 61 |
|
10017 | 62 |
public static DownloadFragment getInstance(DownloadPackage task){ |
63 |
DownloadFragment df = new DownloadFragment(); |
|
64 |
Bundle args = new Bundle(); |
|
65 |
args.putParcelable(DownloadFragment.EXTRA_TASK, task); |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
66 |
|
10017 | 67 |
df.setArguments(args); |
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
68 |
|
10017 | 69 |
return df; |
70 |
} |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
71 |
|
10017 | 72 |
public void onActivityCreated(Bundle savedInstanceState){ |
73 |
super.onActivityCreated(savedInstanceState); |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
74 |
|
10017 | 75 |
messageHandler = new Handler(messageCallback); |
76 |
messenger = new Messenger(messageHandler); |
|
77 |
Intent i = new Intent(getActivity().getApplicationContext(), DownloadService.class); |
|
78 |
getActivity().startService(i); |
|
79 |
getActivity().bindService(new Intent(getActivity().getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
|
80 |
} |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
81 |
|
10017 | 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); |
|
6350 | 86 |
|
10017 | 87 |
//positive = (Button) v.findViewById(R.id.background); |
88 |
negative = (Button) v.findViewById(R.id.cancelDownload); |
|
89 |
//positive.setOnClickListener(backgroundClicker); |
|
90 |
negative.setOnClickListener(cancelClicker); |
|
6350 | 91 |
|
10017 | 92 |
pack = getArguments().getParcelable(DownloadFragment.EXTRA_TASK); |
6350 | 93 |
|
10017 | 94 |
return v; |
95 |
} |
|
6350 | 96 |
|
10017 | 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(messengerService != 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 |
}; |
|
6350 | 118 |
|
10017 | 119 |
private OnClickListener tryAgainClicker = new OnClickListener(){ |
120 |
public void onClick(View v){ |
|
121 |
if(messengerService != 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 |
}; |
|
6350 | 132 |
|
10017 | 133 |
public void onDestroy(){ |
134 |
unBindFromService(); |
|
135 |
super.onDestroy(); |
|
136 |
} |
|
6350 | 137 |
|
10017 | 138 |
private ServiceConnection connection = new ServiceConnection(){ |
6350 | 139 |
|
10017 | 140 |
public void onServiceConnected(ComponentName name, IBinder service) { |
141 |
messengerService = new Messenger(service); |
|
6350 | 142 |
|
10017 | 143 |
try{ |
144 |
//give the service a task |
|
145 |
if(messengerService != null){ |
|
146 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_ADDTASK, pack); |
|
147 |
message.replyTo = messenger; |
|
148 |
messengerService.send(message); |
|
149 |
} |
|
6350 | 150 |
|
10017 | 151 |
}catch (RemoteException e){} |
152 |
} |
|
6350 | 153 |
|
10017 | 154 |
public void onServiceDisconnected(ComponentName name) { |
155 |
messengerService = null; |
|
156 |
} |
|
6350 | 157 |
|
10017 | 158 |
}; |
6350 | 159 |
|
10017 | 160 |
public void unBindFromService(){ |
161 |
if(messengerService != null){ |
|
162 |
try { |
|
163 |
Message message = Message.obtain(messageHandler, DownloadService.MSG_UNREGISTER_CLIENT, pack); |
|
164 |
message.replyTo = messenger; |
|
165 |
messengerService.send(message); |
|
166 |
} catch (RemoteException e) { |
|
167 |
e.printStackTrace(); |
|
168 |
} |
|
169 |
} |
|
6437
4ed58839b13b
Android: fixed serviceconnectionleak, made the result code of asynctask a bit better, fix cancel button
Xeli
parents:
6434
diff
changeset
|
170 |
|
10017 | 171 |
getActivity().unbindService(connection); |
172 |
} |
|
6350 | 173 |
|
10017 | 174 |
private Handler.Callback messageCallback = new Handler.Callback() { |
6350 | 175 |
|
10017 | 176 |
public boolean handleMessage(Message msg) { |
177 |
switch(msg.what){ |
|
178 |
case MSG_START: |
|
179 |
progress.setMax(msg.arg1); |
|
180 |
progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, "")); |
|
181 |
//positive.setText(R.string.download_background); |
|
182 |
//positive.setOnClickListener(backgroundClicker); |
|
183 |
negative.setText(R.string.download_cancel); |
|
184 |
negative.setOnClickListener(cancelClicker); |
|
185 |
break; |
|
186 |
case MSG_UPDATE: |
|
187 |
progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj)); |
|
188 |
progress.setProgress(msg.arg1); |
|
189 |
break; |
|
190 |
case MSG_DONE: |
|
191 |
progress.setProgress(progress.getMax()); |
|
192 |
progress_sub.setText(R.string.download_done); |
|
6350 | 193 |
|
10017 | 194 |
// positive.setText(R.string.download_back); |
195 |
// positive.setOnClickListener(doneClicker); |
|
196 |
||
197 |
negative.setVisibility(View.INVISIBLE); |
|
198 |
break; |
|
199 |
case MSG_FAILED: |
|
200 |
progress.setProgress(progress.getMax()); |
|
6350 | 201 |
|
10017 | 202 |
String errorMsg = getString(R.string.download_failed); |
203 |
switch(msg.arg1){ |
|
204 |
case DownloadAsyncTask.EXIT_CONNERROR: progress_sub.setText(errorMsg + " " + "Connection error"); break; |
|
205 |
case DownloadAsyncTask.EXIT_FNF: progress_sub.setText(errorMsg + " " + "File not found"); break; |
|
206 |
case DownloadAsyncTask.EXIT_MD5: progress_sub.setText(errorMsg + " " + "MD5 check failed"); break; |
|
207 |
case DownloadAsyncTask.EXIT_URLFAIL: progress_sub.setText(errorMsg + " " + "Invalid url"); break; |
|
208 |
} |
|
209 |
negative.setText(R.string.download_tryagain); |
|
210 |
negative.setOnClickListener(tryAgainClicker); |
|
211 |
break; |
|
212 |
} |
|
213 |
return false; |
|
214 |
} |
|
215 |
}; |
|
6350 | 216 |
|
217 |
} |