project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/PasswordDialog.java
author Wuzzy <almikes@aol.com>
Fri, 29 Sep 2017 02:10:37 +0200
changeset 12589 46a10d3fa619
parent 10017 de822cd3df3a
permissions -rw-r--r--
ASA, fruit02: Minor refactor

/*
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.hedgewars.hedgeroid;

import org.hedgewars.hedgeroid.netplay.Netplay;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;

/**
 * Shown when connecting to the server, and the server requests a password.
 */
public class PasswordDialog extends ConnectionDependendDialogFragment {
    String username;

    public PasswordDialog() {
    }

    public PasswordDialog(String username) {
        this.username = username;
    }

    @Override
    public void onSaveInstanceState(Bundle icicle) {
        super.onSaveInstanceState(icicle);
        icicle.putString("username", username);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if(savedInstanceState != null) {
            username = savedInstanceState.getString("username");
        }
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final EditText editText = new EditText(getActivity());
        final Netplay netplay = Netplay.getAppInstance(getActivity().getApplicationContext());

        editText.setHint(R.string.dialog_password_hint);
        editText.setId(android.R.id.text1);
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        builder.setView(editText);
        builder.setTitle(R.string.dialog_password_title);
        builder.setMessage(getString(R.string.dialog_password_message, username));
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String password = editText.getText().toString();
                editText.setText("");
                netplay.sendPassword(password);
            }
        });
        return builder.create();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        Netplay.getAppInstance(getActivity().getApplicationContext()).disconnect();
    }
}