author | Medo <smaxein@googlemail.com> |
Sun, 12 Aug 2012 22:37:57 +0200 | |
changeset 7482 | d70a5b0d1190 |
parent 7476 | 2fb781bbdd51 |
permissions | -rw-r--r-- |
7449 | 1 |
package org.hedgewars.hedgeroid.netplay; |
2 |
||
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
3 |
import org.hedgewars.hedgeroid.R; |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
7461
diff
changeset
|
4 |
import org.hedgewars.hedgeroid.frontlib.Frontlib; |
7449 | 5 |
import org.hedgewars.hedgeroid.netplay.Netplay.State; |
6 |
||
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
7 |
import android.app.Activity; |
7449 | 8 |
import android.content.BroadcastReceiver; |
9 |
import android.content.Context; |
|
10 |
import android.content.Intent; |
|
11 |
import android.content.IntentFilter; |
|
12 |
import android.os.Bundle; |
|
13 |
import android.support.v4.app.Fragment; |
|
14 |
import android.support.v4.content.LocalBroadcastManager; |
|
15 |
import android.widget.Toast; |
|
16 |
||
17 |
/** |
|
18 |
* Fragment for use by an activity that depends on the state of the network |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
19 |
* connection. The activity must implement the NetplayStateListener interface. |
7449 | 20 |
* |
21 |
* This fragment manages a few aspects of the netplay connection: Requesting |
|
22 |
* the network system loop to run at high frequency while the activity is in |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
23 |
* the foreground, and reacting to changes in the networking state by calling |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
24 |
* a callback method on the activity. |
7449 | 25 |
*/ |
26 |
public class NetplayStateFragment extends Fragment { |
|
27 |
private Netplay netplay; |
|
28 |
private Context appContext; |
|
29 |
private LocalBroadcastManager broadcastManager; |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
30 |
private NetplayStateListener listener; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
31 |
private State knownState; |
7449 | 32 |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
33 |
interface NetplayStateListener { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
34 |
/** |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
35 |
* This is called while the activity is running, and every time during resume, if |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
36 |
* a change in the networking state is detected. It is also called once |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
37 |
* with the initial state (which could be called a change from the "unknown" state). |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
38 |
*/ |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
39 |
void onNetplayStateChanged(State newState); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
40 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
41 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
42 |
@Override |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
43 |
public void onAttach(Activity activity) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
44 |
super.onAttach(activity); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
45 |
try { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
46 |
listener = (NetplayStateListener) activity; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
47 |
} catch(ClassCastException e) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
48 |
throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment."); |
7449 | 49 |
} |
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
50 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
51 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
52 |
@Override |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
53 |
public void onDetach() { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
54 |
super.onDetach(); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
55 |
listener = null; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
56 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
57 |
|
7449 | 58 |
@Override |
59 |
public void onCreate(Bundle icicle) { |
|
60 |
super.onCreate(icicle); |
|
61 |
appContext = getActivity().getApplicationContext(); |
|
62 |
broadcastManager = LocalBroadcastManager.getInstance(appContext); |
|
63 |
netplay = Netplay.getAppInstance(appContext); |
|
64 |
} |
|
65 |
||
66 |
@Override |
|
67 |
public void onResume() { |
|
68 |
super.onResume(); |
|
69 |
broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
70 |
broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM)); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
71 |
broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED)); |
7449 | 72 |
netplay.requestFastTicks(); |
73 |
||
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
74 |
State newState = netplay.getState(); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
75 |
if(knownState != newState) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
76 |
listener.onNetplayStateChanged(newState); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
77 |
knownState = newState; |
7449 | 78 |
} |
79 |
} |
|
80 |
||
81 |
@Override |
|
82 |
public void onPause() { |
|
83 |
super.onPause(); |
|
84 |
broadcastManager.unregisterReceiver(disconnectReceiver); |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
85 |
broadcastManager.unregisterReceiver(leaveRoomReceiver); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
86 |
broadcastManager.unregisterReceiver(stateChangeReceiver); |
7449 | 87 |
netplay.unrequestFastTicks(); |
88 |
} |
|
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
89 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
90 |
private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
91 |
@Override |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
92 |
public void onReceive(Context context, Intent intent) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
93 |
if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
94 |
String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
95 |
String toastText = getString(R.string.toast_disconnected, message); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
96 |
Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show(); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
97 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
98 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
99 |
}; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
100 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
101 |
private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
102 |
@Override |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
103 |
public void onReceive(Context context, Intent intent) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
104 |
int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1); |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
7461
diff
changeset
|
105 |
if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) { |
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
106 |
Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show(); |
7476
2fb781bbdd51
Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents:
7461
diff
changeset
|
107 |
} else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) { |
7461
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
108 |
Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show(); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
109 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
110 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
111 |
}; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
112 |
|
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
113 |
private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
114 |
@Override |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
115 |
public void onReceive(Context context, Intent intent) { |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
116 |
State newState = netplay.getState(); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
117 |
listener.onNetplayStateChanged(newState); |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
118 |
knownState = newState; |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
119 |
} |
38acbfdb484f
Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents:
7449
diff
changeset
|
120 |
}; |
7449 | 121 |
} |