Search in sources :

Example 1 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class MyPhoneNumberActivity method getUserWithNumberPhone.

private void getUserWithNumberPhone() {
    FirebaseFirestore database = FirebaseFirestore.getInstance();
    database.collection(Constants.KEY_COLLECTION_USERS).whereEqualTo(Constants.KEY_NUMBER_PHONE, binding.phoneBox.getText().toString()).get().addOnCompleteListener(task -> {
        if (task.isSuccessful() && task.getResult() != null && task.getResult().getDocuments().size() > 0) {
            DocumentSnapshot snapshot = task.getResult().getDocuments().get(0);
            user = new User();
            user.setUid(snapshot.getId());
            user.setName(snapshot.getString(Constants.KEY_NAME));
            user.setProfileImage(snapshot.getString(Constants.KEY_IMAGE));
            user.setPhoneNumber(snapshot.getString(Constants.KEY_NUMBER_PHONE));
            user.setEmail(snapshot.getString(Constants.KEY_EMAIL));
            user.setPassword(snapshot.getString(Constants.KEY_EMAIL));
            getAuthPhone();
        } else {
            Toast.makeText(this, "Doesn't exist number phone ...", Toast.LENGTH_SHORT).show();
            loading(false);
        }
    });
}
Also used : DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) User(com.tutuanle.chatapp.models.User)

Example 2 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class OTPScreenActivity method setUpOTPInput.

private void setUpOTPInput() {
    String phoneNumber = getIntent().getStringExtra("phoneNumber");
    String verificationId = getIntent().getStringExtra("verificationId");
    User user = (User) getIntent().getSerializableExtra(Constants.KEY_USER);
    binding.textView.setText("Check your SMS messages , we have sent your pin at + " + phoneNumber);
    binding.inputCode1.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode2.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    binding.inputCode2.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode3.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    binding.inputCode3.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode4.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    binding.inputCode4.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode5.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    binding.inputCode5.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode6.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    binding.inputCode6.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!charSequence.toString().trim().isEmpty()) {
                binding.inputCode6.onEditorAction(EditorInfo.IME_ACTION_DONE);
                loading(true);
                String code = binding.inputCode1.getText().toString() + binding.inputCode2.getText().toString() + binding.inputCode3.getText().toString() + binding.inputCode4.getText().toString() + binding.inputCode5.getText().toString() + binding.inputCode6.getText().toString();
                PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, code);
                FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnSuccessListener(v -> {
                    Toast.makeText(OTPScreenActivity.this, "login successfully", Toast.LENGTH_SHORT).show();
                    preferenceManager.putBoolean(Constants.KEY_IS_SIGNED_IN, true);
                    preferenceManager.putString(Constants.KEY_USER_ID, user.getUid());
                    preferenceManager.putString(Constants.KEY_NAME, user.getName());
                    preferenceManager.putString(Constants.KEY_IMAGE, user.getProfileImage());
                    preferenceManager.putString(Constants.KEY_EMAIL, user.getEmail());
                    preferenceManager.putString(Constants.KEY_NUMBER_PHONE, user.getPhoneNumber());
                    preferenceManager.putString(Constants.KEY_PASSWORD, user.getPassword());
                    Intent intent = new Intent(getApplicationContext(), MainScreenActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    loading(false);
                }).addOnFailureListener(v -> {
                    Toast.makeText(OTPScreenActivity.this, "get OTP fail", Toast.LENGTH_SHORT).show();
                    loading(false);
                });
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
}
Also used : Bundle(android.os.Bundle) Constants(com.tutuanle.chatapp.utilities.Constants) NonNull(androidx.annotation.NonNull) Intent(android.content.Intent) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) Editable(android.text.Editable) PreferenceManager(com.tutuanle.chatapp.utilities.PreferenceManager) ActivityOtpscreenBinding(com.tutuanle.chatapp.databinding.ActivityOtpscreenBinding) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) PhoneAuthCredential(com.google.firebase.auth.PhoneAuthCredential) Toast(android.widget.Toast) View(android.view.View) User(com.tutuanle.chatapp.models.User) AuthResult(com.google.firebase.auth.AuthResult) EditorInfo(android.view.inputmethod.EditorInfo) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) TextWatcher(android.text.TextWatcher) User(com.tutuanle.chatapp.models.User) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) PhoneAuthCredential(com.google.firebase.auth.PhoneAuthCredential) Intent(android.content.Intent)

