Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package com.ttsl.miguel.tejo;
  2.  
  3. import android.Manifest;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.net.Uri;
  7. import android.os.Build;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.Nullable;
  10. import android.support.v4.app.ActivityCompat;
  11. import android.support.v4.content.ContextCompat;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.ImageView;
  18. import android.widget.Toast;
  19.  
  20. import com.github.clans.fab.FloatingActionButton;
  21. import com.google.android.gms.tasks.OnCompleteListener;
  22. import com.google.android.gms.tasks.OnSuccessListener;
  23. import com.google.android.gms.tasks.Task;
  24. import com.google.firebase.auth.AuthResult;
  25. import com.google.firebase.auth.FirebaseAuth;
  26. import com.google.firebase.auth.FirebaseUser;
  27. import com.google.firebase.auth.UserProfileChangeRequest;
  28. import com.google.firebase.storage.FirebaseStorage;
  29. import com.google.firebase.storage.StorageReference;
  30. import com.google.firebase.storage.UploadTask;
  31.  
  32. public class RegisterActivity extends AppCompatActivity {
  33.  
  34. ImageView ImgUserFoto;
  35. static int PReqCode = 1;
  36. static int REQUESCODE = 1;
  37. Uri pickedImgUri;
  38.  
  39. private EditText userEmail, userPassword, userPassword2, userName;
  40. private Button regBtn;
  41.  
  42. private FirebaseAuth mAuth;
  43.  
  44. FloatingActionButton btnLogin, btnRecuperar;
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_register);
  50.  
  51. btnLogin = (FloatingActionButton) findViewById(R.id.floating_entrar);
  52. btnRecuperar = (FloatingActionButton) findViewById(R.id.floating_senha1);
  53.  
  54. btnLogin.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57.  
  58. Intent mIntent = new Intent(RegisterActivity.this, LoginActivity.class);
  59. startActivity(mIntent);
  60. finish();
  61. }
  62. });
  63.  
  64. btnRecuperar.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67.  
  68. Intent mIntent = new Intent(RegisterActivity.this, SenhaActivity.class);
  69. startActivity(mIntent);
  70. finish();
  71. }
  72. });
  73.  
  74.  
  75. //Metodo para registar conta
  76.  
  77. userEmail = findViewById(R.id.email_reg);
  78. userPassword = findViewById(R.id.senha_reg);
  79. userPassword2 = findViewById(R.id.senha_conf);
  80. userName = findViewById(R.id.nome_reg);
  81. regBtn = findViewById(R.id.btn_registar);
  82. ImgUserFoto = findViewById(R.id.perfil_foto_reg);
  83.  
  84.  
  85. mAuth = FirebaseAuth.getInstance();
  86.  
  87.  
  88. regBtn.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91.  
  92. final String email = userEmail.getText().toString();
  93. final String password = userPassword.getText().toString();
  94. final String password2 = userPassword2.getText().toString();
  95. final String nome = userName.getText().toString();
  96.  
  97. if (email.isEmpty() || nome.isEmpty() || password.isEmpty() || !password.equals(password2) || pickedImgUri == null) {
  98.  
  99. //Se alguma coisa ficar errada
  100. //Vai aparecer uma mensagem de erro
  101. showMessage("Por favor verifique todos os campos!");
  102.  
  103. }
  104. else {
  105. //Se tudo estiver certo e se todos os campos estão preenchidos
  106. //vai criar a conta do usuario se o email é válido...
  107.  
  108. CreateUserAccount(email,nome,password);
  109. }
  110. }
  111. });
  112.  
  113.  
  114. ImgUserFoto.setOnClickListener(new View.OnClickListener() {
  115. @Override
  116. public void onClick(View v) {
  117.  
  118. if (Build.VERSION.SDK_INT >= 22) {
  119.  
  120. checkAndRequestForPermission();
  121. }
  122. else {
  123.  
  124. openGallery();
  125. }
  126. }
  127. });
  128.  
  129. }
  130.  
  131. private void CreateUserAccount(String email, final String nome, String password) {
  132.  
  133. mAuth.createUserWithEmailAndPassword(email,password)
  134. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  135. @Override
  136. public void onComplete(@NonNull Task<AuthResult> task) {
  137. if (task.isSuccessful()) {
  138.  
  139. //Conta do usuario criada com sucesso!
  140. showMessage("Conta criada!");
  141. //Depois de ser criada a conta precisamos de fazer update da foto
  142. //de perfil e nome
  143. updateUserInfo( nome ,pickedImgUri,mAuth.getCurrentUser());
  144.  
  145. }
  146. else {
  147.  
  148. //Falha ao criar conta
  149. showMessage("Erro ao criar conta!" + task.getException().getMessage());
  150. }
  151. }
  152. });
  153.  
  154.  
  155. }
  156.  
  157. //update foto de perfil e nome
  158. private void updateUserInfo(final String nome, Uri pickedImgUri, final FirebaseUser currentUser) {
  159.  
  160. //upload foto para firebase storage
  161. StorageReference mStorage = FirebaseStorage.getInstance().getReference().child("users_photos");
  162. final StorageReference imageFilePath = mStorage.child(pickedImgUri.getLastPathSegment());
  163. imageFilePath.putFile(pickedImgUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  164. @Override
  165. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  166.  
  167. //imagem uploaded com sucesso!
  168. //Agora temos o url da imagem
  169.  
  170. imageFilePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
  171. @Override
  172. public void onSuccess(Uri uri) {
  173.  
  174. //Uri contem url da imagem do usuario
  175.  
  176. UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
  177. .setDisplayName(nome)
  178. .setPhotoUri(uri)
  179. .build();
  180. currentUser.updateProfile(profileUpdate)
  181. .addOnCompleteListener(new OnCompleteListener<Void>() {
  182. @Override
  183. public void onComplete(@NonNull Task<Void> task) {
  184.  
  185. if (task.isSuccessful()) {
  186. //Informaçoes do usuario updated com sucesso!
  187. showMessage("Registro Completo!");
  188. updateUI();
  189. }
  190. }
  191. });
  192.  
  193. }
  194. });
  195.  
  196. }
  197. });
  198.  
  199.  
  200.  
  201. }
  202.  
  203. private void updateUI() {
  204.  
  205. Intent Main2Activity = new Intent(getApplicationContext(), DashboardActivity.class);
  206. startActivity(Main2Activity);
  207. finish();
  208.  
  209. }
  210.  
  211. private void showMessage(String message) {
  212.  
  213. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  214.  
  215.  
  216.  
  217. }
  218.  
  219. private void openGallery() {
  220. //Abre a galeria e espera para o usuario escolher uma foto!
  221.  
  222. Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
  223. galleryIntent.setType("image/*");
  224. startActivityForResult(galleryIntent, REQUESCODE);
  225. }
  226.  
  227. private void checkAndRequestForPermission() {
  228.  
  229. if (ContextCompat.checkSelfPermission(RegisterActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  230.  
  231. showMessage("amkjdfdf");
  232.  
  233. if (ActivityCompat.shouldShowRequestPermissionRationale(RegisterActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  234.  
  235. showMessage("Por favor aceite para permissão requerida");
  236. }
  237. else {
  238. ActivityCompat.requestPermissions(RegisterActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PReqCode);
  239. }
  240. }
  241. else {
  242. openGallery();
  243. }
  244. }
  245.  
  246. @Override
  247. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  248. super.onActivityResult(requestCode, resultCode, data);
  249.  
  250. if (resultCode == RESULT_OK && requestCode == REQUESCODE && data != null) {
  251.  
  252. //Escolheu uma imagem com sucesso!
  253. pickedImgUri = data.getData();
  254. ImgUserFoto.setImageURI(pickedImgUri);
  255. }
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement