Guest User

Untitled

a guest
Jan 4th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. public class Account extends AppCompatActivity {
  2.  
  3. FirebaseUser mUser;
  4. StorageReference mStorage;
  5.  
  6. ImageView displayProfilePhoto;
  7.  
  8. private static final int GALLERY_INTENT = 0;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_account);
  14.  
  15. ImageButton Return = (ImageButton) findViewById(UserToHome);
  16. ImageButton editPhoto = (ImageButton) findViewById(R.id.editProfilePhoto);
  17. ImageButton editName = (ImageButton) findViewById(R.id.editName);
  18. ImageButton editEmail = (ImageButton) findViewById(R.id.editEmail);
  19. ImageButton editPass = (ImageButton) findViewById(R.id.editPassword);
  20. Button VerifyEmail = (Button) findViewById(R.id.DisplayVerification);
  21.  
  22. Return.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View view) {
  25. startActivity(new Intent(Account.this, Home.class));
  26. }
  27. });
  28.  
  29. editPhoto.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View view) {
  32. changePhoto();
  33. }
  34. });
  35.  
  36. editName.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. startActivity(new Intent(Account.this, ChangeName.class));
  40. }
  41. });
  42.  
  43. editEmail.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View view) {
  46. startActivity(new Intent(Account.this, ChangeEmail.class));
  47. }
  48. });
  49.  
  50. editPass.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View view) {
  53. startActivity(new Intent(Account.this, ChangePassword.class));
  54. }
  55. });
  56.  
  57. VerifyEmail.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. EmailVerification();
  61. }
  62. });
  63.  
  64. getUserProfile();
  65. }
  66.  
  67. private void getUserProfile() {
  68. mUser = FirebaseAuth.getInstance().getCurrentUser();
  69. mStorage = FirebaseStorage.getInstance().getReference();
  70.  
  71. if (mUser != null) {
  72. Uri photoUrl = mUser.getPhotoUrl();
  73. String UID = mUser.getUid();
  74. String name = mUser.getDisplayName();
  75. String email = mUser.getEmail();
  76. Boolean verification = mUser.isEmailVerified();
  77.  
  78. displayProfilePhoto = (ImageView) findViewById(R.id.ProfilePhoto);
  79. TextView displayUID = (TextView) findViewById(R.id.UserID);
  80. TextView displayName = (TextView) findViewById(R.id.DisplayName);
  81. TextView displayEmail = (TextView) findViewById(R.id.DisplayEmail);
  82. Button displayEmailVerification = (Button) findViewById(R.id.DisplayVerification);
  83.  
  84. Picasso.with(Account.this).load(photoUrl).fit().centerCrop().into(displayProfilePhoto);
  85. displayUID.setText(UID);
  86. displayName.setText(name);
  87. displayEmail.setText(email);
  88. if(verification){
  89. displayEmailVerification.setText("Verified");
  90. }
  91. else{
  92. displayEmailVerification.setText("Not verified");
  93. }
  94. }
  95. }
  96.  
  97. private void EmailVerification() {
  98. mUser = FirebaseAuth.getInstance().getCurrentUser();
  99.  
  100. Boolean verification = mUser.isEmailVerified();
  101. if(verification){
  102. Toast.makeText(Account.this, "Email verified.",
  103. Toast.LENGTH_SHORT).show();
  104. }
  105. else{
  106. mUser.sendEmailVerification();
  107. Toast.makeText(Account.this, "Verification email sent.",
  108. Toast.LENGTH_SHORT).show();
  109. }
  110. }
  111.  
  112. private void changePhoto() {
  113. mStorage = FirebaseStorage.getInstance().getReference();
  114.  
  115. Intent intent = new Intent(Intent.ACTION_PICK);
  116. intent.setType("image/*");
  117.  
  118. startActivityForResult(intent, GALLERY_INTENT);
  119. }
  120.  
  121. @Override
  122. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  123. super.onActivityResult(requestCode, resultCode, data);
  124.  
  125. mUser = FirebaseAuth.getInstance().getCurrentUser();
  126. String UID = mUser.getUid();
  127.  
  128. displayProfilePhoto = (ImageView) findViewById(R.id.ProfilePhoto);
  129.  
  130. if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
  131. Uri uri = data.getData();
  132.  
  133. final StorageReference childRef = mStorage.child("Profile Pictures")
  134. .child(UID)
  135. .child(uri.getLastPathSegment());
  136.  
  137. childRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  138. @Override
  139. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  140. @SuppressWarnings("VisibleForTests") final
  141. Uri downloadUri = taskSnapshot.getDownloadUrl();
  142. String stringUri = downloadUri.toString();
  143.  
  144.  
  145. UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
  146. .setPhotoUri(Uri.parse(stringUri))
  147. .build();
  148.  
  149. mUser.updateProfile(profileUpdates)
  150. .addOnCompleteListener(new OnCompleteListener<Void>() {
  151. @Override
  152. public void onComplete(@NonNull Task<Void> task) {
  153. if (!task.isSuccessful()) {
  154. Toast.makeText(Account.this, "Profile update failed. Please try again.",
  155. Toast.LENGTH_SHORT).show();
  156. }
  157. else
  158. {
  159. Picasso.with(Account.this).load(downloadUri).fit().centerCrop().into(displayProfilePhoto);
  160.  
  161. Toast.makeText(Account.this, "Profile picture updated.",
  162. Toast.LENGTH_SHORT).show();
  163. }
  164. }
  165. });
  166. }
  167. }).addOnFailureListener(new OnFailureListener() {
  168. @Override
  169. public void onFailure(@NonNull Exception e) {
  170. Toast.makeText(Account.this, "Photo upload failed. Please try again.",
  171. Toast.LENGTH_SHORT).show();
  172. }
  173. });
  174. }
  175. }
  176. }
Add Comment
Please, Sign In to add comment