Guest User

Untitled

a guest
Jun 19th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. package com.example.rahul.mychatapp;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.support.annotation.NonNull;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.google.android.gms.tasks.OnCompleteListener;
  15. import com.google.android.gms.tasks.Task;
  16. import com.google.firebase.auth.FirebaseAuth;
  17. import com.google.firebase.auth.FirebaseUser;
  18. import com.google.firebase.database.DataSnapshot;
  19. import com.google.firebase.database.DatabaseError;
  20. import com.google.firebase.database.DatabaseReference;
  21. import com.google.firebase.database.FirebaseDatabase;
  22. import com.google.firebase.database.ValueEventListener;
  23. import com.google.firebase.storage.FirebaseStorage;
  24. import com.google.firebase.storage.StorageReference;
  25. import com.google.firebase.storage.UploadTask;
  26. import com.squareup.picasso.Picasso;
  27. import com.theartofdev.edmodo.cropper.CropImage;
  28.  
  29. import java.util.Random;
  30.  
  31. import de.hdodenhof.circleimageview.CircleImageView;
  32.  
  33. public class AccountSettingsActivity extends AppCompatActivity {
  34.  
  35.     private TextView displayName;
  36.     private TextView userStatus;
  37.     private CircleImageView mImage;
  38.  
  39.     private Button changeStatusButton;
  40.     private Button changeImageButton;
  41.  
  42.     //integer variable for galary intent
  43.     private static final int GALLARY_PICK = 1;
  44.  
  45.     //firebase database reference
  46.     private DatabaseReference accountDb;
  47.  
  48.     //firebase user
  49.     private FirebaseUser user;
  50.  
  51.     //firebase storage reference
  52.     private StorageReference imageReference;
  53.  
  54.     //for progress dialog
  55.     private ProgressDialog dialog;
  56.  
  57.     @Override
  58.     protected void onCreate(Bundle savedInstanceState) {
  59.         super.onCreate(savedInstanceState);
  60.         setContentView(R.layout.activity_account_settings);
  61.  
  62.         //java object for xml components
  63.         displayName = (TextView) findViewById(R.id.displayName_setting);
  64.         userStatus = (TextView) findViewById(R.id.status_setting);
  65.         mImage = (CircleImageView) findViewById(R.id.settings_image);
  66.  
  67.         //firebase storage reference
  68.         imageReference = FirebaseStorage.getInstance().getReference();
  69.  
  70.         //firebase user
  71.         user = FirebaseAuth.getInstance().getCurrentUser();
  72.  
  73.         //retrieving userid
  74.         String uid = user.getUid();
  75.  
  76.         //firebase database reference
  77.         accountDb = FirebaseDatabase.getInstance().getReference().child("Users").child(uid);
  78.  
  79.         //get data from the firebase database
  80.         accountDb.addValueEventListener(new ValueEventListener() {
  81.             @Override
  82.             public void onDataChange(DataSnapshot dataSnapshot) {
  83.  
  84.                 //getting values form database and storing it in local variables
  85.                 String name = dataSnapshot.child("name").getValue().toString();
  86.                 String image = dataSnapshot.child("image").getValue().toString();
  87.                 String status = dataSnapshot.child("status").getValue().toString();
  88.                 String thumbnail = dataSnapshot.child("thumbnail").getValue().toString();
  89.  
  90.                 //setting the retrieved values from database into xml components
  91.                 displayName.setText(name);
  92.                 userStatus.setText(status);
  93.  
  94.                 //showing the image in circle image view with the help of picasso library
  95.                 Picasso.with(AccountSettingsActivity.this).load(image).into(mImage);
  96.             }
  97.  
  98.             @Override
  99.             public void onCancelled(DatabaseError databaseError) {
  100.  
  101.             }
  102.         });
  103.  
  104.  
  105.         //java object for xml button
  106.         changeStatusButton = (Button) findViewById(R.id.changeStatusButton);
  107.  
  108.         //on button click
  109.         changeStatusButton.setOnClickListener(new View.OnClickListener() {
  110.             @Override
  111.             public void onClick(View v) {
  112.  
  113.                 //getting the status from status field to send it to status activity
  114.                 String statusToPass = userStatus.getText().toString();
  115.  
  116.                 //making intent to status activity
  117.                 Intent intent = new Intent(AccountSettingsActivity.this, StatusActivity.class);
  118.                 intent.putExtra("statusValue", statusToPass);   //sending data from current intent to different intent
  119.                 startActivity(intent);
  120.             }
  121.         });
  122.  
  123.         //java object for changeImageButton xml button
  124.         changeImageButton = (Button) findViewById(R.id.changeImageButton);
  125.  
  126.         //on change image button click
  127.         changeImageButton.setOnClickListener(new View.OnClickListener() {
  128.             @Override
  129.             public void onClick(View v) {
  130.  
  131.                 //making a galary intent
  132.                 Intent galleryIntent = new Intent();
  133.                 galleryIntent.setType("image/*");
  134.                 galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
  135.                 startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLARY_PICK);
  136.             }
  137.         });
  138.     }
  139.  
  140.  
  141.     //this method works while setting image
  142.     @Override
  143.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  144.         super.onActivityResult(requestCode, resultCode, data);
  145.         if (requestCode == GALLARY_PICK && resultCode == RESULT_OK) {
  146.             Uri imageUri = data.getData();
  147.             CropImage.activity(imageUri).setAspectRatio(1,1).start(this);
  148.         }
  149.  
  150.         if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
  151.             CropImage.ActivityResult result = CropImage.getActivityResult(data);
  152.             if (resultCode == RESULT_OK) {
  153.  
  154.                 //dialog bar
  155.                 dialog = new ProgressDialog(AccountSettingsActivity.this);
  156.                 dialog.setTitle("Uploading");
  157.                 dialog.setMessage("Please Wait");
  158.                 dialog.setCanceledOnTouchOutside(false);
  159.                 dialog.show();
  160.  
  161.                 Uri resultUri = result.getUri();
  162.  
  163.                 //getting the userId of user so that we can store image name with UID
  164.                 String uid = user.getUid();
  165.  
  166.                 //storage reference to store the profile pictures of user
  167.                 StorageReference reference = imageReference.child("profile_images").child(uid + ".jpg");
  168.                 reference.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
  169.                     @Override
  170.                     public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
  171.                         if (task.isSuccessful()) {
  172.  
  173.                             //if the image is successfully uploaded store it in database
  174.                             String imageUrl = String.valueOf(task.getResult().getDownloadUrl());
  175.                             accountDb.child("image").setValue(imageUrl).addOnCompleteListener(new OnCompleteListener<Void>() {
  176.                                 @Override
  177.                                 public void onComplete(@NonNull Task<Void> task) {
  178.                                     if (task.isSuccessful()) {
  179.                                         dialog.dismiss();
  180.                                         Toast.makeText(AccountSettingsActivity.this,"Picture Updated Successfully", Toast.LENGTH_LONG).show();
  181.                                     }else{
  182.                                         dialog.dismiss();
  183.                                         Toast.makeText(AccountSettingsActivity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_LONG);
  184.                                     }
  185.                                 }
  186.                             });
  187.  
  188.                         }else{
  189.                             Toast.makeText(AccountSettingsActivity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_LONG).show();
  190.                             dialog.dismiss();
  191.                         }
  192.                     }
  193.                 });
  194.  
  195.             } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
  196.                 Exception error = result.getError();
  197.             }
  198.         }
  199.     }
  200.  
  201. }
Add Comment
Please, Sign In to add comment