Advertisement
Guest User

diary

a guest
Jun 22nd, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. public class RegisterActivity extends Activity implements OnClickListener {
  2.     private static final int YOUR_SELECTED_PICTURE_REQUEST_CODE = 0;
  3.     private static Uri outputFileUri;
  4.     private static String imagePath;
  5.     private Uri selectedImageUri;
  6.     Handler guiThread;
  7.     Context context;
  8.     ProgressDialog progressDialog;
  9.    
  10.    
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_register);
  14.        
  15.         EditText password = (EditText) findViewById( R.id.register_password_text );
  16.         password.setTypeface( Typeface.DEFAULT );
  17.         EditText reppassword = (EditText) findViewById( R.id.register_repeatpassword_text );
  18.         reppassword.setTypeface( Typeface.DEFAULT );
  19.         View registerButton = findViewById(R.id.register_register_button);
  20.         registerButton.setOnClickListener(this);
  21.         ImageButton Picture_Button = (ImageButton) findViewById(R.id.picture_button);
  22.         Picture_Button.setOnClickListener(this);
  23.        
  24.  
  25.         guiThread = new Handler();
  26.         context = this;
  27.         progressDialog = new ProgressDialog(this);
  28.     }
  29.     public boolean onCreateOptionsMenu(Menu menu) {
  30.         // Inflate the menu; this adds items to the action bar if it is present.
  31.         getMenuInflater().inflate(R.menu.auto_diary, menu);
  32.         return true;
  33.     }
  34.    
  35.     private void GetImage(){
  36.        
  37.         final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "AutoDiary" + File.separator);
  38.         root.mkdirs(); //pravim folder za slike
  39.         final String fname = "AutoDiary_" + java.util.UUID.randomUUID() + ".jpg";
  40.         final File sdImageMainDirectory = new File(root,fname);
  41.         imagePath = sdImageMainDirectory.getAbsolutePath();
  42.         outputFileUri = Uri.fromFile(sdImageMainDirectory);
  43.         /*sendBroadcast(new Intent(
  44.                 Intent.ACTION_MEDIA_MOUNTED,
  45.                             Uri.parse("file://" + Environment.getExternalStorageDirectory())));
  46.         da ucita u galeriju, treba u neki on create*/
  47.         // lista aplikacija za kamere
  48.         final List<Intent> cameraIntents = new ArrayList<Intent>();
  49.         final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  50.         final PackageManager packageManager = getPackageManager();
  51.         final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
  52.         for (ResolveInfo res : listCam){
  53.             final String packageName = res.activityInfo.packageName;
  54.             final Intent intent = new Intent(captureIntent);
  55.             intent.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
  56.             intent.setPackage(packageName);
  57.             intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  58.             cameraIntents.add(intent);
  59.         }
  60.         //file System
  61.         final Intent galleryIntent = new Intent();
  62.         galleryIntent.setType("image/*");
  63.         galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
  64.         //chooser of filesystem options
  65.         final Intent chooserIntent = Intent.createChooser(galleryIntent,"Select Application");
  66.         chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
  67.    
  68.         startActivityForResult(chooserIntent, YOUR_SELECTED_PICTURE_REQUEST_CODE);
  69.        
  70.     }
  71.     @Override
  72.     public void onClick(View v) {
  73.        
  74.         switch(v.getId())
  75.         {
  76.         case R.id.register_register_button:
  77.            
  78.             RegisterTask registerTask = new RegisterTask(context);
  79.             registerTask.execute();
  80.            
  81.             break;
  82.            
  83.         case R.id.picture_button:
  84.             GetImage();
  85.         break;
  86.         }
  87.        
  88.     }
  89.     @Override
  90.     protected void onActivityResult(int requestCode, int resultCode, Intent data){
  91.         ImageButton Picture_Button = (ImageButton) findViewById(R.id.picture_button);
  92.         if (resultCode == RESULT_OK)
  93.         {
  94.             if (requestCode == YOUR_SELECTED_PICTURE_REQUEST_CODE)
  95.             {
  96.                 final boolean isCamera;
  97.                 if (data == null)
  98.                 {
  99.                     isCamera = true;
  100.                 }
  101.                 else
  102.                 {
  103.                     final String action = data.getAction();
  104.                     if(action == null)
  105.                         {
  106.                             isCamera = false;
  107.                         }
  108.                     else
  109.                         {
  110.                             isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  111.                         }
  112.                 }
  113.                
  114.                 if(isCamera)
  115.                 {
  116.                     Drawable image = Drawable.createFromPath(imagePath);
  117.                     Picture_Button.setImageDrawable(image);
  118.                    
  119.            
  120.                 }
  121.                 else
  122.                 {
  123.                     selectedImageUri = data == null ? null : data.getData();
  124.                     Picture_Button.setImageURI(selectedImageUri);
  125.                 }
  126.             }
  127.         }
  128.        
  129.        
  130.     }
  131.    
  132.  
  133.  
  134.     private static boolean isValidEmail(String email) {
  135.         boolean isValid = false;
  136.  
  137.         String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
  138.         CharSequence inputStr = email;
  139.  
  140.         Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
  141.         Matcher matcher = pattern.matcher(inputStr);
  142.         if (matcher.matches()) {
  143.             isValid = true;
  144.         }
  145.         return isValid;
  146.     }
  147.    
  148.     private static String md5(String s) {
  149.         MessageDigest m = null;
  150.  
  151.         try {
  152.                 m = MessageDigest.getInstance("MD5");
  153.         } catch (NoSuchAlgorithmException e) {
  154.                 e.printStackTrace();
  155.         }
  156.  
  157.         m.update(s.getBytes(),0,s.length());
  158.         String hash = new BigInteger(1, m.digest()).toString(16);
  159.         return hash;
  160. }
  161.    
  162.     private void guiNotifyUser(final String message) {
  163.         guiThread.post(new Runnable() {
  164.             public void run() {
  165.                 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  166.             }
  167.         });
  168.     }
  169.    
  170.     private void guiProgressDialog(final boolean start) {
  171.         guiThread.post(new Runnable() {
  172.             public void run() {
  173.                 if(start)
  174.                     progressDialog.show();
  175.                 else
  176.                     progressDialog.dismiss();
  177.             }
  178.         });
  179.     }
  180.  
  181.     class RegisterTask extends AsyncTask<String, Void, String>{
  182.  
  183.         Context context;
  184.         private RegisterTask(Context context){
  185.             this.context = context.getApplicationContext();
  186.         }
  187.         @Override
  188.         protected String doInBackground(String... params) {
  189.             String message = null;
  190.             //ExecutorService transThread = Executors.newSingleThreadExecutor();
  191.             EditText username = (EditText) findViewById( R.id.register_username_text );
  192.             EditText password = (EditText) findViewById( R.id.register_password_text );
  193.             EditText passwordrtp = (EditText) findViewById( R.id.register_repeatpassword_text );
  194.             EditText email = (EditText) findViewById( R.id.register_email_text );
  195.             EditText firstname = (EditText) findViewById( R.id.register_firstname_text );
  196.             EditText lastname = (EditText) findViewById( R.id.register_lastname_text );
  197.             final String user = username.getText().toString();
  198.             final String pass = password.getText().toString();
  199.             final String passrtp = passwordrtp.getText().toString();
  200.             final String mail = email.getText().toString();
  201.             final String fname = firstname.getText().toString();
  202.             final String lname = lastname.getText().toString();
  203.            
  204.             guiProgressDialog(true);
  205.             if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
  206.             else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
  207.             else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
  208.             else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
  209.             else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
  210.             else {
  211.                 try {
  212.                     message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
  213.                     guiNotifyUser(message);//prosla vodi na sledeci activity "Succesfull registration"
  214.                    
  215.                 }
  216.                 catch(Exception e) {
  217.                     e.printStackTrace();
  218.                 }
  219.             }
  220.             guiProgressDialog(false);
  221.            
  222.            
  223.            
  224.             return message;
  225.         }
  226.        
  227.         @Override
  228.         protected void onPostExecute(String result)
  229.         {
  230.            
  231.             super.onPostExecute(result);
  232.            
  233.             //if (result == "Successful registration!")
  234.            
  235.                 //String i;
  236.                 //i = "da";
  237.                 context.startActivity(new Intent(context,LoginActivity.class));
  238.            
  239.            
  240.                
  241.         }
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement