1 package org.hedgewars.hedgeroid.netplay; |
1 package org.hedgewars.hedgeroid.netplay; |
2 |
2 |
3 import org.hedgewars.hedgeroid.R; |
3 import org.hedgewars.hedgeroid.R; |
4 |
4 |
5 import android.content.BroadcastReceiver; |
|
6 import android.content.Context; |
5 import android.content.Context; |
7 import android.content.Intent; |
|
8 import android.content.IntentFilter; |
|
9 import android.graphics.drawable.Drawable; |
6 import android.graphics.drawable.Drawable; |
10 import android.os.Bundle; |
7 import android.os.Bundle; |
|
8 import android.support.v4.app.Fragment; |
11 import android.support.v4.app.FragmentActivity; |
9 import android.support.v4.app.FragmentActivity; |
12 import android.support.v4.content.LocalBroadcastManager; |
10 import android.support.v4.app.FragmentTransaction; |
13 import android.view.LayoutInflater; |
11 import android.view.LayoutInflater; |
14 import android.view.Menu; |
12 import android.view.Menu; |
15 import android.view.MenuItem; |
13 import android.view.MenuItem; |
16 import android.view.View; |
14 import android.view.View; |
17 import android.widget.ImageView; |
15 import android.widget.ImageView; |
20 import android.widget.TextView; |
18 import android.widget.TextView; |
21 import android.widget.Toast; |
19 import android.widget.Toast; |
22 |
20 |
23 public class LobbyActivity extends FragmentActivity { |
21 public class LobbyActivity extends FragmentActivity { |
24 private TabHost tabHost; |
22 private TabHost tabHost; |
25 private Netplay netconn; |
23 private Netplay netplay; |
26 private boolean isInForeground; |
|
27 |
|
28 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
|
29 @Override |
|
30 public void onReceive(Context context, Intent intent) { |
|
31 if(isInForeground && intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
|
32 String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE); |
|
33 Toast.makeText(getApplicationContext(), "Disconnected: "+message, Toast.LENGTH_LONG).show(); |
|
34 } |
|
35 finish(); |
|
36 } |
|
37 }; |
|
38 |
24 |
39 @Override |
25 @Override |
40 protected void onCreate(Bundle icicle) { |
26 protected void onCreate(Bundle icicle) { |
41 super.onCreate(icicle); |
27 super.onCreate(icicle); |
42 LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
|
43 netconn = Netplay.getAppInstance(getApplicationContext()); |
|
44 |
28 |
45 setContentView(R.layout.activity_lobby); |
29 setContentView(R.layout.activity_lobby); |
|
30 Fragment chatFragment = getSupportFragmentManager().findFragmentById(R.id.chatFragment); |
|
31 chatFragment.getArguments().putBoolean(ChatFragment.ARGUMENT_INROOM, false); |
|
32 |
|
33 FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); |
|
34 trans.add(new NetplayStateFragment(), "netplayFragment"); |
|
35 trans.commit(); |
|
36 |
|
37 netplay = Netplay.getAppInstance(getApplicationContext()); |
|
38 |
46 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
39 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
47 if(tabHost != null) { |
40 if(tabHost != null) { |
48 tabHost.setup(); |
41 tabHost.setup(); |
49 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
42 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
50 |
43 |
54 |
47 |
55 if (icicle != null) { |
48 if (icicle != null) { |
56 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
49 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
57 } |
50 } |
58 } |
51 } |
59 } |
|
60 |
|
61 @Override |
|
62 protected void onDestroy() { |
|
63 super.onDestroy(); |
|
64 LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(disconnectReceiver); |
|
65 } |
52 } |
66 |
53 |
67 private View createIndicatorView(TabHost tabHost, int label, Drawable icon) { |
54 private View createIndicatorView(TabHost tabHost, int label, Drawable icon) { |
68 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
55 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
69 |
56 |
79 iconView.setImageDrawable(icon); |
66 iconView.setImageDrawable(icon); |
80 } |
67 } |
81 |
68 |
82 return tabIndicator; |
69 return tabIndicator; |
83 } |
70 } |
84 |
|
85 @Override |
|
86 protected void onStart() { |
|
87 super.onStart(); |
|
88 isInForeground = true; |
|
89 Netplay.getAppInstance(getApplicationContext()).requestFastTicks(); |
|
90 } |
|
91 |
|
92 @Override |
|
93 protected void onStop() { |
|
94 super.onStop(); |
|
95 isInForeground = false; |
|
96 Netplay.getAppInstance(getApplicationContext()).unrequestFastTicks(); |
|
97 } |
|
98 |
71 |
99 @Override |
72 @Override |
100 public boolean onCreateOptionsMenu(Menu menu) { |
73 public boolean onCreateOptionsMenu(Menu menu) { |
101 super.onCreateOptionsMenu(menu); |
74 super.onCreateOptionsMenu(menu); |
102 getMenuInflater().inflate(R.menu.lobby_options, menu); |
75 getMenuInflater().inflate(R.menu.lobby_options, menu); |
108 switch(item.getItemId()) { |
81 switch(item.getItemId()) { |
109 case R.id.room_create: |
82 case R.id.room_create: |
110 Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
83 Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
111 return true; |
84 return true; |
112 case R.id.disconnect: |
85 case R.id.disconnect: |
113 netconn.disconnect(); |
86 netplay.disconnect(); |
114 return true; |
87 return true; |
115 default: |
88 default: |
116 return super.onOptionsItemSelected(item); |
89 return super.onOptionsItemSelected(item); |
117 } |
90 } |
118 } |
91 } |
119 |
92 |
120 @Override |
93 @Override |
121 public void onBackPressed() { |
94 public void onBackPressed() { |
122 netconn.disconnect(); |
|
123 super.onBackPressed(); |
95 super.onBackPressed(); |
|
96 netplay.disconnect(); |
124 } |
97 } |
125 |
98 |
126 @Override |
99 @Override |
127 protected void onSaveInstanceState(Bundle icicle) { |
100 protected void onSaveInstanceState(Bundle icicle) { |
128 super.onSaveInstanceState(icicle); |
101 super.onSaveInstanceState(icicle); |