From f008b5ec8e1c74968e4a605d2de5423edf91b854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 29 Apr 2015 12:25:24 +0200 Subject: Creating a user session fragment. I've separated the user session management to it, and encapsulated ProviderAPICommand into its own class. Putting the fragment statically in dashboard.xml isn't working, Android complains about it being duplicated, so I'm going to add it dynamically. --- .../test/testDashboardIntegration.java | 32 ++-- .../se/leap/bitmaskclient/ConfigurationWizard.java | 4 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 3 + .../main/java/se/leap/bitmaskclient/Dashboard.java | 157 +++--------------- .../java/se/leap/bitmaskclient/EipFragment.java | 13 +- .../java/se/leap/bitmaskclient/LeapSRPSession.java | 2 +- .../main/java/se/leap/bitmaskclient/Provider.java | 2 +- .../se/leap/bitmaskclient/ProviderAPICommand.java | 45 +++++ .../java/se/leap/bitmaskclient/SessionDialog.java | 169 ------------------- app/src/main/java/se/leap/bitmaskclient/User.java | 44 ----- .../se/leap/bitmaskclient/UserSessionStatus.java | 109 ------------ .../bitmaskclient/userstatus/SessionDialog.java | 182 +++++++++++++++++++++ .../se/leap/bitmaskclient/userstatus/User.java | 45 +++++ .../userstatus/UserSessionFragment.java | 178 ++++++++++++++++++++ .../userstatus/UserSessionStatus.java | 119 ++++++++++++++ app/src/main/res/layout/dashboard.xml | 28 +--- app/src/main/res/layout/fragment_user_session.xml | 37 +++++ app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- .../se/leap/bitmaskclient/ConfigurationWizard.java | 1 + .../java/se/leap/bitmaskclient/ProviderAPI.java | 4 +- 21 files changed, 665 insertions(+), 513 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/SessionDialog.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/User.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/SessionDialog.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/User.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionFragment.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionStatus.java create mode 100644 app/src/main/res/layout/fragment_user_session.xml (limited to 'app/src') diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java index 13ca4431..262310bd 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java @@ -97,7 +97,7 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2 * @author parmegv */ -public class Dashboard extends Activity implements SessionDialog.SessionDialogInterface, ProviderAPIResultReceiver.Receiver, Observer { +public class Dashboard extends Activity implements ProviderAPIResultReceiver.Receiver { protected static final int CONFIGURE_LEAP = 0; protected static final int SWITCH_PROVIDER = 1; @@ -62,14 +61,10 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @InjectView(R.id.providerName) TextView provider_name; - @InjectView(R.id.user_session_status) - TextView user_session_status_text_view; - @InjectView(R.id.user_session_status_progress) - ProgressBar user_session_status_progress_bar; EipFragment eip_fragment; - private Provider provider = new Provider(); - private UserSessionStatus user_session_status; + UserSessionFragment user_session_fragment; + private static Provider provider = new Provider(); public ProviderAPIResultReceiver providerAPI_result_receiver; private boolean switching_provider; @@ -78,8 +73,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn super.onCreate(savedInstanceState); app = this; - user_session_status = UserSessionStatus.getInstance(getResources()); - user_session_status.addObserver(this); PRNGFixes.apply(); @@ -88,12 +81,16 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn handleVersion(); User.init(); + ProviderAPICommand.initialize(this); + providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); + providerAPI_result_receiver.setReceiver(this); + restoreProvider(savedInstanceState); if (!provider.isConfigured()) startActivityForResult(new Intent(this, ConfigurationWizard.class), CONFIGURE_LEAP); else { buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); - restoreSessionStatus(savedInstanceState); + user_session_fragment.restoreSessionStatus(savedInstanceState); } } @@ -106,20 +103,9 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn provider = getSavedProviderFromSharedPreferences(); } - private void restoreSessionStatus(Bundle savedInstanceState) { - if (savedInstanceState != null) - if (savedInstanceState.containsKey(UserSessionStatus.TAG)) { - UserSessionStatus.SessionStatus status = (UserSessionStatus.SessionStatus) savedInstanceState.getSerializable(UserSessionStatus.TAG); - user_session_status.updateStatus(status, getResources()); - } - } - @Override protected void onSaveInstanceState(@NotNull Bundle outState) { outState.putParcelable(Provider.KEY, provider); - if (user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) - outState.putSerializable(UserSessionStatus.TAG, user_session_status.sessionStatus()); - super.onSaveInstanceState(outState); } @@ -166,7 +152,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn sessionDialog(Bundle.EMPTY); } - } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { + } else if (resultCode == RESULT_CANCELED && data != null && data.hasExtra(ACTION_QUIT)) { finish(); } else configErrorDialog(); @@ -213,6 +199,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn ButterKnife.inject(this); provider_name.setText(provider.getDomain()); + user_session_fragment = new UserSessionFragment(); + if (provider.hasEIP()) { fragment_manager.removePreviousFragment(EipFragment.TAG); eip_fragment = new EipFragment(); @@ -229,7 +217,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn onBackPressed(); } } - handleNewUserSessionStatus(user_session_status); } @Override @@ -261,14 +248,14 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn return true; case R.id.switch_provider: switching_provider = true; - if (LeapSRPSession.loggedIn()) logOut(); + if (User.loggedIn()) user_session_fragment.logOut(); else switchProvider(); return true; case R.id.login_button: sessionDialog(Bundle.EMPTY); return true; case R.id.logout_button: - logOut(); + user_session_fragment.logOut(); return true; case R.id.signup_button: sessionDialog(Bundle.EMPTY); @@ -288,122 +275,18 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn log_window_wrapper.showLog(); } - @Override - public void signUp(String username, String password) { - User.setUserName(username); - Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, 0, ProviderAPI.SIGN_UP); - } - - @Override - public void logIn(String username, String password) { - User.setUserName(username); - Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, 0, ProviderAPI.LOG_IN); - } - - public void logOut() { - providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.LOG_OUT); - } - - @Override - public void update(Observable observable, Object data) { - if (observable instanceof UserSessionStatus) { - UserSessionStatus status = (UserSessionStatus) observable; - handleNewUserSessionStatus(status); - } - } - - private void handleNewUserSessionStatus(UserSessionStatus status) { - user_session_status = status; - if (provider.allowsRegistration()) { - if (user_session_status.inProgress()) - showUserSessionProgressBar(); - else - hideUserSessionProgressBar(); - changeSessionStatusMessage(user_session_status.toString()); - invalidateOptionsMenuOnUiThread(); - } - } - - private void changeSessionStatusMessage(final String message) { - runOnUiThread(new Runnable() { - @Override - public void run() { - user_session_status_text_view.setText(message); - } - }); - } - - private void showUserSessionProgressBar() { - runOnUiThread(new Runnable() { - @Override - public void run() { - user_session_status_progress_bar.setVisibility(ProgressBar.VISIBLE); - } - }); - } - - private void hideUserSessionProgressBar() { - runOnUiThread(new Runnable() { - @Override - public void run() { - user_session_status_progress_bar.setVisibility(ProgressBar.GONE); - } - }); - } - - protected void downloadVpnCertificate() { - boolean is_authenticated = LeapSRPSession.loggedIn(); + public void downloadVpnCertificate() { + boolean is_authenticated = User.loggedIn(); boolean allowed_anon = preferences.getBoolean(Constants.ALLOWED_ANON, false); if (allowed_anon || is_authenticated) - providerApiCommand(Bundle.EMPTY, R.string.downloading_certificate_message, ProviderAPI.DOWNLOAD_CERTIFICATE); + ProviderAPICommand.execute(Bundle.EMPTY, ProviderAPI.DOWNLOAD_CERTIFICATE, providerAPI_result_receiver); else sessionDialog(Bundle.EMPTY); - - } - - private Bundle bundlePassword(String password) { - Bundle parameters = new Bundle(); - if (!password.isEmpty()) - parameters.putString(SessionDialog.PASSWORD, password); - return parameters; - } - - protected void providerApiCommand(Bundle parameters, int progressbar_message_resId, String providerApi_action) { - Intent command = prepareProviderAPICommand(parameters, providerApi_action); - startService(command); - } - - private Intent prepareProviderAPICommand(Bundle parameters, String action) { - providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); - providerAPI_result_receiver.setReceiver(this); - - Intent command = new Intent(this, ProviderAPI.class); - - command.putExtra(ProviderAPI.PARAMETERS, parameters); - command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - command.setAction(action); - return command; - } - - public void cancelLoginOrSignup() { - EipStatus.getInstance().setConnectedOrDisconnected(); } public void sessionDialog(Bundle resultData) { - FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); - - DialogFragment newFragment = new SessionDialog(); - if (provider.getName().equalsIgnoreCase("riseup")) { - resultData = resultData == Bundle.EMPTY ? new Bundle() : resultData; - resultData.putBoolean(SessionDialog.ERRORS.RISEUP_WARNING.toString(), true); - } - if (resultData != null && !resultData.isEmpty()) { - newFragment.setArguments(resultData); - } - newFragment.show(transaction, SessionDialog.TAG); + SessionDialog.getInstance(provider, resultData).show(transaction, SessionDialog.TAG); } private void switchProvider() { @@ -419,7 +302,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn if (resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { String username = resultData.getString(SessionDialog.USERNAME); String password = resultData.getString(SessionDialog.PASSWORD); - logIn(username, password); + user_session_fragment.logIn(username, password); } else if (resultCode == ProviderAPI.FAILED_SIGNUP) { sessionDialog(resultData); } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { @@ -447,6 +330,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn return app; } + public static Provider getProvider() { return provider; } + @Override public void startActivityForResult(Intent intent, int requestCode) { intent.putExtra(Dashboard.REQUEST_CODE, requestCode); diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 30147f65..4d1eb4bf 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -35,7 +35,7 @@ public class EipFragment extends Fragment implements Observer { public static String TAG = EipFragment.class.getSimpleName(); - protected static final String IS_PENDING = TAG + ".is_pending"; + public static final String IS_PENDING = TAG + ".is_pending"; protected static final String IS_CONNECTED = TAG + ".is_connected"; public static final String START_ON_BOOT = "start on boot"; @@ -45,7 +45,7 @@ public class EipFragment extends Fragment implements Observer { Button main_button; private static Dashboard dashboard; - private static EIPReceiver mEIPReceiver; + private static EIPReceiver provider_api_receiver; private static EipStatus eip_status; private boolean wants_to_connect; @@ -53,7 +53,8 @@ public class EipFragment extends Fragment implements Observer { super.onAttach(activity); dashboard = (Dashboard) activity; - dashboard.providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.DOWNLOAD_EIP_SERVICE); + if(provider_api_receiver != null) + ProviderAPICommand.execute(Bundle.EMPTY, ProviderAPI.DOWNLOAD_EIP_SERVICE, provider_api_receiver); } @Override @@ -61,7 +62,7 @@ public class EipFragment extends Fragment implements Observer { super.onCreate(savedInstanceState); eip_status = EipStatus.getInstance(); eip_status.addObserver(this); - mEIPReceiver = new EIPReceiver(new Handler()); + provider_api_receiver = new EIPReceiver(new Handler()); } @Override @@ -219,7 +220,7 @@ public class EipFragment extends Fragment implements Observer { // TODO validate "action"...how do we get the list of intent-filters for a class via Android API? Intent vpn_intent = new Intent(dashboard.getApplicationContext(), EIP.class); vpn_intent.setAction(action); - vpn_intent.putExtra(Constants.RECEIVER_TAG, mEIPReceiver); + vpn_intent.putExtra(Constants.RECEIVER_TAG, provider_api_receiver); dashboard.startService(vpn_intent); } @@ -335,6 +336,6 @@ public class EipFragment extends Fragment implements Observer { public static EIPReceiver getReceiver() { - return mEIPReceiver; + return provider_api_receiver; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java index 1ced6d60..49cf3774 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java +++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java @@ -337,7 +337,7 @@ public class LeapSRPSession { return token; } - protected static boolean loggedIn() { + public static boolean loggedIn() { return !token.isEmpty(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index a66d53e7..559b47d1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -118,7 +118,7 @@ public final class Provider implements Parcelable { protected String certificatePin() { return certificate_pin; } - protected String getName() { + public String getName() { // Should we pass the locale in, or query the system here? String lang = Locale.getDefault().getLanguage(); String name = ""; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java b/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java new file mode 100644 index 00000000..0e4cfe8a --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java @@ -0,0 +1,45 @@ +package se.leap.bitmaskclient; + +import android.content.*; +import android.os.*; + +import org.jetbrains.annotations.*; + +public class ProviderAPICommand { + private static Context context; + + private static String action; + private static Bundle parameters; + private static ResultReceiver result_receiver; + + public static void initialize(Context context) { + ProviderAPICommand.context = context; + } + + private static boolean isInitialized() { + return context != null; + } + + public static void execute(Bundle parameters, @NotNull String action, @NotNull ResultReceiver result_receiver) throws IllegalStateException { + if(!isInitialized()) throw new IllegalStateException(); + + ProviderAPICommand.action = action; + ProviderAPICommand.parameters = parameters; + ProviderAPICommand.result_receiver = result_receiver; + + Intent intent = setUpIntent(); + context.startService(intent); + } + + private static Intent setUpIntent() { + Intent command = new Intent(context, ProviderAPI.class); + + command.setAction(action); + command.putExtra(ProviderAPI.PARAMETERS, parameters); + command.putExtra(ProviderAPI.RECEIVER_KEY, result_receiver); + + return command; + } + + +} diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java deleted file mode 100644 index 30344ff2..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ /dev/null @@ -1,169 +0,0 @@ -/** - * 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; - -import android.app.*; -import android.content.*; -import android.os.*; -import android.view.*; -import android.widget.*; - -import butterknife.*; - -/** - * 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 static enum ERRORS { - USERNAME_MISSING, - PASSWORD_INVALID_LENGTH, - RISEUP_WARNING - } - - @InjectView(R.id.user_message) - TextView user_message; - @InjectView(R.id.username_entered) - EditText username_field; - @InjectView(R.id.password_entered) - EditText password_field; - - private static boolean is_eip_pending = false; - - public SessionDialog() { - setArguments(Bundle.EMPTY); - } - - 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) { - 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(); - interface_with_Dashboard.cancelLoginOrSignup(); - } - }) - .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) { - is_eip_pending = arguments.getBoolean(EipFragment.IS_PENDING, false); - if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) - password_field.setError(getString(R.string.error_not_valid_password_user_message)); - else if (arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { - user_message.setVisibility(TextView.VISIBLE); - user_message.setText(R.string.login_riseup_warning); - } - if (arguments.containsKey(USERNAME)) { - String username = arguments.getString(USERNAME); - username_field.setText(username); - } - if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { - username_field.setError(getString(R.string.username_ask)); - } - if (arguments.containsKey(getString(R.string.user_message))) { - user_message.setText(arguments.getString(getString(R.string.user_message))); - user_message.setVisibility(View.VISIBLE); - } else if (user_message.getVisibility() != TextView.VISIBLE) - user_message.setVisibility(View.GONE); - - if (!username_field.getText().toString().isEmpty() && password_field.isFocusable()) - password_field.requestFocus(); - - } - - private String getEnteredUsername() { - return username_field.getText().toString(); - } - - private String getEnteredPassword() { - return password_field.getText().toString(); - } - - - /** - * Interface used to communicate SessionDialog with Dashboard. - * - * @author parmegv - */ - public interface SessionDialogInterface { - public void logIn(String username, String password); - - public void signUp(String username, String password); - - public void cancelLoginOrSignup(); - } - - SessionDialogInterface interface_with_Dashboard; - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_Dashboard = (SessionDialogInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement LogInDialogListener"); - } - } - - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - if (is_eip_pending) - interface_with_Dashboard.cancelLoginOrSignup(); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/User.java b/app/src/main/java/se/leap/bitmaskclient/User.java deleted file mode 100644 index f3d3b8b1..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/User.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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; - -public class User { - private static String user_name = ""; - private static User user; - public static int DEFAULT_CONJUGATION_PERSON = 1; - - public static User init() { - if (user == null) { - user = new User(); - } - return user; - } - - public static void setUserName(String user_name) { - User.user_name = user_name; - } - - private User() { } - - public static String userName() { - return user_name; - } - - public static boolean loggedIn() { - return LeapSRPSession.loggedIn(); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java deleted file mode 100644 index 28c7ecc7..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * 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; - -import android.content.res.*; - -import java.util.*; - -public class UserSessionStatus extends Observable { - public static String TAG = UserSessionStatus.class.getSimpleName(); - private static UserSessionStatus current_status; - private static Resources resources; - - public enum SessionStatus { - LOGGED_IN, - LOGGED_OUT, - NOT_LOGGED_IN, - DIDNT_LOG_OUT, - LOGGING_IN, - LOGGING_OUT, - SIGNING_UP; - - @Override - public String toString() { - int id = 0; - if(this == SessionStatus.LOGGED_IN) - id = R.string.logged_in_user_status; - else if(this == SessionStatus.LOGGED_OUT) - id = R.string.logged_out_user_status; - else if(this == SessionStatus.NOT_LOGGED_IN) - id = R.string.not_logged_in_user_status; - else if(this == SessionStatus.DIDNT_LOG_OUT) - id = R.string.didnt_log_out_user_status; - else if(this == SessionStatus.LOGGING_IN) - id = R.string.logging_in_user_status; - else if(this == SessionStatus.LOGGING_OUT) - id = R.string.logging_out_user_status; - else if(this == SessionStatus.SIGNING_UP) - id = R.string.signingup_message; - - return resources.getString(id); - } - } - - private static SessionStatus session_status = SessionStatus.NOT_LOGGED_IN; - - public static UserSessionStatus getInstance(Resources resources) { - if (current_status == null) { - current_status = new UserSessionStatus(resources); - } - return current_status; - } - - private UserSessionStatus(Resources resources) { - UserSessionStatus.resources = resources; - } - - private void sessionStatus(SessionStatus session_status) { - this.session_status = session_status; - } - - public SessionStatus sessionStatus() { - return session_status; - } - - public boolean inProgress() { - return session_status == SessionStatus.LOGGING_IN - || session_status == SessionStatus.LOGGING_OUT; - } - - public static void updateStatus(SessionStatus session_status, Resources resources) { - current_status = getInstance(resources); - current_status.sessionStatus(session_status); - current_status.setChanged(); - current_status.notifyObservers(); - } - - @Override - public String toString() { - String user_session_status = User.userName(); - - String default_username = resources.getString(R.string.default_user, ""); - if(user_session_status.isEmpty() && !default_username.equalsIgnoreCase("null")) user_session_status = default_username; - user_session_status += " " + session_status.toString(); - - user_session_status = user_session_status.trim(); - if(User.userName().isEmpty()) - user_session_status = capitalize(user_session_status); - return user_session_status; - } - - private String capitalize(String to_be_capitalized) { - return to_be_capitalized.substring(0,1).toUpperCase() + to_be_capitalized.substring(1); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/userstatus/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/SessionDialog.java new file mode 100644 index 00000000..f51a6779 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/SessionDialog.java @@ -0,0 +1,182 @@ +/** + * 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.*; +import android.content.*; +import android.os.*; +import android.view.*; +import android.widget.*; + +import butterknife.*; +import se.leap.bitmaskclient.EipFragment; +import se.leap.bitmaskclient.Provider; +import se.leap.bitmaskclient.R; + +/** + * 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 static enum ERRORS { + USERNAME_MISSING, + PASSWORD_INVALID_LENGTH, + RISEUP_WARNING + } + + @InjectView(R.id.user_message) + TextView user_message; + @InjectView(R.id.username_entered) + EditText username_field; + @InjectView(R.id.password_entered) + EditText password_field; + + private static boolean is_eip_pending = false; + + 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) { + 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(); + interface_with_Dashboard.cancelLoginOrSignup(); + } + }) + .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) { + is_eip_pending = arguments.getBoolean(EipFragment.IS_PENDING, false); + if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) + password_field.setError(getString(R.string.error_not_valid_password_user_message)); + else if (arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { + user_message.setVisibility(TextView.VISIBLE); + user_message.setText(R.string.login_riseup_warning); + } + if (arguments.containsKey(USERNAME)) { + String username = arguments.getString(USERNAME); + username_field.setText(username); + } + if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { + username_field.setError(getString(R.string.username_ask)); + } + if (arguments.containsKey(getString(R.string.user_message))) { + user_message.setText(arguments.getString(getString(R.string.user_message))); + user_message.setVisibility(View.VISIBLE); + } else if (user_message.getVisibility() != TextView.VISIBLE) + user_message.setVisibility(View.GONE); + + if (!username_field.getText().toString().isEmpty() && password_field.isFocusable()) + password_field.requestFocus(); + + } + + private String getEnteredUsername() { + return username_field.getText().toString(); + } + + private String getEnteredPassword() { + return password_field.getText().toString(); + } + + + /** + * Interface used to communicate SessionDialog with Dashboard. + * + * @author parmegv + */ + public interface SessionDialogInterface { + public void logIn(String username, String password); + + public void signUp(String username, String password); + + public void cancelLoginOrSignup(); + } + + SessionDialogInterface interface_with_Dashboard; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_Dashboard = (SessionDialogInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement LogInDialogListener"); + } + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if (is_eip_pending) + interface_with_Dashboard.cancelLoginOrSignup(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/userstatus/User.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/User.java new file mode 100644 index 00000000..716e2ed6 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/User.java @@ -0,0 +1,45 @@ +/** + * 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 se.leap.bitmaskclient.LeapSRPSession; + +public class User { + private static String user_name = ""; + private static User user; + + public static User init() { + if (user == null) { + user = new User(); + } + return user; + } + + public static void setUserName(String user_name) { + User.user_name = user_name; + } + + private User() { } + + public static String userName() { + return user_name; + } + + public static boolean loggedIn() { + return LeapSRPSession.loggedIn(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionFragment.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionFragment.java new file mode 100644 index 00000000..02b9fbeb --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionFragment.java @@ -0,0 +1,178 @@ +package se.leap.bitmaskclient.userstatus; + +import android.app.*; +import android.os.*; +import android.view.*; +import android.widget.*; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; + +import butterknife.*; +import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.eip.EipStatus; + +public class UserSessionFragment extends Fragment implements Observer, SessionDialog.SessionDialogInterface { + + private static View view; + + public static String TAG = UserSessionFragment.class.getSimpleName(); + private static Dashboard dashboard; + private ProviderAPIResultReceiver providerAPI_result_receiver; + + @InjectView(R.id.user_session_status) + TextView user_session_status_text_view; + @InjectView(R.id.user_session_status_progress) + ProgressBar user_session_status_progress_bar; + @InjectView(R.id.user_session_button) + Button main_button; + + private UserSessionStatus user_session_status; + private boolean allows_registration = false; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + user_session_status = UserSessionStatus.getInstance(getResources()); + user_session_status.addObserver(this); + + handleNewUserSessionStatus(user_session_status); + } + + @Override + public void onSaveInstanceState(@NotNull Bundle outState) { + if (user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) + outState.putSerializable(UserSessionStatus.TAG, user_session_status.sessionStatus()); + + super.onSaveInstanceState(outState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_user_session, container, false); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + Fragment fragment = (getFragmentManager().findFragmentById(R.id.user_session_fragment)); + FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction(); + ft.remove(fragment); + ft.commit(); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + dashboard = (Dashboard) activity; + + providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); + providerAPI_result_receiver.setReceiver(dashboard); + } + + public void restoreSessionStatus(Bundle savedInstanceState) { + if (savedInstanceState != null) + if (savedInstanceState.containsKey(UserSessionStatus.TAG)) { + UserSessionStatus.SessionStatus status = (UserSessionStatus.SessionStatus) savedInstanceState.getSerializable(UserSessionStatus.TAG); + user_session_status.updateStatus(status, getResources()); + } + } + + @OnClick(R.id.user_session_button) + public void handleMainButton() { + if(user_session_status.isLoggedIn()) + logOut(); + else if(user_session_status.isLoggedOut()) + dashboard.sessionDialog(Bundle.EMPTY); + else if(user_session_status.inProgress()) + cancelLoginOrSignup(); + } + + @Override + public void update(Observable observable, Object data) { + if (observable instanceof UserSessionStatus) { + UserSessionStatus status = (UserSessionStatus) observable; + handleNewUserSessionStatus(status); + } + } + + private void handleNewUserSessionStatus(UserSessionStatus status) { + user_session_status = status; + if (allows_registration) { + if (user_session_status.inProgress()) + showUserSessionProgressBar(); + else + hideUserSessionProgressBar(); + changeSessionStatusMessage(); + updateButton(); + } + } + + private void showUserSessionProgressBar() { + dashboard.runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_progress_bar.setVisibility(ProgressBar.VISIBLE); + } + }); + } + + private void hideUserSessionProgressBar() { + dashboard.runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_progress_bar.setVisibility(ProgressBar.GONE); + } + }); + } + + private void changeSessionStatusMessage() { + final String message = user_session_status.toString(); + dashboard.runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_text_view.setText(message); + } + }); + } + + private void updateButton() { + if(User.loggedIn()) + main_button.setText(getString(R.string.logout_button)); + else if(allows_registration) + main_button.setText(getString(R.string.login_button)); + } + + + @Override + public void signUp(String username, String password) { + User.setUserName(username); + Bundle parameters = bundlePassword(password); + ProviderAPICommand.execute(parameters, ProviderAPI.SIGN_UP, providerAPI_result_receiver); + } + + @Override + public void logIn(String username, String password) { + User.setUserName(username); + Bundle parameters = bundlePassword(password); + ProviderAPICommand.execute(parameters, ProviderAPI.LOG_IN, providerAPI_result_receiver); + } + + public void logOut() { + ProviderAPICommand.execute(Bundle.EMPTY, ProviderAPI.LOG_OUT, providerAPI_result_receiver); + } + + public void cancelLoginOrSignup() { + EipStatus.getInstance().setConnectedOrDisconnected(); + } + + private Bundle bundlePassword(String password) { + Bundle parameters = new Bundle(); + if (!password.isEmpty()) + parameters.putString(SessionDialog.PASSWORD, password); + return parameters; + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionStatus.java new file mode 100644 index 00000000..dec0e719 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionStatus.java @@ -0,0 +1,119 @@ +/** + * 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.content.res.*; + +import java.util.*; + +import se.leap.bitmaskclient.R; + +public class UserSessionStatus extends Observable { + public static String TAG = UserSessionStatus.class.getSimpleName(); + private static UserSessionStatus current_status; + private static Resources resources; + + public enum SessionStatus { + LOGGED_IN, + LOGGED_OUT, + NOT_LOGGED_IN, + DIDNT_LOG_OUT, + LOGGING_IN, + LOGGING_OUT, + SIGNING_UP; + + @Override + public String toString() { + int id = 0; + if(this == SessionStatus.LOGGED_IN) + id = R.string.logged_in_user_status; + else if(this == SessionStatus.LOGGED_OUT) + id = R.string.logged_out_user_status; + else if(this == SessionStatus.NOT_LOGGED_IN) + id = R.string.not_logged_in_user_status; + else if(this == SessionStatus.DIDNT_LOG_OUT) + id = R.string.didnt_log_out_user_status; + else if(this == SessionStatus.LOGGING_IN) + id = R.string.logging_in_user_status; + else if(this == SessionStatus.LOGGING_OUT) + id = R.string.logging_out_user_status; + else if(this == SessionStatus.SIGNING_UP) + id = R.string.signingup_message; + + return resources.getString(id); + } + } + + private static SessionStatus session_status = SessionStatus.NOT_LOGGED_IN; + + public static UserSessionStatus getInstance(Resources resources) { + if (current_status == null) { + current_status = new UserSessionStatus(resources); + } + return current_status; + } + + private UserSessionStatus(Resources resources) { + UserSessionStatus.resources = resources; + } + + private void sessionStatus(SessionStatus session_status) { + this.session_status = session_status; + } + + public SessionStatus sessionStatus() { + return session_status; + } + + public boolean inProgress() { + return session_status == SessionStatus.LOGGING_IN + || session_status == SessionStatus.LOGGING_OUT; + } + + public boolean isLoggedIn() { + return session_status == SessionStatus.LOGGING_IN; + } + + public boolean isLoggedOut() { + return session_status == SessionStatus.LOGGED_OUT; + } + + public static void updateStatus(SessionStatus session_status, Resources resources) { + current_status = getInstance(resources); + current_status.sessionStatus(session_status); + current_status.setChanged(); + current_status.notifyObservers(); + } + + @Override + public String toString() { + String user_session_status = User.userName(); + + String default_username = resources.getString(R.string.default_user, ""); + if(user_session_status.isEmpty() && !default_username.equalsIgnoreCase("null")) user_session_status = default_username; + user_session_status += " " + session_status.toString(); + + user_session_status = user_session_status.trim(); + if(User.userName().isEmpty()) + user_session_status = capitalize(user_session_status); + return user_session_status; + } + + private String capitalize(String to_be_capitalized) { + return to_be_capitalized.substring(0,1).toUpperCase() + to_be_capitalized.substring(1); + } +} diff --git a/app/src/main/res/layout/dashboard.xml b/app/src/main/res/layout/dashboard.xml index d76ccbec..30074b6a 100644 --- a/app/src/main/res/layout/dashboard.xml +++ b/app/src/main/res/layout/dashboard.xml @@ -18,29 +18,13 @@ android:singleLine="true" android:text="@string/provider_label_none" android:textAppearance="?android:attr/textAppearanceMedium" /> - - - - - + + + + + + + + + + + +