Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RegisterActivity extends Activity implements OnClickListener {
- private static final int YOUR_SELECTED_PICTURE_REQUEST_CODE = 0;
- private static Uri outputFileUri;
- private static String imagePath;
- private Uri selectedImageUri;
- Handler guiThread;
- Context context;
- ProgressDialog progressDialog;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- EditText password = (EditText) findViewById( R.id.register_password_text );
- password.setTypeface( Typeface.DEFAULT );
- EditText reppassword = (EditText) findViewById( R.id.register_repeatpassword_text );
- reppassword.setTypeface( Typeface.DEFAULT );
- View registerButton = findViewById(R.id.register_register_button);
- registerButton.setOnClickListener(this);
- ImageButton Picture_Button = (ImageButton) findViewById(R.id.picture_button);
- Picture_Button.setOnClickListener(this);
- guiThread = new Handler();
- context = this;
- progressDialog = new ProgressDialog(this);
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.auto_diary, menu);
- return true;
- }
- private void GetImage(){
- final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "AutoDiary" + File.separator);
- root.mkdirs(); //pravim folder za slike
- final String fname = "AutoDiary_" + java.util.UUID.randomUUID() + ".jpg";
- final File sdImageMainDirectory = new File(root,fname);
- imagePath = sdImageMainDirectory.getAbsolutePath();
- outputFileUri = Uri.fromFile(sdImageMainDirectory);
- /*sendBroadcast(new Intent(
- Intent.ACTION_MEDIA_MOUNTED,
- Uri.parse("file://" + Environment.getExternalStorageDirectory())));
- da ucita u galeriju, treba u neki on create*/
- // lista aplikacija za kamere
- final List<Intent> cameraIntents = new ArrayList<Intent>();
- final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- final PackageManager packageManager = getPackageManager();
- final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
- for (ResolveInfo res : listCam){
- final String packageName = res.activityInfo.packageName;
- final Intent intent = new Intent(captureIntent);
- intent.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
- intent.setPackage(packageName);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
- cameraIntents.add(intent);
- }
- //file System
- final Intent galleryIntent = new Intent();
- galleryIntent.setType("image/*");
- galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
- //chooser of filesystem options
- final Intent chooserIntent = Intent.createChooser(galleryIntent,"Select Application");
- chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
- startActivityForResult(chooserIntent, YOUR_SELECTED_PICTURE_REQUEST_CODE);
- }
- @Override
- public void onClick(View v) {
- switch(v.getId())
- {
- case R.id.register_register_button:
- RegisterTask registerTask = new RegisterTask(context);
- registerTask.execute();
- break;
- case R.id.picture_button:
- GetImage();
- break;
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
- ImageButton Picture_Button = (ImageButton) findViewById(R.id.picture_button);
- if (resultCode == RESULT_OK)
- {
- if (requestCode == YOUR_SELECTED_PICTURE_REQUEST_CODE)
- {
- final boolean isCamera;
- if (data == null)
- {
- isCamera = true;
- }
- else
- {
- final String action = data.getAction();
- if(action == null)
- {
- isCamera = false;
- }
- else
- {
- isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- }
- }
- if(isCamera)
- {
- Drawable image = Drawable.createFromPath(imagePath);
- Picture_Button.setImageDrawable(image);
- }
- else
- {
- selectedImageUri = data == null ? null : data.getData();
- Picture_Button.setImageURI(selectedImageUri);
- }
- }
- }
- }
- private static boolean isValidEmail(String email) {
- boolean isValid = false;
- String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
- CharSequence inputStr = email;
- Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(inputStr);
- if (matcher.matches()) {
- isValid = true;
- }
- return isValid;
- }
- private static String md5(String s) {
- MessageDigest m = null;
- try {
- m = MessageDigest.getInstance("MD5");
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- m.update(s.getBytes(),0,s.length());
- String hash = new BigInteger(1, m.digest()).toString(16);
- return hash;
- }
- private void guiNotifyUser(final String message) {
- guiThread.post(new Runnable() {
- public void run() {
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void guiProgressDialog(final boolean start) {
- guiThread.post(new Runnable() {
- public void run() {
- if(start)
- progressDialog.show();
- else
- progressDialog.dismiss();
- }
- });
- }
- class RegisterTask extends AsyncTask<String, Void, String>{
- Context context;
- private RegisterTask(Context context){
- this.context = context.getApplicationContext();
- }
- @Override
- protected String doInBackground(String... params) {
- String message = null;
- //ExecutorService transThread = Executors.newSingleThreadExecutor();
- EditText username = (EditText) findViewById( R.id.register_username_text );
- EditText password = (EditText) findViewById( R.id.register_password_text );
- EditText passwordrtp = (EditText) findViewById( R.id.register_repeatpassword_text );
- EditText email = (EditText) findViewById( R.id.register_email_text );
- EditText firstname = (EditText) findViewById( R.id.register_firstname_text );
- EditText lastname = (EditText) findViewById( R.id.register_lastname_text );
- final String user = username.getText().toString();
- final String pass = password.getText().toString();
- final String passrtp = passwordrtp.getText().toString();
- final String mail = email.getText().toString();
- final String fname = firstname.getText().toString();
- final String lname = lastname.getText().toString();
- guiProgressDialog(true);
- if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
- else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
- else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
- else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
- else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
- else {
- try {
- message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
- guiNotifyUser(message);//prosla vodi na sledeci activity "Succesfull registration"
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- guiProgressDialog(false);
- return message;
- }
- @Override
- protected void onPostExecute(String result)
- {
- super.onPostExecute(result);
- //if (result == "Successful registration!")
- //String i;
- //i = "da";
- context.startActivity(new Intent(context,LoginActivity.class));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement