Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. package com.sendbird.androidchattutorial;
  2.  
  3. import android.content.Intent;
  4. import android.support.design.widget.TextInputEditText;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10.  
  11. import com.sendbird.android.SendBird;
  12. import com.sendbird.android.SendBirdException;
  13. import com.sendbird.android.User;
  14.  
  15. public class LoginActivity extends AppCompatActivity {
  16.  
  17. private static final String APP_ID = "9DA1B1F4-0BE6-4DA8-82C5-2E81DAB56F23"; // SAMPLE
  18.  
  19. private Button mConnectButton;
  20. private TextInputEditText mUserIdEditText, mUserNicknameEditText;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_login);
  26.  
  27. mConnectButton = (Button) findViewById(R.id.button_login);
  28. mUserIdEditText = (TextInputEditText) findViewById(R.id.edit_text_login_user_id);
  29. mUserNicknameEditText = (TextInputEditText) findViewById(R.id.edit_text_login_user_nickname);
  30.  
  31. // Initialize the SendBird SDK.
  32. SendBird.init(APP_ID, this.getApplicationContext());
  33.  
  34. mConnectButton.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. String userId = mUserIdEditText.getText().toString();
  38. // Remove all spaces from userID
  39. userId = userId.replaceAll("\\s", "");
  40.  
  41. String userNickname = mUserNicknameEditText.getText().toString();
  42.  
  43. connectToSendBird(userId, userNickname);
  44. }
  45. });
  46. }
  47.  
  48. /**
  49. * Attempts to connect a user to SendBird.
  50. * @param userId The unique ID of the user.
  51. * @param userNickname The user's nickname, which will be displayed in chats.
  52. */
  53. private void connectToSendBird(final String userId, final String userNickname) {
  54. mConnectButton.setEnabled(false);
  55.  
  56. SendBird.connect(userId, new SendBird.ConnectHandler() {
  57. @Override
  58. public void onConnected(User user, SendBirdException e) {
  59.  
  60. if (e != null) {
  61. // Error!
  62. Toast.makeText(
  63. LoginActivity.this, "" + e.getCode() + ": " + e.getMessage(),
  64. Toast.LENGTH_SHORT)
  65. .show();
  66.  
  67. // Show login failure snackbar
  68. mConnectButton.setEnabled(true);
  69. return;
  70. }
  71.  
  72. // Update the user's nickname
  73. updateCurrentUserInfo(userNickname);
  74.  
  75. Intent intent = new Intent(LoginActivity.this, ChatActivity.class);
  76. startActivity(intent);
  77. finish();
  78. }
  79. });
  80. }
  81.  
  82. /**
  83. * Updates the user's nickname.
  84. * @param userNickname The new nickname of the user.
  85. */
  86. private void updateCurrentUserInfo(String userNickname) {
  87. SendBird.updateCurrentUserInfo(userNickname, null, new SendBird.UserInfoUpdateHandler() {
  88. @Override
  89. public void onUpdated(SendBirdException e) {
  90. if (e != null) {
  91. // Error!
  92. Toast.makeText(
  93. LoginActivity.this, "" + e.getCode() + ":" + e.getMessage(),
  94. Toast.LENGTH_SHORT)
  95. .show();
  96.  
  97. return;
  98. }
  99.  
  100. }
  101. });
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement