|
1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU General Public License |
|
7 * as published by the Free Software Foundation; either version 2 |
|
8 * of the License, or (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 package org.hedgewars.hedgeroid; |
|
21 |
|
22 import org.hedgewars.hedgeroid.R; |
|
23 import org.hedgewars.hedgeroid.frontlib.Frontlib; |
|
24 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
25 import org.hedgewars.hedgeroid.netplay.Netplay.State; |
|
26 |
|
27 import android.app.Activity; |
|
28 import android.content.BroadcastReceiver; |
|
29 import android.content.Context; |
|
30 import android.content.Intent; |
|
31 import android.content.IntentFilter; |
|
32 import android.os.Bundle; |
|
33 import android.support.v4.app.Fragment; |
|
34 import android.support.v4.content.LocalBroadcastManager; |
|
35 import android.widget.Toast; |
|
36 |
|
37 /** |
|
38 * Fragment for use by an activity that depends on the state of the network |
|
39 * connection. The activity must implement the NetplayStateListener interface. |
|
40 * |
|
41 * This fragment manages reacting to changes in the networking state by calling |
|
42 * a callback method on the activity. |
|
43 */ |
|
44 public class NetplayStateFragment extends Fragment { |
|
45 private Netplay netplay; |
|
46 private Context appContext; |
|
47 private LocalBroadcastManager broadcastManager; |
|
48 private NetplayStateListener listener; |
|
49 private State knownState; |
|
50 |
|
51 interface NetplayStateListener { |
|
52 /** |
|
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 |
|
55 * with the initial state (which could be called a change from the "unknown" state). |
|
56 */ |
|
57 void onNetplayStateChanged(State newState); |
|
58 } |
|
59 |
|
60 @Override |
|
61 public void onAttach(Activity activity) { |
|
62 super.onAttach(activity); |
|
63 try { |
|
64 listener = (NetplayStateListener) activity; |
|
65 } catch(ClassCastException e) { |
|
66 throw new ClassCastException("Activity " + activity + " must implement NetplayStateListener to use NetplayStateFragment."); |
|
67 } |
|
68 } |
|
69 |
|
70 @Override |
|
71 public void onDetach() { |
|
72 super.onDetach(); |
|
73 listener = null; |
|
74 } |
|
75 |
|
76 @Override |
|
77 public void onCreate(Bundle icicle) { |
|
78 super.onCreate(icicle); |
|
79 appContext = getActivity().getApplicationContext(); |
|
80 broadcastManager = LocalBroadcastManager.getInstance(appContext); |
|
81 netplay = Netplay.getAppInstance(appContext); |
|
82 } |
|
83 |
|
84 @Override |
|
85 public void onResume() { |
|
86 super.onResume(); |
|
87 broadcastManager.registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
|
88 broadcastManager.registerReceiver(leaveRoomReceiver, new IntentFilter(Netplay.ACTION_LEFT_ROOM)); |
|
89 broadcastManager.registerReceiver(stateChangeReceiver, new IntentFilter(Netplay.ACTION_STATE_CHANGED)); |
|
90 |
|
91 State newState = netplay.getState(); |
|
92 if(knownState != newState) { |
|
93 listener.onNetplayStateChanged(newState); |
|
94 knownState = newState; |
|
95 } |
|
96 } |
|
97 |
|
98 @Override |
|
99 public void onPause() { |
|
100 super.onPause(); |
|
101 broadcastManager.unregisterReceiver(disconnectReceiver); |
|
102 broadcastManager.unregisterReceiver(leaveRoomReceiver); |
|
103 broadcastManager.unregisterReceiver(stateChangeReceiver); |
|
104 } |
|
105 |
|
106 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
|
107 @Override |
|
108 public void onReceive(Context context, Intent intent) { |
|
109 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
|
110 String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
|
111 String toastText = getString(R.string.toast_disconnected, message); |
|
112 Toast.makeText(appContext, toastText, Toast.LENGTH_LONG).show(); |
|
113 } |
|
114 } |
|
115 }; |
|
116 |
|
117 private final BroadcastReceiver leaveRoomReceiver = new BroadcastReceiver() { |
|
118 @Override |
|
119 public void onReceive(Context context, Intent intent) { |
|
120 int reason = intent.getIntExtra(Netplay.EXTRA_REASON, -1); |
|
121 if(reason == Frontlib.NETCONN_ROOMLEAVE_ABANDONED) { |
|
122 Toast.makeText(appContext, R.string.toast_room_abandoned, Toast.LENGTH_LONG).show(); |
|
123 } else if(reason == Frontlib.NETCONN_ROOMLEAVE_KICKED) { |
|
124 Toast.makeText(appContext, R.string.toast_kicked, Toast.LENGTH_LONG).show(); |
|
125 } |
|
126 } |
|
127 }; |
|
128 |
|
129 private final BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() { |
|
130 @Override |
|
131 public void onReceive(Context context, Intent intent) { |
|
132 State newState = netplay.getState(); |
|
133 listener.onNetplayStateChanged(newState); |
|
134 knownState = newState; |
|
135 } |
|
136 }; |
|
137 } |