Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Account extends AppCompatActivity {
- FirebaseUser mUser;
- StorageReference mStorage;
- ImageView displayProfilePhoto;
- private static final int GALLERY_INTENT = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_account);
- ImageButton Return = (ImageButton) findViewById(UserToHome);
- ImageButton editPhoto = (ImageButton) findViewById(R.id.editProfilePhoto);
- ImageButton editName = (ImageButton) findViewById(R.id.editName);
- ImageButton editEmail = (ImageButton) findViewById(R.id.editEmail);
- ImageButton editPass = (ImageButton) findViewById(R.id.editPassword);
- Button VerifyEmail = (Button) findViewById(R.id.DisplayVerification);
- Return.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(Account.this, Home.class));
- }
- });
- editPhoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- changePhoto();
- }
- });
- editName.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(Account.this, ChangeName.class));
- }
- });
- editEmail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(Account.this, ChangeEmail.class));
- }
- });
- editPass.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startActivity(new Intent(Account.this, ChangePassword.class));
- }
- });
- VerifyEmail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- EmailVerification();
- }
- });
- getUserProfile();
- }
- private void getUserProfile() {
- mUser = FirebaseAuth.getInstance().getCurrentUser();
- mStorage = FirebaseStorage.getInstance().getReference();
- if (mUser != null) {
- Uri photoUrl = mUser.getPhotoUrl();
- String UID = mUser.getUid();
- String name = mUser.getDisplayName();
- String email = mUser.getEmail();
- Boolean verification = mUser.isEmailVerified();
- displayProfilePhoto = (ImageView) findViewById(R.id.ProfilePhoto);
- TextView displayUID = (TextView) findViewById(R.id.UserID);
- TextView displayName = (TextView) findViewById(R.id.DisplayName);
- TextView displayEmail = (TextView) findViewById(R.id.DisplayEmail);
- Button displayEmailVerification = (Button) findViewById(R.id.DisplayVerification);
- Picasso.with(Account.this).load(photoUrl).fit().centerCrop().into(displayProfilePhoto);
- displayUID.setText(UID);
- displayName.setText(name);
- displayEmail.setText(email);
- if(verification){
- displayEmailVerification.setText("Verified");
- }
- else{
- displayEmailVerification.setText("Not verified");
- }
- }
- }
- private void EmailVerification() {
- mUser = FirebaseAuth.getInstance().getCurrentUser();
- Boolean verification = mUser.isEmailVerified();
- if(verification){
- Toast.makeText(Account.this, "Email verified.",
- Toast.LENGTH_SHORT).show();
- }
- else{
- mUser.sendEmailVerification();
- Toast.makeText(Account.this, "Verification email sent.",
- Toast.LENGTH_SHORT).show();
- }
- }
- private void changePhoto() {
- mStorage = FirebaseStorage.getInstance().getReference();
- Intent intent = new Intent(Intent.ACTION_PICK);
- intent.setType("image/*");
- startActivityForResult(intent, GALLERY_INTENT);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- mUser = FirebaseAuth.getInstance().getCurrentUser();
- String UID = mUser.getUid();
- displayProfilePhoto = (ImageView) findViewById(R.id.ProfilePhoto);
- if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
- Uri uri = data.getData();
- final StorageReference childRef = mStorage.child("Profile Pictures")
- .child(UID)
- .child(uri.getLastPathSegment());
- childRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- @SuppressWarnings("VisibleForTests") final
- Uri downloadUri = taskSnapshot.getDownloadUrl();
- String stringUri = downloadUri.toString();
- UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
- .setPhotoUri(Uri.parse(stringUri))
- .build();
- mUser.updateProfile(profileUpdates)
- .addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- if (!task.isSuccessful()) {
- Toast.makeText(Account.this, "Profile update failed. Please try again.",
- Toast.LENGTH_SHORT).show();
- }
- else
- {
- Picasso.with(Account.this).load(downloadUri).fit().centerCrop().into(displayProfilePhoto);
- Toast.makeText(Account.this, "Profile picture updated.",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Toast.makeText(Account.this, "Photo upload failed. Please try again.",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- }
Add Comment
Please, Sign In to add comment