Guest User

Untitled

a guest
Mar 7th, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.67 KB | None | 0 0
  1. package enkay.avbio.Activities;
  2.  
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.database.Cursor;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.provider.MediaStore;
  11. import android.support.design.widget.Snackbar;
  12. import android.support.design.widget.TextInputEditText;
  13. import android.support.design.widget.TextInputLayout;
  14. import android.support.v4.app.ActivityCompat;
  15. import android.support.v4.widget.NestedScrollView;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.os.Bundle;
  18. import android.support.v7.widget.AppCompatButton;
  19. import android.support.v7.widget.AppCompatImageView;
  20. import android.support.v7.widget.AppCompatTextView;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.widget.ImageView;
  24. import android.widget.Toast;
  25.  
  26. import java.io.ByteArrayOutputStream;
  27.  
  28. import enkay.avbio.Helper.InputValidation;
  29. import enkay.avbio.Manifest;
  30. import enkay.avbio.Model.User;
  31.  
  32. import enkay.avbio.R;
  33. import enkay.avbio.Sql.DatabaseHelper;
  34.  
  35. public class RegisterActivity extends AppCompatActivity  implements View.OnClickListener{
  36.  
  37.     private final AppCompatActivity activity = RegisterActivity.this;
  38.     private TextInputLayout textInputLayoutName;
  39.     private TextInputLayout textInputLayoutEmail;
  40.     private TextInputLayout TextInputLayoutPassword;
  41.     private TextInputLayout textInputLayoutConfirmPassword;
  42.     //private TextInputLayout textInputLayoutAddress;
  43.     private TextInputLayout textInputLayoutCompanyName;
  44.     private TextInputLayout textInputLayoutCompanyAddress;
  45.     private TextInputLayout textInputLayoutGstNo;
  46.     private TextInputLayout textInputLayoutPanNo;
  47.     private NestedScrollView nestedScrollView;
  48.  
  49.  
  50.  
  51.     private TextInputEditText textInputEditTextName;
  52.     private TextInputEditText textInputEditTextEmail;
  53.     private TextInputEditText textInputEditTextPassword;
  54.     private TextInputEditText textInputEditTextConfirmPassword;
  55.     //private TextInputEditText textInputEditTextAddress;
  56.     private TextInputEditText textInputEditTextCompanyName;
  57.     private TextInputEditText textInputEditTextCompanyAddress;
  58.     private TextInputEditText textInputEditTextGstNo;
  59.     private TextInputEditText textInputEditTextPanNo;
  60.     private ImageView img;
  61.  
  62.  
  63.  
  64.  
  65.  
  66.     private AppCompatButton appCompatButton;
  67.     private AppCompatTextView appCompatTextView;
  68.     private AppCompatTextView appCompatTextView2;
  69.  
  70.  
  71.     private InputValidation inputValidation;
  72.     private DatabaseHelper databaseHelper;
  73.     private User user;
  74.     private static int RESULT_LOAD_IMG = 1;
  75.     String imgDecodableString;
  76.  
  77.  
  78.  
  79.  
  80.     @Override
  81.     protected void onCreate(Bundle savedInstanceState) {
  82.         super.onCreate(savedInstanceState);
  83.         setContentView(R.layout.activity_register);
  84.  
  85.         initViews();
  86.         initListeners();
  87.         initObjects();
  88.     }
  89.  
  90.  
  91.     private void initViews() {
  92.         textInputLayoutName = (TextInputLayout) findViewById(R.id.TextInputLayoutName);
  93.         textInputLayoutEmail = (TextInputLayout) findViewById(R.id.TextInputLayoutEmail);
  94.         TextInputLayoutPassword = (TextInputLayout) findViewById(R.id.TextInputLayoutPassword);
  95.         textInputLayoutConfirmPassword = (TextInputLayout) findViewById(R.id.TextInputLayoutConfirmPassword);
  96.        // textInputLayoutAddress = (TextInputLayout) findViewById(R.id.TextInputLayoutAddress);
  97.         textInputLayoutCompanyName=(TextInputLayout)findViewById(R.id.TextInputLayoutCompanyName);
  98.         textInputLayoutCompanyAddress=(TextInputLayout)findViewById(R.id.TextInputLayoutCompanyAddress);
  99.         textInputLayoutGstNo=(TextInputLayout)findViewById(R.id.TextInputLayoutGstNo);
  100.         textInputLayoutPanNo=(TextInputLayout)findViewById(R.id.TextInputLayoutPanNo);
  101.  
  102.  
  103.         textInputEditTextName = (TextInputEditText) findViewById(R.id.TextInputEditTextName);
  104.         textInputEditTextEmail = (TextInputEditText) findViewById(R.id.TextInputEditTextEmail);
  105.         textInputEditTextPassword = (TextInputEditText) findViewById(R.id.TextInputEditTextPassword);
  106.         textInputEditTextConfirmPassword = (TextInputEditText) findViewById(R.id.TextInputEditTextConfirmPassword);
  107.        // textInputEditTextAddress = (TextInputEditText) findViewById(R.id.TextInputEditTextAddress);
  108.         textInputEditTextCompanyName = (TextInputEditText) findViewById(R.id.TextInputEditTextCompanyName);
  109.         textInputEditTextCompanyAddress = (TextInputEditText) findViewById(R.id.TextInputEditTextCompanyAddress);
  110.         textInputEditTextGstNo=(TextInputEditText)findViewById(R.id.TextInputEditTextGstNo);
  111.         textInputEditTextPanNo=(TextInputEditText)findViewById(R.id.TextInputEditTextPanNo);
  112.  
  113.  
  114.  
  115.  
  116.         appCompatTextView=(AppCompatTextView)findViewById(R.id.txt) ;
  117.         nestedScrollView=(NestedScrollView)findViewById(R.id.newscroll);
  118.         appCompatButton=(AppCompatButton)findViewById(R.id.appCompatButtonregister);
  119.         appCompatTextView2=(AppCompatTextView)findViewById(R.id.textViewLinkLogin);
  120.         appCompatTextView2.setOnClickListener(new View.OnClickListener() {
  121.             @Override
  122.             public void onClick(View v)
  123.             {
  124.                 Intent mv=new Intent(getApplicationContext(),LoginActivity.class);
  125.                 startActivity(mv);
  126.             }
  127.         });
  128.  
  129.  
  130.     }
  131.     private void initListeners()
  132.     {
  133.         appCompatButton.setOnClickListener(this);
  134.         appCompatTextView.setOnClickListener(this);
  135.     }
  136.     private void initObjects()
  137.     {
  138.         inputValidation = new InputValidation(activity);
  139.         databaseHelper = new DatabaseHelper(activity);
  140.         user=new User();
  141.     }
  142.     @Override
  143.     public void onClick(View view)
  144.     {
  145.         switch(view.getId())
  146.         {
  147.             case R.id.appCompatButtonregister:
  148.                 postDataToSQlite();
  149.                 break;
  150.             case R.id.txt:
  151.                 finish();
  152.  
  153.                 break;
  154.         }
  155.     }
  156.     private void postDataToSQlite()
  157.     {
  158.         if (!inputValidation.isInputEditTextFilled(textInputEditTextName,textInputLayoutName,getString(R.string.error_message_name)))
  159.         {
  160.             return;
  161.         }
  162.         if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail,textInputLayoutEmail,getString(R.string.error_message_email)))
  163.         {
  164.             return;
  165.         }
  166.         if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail,textInputLayoutEmail,getString(R.string.error_message_email)))
  167.         {
  168.             return;
  169.         }
  170.         if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword,textInputLayoutConfirmPassword,getString(R.string.error_message_password)))
  171.         {
  172.             return;
  173.         }
  174.        // if (!inputValidation.isInputEditTextAdress(textInputEditTextAddress,textInputLayoutAddress,getString(R.string.error_valid_address)))
  175.        // {
  176.            // return;
  177.        // }
  178.         if (!inputValidation.isInputEditTextCompanyName(textInputEditTextCompanyName,textInputLayoutCompanyName,getString(R.string.error_valid_Company_name)))
  179.         {
  180.             return;
  181.         }
  182.         if (!inputValidation.isInputEditTextCompanyAddress(textInputEditTextCompanyAddress,textInputLayoutCompanyAddress,getString(R.string.error_valid_Company_address)))
  183.         {
  184.             return;
  185.         }
  186.         if (!inputValidation.isInputEditTextGstNo(textInputEditTextGstNo,textInputLayoutGstNo,getString(R.string.error_valid_gst_no)))
  187.         {
  188.             return;
  189.         }
  190.         if (!inputValidation.isInputEditTextPanNo(textInputEditTextPanNo,textInputLayoutPanNo,getString(R.string.error_valid_pan_no)))
  191.         {
  192.             return;
  193.         }
  194.         if (!inputValidation.isInputEditTextMatches(textInputEditTextPassword,textInputEditTextConfirmPassword,textInputLayoutConfirmPassword,getString(R.string.error_password_match)))
  195.         {
  196.             return;
  197.         }
  198.         if (!databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim())){
  199.             user.setName(textInputEditTextName.getText().toString().trim());
  200.             user.setEmail(textInputEditTextEmail.getText().toString().trim());
  201.             user.setPasswod(textInputEditTextPassword.getText().toString().trim());
  202.            // user.setAddress(textInputEditTextAddress.getText().toString());
  203.             user.setCompanyName(textInputEditTextCompanyName.getText().toString().trim());
  204.             user.setCompanyAddress(textInputEditTextCompanyAddress.getText().toString().trim());
  205.  
  206.             user.setGstNo(textInputEditTextGstNo.getText().toString().trim());
  207.             user.setPanNo(textInputEditTextPanNo.getText().toString().trim());
  208.             user.setImageByte(imageData);
  209.  
  210.  
  211.  
  212.             databaseHelper.addUser(user);
  213.  
  214.  
  215.             Snackbar.make(nestedScrollView,getString(R.string.success_message),Snackbar.LENGTH_LONG);
  216.             emptyInputEditText();
  217.  
  218.         }else
  219.         {
  220.             Snackbar.make(nestedScrollView,getString(R.string.text_email_exists),Snackbar.LENGTH_LONG);
  221.         }
  222.  
  223.     }
  224.     private void emptyInputEditText()
  225.     {
  226.         textInputEditTextName.setText(null);
  227.         textInputEditTextEmail.setText(null);
  228.         textInputEditTextPassword.setText(null);
  229.         textInputEditTextConfirmPassword.setText(null);
  230.         //textInputEditTextAddress.setText(null);
  231.         textInputEditTextCompanyName.setText(null);
  232.         textInputEditTextCompanyAddress.setText(null);
  233.         textInputEditTextGstNo.setText(null);
  234.         textInputEditTextPanNo.setText(null);
  235.  
  236.     }
  237.  
  238.  
  239.     byte[] imageData;
  240.  
  241.     public void ChangeLogo(View view)
  242.     {
  243.  
  244. // Create intent to Open Image applications like Gallery, Google Photos
  245.         Intent galleryIntent=new Intent(Intent.ACTION_PICK,
  246.                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  247.         // Start the Intent
  248.  
  249.         if(isStoragePermissionGranted())
  250.             startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
  251.         else {
  252.             //Toast.makeText(activity, "Permission not granted", Toast.LENGTH_SHORT).show();
  253.  
  254.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  255.                 ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 101);
  256.             }
  257.  
  258.         }
  259.  
  260.     }
  261.     @Override
  262.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  263.         super.onActivityResult(requestCode, resultCode, data);
  264.         // When an Image is picked
  265.         if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
  266.                 && null != data) {
  267.             // Get the Image from data
  268.  
  269.             pickImage(data);
  270.  
  271.         }
  272.  
  273.  
  274.  
  275.  
  276.         else {
  277.             Toast.makeText(this, "You haven't picked Image",
  278.                     Toast.LENGTH_LONG).show();
  279.         }
  280.  
  281.  
  282.     }
  283.  
  284.  
  285.     @Override
  286.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  287.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  288.         if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
  289.             Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
  290.             //resume tasks needing this permission
  291.  
  292.             Intent galleryIntent=new Intent(Intent.ACTION_PICK,
  293.                     MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  294.  
  295.             startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
  296.  
  297.         }
  298.  
  299.         else
  300.             Toast.makeText(activity, "Permission not granted", Toast.LENGTH_SHORT).show();
  301.     }
  302.  
  303.     private void pickImage(Intent data){
  304.  
  305.         try {
  306.  
  307.             Uri selectedImage = data.getData();
  308.             String[] filePathColumn = {MediaStore.Images.Media.DATA};
  309.  
  310.             // Get the cursor
  311.             Cursor cursor = getContentResolver().query(selectedImage,
  312.                     filePathColumn, null, null, null);
  313.             // Move to first row
  314.             cursor.moveToFirst();
  315.  
  316.             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  317.             imgDecodableString = cursor.getString(columnIndex);
  318.             cursor.close();
  319.             AppCompatImageView imgView = (AppCompatImageView) findViewById(R.id.img);
  320.             // Set the Image in ImageView after decoding the String
  321.             Bitmap bitmap = BitmapFactory
  322.                     .decodeFile(imgDecodableString);
  323.             imgView.setImageBitmap(bitmap);
  324.  
  325.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  326.             bitmap.compress(Bitmap.CompressFormat.PNG, 50, bout);
  327.             imageData = bout.toByteArray();
  328.         }
  329.         catch (Exception e) {
  330.             Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
  331.                     .show();
  332.             Log.e("TAG", "onActivityResult: error = "+e.toString());
  333.             e.printStackTrace();
  334.         }
  335.  
  336.     }
  337.  
  338.  
  339.     String TAG = RegisterActivity.class.getCanonicalName();
  340.  
  341.  
  342.     public  boolean isStoragePermissionGranted() {
  343.         if (Build.VERSION.SDK_INT >= 23) {
  344.             if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
  345.                     == PackageManager.PERMISSION_GRANTED) {
  346.                 Log.v(TAG,"Permission is granted");
  347.                 return true;
  348.             } else {
  349.  
  350.                 Log.v(TAG,"Permission is revoked");
  351.                 //  ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
  352.                 return false;
  353.             }
  354.         }
  355.         else { //permission is automatically granted on sdk<23 upon installation
  356.             Log.v(TAG,"Permission is granted");
  357.             return true;
  358.         }
  359.     }
  360.  
  361. }
Add Comment
Please, Sign In to add comment