Search in sources :

Example 6 with ServerStatusCode

use of cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode in project androidApp by InspectorIncognito.

the class TranSappAccountManager method logOut.

public static void logOut(final AccountLogOutListener listener) {
    SocialAccountLoaderManager manager = new SocialAccountLoaderManager();
    manager.unloadAccount();
    if (account == null) {
        listener.onLoggedOut();
        return;
    }
    account.socialMediaAccount.logout();
    LogoutRequest request = new LogoutRequest(new Request.RequestListener<String>() {

        @Override
        public void onRequestError() {
            account = null;
            listener.onError(new ServerStatusCode(ServerStatusCode.SERVER_ERROR));
            listener.onLoggedOut();
        }

        @Override
        public void onRequestResult(@NonNull String result) {
            account = null;
            try {
                JSONObject data = new JSONObject(result);
                int status = data.getInt("status");
                if (status != ServerStatusCode.OK) {
                    listener.onError(new ServerStatusCode(status));
                }
            } catch (JSONException e) {
                e.printStackTrace();
                listener.onError(new ServerStatusCode(ServerStatusCode.PARSE_ERROR));
            }
            listener.onLoggedOut();
        }
    }, account);
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }
    ServerController.sendRequest(request, null);
}
Also used : JSONObject(org.json.JSONObject) ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode) Request(cl.smartcities.isci.transportinspector.serverConnection.requests.Request) LogoutRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LogoutRequest) LoginRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LoginRequest) JSONException(org.json.JSONException) LogoutRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LogoutRequest)

Example 7 with ServerStatusCode

use of cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode in project androidApp by InspectorIncognito.

the class TranSappAccountManager method loginWithTransapp.

public static void loginWithTransapp(final AccountLogInListener outerListener, final String nickname, final SocialMediaAccount socialMediaAccount) {
    LoginRequest request = new LoginRequest(new Request.RequestListener<String>() {

        @Override
        public void onRequestError() {
            socialMediaAccount.logout();
            outerListener.onError(new ServerStatusCode(ServerStatusCode.SERVER_ERROR));
        }

        @Override
        public void onRequestResult(@NonNull String result) {
            Log.d(TAG, "Server responded");
            handleLoginResult(result, outerListener, nickname, socialMediaAccount);
        }
    }, nickname, socialMediaAccount);
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }
    ServerController.sendRequest(request, null);
}
Also used : ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode) Request(cl.smartcities.isci.transportinspector.serverConnection.requests.Request) LogoutRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LogoutRequest) LoginRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LoginRequest) LoginRequest(cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LoginRequest)

Example 8 with ServerStatusCode

use of cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode in project androidApp by InspectorIncognito.

the class FacebookAccountLoader method load.

@Override
public void load(final CredentialsListener listener) {
    Log.d(TAG, "Loading account");
    boolean hasFacebookLoggedIn = TranSappApplication.getAppSharedPreferences().getBoolean(TranSappApplication.getAppContext().getString(R.string.facebook_log_in), false);
    if (!hasFacebookLoggedIn) {
        Log.d(TAG, "First time login");
        listener.onError(new ServerStatusCode(ServerStatusCode.LOGIN_API_ERROR));
        return;
    }
    if (AccessToken.getCurrentAccessToken() != null) {
        Log.d(TAG, "loadDataFromToken");
        dataManager.getCredentials(listener);
        return;
    }
    final Timer timer = new Timer();
    final AccessTokenTracker tracker = new AccessTokenTracker() {

        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
            timer.cancel();
            stopTracking();
            if (currentAccessToken == null) {
                Log.d(TAG, "Facebook logged out");
                listener.onError(new ServerStatusCode(ServerStatusCode.LOGIN_API_ERROR));
            } else {
                Log.d(TAG, "Facebook logged in");
                dataManager.getCredentials(listener);
            }
        }
    };
    Log.d(TAG, "Starting tracker");
    tracker.startTracking();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            tracker.stopTracking();
            Log.d(TAG, "Time up");
            listener.onError(new ServerStatusCode(ServerStatusCode.LOGIN_API_ERROR));
        }
    }, 5000);
}
Also used : AccessTokenTracker(com.facebook.AccessTokenTracker) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AccessToken(com.facebook.AccessToken) ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode)

Example 9 with ServerStatusCode

use of cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode in project androidApp by InspectorIncognito.

the class NicknameFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_account_nickname_fragment, container, false);
    AvatarView userPicture = view.findViewById(R.id.user_image);
    final SocialMediaAccount account = SocialMediaAccount.getCurrentAccount();
    if (account == null) {
        getActivity().finish();
        return view;
    }
    account.loadImage(userPicture);
    View submitButton = view.findViewById(R.id.submit_button);
    final EditText nicknameEditText = view.findViewById(R.id.nickname_edit_text);
    submitButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final String nickname = nicknameEditText.getText().toString();
            if (nickname.length() == 0) {
                Toast.makeText(getContext(), "Debes ingresar un nickname", Toast.LENGTH_SHORT).show();
            } else if (nickname.length() > 30) {
                Toast.makeText(getContext(), "Ingresa un nick con menos de 30 caracteres", Toast.LENGTH_SHORT).show();
            } else {
                final ProgressDialog dialog = new ProgressDialog(getActivity());
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setMessage("Iniciando Sesión");
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                TranSappAccountManager.loginWithTransapp(new AccountLogInListener() {

                    @Override
                    public void onAccountReady(TranSappAccount account) {
                        loginCallback.onTranSappLogin();
                        dialog.dismiss();
                    }

                    @Override
                    public void onError(ServerStatusCode error) {
                        dialog.dismiss();
                        if (error.getErrorCode() == ServerStatusCode.SERVER_ERROR || error.getErrorCode() == ServerStatusCode.INTERNAL_SERVER_ERROR || error.getErrorCode() == ServerStatusCode.PARSE_ERROR || error.getErrorCode() == ServerStatusCode.INVALID_SESSION_TOKEN) {
                            Toast.makeText(TranSappApplication.getAppContext(), "No se pudo iniciar sesión con TranSapp", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, nickname, account);
            }
        }
    });
    return view;
}
Also used : EditText(android.widget.EditText) SocialMediaAccount(cl.smartcities.isci.transportinspector.gamification.socialAccount.SocialMediaAccount) TranSappAccount(cl.smartcities.isci.transportinspector.gamification.transappUser.TranSappAccount) AvatarView(agency.tango.android.avatarview.views.AvatarView) ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode) ProgressDialog(android.app.ProgressDialog) AvatarView(agency.tango.android.avatarview.views.AvatarView) View(android.view.View) AccountLogInListener(cl.smartcities.isci.transportinspector.gamification.ui.listener.AccountLogInListener)

Example 10 with ServerStatusCode

use of cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode in project androidApp by InspectorIncognito.

the class UserFragment method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (super.onOptionsItemSelected(item)) {
        return true;
    }
    switch(item.getItemId()) {
        case R.id.action_settings:
            startActivity(new Intent(this.getActivity(), AccountSettingsActivity.class));
            return true;
        case R.id.action_close_session:
            CloseSessionDialog dialog = new CloseSessionDialog();
            dialog.setListener(new CloseSessionDialog.OnButtonClickListener() {

                @Override
                public void onPositiveButtonClick() {
                    TranSappAccountManager.logOut(new AccountLogOutListener() {

                        @Override
                        public void onLoggedOut() {
                            callback.onLogout();
                        }

                        @Override
                        public void onError(ServerStatusCode error) {
                            // TODO(aantoine): show error message
                            callback.onLogout();
                        }
                    });
                }

                @Override
                public void onNegativeButtonClick() {
                }
            });
            dialog.show(getFragmentManager(), dialog.getClass().getSimpleName());
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Also used : AccountLogOutListener(cl.smartcities.isci.transportinspector.gamification.ui.listener.AccountLogOutListener) CloseSessionDialog(cl.smartcities.isci.transportinspector.dialogs.CloseSessionDialog) ServerStatusCode(cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode) Intent(android.content.Intent) AccountSettingsActivity(cl.smartcities.isci.transportinspector.gamification.ui.settings.AccountSettingsActivity)

Aggregations

ServerStatusCode (cl.smartcities.isci.transportinspector.serverConnection.ServerStatusCode)13 SocialMediaAccount (cl.smartcities.isci.transportinspector.gamification.socialAccount.SocialMediaAccount)4 Intent (android.content.Intent)3 View (android.view.View)3 CredentialsListener (cl.smartcities.isci.transportinspector.gamification.socialAccount.CredentialsListener)3 AccessToken (com.facebook.AccessToken)3 Bundle (android.os.Bundle)2 TextView (android.widget.TextView)2 CloseSessionDialog (cl.smartcities.isci.transportinspector.dialogs.CloseSessionDialog)2 AccountLogOutListener (cl.smartcities.isci.transportinspector.gamification.ui.listener.AccountLogOutListener)2 Request (cl.smartcities.isci.transportinspector.serverConnection.requests.Request)2 LoginRequest (cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LoginRequest)2 LogoutRequest (cl.smartcities.isci.transportinspector.serverConnection.requests.pullRequests.LogoutRequest)2 AccessTokenTracker (com.facebook.AccessTokenTracker)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AvatarView (agency.tango.android.avatarview.views.AvatarView)1 ProgressDialog (android.app.ProgressDialog)1 NonNull (android.support.annotation.NonNull)1 EditText (android.widget.EditText)1