Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.67 KB | None | 0 0
  1. package com.retinax.metoo.UI;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.net.URI;
  6. import java.util.Calendar;
  7. import java.util.TimeZone;
  8. import java.util.regex.Pattern;
  9.  
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.app.ProgressDialog;
  13. import android.content.ContentValues;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.content.pm.ActivityInfo;
  18. import android.content.res.Configuration;
  19. import android.database.Cursor;
  20. import android.graphics.Bitmap;
  21. import android.graphics.Color;
  22. import android.graphics.PixelFormat;
  23. import android.net.Uri;
  24. import android.os.Bundle;
  25. import android.os.Environment;
  26. import android.os.Handler;
  27. import android.os.Message;
  28. import android.provider.MediaStore;
  29. import android.provider.MediaStore.Images;
  30. import android.provider.MediaStore.Images.Media;
  31. import android.util.Log;
  32. import android.view.Gravity;
  33. import android.view.View;
  34. import android.view.Window;
  35. import android.view.WindowManager;
  36. import android.view.View.OnClickListener;
  37. import android.widget.Button;
  38. import android.widget.EditText;
  39. import android.widget.ImageView;
  40. import android.widget.Toast;
  41.  
  42.  
  43. import com.retinax.metoo.R;
  44. import com.retinax.metoo.API.Http;
  45. import com.retinax.metoo.API.sharedPreference;
  46.  
  47.  
  48. public class signup extends Activity {
  49.  
  50. protected static final String LOG_TAG = "LOGIN SCREEN";
  51.  
  52. String path , name;
  53. int USERNAME_ALREADY_EXISTS=5;
  54.  
  55. Uri currImageURI = null;
  56. ImageView iv;
  57. Http hp;
  58. EditText user , pass , cpass , email;
  59.  
  60. String username,password , email_string;
  61. private ProgressDialog progressDialog;
  62. Handler hd;
  63. /** Called when the activity is first created. */
  64. @Override
  65. public void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. requestWindowFeature(Window.FEATURE_NO_TITLE);
  68. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  69. setContentView(R.layout.signup);
  70.  
  71.  
  72. hp = new Http();
  73. // get username and password from SQLite database
  74. //getUserInfo();
  75. user =(EditText)findViewById(R.id.username_value);
  76. pass =(EditText)findViewById(R.id.password_value);
  77. cpass =(EditText)findViewById(R.id.cpassword_value);
  78. email = (EditText)findViewById(R.id.email_value);
  79.  
  80.  
  81.  
  82.  
  83. hd = new Handler(){
  84.  
  85. @Override
  86. public void dispatchMessage(Message msg) {
  87. // TODO Auto-generated method stub
  88. super.dispatchMessage(msg);
  89.  
  90. switch(msg.what)
  91. {
  92. case 0: { user.setHintTextColor(Color.RED);
  93. pass.setHintTextColor(Color.RED);
  94. email.setHintTextColor(Color.RED);
  95. cpass.setHintTextColor(Color.RED);
  96. Toast.makeText( getApplicationContext(),
  97.  
  98. "Fields in Red are Mandatory.",
  99.  
  100. Toast.LENGTH_SHORT ).show();
  101.  
  102. }
  103. break;
  104. case 1:{ progressDialog.dismiss();
  105. Toast.makeText( getApplicationContext(),
  106.  
  107. "User Created Successfully",
  108.  
  109. Toast.LENGTH_SHORT ).show();
  110.  
  111. signup.this.finish();}
  112. break;
  113. case 5 :{
  114.  
  115. progressDialog.dismiss();
  116. Toast.makeText( getApplicationContext(),
  117.  
  118. "Username already exists.",
  119.  
  120. Toast.LENGTH_SHORT ).show();
  121.  
  122. // signup.this.finish();
  123.  
  124.  
  125. }
  126.  
  127. case 6 :{
  128.  
  129. progressDialog.dismiss();
  130. Toast.makeText( getApplicationContext(),
  131.  
  132. "Email Already exists",
  133.  
  134. Toast.LENGTH_SHORT ).show();
  135.  
  136. // signup.this.finish();
  137.  
  138.  
  139. }
  140. break;
  141. case 7 :{
  142.  
  143. progressDialog.dismiss();
  144. Toast.makeText( getApplicationContext(),
  145.  
  146. "Confirm Password must match with Password.",
  147.  
  148. Toast.LENGTH_SHORT ).show();
  149.  
  150. // signup.this.finish();
  151.  
  152.  
  153. }
  154.  
  155. }
  156. }
  157.  
  158.  
  159.  
  160.  
  161. };
  162.  
  163.  
  164.  
  165.  
  166.  
  167. Button b1 , b2;
  168.  
  169. iv = (ImageView)findViewById(R.id.image);
  170. b1 = (Button)findViewById(R.id.signup);
  171. b1.setOnClickListener(new OnClickListener() {
  172.  
  173. public void onClick(View v) {
  174. // TODO Auto-generated method stub
  175.  
  176.  
  177.  
  178.  
  179. username = user.getEditableText().toString();
  180. password = pass.getEditableText().toString();
  181. email_string=email.getEditableText().toString();
  182.  
  183. if(username.contains("<")||username.contains(">")||username.contains("'")||username.contains("\""))
  184. {
  185.  
  186.  
  187. Toast.makeText(getApplicationContext(), "Invalid Username, Enter only alphanumeric value", Toast.LENGTH_SHORT).show();
  188. }
  189. else if(password.contains(" ")||username.contains(" "))
  190. {
  191. Toast.makeText(getApplicationContext(), "Spaces are not allowed", Toast.LENGTH_SHORT).show();
  192. }
  193.  
  194. // Log.e("username",""+username);
  195. else if(username=="")
  196. {
  197. Toast.makeText(getApplicationContext(), "Enter Username", Toast.LENGTH_SHORT).show();
  198.  
  199. }
  200.  
  201. else if(username.length()<5||username.length()>20)
  202. {
  203. Toast.makeText(getApplicationContext(), "Username must be 5 to 20 char long", Toast.LENGTH_SHORT).show();
  204.  
  205. }
  206.  
  207. else if(password=="")
  208. {
  209. Toast.makeText(getApplicationContext(), "Enter password", Toast.LENGTH_SHORT).show();
  210.  
  211. }
  212.  
  213.  
  214. else if(password.length()<5||password.length()>20)
  215. {
  216. Toast.makeText(getApplicationContext(), "Password must be 5 to 20 char log", Toast.LENGTH_SHORT).show();
  217.  
  218. }
  219. else
  220. signupAction();
  221.  
  222. // if(checkEmail(email_string))
  223. // {
  224. // signupAction();
  225. // }
  226. // else
  227. // {
  228. // Toast.makeText(getApplicationContext(), "Email incorrect", Toast.LENGTH_SHORT).show();
  229. // }
  230. //
  231.  
  232. // Intent myIntent = new Intent(signup.this, homeActivity.class);
  233. // startActivity(myIntent);
  234. }
  235.  
  236.  
  237. });
  238.  
  239.  
  240.  
  241.  
  242. // File imageDirectory = new File("1111.jpg");
  243. // if (!imageDirectory.isDirectory()) imageDirectory.mkdir();
  244. //
  245. // path = imageDirectory.toString().toLowerCase();
  246. //
  247. // File image = new File("1111.jpg");
  248. // if(image.exists())
  249. // {
  250. // image.delete();
  251. // }
  252.  
  253. b2 = (Button)findViewById(R.id.takephotobutton);
  254. b2.setOnClickListener(new OnClickListener() {
  255.  
  256. @Override
  257. public void onClick(View v) {
  258. // TODO Auto-generated method stub
  259. final CharSequence[] items = {"From Gallery", "From Camera" };
  260. AlertDialog.Builder builder = new AlertDialog.Builder(signup.this);
  261. builder.setTitle("Add Picture");
  262. builder.setItems(items, new DialogInterface.OnClickListener() {
  263.  
  264. // Click listener
  265.  
  266. public void onClick(DialogInterface dialog, int item) {
  267.  
  268. // Toast.makeText(getApplicationContext(), item +"", Toast.LENGTH_SHORT).show();
  269.  
  270. if(item==0)
  271. {
  272. //from gallery
  273.  
  274. Intent intent = new Intent();
  275. intent.setType("image/*");
  276. intent.setAction(Intent.ACTION_GET_CONTENT);
  277. startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
  278. }
  279. if(item==1)
  280. {
  281.  
  282. // from camera
  283. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  284. startActivityForResult(intent, 0);
  285.  
  286.  
  287. // if(hasStorage(true))
  288. // {
  289. //
  290. // ContentValues values = new ContentValues();
  291. // values.put(Media.TITLE, "Image");
  292. // values.put(Images.Media.BUCKET_ID, path.hashCode());
  293. // values.put(Images.Media.BUCKET_DISPLAY_NAME,name);
  294. //
  295. // values.put(Images.Media.MIME_TYPE, "image/jpeg");
  296. // values.put(Media.DESCRIPTION, "Image capture by camera");
  297. // values.put("_data", "1111.jpg");
  298. // Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
  299. // Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
  300. //
  301. // i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  302. //
  303. // startActivityForResult(i, 0);
  304. // }
  305. // else
  306. // {
  307. //
  308. // Toast toast = Toast.makeText(signup.this, "No External Storage found",
  309. // Toast.LENGTH_SHORT);
  310. // toast.setGravity(Gravity.CLIP_VERTICAL, 0, 0);
  311. // toast.show();
  312. // }
  313. }
  314.  
  315. }
  316.  
  317. });
  318. AlertDialog alert = builder.create();
  319.  
  320. //display dialog box
  321.  
  322. alert.show();
  323.  
  324. }
  325. });
  326.  
  327.  
  328. }
  329.  
  330.  
  331.  
  332. private void signupAction() {
  333. // TODO Auto-generated method stub
  334.  
  335. progressDialog = ProgressDialog.show(signup.this, null, getResources()
  336. .getString(R.string.loginProgressMessage), true);
  337.  
  338. new Thread(){
  339.  
  340. public void run(){
  341.  
  342. String path = null;
  343.  
  344.  
  345. Log.e(getResources().getDrawable(R.drawable.default_dp)+"","DRAWABLE PATH");
  346. //Log.e("path of image",getRealPathFromURI(currImageURI));
  347. if(username.equals("")||password.equals("")||email_string.equals("")||cpass.getEditableText().toString().equals(""))
  348. {
  349.  
  350.  
  351. hd.sendEmptyMessage(0);
  352. progressDialog.dismiss();
  353. return;
  354.  
  355. }
  356. if(!password.equals(cpass.getEditableText().toString()))
  357.  
  358. {
  359. Log.e("password doesnt match","----------------------------");
  360. hd.sendEmptyMessage(7);
  361. progressDialog.dismiss();
  362. return;
  363. }
  364. if(currImageURI==null)
  365. {
  366.  
  367. String g = hp.signup_no_photo(username, password, email_string);
  368. Log.i("Sign up",g);
  369. if(g.contains("username already exist"))
  370. {
  371. hd.sendEmptyMessage(5);
  372. }
  373.  
  374. if(g.contains("email already exist"))
  375. {
  376. hd.sendEmptyMessage(6);
  377. }
  378. if(g.contains("SUCCESS"))
  379. {
  380.  
  381. String splits[] = g.split("\\_");
  382. Log.i("SPLITTED STRING",splits[0]);
  383. Log.i("SPLITTED STRING",splits[1]);
  384.  
  385. sharedPreference sp = new sharedPreference();
  386. sp.putdata(getApplicationContext(), "USERID", splits[2]);
  387. sp.putdata(getApplicationContext(), "ISLOGIN", "1");
  388. sp.putdata(getApplicationContext(), "USERNAME", username);
  389. signup.this.finish();
  390. Intent myIntent = new Intent(signup.this, homeActivity.class);
  391. startActivity(myIntent);
  392. hd.sendEmptyMessage(1);
  393. }
  394.  
  395.  
  396.  
  397. // getResources().getDrawable(R.drawable.default_dp)
  398. // path =getRealPathFromURI("drawable/default_dp");
  399. }
  400. else
  401. {
  402. path = getRealPathFromURI(currImageURI);
  403.  
  404. Log.i("username and password",username+"------"+password);
  405. String g = hp.uploadFile(password, username,email_string, path, getApplicationContext());
  406. Log.i(g,"OUTPUT");
  407.  
  408. if(g.contains("username already exist"))
  409. {
  410. hd.sendEmptyMessage(5);
  411. }
  412.  
  413. if(g.contains("email already exist"))
  414. {
  415. hd.sendEmptyMessage(6);
  416. }
  417.  
  418. if(g.contains("SUCCESS"))
  419. {
  420. hd.sendEmptyMessage(1);
  421. }
  422.  
  423. if(g.contains("SUCCESS"))
  424. {
  425.  
  426. String splits[] = g.split("\\_");
  427. Log.i("SPLITTED STRING",splits[0]);
  428. Log.i("SPLITTED STRING",splits[1]);
  429.  
  430. sharedPreference sp = new sharedPreference();
  431. sp.putdata(getApplicationContext(), "USERID", splits[2]);
  432. sp.putdata(getApplicationContext(), "ISLOGIN", "1");
  433. sp.putdata(getApplicationContext(), "USERNAME", username);
  434. signup.this.finish();
  435. Intent myIntent = new Intent(signup.this, homeActivity.class);
  436. startActivity(myIntent);
  437. hd.sendEmptyMessage(1);
  438. }
  439. }
  440.  
  441.  
  442. }
  443.  
  444. }.start();
  445.  
  446. }
  447.  
  448. public void onConfigurationChanged(Configuration newConfig) {
  449. super.onConfigurationChanged(newConfig);
  450. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  451. }
  452.  
  453. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  454.  
  455. if (resultCode == RESULT_OK) {
  456.  
  457. if (requestCode == 1) {
  458.  
  459. // currImageURI is the global variable I'm using to hold the content:// URI of the image
  460.  
  461.  
  462.  
  463. currImageURI = data.getData();
  464.  
  465. Log.i(currImageURI.toString(),"path");
  466.  
  467.  
  468. iv.setImageURI(currImageURI);
  469.  
  470. }
  471.  
  472. if(requestCode==0)
  473. {
  474. Log.i("IMAGE CAPTURED","IMAGE CAPTURED" );
  475.  
  476. Bundle extras = data.getExtras();
  477. Bitmap b = (Bitmap) extras.get("data");
  478.  
  479.  
  480. currImageURI = data.getData();
  481.  
  482. // Log.i(currImageURI.toString(),"path");
  483.  
  484.  
  485. iv.setImageBitmap(b);
  486. }
  487. }
  488. }
  489.  
  490.  
  491. @Override
  492. public void onAttachedToWindow() {
  493. // TODO Auto-generated method stub
  494. super.onAttachedToWindow();
  495.  
  496. Window window = getWindow();
  497.  
  498. window.setFormat(PixelFormat.RGBA_8888);
  499.  
  500. }
  501.  
  502. public String getRealPathFromURI(Uri contentUri) {
  503.  
  504. // can post image
  505. String [] proj={MediaStore.Images.Media.DATA};
  506. Cursor cursor = managedQuery( contentUri,
  507. proj, // Which columns to return
  508. null, // WHERE clause; which rows to return (all rows)
  509. null, // WHERE clause selection arguments (none)
  510. null); // Order-by clause (ascending by name)
  511. int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  512. cursor.moveToFirst();
  513.  
  514. return cursor.getString(column_index);
  515. }
  516.  
  517. public static boolean hasStorage(boolean requireWriteAccess) {
  518. String state = Environment.getExternalStorageState();
  519.  
  520. if (Environment.MEDIA_MOUNTED.equals(state)) {
  521. return true;
  522. } else if (!requireWriteAccess
  523. && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
  524. return true;
  525. }
  526. return false;
  527. }
  528.  
  529. private boolean checkEmail(String email) {
  530. return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
  531. }
  532.  
  533. public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
  534. "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
  535. "\\@" +
  536. "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
  537. "(" +
  538. "\\." +
  539. "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
  540. ")+"
  541. );
  542.  
  543.  
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement