/** * Copyright (c) 2013 LEAP Encryption Access Project and contributers * * 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 3 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, see . */ package se.leap.bitmaskclient.userstatus; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.TextView; import butterknife.ButterKnife; import butterknife.InjectView; import se.leap.bitmaskclient.Provider; import se.leap.bitmaskclient.R; import static android.view.View.VISIBLE; /** * Implements the log in dialog, currently without progress dialog. *

* It returns to the previous fragment when finished, and sends username and password to the authenticate method. *

* It also notifies the user if the password is not valid. * * @author parmegv */ public class SessionDialog extends DialogFragment { final public static String TAG = SessionDialog.class.getSimpleName(); final public static String USERNAME = "username"; final public static String PASSWORD = "password"; public enum ERRORS { USERNAME_MISSING, PASSWORD_INVALID_LENGTH, RISEUP_WARNING } @InjectView(R.id.user_message) TextView userMessage; @InjectView(R.id.username_entered) EditText usernameField; @InjectView(R.id.password_entered) EditText passwordField; public static SessionDialog getInstance(Provider provider, Bundle arguments) { SessionDialog dialog = new SessionDialog(); if (provider.getName().equalsIgnoreCase("riseup")) { arguments = arguments == Bundle.EMPTY ? new Bundle() : arguments; arguments.putBoolean(SessionDialog.ERRORS.RISEUP_WARNING.toString(), true); } if (arguments != null && !arguments.isEmpty()) { dialog.setArguments(arguments); } return dialog; } public AlertDialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.session_dialog, null); ButterKnife.inject(this, view); Bundle arguments = getArguments(); if (arguments != Bundle.EMPTY && arguments != null) { setUp(arguments); } builder.setView(view) .setPositiveButton(R.string.login_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String username = getEnteredUsername(); String password = getEnteredPassword(); dialog.dismiss(); interface_with_Dashboard.logIn(username, password); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }) .setNeutralButton(R.string.signup_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String username = getEnteredUsername(); String password = getEnteredPassword(); dialog.dismiss(); interface_with_Dashboard.signUp(username, password); } }); return builder.create(); } private void setUp(Bundle arguments) { if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) passwordField.setError(getString(R.string.error_not_valid_password_user_message)); else if (arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { userMessage.setVisibility(VISIBLE); userMessage.setText(R.string.login_riseup_warning); } if (arguments.containsKey(USERNAME)) { String username = arguments.getString(USERNAME); usernameField.setText(username); } if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { usernameField.setError(getString(R.string.username_ask)); } if (arguments.containsKey(getString(R.string.user_message))) { userMessage.setText(arguments.getString(getString(R.string.user_message))); userMessage.setVisibility(VISIBLE); } else if (userMessage.getVisibility() != VISIBLE) userMessage.setVisibility(View.GONE); if (!usernameField.getText().toString().isEmpty() && passwordField.isFocusable()) passwordField.requestFocus(); } private String getEnteredUsername() { return usernameField.getText().toString(); } private String getEnteredPassword() { return passwordField.getText().toString(); } /** * Interface used to communicate SessionDialog with Dashboard. * * @author parmegv */ public interface SessionDialogInterface { void logIn(String username, String password); void signUp(String username, String password); } SessionDialogInterface interface_with_Dashboard; @Override public void onAttach(Context context) { super.onAttach(context); try { interface_with_Dashboard = (SessionDialogInterface) ((AppCompatActivity) context).getSupportFragmentManager().getFragments().get(0); } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement LogInDialogListener"); } } }