author | Xeli |
Sun, 09 Oct 2011 20:41:17 +0200 | |
branch | hedgeroid |
changeset 6045 | 9a7cc0f29430 |
parent 5671 | ba4c3a4c8b09 |
permissions | -rw-r--r-- |
5621 | 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 |
||
5412
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
20 |
package org.hedgewars.mobile.Downloader; |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
21 |
|
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
22 |
import org.hedgewars.mobile.MainActivity; |
ab055114c788
Moved download classes to their own dir and fixed the way the dest dir is being 'build'
Xeli
parents:
5397
diff
changeset
|
23 |
import org.hedgewars.mobile.R; |
5397 | 24 |
|
25 |
import android.app.Activity; |
|
26 |
import android.content.ComponentName; |
|
27 |
import android.content.Context; |
|
28 |
import android.content.Intent; |
|
29 |
import android.content.ServiceConnection; |
|
30 |
import android.os.Bundle; |
|
31 |
import android.os.Handler; |
|
32 |
import android.os.IBinder; |
|
33 |
import android.os.Message; |
|
34 |
import android.os.Messenger; |
|
35 |
import android.os.RemoteException; |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
36 |
import android.preference.PreferenceManager; |
5397 | 37 |
import android.view.View; |
38 |
import android.view.View.OnClickListener; |
|
39 |
import android.widget.Button; |
|
40 |
import android.widget.ProgressBar; |
|
41 |
import android.widget.TextView; |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
42 |
import android.widget.Toast; |
5397 | 43 |
|
44 |
public class DownloadActivity extends Activity{ |
|
45 |
private Messenger messageService; |
|
46 |
private boolean boundToService = false; |
|
47 |
||
48 |
private TextView progress_sub; |
|
49 |
private ProgressBar progress; |
|
50 |
private Button positive, negative; |
|
51 |
||
52 |
public static final int MSG_START = 0; |
|
53 |
public static final int MSG_UPDATE = 1; |
|
54 |
public static final int MSG_DONE = 2; |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
55 |
public static final int MSG_FAILED = 3; |
5397 | 56 |
private Handler.Callback messageCallback = new Handler.Callback() { |
57 |
||
58 |
public boolean handleMessage(Message msg) { |
|
59 |
switch(msg.what){ |
|
60 |
case MSG_START: |
|
61 |
progress.setMax(msg.arg1); |
|
62 |
progress_sub.setText(String.format("%dkb/%dkb\n%s", 0, msg.arg1, "")); |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
63 |
positive.setText(R.string.download_background); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
64 |
positive.setOnClickListener(backgroundClicker); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
65 |
negative.setText(R.string.download_cancel); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
66 |
negative.setOnClickListener(cancelClicker); |
5397 | 67 |
break; |
68 |
case MSG_UPDATE: |
|
69 |
progress_sub.setText(String.format("%d%% - %dkb/%dkb\n%s",(msg.arg1*100)/msg.arg2, msg.arg1, msg.arg2, msg.obj)); |
|
70 |
progress.setProgress(msg.arg1); |
|
71 |
break; |
|
72 |
case MSG_DONE: |
|
73 |
progress.setProgress(progress.getMax()); |
|
74 |
progress_sub.setText(R.string.download_done); |
|
75 |
||
76 |
positive.setText(R.string.download_back); |
|
77 |
positive.setOnClickListener(doneClicker); |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
78 |
|
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
79 |
negative.setVisibility(View.INVISIBLE); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
80 |
break; |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
81 |
case MSG_FAILED: |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
82 |
progress.setProgress(progress.getMax()); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
83 |
progress_sub.setText(R.string.download_failed); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
84 |
positive.setText(R.string.download_back); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
85 |
positive.setOnClickListener(doneClicker); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
86 |
|
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
87 |
negative.setText(R.string.download_tryagain); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
88 |
negative.setOnClickListener(tryAgainClicker); |
5397 | 89 |
break; |
90 |
} |
|
91 |
return false; |
|
92 |
} |
|
93 |
}; |
|
94 |
private Handler messageHandler = new Handler(messageCallback); |
|
95 |
private Messenger messenger = new Messenger(messageHandler); |
|
96 |
||
97 |
public void onCreate(Bundle savedInstanceState){ |
|
98 |
super.onCreate(savedInstanceState); |
|
99 |
setContentView(R.layout.download); |
|
100 |
||
101 |
progress_sub = (TextView)findViewById(R.id.progressbar_sub); |
|
102 |
progress = (ProgressBar)findViewById(R.id.progressbar); |
|
103 |
||
104 |
positive = (Button) findViewById(R.id.background); |
|
105 |
negative = (Button) findViewById(R.id.cancelDownload); |
|
106 |
positive.setOnClickListener(backgroundClicker); |
|
107 |
negative.setOnClickListener(cancelClicker); |
|
108 |
||
109 |
} |
|
110 |
||
111 |
private OnClickListener backgroundClicker = new OnClickListener(){ |
|
112 |
public void onClick(View v){ |
|
113 |
finish(); |
|
114 |
} |
|
115 |
}; |
|
116 |
private OnClickListener cancelClicker = new OnClickListener(){ |
|
117 |
public void onClick(View v){ |
|
118 |
Intent i = new Intent(getApplicationContext(), DownloadService.class); |
|
119 |
i.putExtra("taskID", DownloadService.TASKID_CANCEL); |
|
120 |
startService(i); |
|
121 |
finish(); |
|
122 |
} |
|
123 |
}; |
|
124 |
private OnClickListener doneClicker = new OnClickListener(){ |
|
125 |
public void onClick(View v){ |
|
126 |
finish(); |
|
127 |
startActivity(new Intent(getApplicationContext(), MainActivity.class)); |
|
128 |
} |
|
129 |
}; |
|
130 |
||
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
131 |
private OnClickListener tryAgainClicker = new OnClickListener(){ |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
132 |
public void onClick(View v){ |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
133 |
bindToService(DownloadService.TASKID_RETRY); |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
134 |
} |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
135 |
}; |
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
136 |
|
5397 | 137 |
public void onStart(){ |
138 |
super.onStart(); |
|
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
139 |
bindToService(DownloadService.TASKID_START); |
5397 | 140 |
} |
141 |
||
142 |
public void onStop(){ |
|
143 |
super.onStop(); |
|
144 |
unBindFromService(); |
|
145 |
} |
|
146 |
||
147 |
private ServiceConnection connection = new ServiceConnection(){ |
|
148 |
||
149 |
public void onServiceConnected(ComponentName name, IBinder service) { |
|
150 |
messageService = new Messenger(service); |
|
151 |
||
152 |
try{ |
|
153 |
Message msg = Message.obtain(null, DownloadService.MSG_REGISTER_CLIENT); |
|
154 |
msg.replyTo = messenger; |
|
155 |
messageService.send(msg); |
|
156 |
||
157 |
}catch (RemoteException e){} |
|
158 |
} |
|
159 |
||
160 |
public void onServiceDisconnected(ComponentName name) { |
|
161 |
messageService = null; |
|
162 |
} |
|
163 |
||
164 |
}; |
|
165 |
||
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
166 |
private void bindToService(int taskId){ |
5397 | 167 |
Intent i = new Intent(getApplicationContext(), DownloadService.class); |
5671
ba4c3a4c8b09
Check if we need to download and prevent user from reaching the startgame menu because it needs images from the download
Xeli
parents:
5657
diff
changeset
|
168 |
i.putExtra("taskID", taskId); |
5397 | 169 |
startService(i); |
170 |
bindService(new Intent(getApplicationContext(), DownloadService.class), connection, Context.BIND_AUTO_CREATE); |
|
171 |
boundToService = true; |
|
172 |
} |
|
173 |
||
174 |
private void unBindFromService(){ |
|
175 |
if(boundToService){ |
|
176 |
boundToService = false; |
|
177 |
unbindService(connection); |
|
178 |
} |
|
179 |
} |
|
180 |
} |