project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/ConnectingDialog.java
author Medo <smaxein@googlemail.com>
Thu, 23 Aug 2012 18:28:33 +0200
changeset 7588 27e5857da6af
parent 7584 7831c84cc644
child 10017 de822cd3df3a
permissions -rw-r--r--
Hedgeroid improvements: - Moved frontlib initialization to a slightly better place - Added new JNA type ByteArrayPtr to move more JNA code into the frontlib package and to improve pointer type safety
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    19
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.netplay.Netplay;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.Netplay.State;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.app.Dialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.app.ProgressDialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.content.BroadcastReceiver;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.Context;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.DialogInterface;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.content.Intent;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.content.IntentFilter;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.os.Bundle;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.support.v4.content.LocalBroadcastManager;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    34
import android.widget.Toast;
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    36
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    37
 * Indeterminate progress dialog that is shown in the MainActivity while trying
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    38
 * to connect to the server. If the connection fails (disconnect before we reach
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    39
 * lobby state), an error toast with the disconnect message is shown.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    40
 * 
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    41
 */
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
public class ConnectingDialog extends ConnectionDependendDialogFragment {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	public void onStart() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
		super.onStart();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    47
		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(disconnectedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    48
		
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		if(Netplay.getAppInstance(getActivity().getApplicationContext()).getState() != State.CONNECTING) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
			dismiss();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
	public void onStop() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
		super.onStop();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(connectedReceiver);
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    58
		LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(disconnectedReceiver);
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	public Dialog onCreateDialog(Bundle savedInstanceState) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
		ProgressDialog dialog = new ProgressDialog(getActivity());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		dialog.setIndeterminate(true);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		dialog.setTitle(R.string.dialog_connecting_title);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
		dialog.setMessage(getString(R.string.dialog_connecting_message));
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
		return dialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		public void onReceive(Context context, Intent intent) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
			Dialog dialog = getDialog();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
			if(dialog != null) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
				dialog.dismiss();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
			} else {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
				dismiss();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
			}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	};
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    83
	private BroadcastReceiver disconnectedReceiver = new BroadcastReceiver() {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    84
		@Override
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    85
		public void onReceive(Context context, Intent intent) {
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    86
			Toast.makeText(getActivity(), intent.getExtras().getString(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    87
		}
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    88
	};
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    89
	
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	public void onCancel(DialogInterface dialog) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
		super.onCancel(dialog);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
		Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
	};
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
}