Advertisement
Guest User

Untitled

a guest
May 20th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.97 KB | None | 0 0
  1. public class Login extends ActionBarActivity {
  2. // flag for Internet connection status
  3. Boolean isInternetPresent = false;
  4. String shareduid;
  5. // Connection detector class
  6. ConnectionDetector cd;
  7. ImageView imgview;
  8. EditText uname;
  9. EditText pass;
  10. Button create,login;
  11. TextView trouble;
  12. public static final String MyPREFERENCES = "MyPrefs";
  13. public static String userid = null;
  14. SharedPreferences sharedpreferences;
  15. private static final String TAG = "myAppSurun";
  16.  
  17.  
  18. //private ActionBarDrawerToggle mDrawerToggle;
  19. private String mActivityTitle;
  20.  
  21. //End Drawer
  22.  
  23.  
  24.  
  25.  
  26. private ProgressDialog pDialog;
  27.  
  28. // Creating JSON Parser object
  29. JSONParser jParser = new JSONParser();
  30.  
  31. ArrayList<HashMap<String, String>> productsList;
  32.  
  33. String otpkey,vrfy;
  34.  
  35. // url to get all products list
  36. private static String url_all_login = "http://xxx/xxx/xxxx/xxx";
  37.  
  38.  
  39. //Globalstring
  40. String username =null;
  41. String password = null;
  42.  
  43. //Global Variable for login state checking
  44. public boolean loginflag = false;
  45. // RelativeLayout relativeLayout=new RelativeLayout(this);
  46.  
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_login);
  51. cd = new ConnectionDetector(getApplicationContext());
  52. imgview=(ImageView)findViewById(R.id.imageView2);
  53. uname=(EditText)findViewById(R.id.edituser);
  54. pass=(EditText)findViewById(R.id.editpassword);
  55. create=(Button)findViewById(R.id.create);
  56. login=(Button)findViewById(R.id.Login);
  57. trouble=(TextView)findViewById(R.id.trouble_login);
  58. getSupportActionBar().setTitle("Surun Support");
  59. ColorDrawable(Color.parseColor("#F58634")));
  60.  
  61.  
  62. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  63. getSupportActionBar().setHomeButtonEnabled(true);
  64. ActionBar bar = getSupportActionBar();
  65. bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F58634")));
  66.  
  67.  
  68. //Click Text Animation
  69. final Animation myanim, imganim;
  70. myanim = AnimationUtils.loadAnimation(this, R.anim.link_text_anim);
  71. imganim = AnimationUtils.loadAnimation(this, R.anim.rotate);
  72.  
  73.  
  74. uname.addTextChangedListener(new TextWatcher() {
  75. @Override
  76. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  77.  
  78. }
  79.  
  80. @Override
  81. public void onTextChanged(CharSequence s, int start, int before, int count) {
  82.  
  83. }
  84.  
  85. @Override
  86. public void afterTextChanged(Editable s) {
  87.  
  88.  
  89. validation.isValid(uname, "^[_a-zA-Z]+(\.[_a-zA-Z 0-9-]+)*@[a-zA-Z]+(\.[a-zA-Z]+)*(\.[a-zA-Z]{2,})$", "Invalid UserName", true);
  90.  
  91.  
  92. }
  93. });
  94.  
  95. pass.addTextChangedListener(new TextWatcher() {
  96. @Override
  97. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  98.  
  99. }
  100.  
  101. @Override
  102. public void onTextChanged(CharSequence s, int start, int before, int count) {
  103.  
  104. }
  105.  
  106. @Override
  107. public void afterTextChanged(Editable s) {
  108. validation.isValid(pass, "[0-9]{10}", "Invalid Mobile No", true);
  109. }
  110. });
  111.  
  112. //end of initializing component
  113.  
  114.  
  115.  
  116.  
  117.  
  118. create.setOnClickListener(new View.OnClickListener() {
  119. public void onClick(View v) {
  120. Toast.makeText(getApplicationContext(), "Create Click", Toast.LENGTH_LONG).show();
  121. Intent i = new Intent(getApplicationContext(), Registration_user.class);
  122. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  123. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  124. startActivity(i);
  125.  
  126.  
  127. }
  128. });//End of On click for button
  129.  
  130. //starting font settings this has prone to error try catch is mandatory while setting font(Overriding native font interface).
  131. try {
  132. Typeface myTypeface = Typeface.createFromAsset(this.getAssets(), "fonts/robotoregular.ttf");
  133. create.setTypeface(myTypeface);
  134. } catch (Exception e) {
  135. Log.v(TAG, "Exception " + e);
  136. }
  137. //End of font settings
  138.  
  139. //Initializing shared preferences
  140. sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  141.  
  142. //load preferences if exist
  143. Log.v(TAG,""+sharedpreferences);
  144. Log.v(TAG,"Preference is present loading it");
  145. String shareduser = sharedpreferences.getString("User", "");
  146. String sharedpass = sharedpreferences.getString("Password", "");
  147. shareduid = sharedpreferences.getString("userid", "");
  148. String sharedotp = sharedpreferences.getString("otp", "");
  149. String sharedvrfy = sharedpreferences.getString("verified", "");
  150. Log.v(TAG,"uid"+shareduser);
  151. Log.v(TAG,"otp"+sharedpass);
  152. Log.v(TAG,"vrfy"+sharedvrfy);
  153.  
  154.  
  155. if ((shareduser.length() > 0) && (sharedpass.length() > 0)) {
  156. //Navigating to main page
  157. Log.v(TAG,"navigate to main");
  158. Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
  159. i.putExtra("user", shareduser);
  160. i.putExtra("pass", sharedpass);
  161. i.putExtra("userid", shareduid);
  162. i.putExtra("otpkey",sharedotp);
  163. i.putExtra("vrfy", sharedvrfy);
  164. //Starting An Activity
  165. startActivity(i);
  166. finish();
  167. } else {
  168. login.setOnClickListener(new View.OnClickListener() {
  169. public void onClick(View v) {
  170. // get Internet status
  171. isInternetPresent = cd.isConnectingToInternet();
  172.  
  173. // check for Internet status
  174. if (isInternetPresent) {
  175. if (uname.getText().length() <= 0 || pass.getText().length() <= 0) {
  176. AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
  177.  
  178. // Setting Dialog Title
  179. alertDialog.setTitle("Alert Dialog");
  180.  
  181. // Setting Dialog Message
  182. alertDialog.setMessage("All Fields Are Mandatory");
  183.  
  184. // Setting Icon to Dialog
  185. // alertDialog.setIcon(R.drawable.tick);
  186.  
  187. // Setting OK Button
  188. alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  189. public void onClick(DialogInterface dialog, int which) {
  190. // Write your code here to execute after dialog closed
  191. Toast.makeText(getApplicationContext(), "Please Enter Correct User Name And Password", Toast.LENGTH_SHORT).show();
  192. }
  193. });
  194.  
  195. // Showing Alert Message
  196. alertDialog.show();
  197.  
  198. if (pass.getText().length() > 10) {
  199. Toast toast1 = Toast.makeText(getApplicationContext(), "Four Characters Only...", Toast.LENGTH_SHORT);
  200. toast1.show();
  201. trouble.setVisibility(View.VISIBLE);
  202. //Log.v(TAG,"Not Valid");
  203.  
  204. }
  205. } else if (pass.getText().length() > 10) {
  206. AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
  207.  
  208. // Setting Dialog Title
  209. alertDialog.setTitle("Alert Dialog");
  210.  
  211. // Setting Dialog Message
  212. alertDialog.setMessage("Mobile no Must be 10 digit only ");
  213.  
  214. // Setting Icon to Dialog
  215. // alertDialog.setIcon(R.drawable.tick);
  216.  
  217. // Setting OK Button
  218. alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  219. public void onClick(DialogInterface dialog, int which) {
  220. // Write your code here to execute after dialog closed
  221. Toast.makeText(getApplicationContext(), "Please Enter Correct User Name And Password", Toast.LENGTH_SHORT).show();
  222. }
  223. });
  224.  
  225. // Showing Alert Message
  226. alertDialog.show();
  227.  
  228. } else if (!(validation.isValid(uname, "^[_a-zA-Z]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z]+(\.[a-zA-Z]+)*(\.[a-zA-Z]{2,})$", "Invalid UserName", true))) {
  229. AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
  230.  
  231. // Setting Dialog Title
  232. alertDialog.setTitle("Alert Dialog");
  233.  
  234. // Setting Dialog Message
  235. alertDialog.setMessage("Email Is Incorrect");
  236.  
  237. // Setting Icon to Dialog
  238. // alertDialog.setIcon(R.drawable.tick);
  239.  
  240. // Setting OK Button
  241. alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  242. public void onClick(DialogInterface dialog, int which) {
  243. // Write your code here to execute after dialog closed
  244. Toast.makeText(getApplicationContext(), "Email Id Is InCorrect", Toast.LENGTH_SHORT).show();
  245. uname.requestFocus();
  246. }
  247. });
  248.  
  249. // Showing Alert Message
  250. alertDialog.show();
  251. } else {
  252. //Animate Button load animation from anim/rotate.xml
  253. imgview.startAnimation(imganim);
  254. username = uname.getText().toString().toLowerCase();
  255. password = pass.getText().toString();
  256.  
  257. //Sending Login Request To Server for validation Using Asynchronus Tasks where username and password as a parameter to method
  258. Log.v(TAG, "Excuting check detail");
  259. new CheckDetail().execute();
  260.  
  261. //Creating Shared Preferences
  262.  
  263.  
  264. }//end of else_if fields are valid
  265.  
  266.  
  267. }
  268. else
  269. {
  270. AlertDialog alertDialog = new AlertDialog.Builder(Login.this).create();
  271.  
  272. // Setting Dialog Title
  273. alertDialog.setTitle("Alert Dialog");
  274.  
  275. // Setting Dialog Message
  276. alertDialog.setMessage("Internet is not active.Please Check Your NEtwork Setting");
  277.  
  278. // Setting Icon to Dialog
  279. // alertDialog.setIcon(R.drawable.tick);
  280.  
  281. // Setting OK Button
  282. alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  283. public void onClick(DialogInterface dialog, int which) {
  284. // Write your code here to execute after dialog closed
  285. Toast.makeText(getApplicationContext(), "Internet Is Inactive", Toast.LENGTH_SHORT).show();
  286.  
  287. }
  288. });
  289.  
  290. // Showing Alert Message
  291. alertDialog.show();
  292. }
  293. }
  294. });//End of On click for button
  295. }//If no shared preferences found
  296.  
  297. }//End of onCreate function
  298.  
  299.  
  300. @Override
  301. public boolean onCreateOptionsMenu(Menu menu) {
  302. // Inflate the menu; this adds items to the action bar if it is present.
  303. getMenuInflater().inflate(R.menu.menu_admin_home, menu);
  304. return true;
  305. }//End of onCreateOptionMenu
  306.  
  307. @Override
  308. public boolean onOptionsItemSelected(MenuItem item) {
  309. // Handle action bar item clicks here. The action bar will
  310. // automatically handle clicks on the Home/Up button, so long
  311. // as you specify a parent activity in AndroidManifest.xml.
  312. int id = item.getItemId();
  313.  
  314. //noinspection SimplifiableIfStatement
  315. if (id == R.id.action_settings) {
  316. return true;
  317. }
  318.  
  319. //Remove Following Comment To Enable Drawer Toggling On Login Page
  320.  
  321. /*// Activate the navigation drawer toggle
  322. if (mDrawerToggle.onOptionsItemSelected(item)) {
  323. return true;
  324. }*/
  325.  
  326. return super.onOptionsItemSelected(item);
  327. }//End of onOptionItemSelected
  328.  
  329. //Alert Dialog when User Click Back Button
  330. @Override
  331. public boolean onKeyDown(int keyCode, KeyEvent event) {
  332. //Handle the back button
  333. if(keyCode == KeyEvent.KEYCODE_BACK) {
  334. //Ask the user if they want to quit
  335. new AlertDialog.Builder(this)
  336. .setIcon(android.R.drawable.ic_dialog_alert)
  337. .setTitle(R.string.quit)
  338. .setMessage(R.string.really_quit)
  339. .setPositiveButton("yes", new DialogInterface.OnClickListener() {
  340.  
  341. @Override
  342. public void onClick(DialogInterface dialog, int which) {
  343.  
  344. //Stop the activity
  345. Login.this.finish();
  346. }
  347.  
  348. })
  349. .setNegativeButton("no", null)
  350. .show();
  351.  
  352. return true;
  353. }
  354. else {
  355. return super.onKeyDown(keyCode, event);
  356. }
  357.  
  358. }//End of Alert Dialog Box
  359.  
  360. class CheckDetail extends AsyncTask<String,String, String> {
  361.  
  362. JSONArray datail=null;
  363. @Override
  364. protected void onPreExecute() {
  365. super.onPreExecute();
  366. pDialog = new ProgressDialog(Login.this);
  367. pDialog.setMessage("Logging in. Please wait...");
  368. pDialog.setIndeterminate(false);
  369. pDialog.setCancelable(false);
  370. pDialog.show();
  371. }
  372.  
  373. /**
  374. * getting All products from url
  375. */
  376. protected String doInBackground(String... args) {
  377. try {
  378. Log.v(TAG, "In Do in Background");
  379. List<NameValuePair> params = new ArrayList<NameValuePair>();
  380. params.add(new BasicNameValuePair("email", username));
  381. params.add(new BasicNameValuePair("pwd", password));
  382. // getting JSON string from URL
  383. JSONObject json =jParser.makeHttpRequest(url_all_login, "POST", params);
  384. //Object Parsing Failed here hence by using trail guide using JSONParser.alternateJSONArray to parse user data.
  385. //Hence not using json instance of object using a BACKUP static variable of Parser class for proccessing.
  386. //To use this backup utility theme the process should be standard and return unique or two out put only or ether way use three logical step
  387. if(json != null) {
  388. // As if login fails it returns object handling fail logic here.
  389. //We can make it general by sending array from server side so we can only use alternateJSONArray variable
  390.  
  391. }
  392. if(JSONParser.alternateJSONArray != null)
  393. {
  394. Log.v(TAG, "USING BACKUP ARRAY");
  395. //Check your log cat for JSON futher details
  396. for (int jsonArrayElementIndex=0; jsonArrayElementIndex < JSONParser.alternateJSONArray.length(); jsonArrayElementIndex++) {
  397. JSONObject jsonObjectAtJsonArrayElementIndex = JSONParser.alternateJSONArray.getJSONObject(jsonArrayElementIndex);
  398. userid=jsonObjectAtJsonArrayElementIndex.getString("u_id");
  399. otpkey=jsonObjectAtJsonArrayElementIndex.getString("OTP");
  400. vrfy=jsonObjectAtJsonArrayElementIndex.getString("is_verified");
  401. Log.v(TAG,"" +userid);
  402. Log.v(TAG,"" +otpkey);
  403. Log.v(TAG,"" +vrfy);
  404. if(jsonObjectAtJsonArrayElementIndex.getString("email").equals(username) && jsonObjectAtJsonArrayElementIndex.getString("mobile").equals(password))
  405. {
  406. Log.v(TAG,"Login Successful Now setting loginflag true");
  407. loginflag = true;
  408. }
  409. }
  410.  
  411.  
  412. }
  413. else
  414. {
  415. loginflag=false;
  416. }
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. Log.v(TAG, "Exception at end :" + e.toString());
  420. //Log.e("TAG", "Error......!RecoverIt");
  421. }
  422.  
  423. return null ;
  424. }
  425.  
  426.  
  427. protected void onPostExecute(String result)
  428. {
  429. // dismiss the dialog after getting all products
  430. //super.onPostExecute();
  431. // pDialog.dismiss();
  432. Log.v(TAG,"verification"+vrfy);
  433. Log.v(TAG,"userid"+userid);
  434.  
  435. /* if(vrfy==false)
  436. {
  437. Intent i = new Intent(getApplicationContext(), Verifyotp.class);
  438. i.putExtra("userid", userid);
  439. i.putExtra("isverified", vrfy);
  440. startActivity(i);
  441. }else*/
  442.  
  443.  
  444.  
  445. if(loginflag==true) {
  446. Log.v(TAG, "Executing Shared Preferences...");
  447. SharedPreferences.Editor editor = sharedpreferences.edit();
  448. editor.putString("User", uname.getText().toString());
  449. editor.putString("Password", pass.getText().toString());
  450.  
  451. Log.v(TAG, "Userid==================" + userid);
  452. Log.v(TAG, "otpkey==============" + otpkey);
  453. Log.v(TAG, "is verified " + vrfy);
  454. editor.putString("userid", userid);
  455. editor.putString("otp", otpkey);
  456. editor.putString("vrfy", vrfy);
  457.  
  458. editor.commit();
  459. Toast.makeText(getApplicationContext(), "Login Succeed", Toast.LENGTH_SHORT).show();
  460. android.util.Log.v(TAG, "Login Succeed");
  461. Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
  462. i.putExtra("user", shareduid);
  463. Log.v(TAG, "" + userid);
  464. startActivity(i);
  465. finish();
  466. }
  467.  
  468. else
  469. {
  470. Toast.makeText(getApplicationContext(),"Login failed,Invalid Details...!",Toast.LENGTH_LONG).show();
  471. trouble.setVisibility(View.VISIBLE);
  472. }
  473. pDialog.dismiss();
  474. }
  475.  
  476. }
  477.  
  478. Intent iin = getIntent();
  479. Bundle b = iin.getExtras();
  480. if (b != null) {
  481. u_id = (String) b.get("userid");//first time it shows null but after restart it get correct value
  482. Log.v(TAG, "userlogged in" + u_id);
  483.  
  484. }
  485.  
  486. Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
  487. i.putExtra("user", shareduid);
  488. Log.v(TAG, "" + userid);
  489. startActivity(i);
  490.  
  491. Intent i = new Intent(getApplicationContext(), UserLogedIn.class);
  492. i.putExtra("user", userid);
  493. Log.v(TAG, "" + userid);
  494. startActivity(i);
  495. check with this and let me know also i think check with key if you putextra with 'user' key you have to access with the same 'user' key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement