35 import android.widget.Toast; |
35 import android.widget.Toast; |
36 |
36 |
37 /** |
37 /** |
38 * Fragment for use by an activity that depends on the state of the network |
38 * Fragment for use by an activity that depends on the state of the network |
39 * connection. The activity must implement the NetplayStateListener interface. |
39 * connection. The activity must implement the NetplayStateListener interface. |
40 * |
40 * |
41 * This fragment manages reacting to changes in the networking state by calling |
41 * This fragment manages reacting to changes in the networking state by calling |
42 * a callback method on the activity. |
42 * a callback method on the activity. |
43 */ |
43 */ |
44 public class NetplayStateFragment extends Fragment { |
44 public class NetplayStateFragment extends Fragment { |
45 private Netplay netplay; |
45 private Netplay netplay; |
46 private Context appContext; |
46 private Context appContext; |
47 private LocalBroadcastManager broadcastManager; |
47 private LocalBroadcastManager broadcastManager; |
48 private NetplayStateListener listener; |
48 private NetplayStateListener listener; |
49 private State knownState; |
49 private State knownState; |
50 |
50 |
51 interface NetplayStateListener { |
51 interface NetplayStateListener { |
52 /** |
52 /** |
53 * This is called while the activity is running, and every time during resume, if |
53 * This is called while the activity is running, and every time during resume, if |
54 * a change in the networking state is detected. It is also called once |
54 * a change in the networking state is detected. It is also called once |
55 * with the initial state (which could be called a change from the "unknown" state). |
55 * with the initial state (which could be called a change from the "unknown" state). |
56 */ |
56 */ |
57 void onNetplayStateChanged(State newState); |
57 void onNetplayStateChanged(State newState); |
58 } |
58 } |
59 |
59 |
60 @Override |
60 @Override |
61 public void onAttach(Activity activity) { |
61 public void onAttach(Activity activity) { |
62 super.onAttach(activity); |
62 super.onAttach(activity); |
63 try { |
63 try { |
64 listener = (NetplayStateListener) activity; |
64 listener = (NetplayStateListener) activity; |
65 } catch(ClassCastException e) { |
65 } catch(ClassCastException e) { |
66 throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment."); |
66 throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment."); |
67 } |
67 } |
68 } |
68 } |
69 |
69 |
70 @Override |
70 @Override |
71 public void onDetach() { |
71 public void onDetach() { |
72 super.onDetach(); |
72 super.onDetach(); |
73 listener = null; |
73 listener = null; |
74 } |
74 } |
75 |
75 |
76 @Override |
76 @Override |
77 public void onCreate(Bundle icicle) { |
77 public void onCreate(Bundle icicle) { |
78 super.onCreate(icicle); |
78 super.onCreate(icicle); |
79 appContext = getActivity().getApplicationContext(); |
79 appContext = getActivity().getApplicationContext(); |
80 broadcastManager = LocalBroadcastManager.getInstance(appContext); |
80 broadcastManager = LocalBroadcastManager.getInstance(appContext); |
81 netplay = Netplay.getAppInstance(appContext); |
81 netplay = Netplay.getAppInstance(appContext); |
82 } |
82 } |
83 |
83 |
84 @Override |
84 @Override |
85 public void onResume() { |
85 public void onResume() { |
86 super.onResume(); |
86 super.onResume(); |
87 broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
87 broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
88 broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM)); |
88 broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM)); |
89 broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED)); |
89 broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED)); |
90 |
90 |
91 State newState = netplay.getState(); |
91 State newState = netplay.getState(); |
92 if(knownState != newState) { |
92 if(knownState != newState) { |
93 listener.onNetplayStateChanged(newState); |
93 listener.onNetplayStateChanged(newState); |
94 knownState = newState; |
94 knownState = newState; |
95 } |
95 } |
96 } |
96 } |
97 |
97 |
98 @Override |
98 @Override |
99 public void onPause() { |
99 public void onPause() { |
100 super.onPause(); |
100 super.onPause(); |
101 broadcastManager.unregisterReceiver(disconnectReceiver); |
101 broadcastManager.unregisterReceiver(disconnectReceiver); |
102 broadcastManager.unregisterReceiver(leaveRoomReceiver); |
102 broadcastManager.unregisterReceiver(leaveRoomReceiver); |
103 broadcastManager.unregisterReceiver(stateChangeReceiver); |
103 broadcastManager.unregisterReceiver(stateChangeReceiver); |
104 } |
104 } |
105 |
105 |
106 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
106 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
107 @Override |
107 @Override |
108 public void onReceive(Context context, Intent intent) { |
108 public void onReceive(Context context, Intent intent) { |
109 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
109 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
110 String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
110 String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
111 String toastText = getString(R.string.toast_disconnected, message); |
111 String toastText = getString(R.string.toast_disconnected, message); |
112 Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show(); |
112 Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show(); |
113 } |
113 } |
114 } |
114 } |
115 }; |
115 }; |
116 |
116 |
117 private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() { |
117 private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() { |
118 @Override |
118 @Override |
119 public void onReceive(Context context, Intent intent) { |
119 public void onReceive(Context context, Intent intent) { |
120 int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1); |
120 int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1); |
121 if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) { |
121 if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) { |
122 Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show(); |
122 Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show(); |
123 } else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) { |
123 } else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) { |
124 Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show(); |
124 Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show(); |
125 } |
125 } |
126 } |
126 } |
127 }; |
127 }; |
128 |
128 |
129 private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { |
129 private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { |
130 @Override |
130 @Override |
131 public void onReceive(Context context, Intent intent) { |
131 public void onReceive(Context context, Intent intent) { |
132 State newState = netplay.getState(); |
132 State newState = netplay.getState(); |
133 listener.onNetplayStateChanged(newState); |
133 listener.onNetplayStateChanged(newState); |
134 knownState = newState; |
134 knownState = newState; |
135 } |
135 } |
136 }; |
136 }; |
137 } |
137 } |