author | koda |
Wed, 10 Jan 2018 15:27:47 +0100 | |
changeset 12889 | 8f99199b33ea |
parent 10017 | de822cd3df3a |
child 15533 | fb23e49b2d4e |
permissions | -rw-r--r-- |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
1 |
/* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
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> |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
4 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
5 |
* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
6 |
* 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:
7582
diff
changeset
|
7 |
* 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:
7582
diff
changeset
|
8 |
* 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:
7582
diff
changeset
|
9 |
* of the License, or (at your option) any later version. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
10 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
14 |
* GNU General Public License for more details. |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
15 |
* |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
17 |
* 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:
7582
diff
changeset
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
19 |
*/ |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
21 |
|
7344
25b8906f901a
Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents:
7330
diff
changeset
|
22 |
import java.io.File; |
25b8906f901a
Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents:
7330
diff
changeset
|
23 |
import java.io.FileInputStream; |
25b8906f901a
Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents:
7330
diff
changeset
|
24 |
import java.io.IOException; |
25b8906f901a
Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents:
7330
diff
changeset
|
25 |
|
6350 | 26 |
import org.hedgewars.hedgeroid.Downloader.DownloadAssets; |
6343 | 27 |
import org.hedgewars.hedgeroid.Downloader.DownloadListActivity; |
7358
57a508884052
Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
7355
diff
changeset
|
28 |
import org.hedgewars.hedgeroid.netplay.Netplay; |
57a508884052
Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
7355
diff
changeset
|
29 |
import org.hedgewars.hedgeroid.netplay.Netplay.State; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
7485
diff
changeset
|
30 |
import org.hedgewars.hedgeroid.util.FileUtils; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
31 |
|
6456
fed715edc3ee
android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents:
6350
diff
changeset
|
32 |
import android.app.AlertDialog; |
fed715edc3ee
android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents:
6350
diff
changeset
|
33 |
import android.app.Dialog; |
6350 | 34 |
import android.app.ProgressDialog; |
7355
5673e95ef647
Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
7352
diff
changeset
|
35 |
import android.content.BroadcastReceiver; |
5673e95ef647
Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
7352
diff
changeset
|
36 |
import android.content.Context; |
6456
fed715edc3ee
android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents:
6350
diff
changeset
|
37 |
import android.content.DialogInterface; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
38 |
import android.content.Intent; |
7355
5673e95ef647
Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
7352
diff
changeset
|
39 |
import android.content.IntentFilter; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
40 |
import android.os.Bundle; |
6350 | 41 |
import android.support.v4.app.FragmentActivity; |
7444
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
42 |
import android.support.v4.app.FragmentManager; |
7355
5673e95ef647
Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
7352
diff
changeset
|
43 |
import android.support.v4.content.LocalBroadcastManager; |
7444
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
44 |
import android.view.Menu; |
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
45 |
import android.view.MenuItem; |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
46 |
import android.view.View; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
47 |
import android.view.View.OnClickListener; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
48 |
import android.widget.Button; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
49 |
import android.widget.Toast; |
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
50 |
|
6350 | 51 |
public class MainActivity extends FragmentActivity { |
10017 | 52 |
private static final int DIALOG_NO_SDCARD = 0; |
53 |
||
54 |
private LocalBroadcastManager broadcastManager; |
|
55 |
private ProgressDialog assetsDialog; |
|
56 |
||
57 |
public void onCreate(Bundle sis){ |
|
58 |
super.onCreate(sis); |
|
59 |
setContentView(R.layout.activity_main); |
|
60 |
||
61 |
broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); |
|
62 |
Button startLocalGame = (Button)findViewById(R.id.startGame); |
|
63 |
Button startNetGame = (Button)findViewById(R.id.joinLobby); |
|
64 |
||
65 |
startLocalGame.setOnClickListener(startGameListener); |
|
66 |
startNetGame.setOnClickListener(startNetGameListener); |
|
6350 | 67 |
|
10017 | 68 |
if(!FileUtils.isDataPathAvailable()){ |
69 |
showDialog(DIALOG_NO_SDCARD); |
|
70 |
} else { |
|
71 |
String existingVersion = ""; |
|
72 |
try { |
|
73 |
File versionFile = new File(FileUtils.getCachePath(this), "assetsversion.txt"); |
|
74 |
existingVersion = FileUtils.readToString(new FileInputStream(versionFile)); |
|
75 |
} catch(IOException e) { |
|
76 |
} |
|
6350 | 77 |
|
10017 | 78 |
String newVersion = ""; |
79 |
try { |
|
80 |
newVersion = FileUtils.readToString(getAssets().open("assetsversion.txt")); |
|
81 |
} catch(IOException e) { |
|
82 |
} |
|
6350 | 83 |
|
10017 | 84 |
if(!existingVersion.equals(newVersion)) { |
85 |
DownloadAssets assetsAsyncTask = new DownloadAssets(this); |
|
86 |
assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets to SD card..."); |
|
87 |
assetsAsyncTask.execute(); |
|
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
protected void onResume() { |
|
94 |
super.onResume(); |
|
95 |
broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED)); |
|
96 |
broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
|
97 |
broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED)); |
|
98 |
} |
|
99 |
||
100 |
@Override |
|
101 |
protected void onPause() { |
|
102 |
super.onPause(); |
|
103 |
broadcastManager.unregisterReceiver(connectedReceiver); |
|
104 |
broadcastManager.unregisterReceiver(connectionFailedReceiver); |
|
105 |
broadcastManager.unregisterReceiver(passwordRequestedReceiver); |
|
106 |
Netplay netplay = Netplay.getAppInstance(getApplicationContext()); |
|
107 |
if(netplay.getState() == State.CONNECTING) { |
|
108 |
netplay.disconnect(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
public boolean onCreateOptionsMenu(Menu menu) { |
|
114 |
super.onCreateOptionsMenu(menu); |
|
115 |
getMenuInflater().inflate(R.menu.main_options, menu); |
|
116 |
return true; |
|
117 |
} |
|
6350 | 118 |
|
10017 | 119 |
@Override |
120 |
public boolean onOptionsItemSelected(MenuItem item) { |
|
121 |
switch(item.getItemId()) { |
|
122 |
case R.id.download: |
|
123 |
startActivityForResult(new Intent(this, DownloadListActivity.class), 0); |
|
124 |
return true; |
|
125 |
case R.id.preferences: |
|
126 |
Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
|
127 |
return true; |
|
128 |
case R.id.edit_weaponsets: |
|
129 |
startActivity(new Intent(this, WeaponsetListActivity.class)); |
|
130 |
return true; |
|
131 |
case R.id.edit_teams: |
|
132 |
startActivity(new Intent(this, TeamListActivity.class)); |
|
133 |
return true; |
|
134 |
default: |
|
135 |
return super.onOptionsItemSelected(item); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
public Dialog onCreateDialog(int id, Bundle args){ |
|
140 |
switch(id) { |
|
141 |
case DIALOG_NO_SDCARD: |
|
142 |
return createNoSdcardDialog(); |
|
143 |
default: |
|
144 |
throw new IndexOutOfBoundsException(); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
private Dialog createNoSdcardDialog() { |
|
149 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
150 |
builder.setTitle(R.string.sdcard_not_mounted_title); |
|
151 |
builder.setMessage(R.string.sdcard_not_mounted); |
|
152 |
builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){ |
|
153 |
public void onClick(DialogInterface dialog, int which) { |
|
154 |
finish(); |
|
155 |
} |
|
156 |
}); |
|
157 |
||
158 |
return builder.create(); |
|
159 |
} |
|
160 |
||
161 |
public void onAssetsDownloaded(boolean result){ |
|
162 |
if(!result){ |
|
163 |
Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show(); |
|
164 |
} |
|
165 |
assetsDialog.dismiss(); |
|
166 |
} |
|
7352
641f11cdd319
Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents:
7349
diff
changeset
|
167 |
|
10017 | 168 |
private final OnClickListener startGameListener = new OnClickListener(){ |
169 |
public void onClick(View v){ |
|
170 |
startActivity(new Intent(getApplicationContext(), LocalRoomActivity.class)); |
|
171 |
} |
|
172 |
}; |
|
6456
fed715edc3ee
android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents:
6350
diff
changeset
|
173 |
|
10017 | 174 |
private final OnClickListener startNetGameListener = new OnClickListener() { |
175 |
public void onClick(View v) { |
|
176 |
State state = Netplay.getAppInstance(getApplicationContext()).getState(); |
|
177 |
switch(state) { |
|
178 |
case NOT_CONNECTED: |
|
179 |
FragmentManager fm = getSupportFragmentManager(); |
|
180 |
StartNetgameDialog startNetgameDialog = new StartNetgameDialog(); |
|
181 |
startNetgameDialog.show(fm, "start_netgame_dialog"); |
|
182 |
break; |
|
183 |
case CONNECTING: |
|
184 |
onNetConnectingStarted(); |
|
185 |
break; |
|
186 |
default: |
|
187 |
startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
}; |
|
6350 | 192 |
|
10017 | 193 |
private BroadcastReceiver connectedReceiver = new BroadcastReceiver() { |
194 |
@Override |
|
195 |
public void onReceive(Context context, Intent intent) { |
|
196 |
startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
|
197 |
} |
|
198 |
}; |
|
7444
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
199 |
|
10017 | 200 |
private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() { |
201 |
@Override |
|
202 |
public void onReceive(Context context, Intent intent) { |
|
203 |
if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
|
204 |
Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show(); |
|
205 |
} |
|
206 |
} |
|
207 |
}; |
|
208 |
||
209 |
private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() { |
|
210 |
@Override |
|
211 |
public void onReceive(Context context, Intent intent) { |
|
212 |
FragmentManager fm = getSupportFragmentManager(); |
|
213 |
PasswordDialog passwordDialog = new PasswordDialog(intent.getStringExtra(Netplay.EXTRA_PLAYERNAME)); |
|
214 |
passwordDialog.show(fm, "fragment_password_dialog"); |
|
215 |
} |
|
216 |
}; |
|
217 |
||
218 |
public void onNetConnectingStarted() { |
|
7444
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
219 |
FragmentManager fm = getSupportFragmentManager(); |
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
220 |
ConnectingDialog connectingDialog = new ConnectingDialog(); |
2e31f114f57e
Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
7358
diff
changeset
|
221 |
connectingDialog.show(fm, "fragment_connecting_dialog"); |
10017 | 222 |
} |
6047
10011f051f9c
Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff
changeset
|
223 |
} |