From 237e51e09ea7ae13c4cdf7fd9247447f6d75fb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 30 Apr 2015 13:51:43 +0200 Subject: Works smoothly, correct texts. Next step: beautify. --- .../test/testDashboardIntegration.java | 32 +++- .../java/se/leap/bitmaskclient/ProviderAPI.java | 17 +- .../main/java/se/leap/bitmaskclient/Dashboard.java | 26 +-- .../bitmaskclient/userstatus/SessionDialog.java | 2 +- .../se/leap/bitmaskclient/userstatus/User.java | 5 +- .../userstatus/UserSessionFragment.java | 191 --------------------- .../userstatus/UserSessionStatus.java | 119 ------------- .../leap/bitmaskclient/userstatus/UserStatus.java | 119 +++++++++++++ .../userstatus/UserStatusFragment.java | 178 +++++++++++++++++++ app/src/main/res/layout/dashboard.xml | 2 +- app/src/main/res/layout/eip_service_fragment.xml | 1 - app/src/main/res/layout/fragment_user_session.xml | 63 ++++--- app/src/main/res/menu/client_dashboard.xml | 10 -- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 16 +- 16 files changed, 391 insertions(+), 394 deletions(-) delete mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionFragment.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserSessionStatus.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatus.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatusFragment.java 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 7551091b..a41a1acf 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java @@ -161,20 +161,25 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2. - */ -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/java/se/leap/bitmaskclient/userstatus/UserStatus.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatus.java new file mode 100644 index 00000000..edfed8d6 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatus.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 UserStatus extends Observable { + public static String TAG = UserStatus.class.getSimpleName(); + private static UserStatus 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.LOGGED_OUT; + + public static UserStatus getInstance(Resources resources) { + if (current_status == null) { + current_status = new UserStatus(resources); + } + return current_status; + } + + private UserStatus(Resources resources) { + UserStatus.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.LOGGED_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_username, ""); + 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/UserStatusFragment.java b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatusFragment.java new file mode 100644 index 00000000..0766dbd5 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/userstatus/UserStatusFragment.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 mbanje.kurt.fabbutton.FabButton; +import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.eip.EipStatus; + +public class UserStatusFragment extends Fragment implements Observer, SessionDialog.SessionDialogInterface { + + public static String TAG = UserStatusFragment.class.getSimpleName(); + private static Dashboard dashboard; + private ProviderAPIResultReceiver providerAPI_result_receiver; + + @InjectView(R.id.user_status_username) + TextView username; + @InjectView(R.id.user_status_icon) + FabButton icon; + @InjectView(R.id.user_status_button) + Button button; + + private UserStatus status; + private boolean allows_registration = false; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + status = UserStatus.getInstance(getResources()); + status.addObserver(this); + } + + @Override + public void onSaveInstanceState(@NotNull Bundle outState) { + if (username != null && username.getVisibility() == TextView.VISIBLE) + outState.putSerializable(UserStatus.TAG, status.sessionStatus()); + + super.onSaveInstanceState(outState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.fragment_user_session, container, false); + ButterKnife.inject(this, view); + + Bundle arguments = getArguments(); + allows_registration = arguments.getBoolean(Provider.ALLOW_REGISTRATION); + handleNewStatus(status); + + return view; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + Fragment fragment = (getFragmentManager().findFragmentById(R.id.user_status_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(), dashboard); + } + + public void restoreSessionStatus(Bundle savedInstanceState) { + if (savedInstanceState != null) + if (savedInstanceState.containsKey(UserStatus.TAG)) { + UserStatus.SessionStatus status = (UserStatus.SessionStatus) savedInstanceState.getSerializable(UserStatus.TAG); + this.status.updateStatus(status, getResources()); + } + } + + @OnClick(R.id.user_status_button) + public void handleButton() { + android.util.Log.d(TAG, status.toString()); + if(status.isLoggedIn()) + logOut(); + else if(status.isLoggedOut()) + dashboard.sessionDialog(Bundle.EMPTY); + else if(status.inProgress()) + cancelLoginOrSignup(); + } + + @Override + public void update(Observable observable, Object data) { + if (observable instanceof UserStatus) { + final UserStatus status = (UserStatus) observable; + dashboard.runOnUiThread(new Runnable() { + @Override + public void run() { + handleNewStatus(status); + } + }); + } + } + + private void handleNewStatus(UserStatus status) { + this.status = status; + if (allows_registration) { + if (this.status.inProgress()) + showUserSessionProgressBar(); + else + hideUserSessionProgressBar(); + changeMessage(); + updateButton(); + } + } + + private void showUserSessionProgressBar() { + icon.showProgress(true); + } + + private void hideUserSessionProgressBar() { + icon.showProgress(false); + } + + private void changeMessage() { + final String message = User.userName(); + username.setText(message); + } + + private void updateButton() { + if(status.isLoggedIn()) + button.setText(dashboard.getString(R.string.logout_button)); + else if(allows_registration) { + if (status.isLoggedOut()) + button.setText(dashboard.getString(R.string.login_button)); + else if (status.inProgress()) + button.setText(dashboard.getString(android.R.string.cancel)); + } + } + + + @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() { + android.util.Log.d(TAG, "Log out"); + 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/res/layout/dashboard.xml b/app/src/main/res/layout/dashboard.xml index 95445d63..6a9bbe97 100644 --- a/app/src/main/res/layout/dashboard.xml +++ b/app/src/main/res/layout/dashboard.xml @@ -20,7 +20,7 @@ android:textAppearance="?android:attr/textAppearanceMedium" /> diff --git a/app/src/main/res/layout/eip_service_fragment.xml b/app/src/main/res/layout/eip_service_fragment.xml index 1360b8e0..06b514d3 100644 --- a/app/src/main/res/layout/eip_service_fragment.xml +++ b/app/src/main/res/layout/eip_service_fragment.xml @@ -40,7 +40,6 @@ class="mbanje.kurt.fabbutton.FabButton" android:layout_gravity="center" android:visibility="visible" - android:layout_centerInParent="true" android:indeterminate="true" android:max="100" fbb_autoStart="true" diff --git a/app/src/main/res/layout/fragment_user_session.xml b/app/src/main/res/layout/fragment_user_session.xml index 0d2a1840..81e68199 100644 --- a/app/src/main/res/layout/fragment_user_session.xml +++ b/app/src/main/res/layout/fragment_user_session.xml @@ -1,36 +1,51 @@ - + android:layout_marginTop="10dp" + tools:context="se.leap.bitmaskclient.userstatus.UserStatusFragment"> + + + android:layout_height="wrap_content" + android:layout_below="@id/user.status.username" + android:layout_centerInParent="true"> - + /> - + - -