Example 3 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    dialog = new ProgressDialog(this);
    dialog.setMessage("upLoading Image ...");
    dialog.setCancelable(false);
    database = FirebaseDatabase.getInstance();
    users = new ArrayList<User>();
    usersAdapter = new UsersAdapter(this, users);
    userStatuses = new ArrayList<>();
    database.getReference().child("users").child(FirebaseAuth.getInstance().getUid()).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            user = snapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    statusAdapter = new TopStatusAdapter(this, userStatuses);
    // binding.statusList.setLayoutManager((new LinearLayoutManager(this)));
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(RecyclerView.HORIZONTAL);
    binding.statusList.setLayoutManager(layoutManager);
    // binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
    binding.statusList.setAdapter(statusAdapter);
    binding.recyclerView.setAdapter(usersAdapter);
    database.getReference().child("users").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            users.clear();
            for (DataSnapshot snapshot1 : snapshot.getChildren()) {
                User user = snapshot1.getValue(User.class);
                Log.d("log", user.toString());
                users.add(user);
            }
            usersAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    database.getReference().child("stories").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                userStatuses.clear();
                for (DataSnapshot storySnapshot : snapshot.getChildren()) {
                    UserStatus status = new UserStatus();
                    status.setName(storySnapshot.child("name").getValue(String.class));
                    status.setProfileImage(storySnapshot.child("profileImage").getValue(String.class));
                    status.setLastUpdated(storySnapshot.child("lastUpdate").getValue(Long.class));
                    ArrayList<Status> statuses = new ArrayList<>();
                    for (DataSnapshot statusSnapshot : storySnapshot.child("statuses").getChildren()) {
                        Status sampleStatus = statusSnapshot.getValue(Status.class);
                        statuses.add(sampleStatus);
                    }
                    status.setStatuses(statuses);
                    userStatuses.add(status);
                }
                statusAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    binding.BottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()) {
                case R.id.status:
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, 75);
                    break;
            }
            ;
            return false;
        }
    });
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) User(com.tutuanle.chatapp.models.User) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) TopStatusAdapter(com.tutuanle.chatapp.adapters.TopStatusAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) UserStatus(com.tutuanle.chatapp.models.UserStatus) UsersAdapter(com.tutuanle.chatapp.adapters.UsersAdapter) DatabaseError(com.google.firebase.database.DatabaseError) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 4 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class MessagingService method onMessageReceived.

@SuppressLint("UnspecifiedImmutableFlag")
@RequiresApi(api = Build.VERSION_CODES.N)
public void onMessageReceived(@NonNull RemoteMessage message) {
    String type = message.getData().get(Constants.REMOTE_MSG_TYPE);
    Log.d("TAG_TYPE", "setUpVideoCall: ");
    super.onMessageReceived(message);
    setUpVideoCall(message);
    User user = new User();
    user.setUid(message.getData().get(Constants.KEY_USER_ID));
    user.setName(message.getData().get(Constants.KEY_NAME));
    user.setToken(message.getData().get(Constants.KEY_FCM_TOKEN));
    int notificationID = new Random().nextInt();
    String channelId = "chat_message";
    Intent intent = new Intent(this, ChatScreenActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(Constants.KEY_USER, user);
    PendingIntent pendingIntent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
    } else {
        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    }
    // custom notification
    @SuppressLint("RemoteViewLayout") RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.custom_notification);
    // notificationLayout.setTextViewText(R.id.username, user.getName());
    // notificationLayout.setTextViewText(R.id.message, message.getData().get(Constants.KEY_MESSAGE));
    // notificationLayout.setImageViewBitmap();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
    builder.setSmallIcon(R.drawable.ic_baseline_toys_24);
    builder.setContentTitle(user.getName());
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message.getData().get(Constants.KEY_MESSAGE)));
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "Chat Message ";
        String channelDescription = "This notification channel is used for chat message notification";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setDescription(channelDescription);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(notificationID, builder.build());
}
Also used : User(com.tutuanle.chatapp.models.User) NotificationManager(android.app.NotificationManager) NotificationManagerCompat(androidx.core.app.NotificationManagerCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SuppressLint(android.annotation.SuppressLint) NotificationChannel(android.app.NotificationChannel) RemoteViews(android.widget.RemoteViews) Random(java.util.Random) SuppressLint(android.annotation.SuppressLint) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) RequiresApi(androidx.annotation.RequiresApi) SuppressLint(android.annotation.SuppressLint)

Example 5 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class ChatFragment method getUSer.

private void getUSer() {
    loading(true);
    FirebaseFirestore database = FirebaseFirestore.getInstance();
    database.collection(Constants.KEY_COLLECTION_USERS).get().addOnCompleteListener(task -> {
        loading(false);
        String currentUserId = preferenceManager.getString(Constants.KEY_USER_ID);
        if (task.isSuccessful() && task.getResult() != null) {
            List<User> users = new ArrayList<>();
            for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()) {
                if (currentUserId.equals(queryDocumentSnapshot.getId())) {
                    continue;
                }
                User user = new User();
                user.setName(queryDocumentSnapshot.getString(Constants.KEY_NAME));
                user.setEmail(queryDocumentSnapshot.getString(Constants.KEY_EMAIL));
                user.setProfileImage(queryDocumentSnapshot.getString(Constants.KEY_IMAGE));
                user.setToken(queryDocumentSnapshot.getString(Constants.KEY_FCM_TOKEN));
                user.setUid(queryDocumentSnapshot.getId());
                user.setAvailability(queryDocumentSnapshot.getLong(Constants.KEY_AVAILABILITY));
                users.add(user);
            }
            if (users.size() > 0) {
                Users_Adapter users_adapter = new Users_Adapter(users, mainScreenActivity);
                RecyclerView temp = view.findViewById(R.id.userRecyclerView);
                temp.setAdapter(users_adapter);
                temp.setVisibility(View.VISIBLE);
            } else {
                showErrorMessage();
            }
        }
    });
}
Also used : FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) User(com.tutuanle.chatapp.models.User) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) Users_Adapter(com.tutuanle.chatapp.adapters.Users_Adapter) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

User (com.tutuanle.chatapp.models.User)9 Intent (android.content.Intent)5 View (android.view.View)3 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)3 ArrayList (java.util.ArrayList)3 SuppressLint (android.annotation.SuppressLint)2 ProgressDialog (android.app.ProgressDialog)2 NonNull (androidx.annotation.NonNull)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)2 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 DatabaseError (com.google.firebase.database.DatabaseError)2 ValueEventListener (com.google.firebase.database.ValueEventListener)2 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)2 SearchAdapter (com.tutuanle.chatapp.adapters.SearchAdapter)2 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Uri (android.net.Uri)1