private void loginApiSuccess(Object obj) { if (obj instanceof User) { final User user = (User) obj; if (user.getCode() == 200) { if (DemoContext.getInstance() != null && user.getResult() != null) { SharedPreferences.Editor edit = DemoContext.getInstance().getSharedPreferences().edit(); edit.putString("DEMO_USER_ID", user.getResult().getId()); edit.putString("DEMO_USER_NAME", user.getResult().getUsername()); edit.putString("DEMO_USER_PORTRAIT", user.getResult().getPortrait()); edit.apply(); Log.e(TAG, "-------login success------"); httpLoginSuccess(user); } } else if (user.getCode() == 103) { if (mDialog != null) mDialog.dismiss(); WinToast.toast(LoginActivity.this, "密码错误"); } else if (user.getCode() == 104) { if (mDialog != null) mDialog.dismiss(); WinToast.toast(LoginActivity.this, "账号错误"); } } }
@Override public boolean handleMessage(Message msg) { if (msg.what == HANDLER_LOGIN_FAILURE) { if (mDialog != null) mDialog.dismiss(); WinToast.toast(LoginActivity.this, R.string.login_failure); startActivity(new Intent(this, MainActivity.class)); finish(); } else if (msg.what == HANDLER_LOGIN_SUCCESS) { if (mDialog != null) mDialog.dismiss(); WinToast.toast(LoginActivity.this, R.string.login_success); startActivity(new Intent(this, MainActivity.class)); finish(); } else if (msg.what == HANDLER_LOGIN_HAS_FOCUS) { mLoginImg.setVisibility(View.GONE); mRegister.setVisibility(View.GONE); mFogotPassWord.setVisibility(View.GONE); mIsShowTitle.setVisibility(View.VISIBLE); mLeftTitle.setText(R.string.app_sign_up); mRightTitle.setText(R.string.app_fogot_password); } else if (msg.what == HANDLER_LOGIN_HAS_NO_FOCUS) { mLoginImg.setVisibility(View.VISIBLE); mRegister.setVisibility(View.VISIBLE); mFogotPassWord.setVisibility(View.VISIBLE); mIsShowTitle.setVisibility(View.GONE); } return false; }
@Override public void onCallApiFailure(AbstractHttpRequest request, BaseException e) { if (loginHttpRequest != null && loginHttpRequest.equals(request)) { if (mDialog != null) mDialog.dismiss(); } else if (getTokenHttpRequest != null && getTokenHttpRequest.equals(request)) { if (mDialog != null) mDialog.dismiss(); } }
/** * 收到push消息后做重连,重新连接融云 * * @param token */ private void reconnect(String token) { mDialog = new LoadingDialog(this); mDialog.setCancelable(false); mDialog.setText("正在连接中..."); mDialog.show(); try { RongIM.connect( token, new RongIMClient.ConnectCallback() { @Override public void onTokenIncorrect() {} @Override public void onSuccess(String userId) { mHandler.post( new Runnable() { @Override public void run() { mDialog.dismiss(); Intent intent = getIntent(); if (intent != null) { enterFragment(intent); } } }); } @Override public void onError(RongIMClient.ErrorCode e) { mHandler.post( new Runnable() { @Override public void run() { mDialog.dismiss(); } }); } }); } catch (Exception e) { mHandler.post( new Runnable() { @Override public void run() { mDialog.dismiss(); } }); e.printStackTrace(); } }
@Override protected void onStop() { super.onStop(); if (mDialog != null) { mDialog.dismiss(); mDialog = null; } }
/** * 收到push消息后做重连,重新连接融云 * * @param token */ private void reconnect( final String token, final String conversation, final String conversationType, final String targetId) { mDialog.setText("正在连接中..."); mDialog.show(); try { RongIM.connect( token, new RongIMClient.ConnectCallback() { @Override public void onTokenIncorrect() { Log.e(TAG, "--onTokenIncorrect---"); } @Override public void onSuccess(String userId) { if (mDialog != null) mDialog.dismiss(); if (conversation.equals("conversation")) { if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) { RongIM.getInstance() .getRongIMClient() .getConversation( Conversation.ConversationType.valueOf(conversationType), targetId, new RongIMClient.ResultCallback<Conversation>() { @Override public void onSuccess(Conversation conversation) { if (conversation != null) { if (conversation.getLatestMessage() instanceof ContactNotificationMessage) { startActivity( new Intent(MainActivity.this, NewFriendListActivity.class)); } else { Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName) .buildUpon() .appendPath("conversation") .appendPath(conversationType) .appendQueryParameter("targetId", targetId) .build(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } } } @Override public void onError(RongIMClient.ErrorCode e) {} }); } } } @Override public void onError(RongIMClient.ErrorCode e) { mDialog.dismiss(); } }); } catch (Exception e) { mDialog.dismiss(); e.printStackTrace(); } }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.app_sign_in_bt: // 登录 userName = mUserNameEt.getEditableText().toString(); String passWord = mPassWordEt.getEditableText().toString(); String name = null; if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) { WinToast.toast(this, R.string.login_erro_is_null); return; } if (mDialog != null && !mDialog.isShowing()) { mDialog.show(); } // 发起登录 http请求 (注:非融云SDK接口,是demo接口) if (DemoContext.getInstance() != null) { // 如果切换了一个用户,token和 cookie 都需要重新获取 if (DemoContext.getInstance() != null) { name = DemoContext.getInstance() .getSharedPreferences() .getString("DEMO_USERNAME", "DEFAULT"); } if (!userName.equals(name)) { loginHttpRequest = DemoContext.getInstance().getDemoApi().login(userName, passWord, this); isFirst = true; } else { isFirst = false; String cookie = DemoContext.getInstance() .getSharedPreferences() .getString("DEMO_COOKIE", "DEFAULT"); String token = DemoContext.getInstance().getSharedPreferences().getString("DEMO_TOKEN", "DEFAULT"); if (!cookie.equals("DEFAULT") && !token.equals("DEFAULT")) { httpGetTokenSuccess(token); } else { loginHttpRequest = DemoContext.getInstance().getDemoApi().login(userName, passWord, this); } } } break; case R.id.de_left: // 注册 case R.id.de_login_register: // 注册 Intent intent = new Intent(this, RegisterActivity.class); startActivityForResult(intent, REQUEST_CODE_REGISTER); break; case R.id.de_login_forgot: // 忘记密码 WinToast.toast(this, "忘记密码"); break; case R.id.de_right: // 忘记密码 Intent intent1 = new Intent(this, RegisterActivity.class); startActivityForResult(intent1, REQUEST_CODE_REGISTER); break; case R.id.app_username_et: case R.id.app_password_et: Message mess = Message.obtain(); mess.what = HANDLER_LOGIN_HAS_FOCUS; mHandler.sendMessage(mess); break; } }