Android Open Source - PlayTogether Signup Activity






From Project

Back to project page PlayTogether.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project PlayTogether listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rockylearnstodevelop.playtogether;
/*ww  w. ja v  a 2s .c o m*/
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

public class SignupActivity extends Activity {
  
  protected TextView mUsername;
  protected TextView mWeChatID;
  protected TextView mBirthday;
  protected TextView mPassword;
  
  protected ImageView mUserPhoto;
  protected RadioGroup mSexGroup;
  protected Button mSignupButton;
  
  protected static final int PHOTO_REQUEST_CODE = 1;
  
  //info to save the user's info
  private String userName;
  private String weChatID;
  private String gender;
  private String birthday;
  private String password;
  
  //file to save the photo
  private ParseFile mfile;
  protected Uri mediaUri;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_signup);
    
    mUsername = (TextView) findViewById(R.id.userName);
    mWeChatID = (TextView) findViewById(R.id.wechatId);
    mBirthday = (TextView) findViewById(R.id.birthday);
    mPassword = (TextView) findViewById(R.id.password);
    
    mUserPhoto = (ImageView) findViewById(R.id.userImage);
    mSexGroup = (RadioGroup) findViewById(R.id.sexGroup);
    mSignupButton = (Button) findViewById(R.id.signupButton);
    
    //choose an Image
    mUserPhoto.setOnClickListener(new View.OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
        choosePhotoIntent.setType("image/*");
        startActivityForResult(choosePhotoIntent, PHOTO_REQUEST_CODE);
      }
    });
     

    //select the birthday
    setBirthday();
    
    //sign up button onClickListener
    mSignupButton.setOnClickListener(new  View.OnClickListener() {
      
      @SuppressLint("NewApi")
      @Override
      public void onClick(View e) {  
        userName = mUsername.getText().toString();
        weChatID = mWeChatID.getText().toString();
        password = mPassword.getText().toString();
        birthday = mBirthday.getText().toString();
        
        userName = userName.trim();
        weChatID = weChatID.trim();        
        password = password.trim();
        birthday = birthday.trim();
    
        Log.d("wechatId", weChatID);
        Log.d("birthday", birthday);
      
          //Check if the name and password if empty
        if(userName.isEmpty() || password.isEmpty() || mfile == null){
          //warning dialog
          AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
          builder.setMessage(R.string.sign_up_error_message);
          builder.setTitle(R.string.sign_up_error_title);
          builder.setPositiveButton(android.R.string.ok, null);
          
          AlertDialog dialog = builder.create();
          dialog.show();
        }
        else{
          navigateToMain();
          
          // select the gender
          getGender();
          
          Log.d("Gender", gender);
          
          //create the new user
          ParseUser newUser = new ParseUser();
          newUser.setUsername(userName);
          newUser.setPassword(password);
          
          setProgressBarIndeterminateVisibility(true);
          newUser.signUpInBackground(new SignUpCallback() {
            
            @Override
            public void done(ParseException e) {
              setProgressBarIndeterminateVisibility(false);
              if(e == null){
                //Success!
                createUserInfo();
              }
              else{
                //error!
                AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
                builder.setMessage(e.getMessage());
                builder.setTitle(R.string.sign_up_error_title);
                builder.setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
              }
            }
          });
        }
      }
    });
  }

  
  private void getGender() {
    switch (mSexGroup.getCheckedRadioButtonId()) {
    case R.id.boyButton:
      gender = "boy";
      break;
    case R.id.girlButton:
      gender = "girl";
      break;
    } ;
  }
  
  public void setBirthday() {
    final Calendar myCalendar = Calendar.getInstance();

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.YEAR, year);
            updateLabel();
        }

        private void updateLabel() {
          String myFormat = "dd/MM/yyyy"; //In which you need put here
          SimpleDateFormat sdf = new SimpleDateFormat(myFormat);
          Log.d("date", sdf.format(myCalendar.getTime()));
          mBirthday.setText(sdf.format(myCalendar.getTime()));
          }
    };
    
    mBirthday.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new DatePickerDialog(SignupActivity.this, date, 
                    myCalendar.get(Calendar.DAY_OF_MONTH), 
                    myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.YEAR)).show();
            }
        });
  }

  private void createUserInfo() {
    //get the current user
    String currentUserId = ParseUser.getCurrentUser().getObjectId();
    
    //save the photo and current UserId
    //Create Parse Object message
    ParseObject userInfo = new ParseObject(ParseConstants.CLASS_USERINFO);
    userInfo.put(ParseConstants.KEY_USERNAME, userName);
    userInfo.put(ParseConstants.KEY_WECHATID, weChatID);
    userInfo.put(ParseConstants.KEY_BIRTHDAY, birthday);
    userInfo.put(ParseConstants.KEY_GENDER, gender);
    if(mfile != null){
      userInfo.put("file", mfile);
      userInfo.put("userId", currentUserId);
    }else{
      //upload a default image for user's photo. Error! need to fix
      
      Uri defaultUri = Uri.parse("android.resource://" + SignupActivity.this.getPackageName() + "/drawable/"+"appicon_72");
      //upload the file that choose
      byte[] fileBytes = FileHelper.getByteArrayFromFile(SignupActivity.this, defaultUri);
      
      if(fileBytes != null){
        //reduce the image size 
        fileBytes = FileHelper.reduceImageForUpload(fileBytes);
        String fileName = FileHelper.getFileName(SignupActivity.this, defaultUri, "image");
        mfile = new ParseFile(fileName, fileBytes);
      }
      else{
        Toast.makeText(SignupActivity.this, "No file", Toast.LENGTH_LONG).show();
      }  
      
      userInfo.put("file", mfile);
    }
    
    userInfo.saveInBackground(new SaveCallback() {
      
      @Override
      public void done(ParseException e) {
        if(e != null){
          Log.e("SignUp", e.getMessage());
        }
        
      }
    });
    
    
    //
  }


  private void navigateToMain() {
    //start the main activity
    Intent intent = new Intent(SignupActivity.this, MainActivity.class);
    intent.putExtra(ParseConstants.KEY_BIRTHDAY, birthday);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.signup, menu);
    return true;
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
      if(requestCode == PHOTO_REQUEST_CODE){
        if(data != null){
          mediaUri = data.getData();
          mUserPhoto.setImageURI(mediaUri);
          
          //upload the file that choose
          byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mediaUri);
          
          if(fileBytes != null){
            //reduce the image size 
            fileBytes = FileHelper.reduceImageForUpload(fileBytes);
            String fileName = FileHelper.getFileName(this, mediaUri, "image");
            mfile = new ParseFile(fileName, fileBytes);
          }
          else{
            throw new RuntimeException();
          }
        }
      }else{
        //
      }
    }
    else{
      //
    }
  }
}




Java Source Code List

com.rockylearnstodevelop.playtogether.ActivitiesFragment.java
com.rockylearnstodevelop.playtogether.ActivityAdapter.java
com.rockylearnstodevelop.playtogether.FileHelper.java
com.rockylearnstodevelop.playtogether.ImageResizer.java
com.rockylearnstodevelop.playtogether.LoginActivity.java
com.rockylearnstodevelop.playtogether.MainActivity.java
com.rockylearnstodevelop.playtogether.MatchActivity.java
com.rockylearnstodevelop.playtogether.NoMatchActivity.java
com.rockylearnstodevelop.playtogether.ParseConstants.java
com.rockylearnstodevelop.playtogether.PlayWithMe2Application.java
com.rockylearnstodevelop.playtogether.RequestAdapter.java
com.rockylearnstodevelop.playtogether.RequestFragment.java
com.rockylearnstodevelop.playtogether.SectionsPagerAdapter.java
com.rockylearnstodevelop.playtogether.SignupActivity.java
com.rockylearnstodevelop.playtogether.UserInfoActivity.java
com.rockylearnstodevelop.playtogether.WannaPlayFragment.java