Search in sources :

Example 1 with OnCompleteListener

use of com.google.android.gms.tasks.OnCompleteListener in project MadMax by deviz92.

the class SignUpFragment method sendVerificationEmail.

private void sendVerificationEmail() {
    Log.i(TAG, "sendVerificationEmail");
    final FirebaseUser user = auth.getCurrentUser();
    if (user == null) {
        Log.e(TAG, "Error while retriving current user from db");
        Toast.makeText(getContext(), "Error while retriving current user from db", Toast.LENGTH_LONG).show();
        return;
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getContext());
    String defaultCurrency = sharedPref.getString(SettingsFragment.DEFAULT_CURRENCY, "");
    String UID = user.getUid();
    final User u = new User(UID, usernameView.getText().toString(), nameView.getText().toString(), surnameView.getText().toString(), emailView.getText().toString(), passwordView.getText().toString(), "", defaultCurrency);
    progressDialog.setMessage("Sending email verification, please wait...");
    progressDialog.show();
    // for saving image
    if (imageSetted) {
        StorageReference uProfileImageFilenameRef = storageReference.child("users").child(UID).child(UID + "_profileImage.jpg");
        // Get the data from an ImageView as bytes
        profileImageView.setDrawingCacheEnabled(true);
        profileImageView.buildDrawingCache();
        Bitmap bitmap = profileImageView.getDrawingCache();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data = baos.toByteArray();
        UploadTask uploadTask = uProfileImageFilenameRef.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                // todo Handle unsuccessful uploads
                Log.e(TAG, "image upload failed");
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                u.setProfileImage(taskSnapshot.getMetadata().getDownloadUrl().toString());
                user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {

                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        progressDialog.dismiss();
                        if (task.isSuccessful()) {
                            Log.i(TAG, "verification email successful sent");
                            Log.i(TAG, "insert new user into db");
                            HashMap<String, String> newUserEntry = new HashMap<>();
                            newUserEntry.put("email", u.getEmail());
                            newUserEntry.put("password", u.getPassword());
                            //                                    newUserEntry.put("friends", u.getUserFriends().toString());
                            //                                    newUserEntry.put("groups", u.getUserGroups().toString());
                            newUserEntry.put("image", u.getProfileImage());
                            newUserEntry.put("name", u.getName());
                            newUserEntry.put("surname", u.getSurname());
                            newUserEntry.put("username", u.getUsername());
                            databaseReference.child("users").child(u.getID()).setValue(newUserEntry);
                            Toast.makeText(getContext(), R.string.emailVerification_text, Toast.LENGTH_LONG).show();
                        } else {
                            // todo delete the account and restart the activity
                            Log.e(TAG, "verification email not sent, exception: " + task.getException());
                        }
                        onClickSignUpInterface.itemClicked(SignUpFragment.class.getSimpleName(), "1");
                    }
                });
            }
        });
    } else {
        user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(@NonNull Task<Void> task) {
                progressDialog.dismiss();
                if (task.isSuccessful()) {
                    Log.i(TAG, "verification email successful sent");
                    Log.i(TAG, "insert new user into db");
                    HashMap<String, String> newUserEntry = new HashMap<>();
                    newUserEntry.put("email", u.getEmail());
                    newUserEntry.put("password", u.getPassword());
                    //                        newUserEntry.put("friends", u.getUserFriends().toString());
                    //                        newUserEntry.put("groups", u.getUserGroups().toString());
                    newUserEntry.put("image", u.getProfileImage());
                    newUserEntry.put("name", u.getName());
                    newUserEntry.put("surname", u.getSurname());
                    newUserEntry.put("username", u.getUsername());
                    databaseReference.child("users").child(u.getID()).setValue(newUserEntry);
                    Toast.makeText(getContext(), R.string.emailVerification_text, Toast.LENGTH_LONG).show();
                } else {
                    // todo delete the account and restart the activity
                    Log.e(TAG, "verification email not sent, exception: " + task.getException());
                }
                onClickSignUpInterface.itemClicked(SignUpFragment.class.getSimpleName(), "1");
            }
        });
    }
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) User(com.polito.mad17.madmax.entities.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) HashMap(java.util.HashMap) Bitmap(android.graphics.Bitmap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) StorageReference(com.google.firebase.storage.StorageReference) SharedPreferences(android.content.SharedPreferences) FirebaseUser(com.google.firebase.auth.FirebaseUser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) IOException(java.io.IOException) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 NonNull (android.support.annotation.NonNull)1 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)1 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 Task (com.google.android.gms.tasks.Task)1 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)1 FirebaseAuthWeakPasswordException (com.google.firebase.auth.FirebaseAuthWeakPasswordException)1 FirebaseUser (com.google.firebase.auth.FirebaseUser)1 StorageReference (com.google.firebase.storage.StorageReference)1 UploadTask (com.google.firebase.storage.UploadTask)1 User (com.polito.mad17.madmax.entities.User)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1