project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/StartNetgameDialog.java
author Vittorio Giovara <vittorio.giovara@gmail.com>
Tue, 10 Nov 2015 13:53:40 +0100
changeset 11370 7ab863821864
parent 10017 de822cd3df3a
permissions -rw-r--r--
videorec: Drop a commented out option
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
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
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import android.app.AlertDialog;
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.content.Context;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.content.DialogInterface;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.SharedPreferences;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.SharedPreferences.Editor;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.os.Bundle;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.support.v4.app.DialogFragment;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.view.KeyEvent;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.inputmethod.EditorInfo;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.EditText;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.widget.TextView;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.widget.TextView.OnEditorActionListener;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
public class StartNetgameDialog extends DialogFragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
    private static final String PREF_PLAYERNAME = "playerName";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    public Dialog onCreateDialog(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
        SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
        final String playerName = prefs.getString(PREF_PLAYERNAME, "Player");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
        final EditText editText = new EditText(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        editText.setText(playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
        editText.setHint(R.string.start_netgame_dialog_playername_hint);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
        editText.setId(android.R.id.text1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        editText.setSingleLine();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        builder.setTitle(R.string.start_netgame_dialog_title);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        builder.setMessage(R.string.start_netgame_dialog_message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
        builder.setView(editText);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
            public void onClick(DialogInterface dialog, int which) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
                editText.setText(playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
        });
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        editText.setOnEditorActionListener(new OnEditorActionListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
                boolean handled = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
                if(actionId == EditorInfo.IME_ACTION_DONE) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
                    startConnection(v.getText().toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
                    handled = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
                return handled;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
            public void onClick(DialogInterface dialog, int which) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
                startConnection(editText.getText().toString());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
        });
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        return builder.create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
    private void startConnection(String username) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
        if(username.length() > 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
            SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
            Editor edit = prefs.edit();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
            edit.putString(PREF_PLAYERNAME, username);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
            edit.commit();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
            Netplay.getAppInstance(getActivity().getApplicationContext()).connectToDefaultServer(username);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            getDialog().dismiss();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
            ((MainActivity)getActivity()).onNetConnectingStarted();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
    }
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
}