1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.frontlib.Frontlib; |
|
5 import org.hedgewars.hedgeroid.netplay.Netplay.State; |
|
6 |
|
7 import android.app.Activity; |
|
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 |
|
19 * connection. The activity must implement the NetplayStateListener interface. |
|
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 |
|
23 * the foreground, and reacting to changes in the networking state by calling |
|
24 * a callback method on the activity. |
|
25 */ |
|
26 public class NetplayStateFragment extends Fragment { |
|
27 private Netplay netplay; |
|
28 private Context appContext; |
|
29 private LocalBroadcastManager broadcastManager; |
|
30 private NetplayStateListener listener; |
|
31 private State knownState; |
|
32 |
|
33 interface NetplayStateListener { |
|
34 /** |
|
35 * This is called while the activity is running, and every time during resume, if |
|
36 * a change in the networking state is detected. It is also called once |
|
37 * with the initial state (which could be called a change from the "unknown" state). |
|
38 */ |
|
39 void onNetplayStateChanged(State newState); |
|
40 } |
|
41 |
|
42 @Override |
|
43 public void onAttach(Activity activity) { |
|
44 super.onAttach(activity); |
|
45 try { |
|
46 listener = (NetplayStateListener) activity; |
|
47 } catch(ClassCastException e) { |
|
48 throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment."); |
|
49 } |
|
50 } |
|
51 |
|
52 @Override |
|
53 public void onDetach() { |
|
54 super.onDetach(); |
|
55 listener = null; |
|
56 } |
|
57 |
|
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)); |
|
70 broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM)); |
|
71 broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED)); |
|
72 netplay.requestFastTicks(); |
|
73 |
|
74 State newState = netplay.getState(); |
|
75 if(knownState != newState) { |
|
76 listener.onNetplayStateChanged(newState); |
|
77 knownState = newState; |
|
78 } |
|
79 } |
|
80 |
|
81 @Override |
|
82 public void onPause() { |
|
83 super.onPause(); |
|
84 broadcastManager.unregisterReceiver(disconnectReceiver); |
|
85 broadcastManager.unregisterReceiver(leaveRoomReceiver); |
|
86 broadcastManager.unregisterReceiver(stateChangeReceiver); |
|
87 netplay.unrequestFastTicks(); |
|
88 } |
|
89 |
|
90 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
|
91 @Override |
|
92 public void onReceive(Context context, Intent intent) { |
|
93 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
|
94 String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
|
95 String toastText = getString(R.string.toast_disconnected, message); |
|
96 Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show(); |
|
97 } |
|
98 } |
|
99 }; |
|
100 |
|
101 private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() { |
|
102 @Override |
|
103 public void onReceive(Context context, Intent intent) { |
|
104 int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1); |
|
105 if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) { |
|
106 Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show(); |
|
107 } else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) { |
|
108 Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show(); |
|
109 } |
|
110 } |
|
111 }; |
|
112 |
|
113 private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { |
|
114 @Override |
|
115 public void onReceive(Context context, Intent intent) { |
|
116 State newState = netplay.getState(); |
|
117 listener.onNetplayStateChanged(newState); |
|
118 knownState = newState; |
|
119 } |
|
120 }; |
|
121 } |
|