Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.74 KB | None | 0 0
  1. private ProgressDialog pDialog;
  2. private SessionManager session;
  3. private SQLiteHandler db;
  4.  
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_login);
  9.  
  10. inputEmail = (EditText) findViewById(R.id.email);
  11. inputPassword = (EditText) findViewById(R.id.password);
  12. btnLogin = (Button) findViewById(R.id.btnLogin);
  13. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  14.  
  15. // Progress dialog
  16. pDialog = new ProgressDialog(this);
  17. pDialog.setCancelable(false);
  18.  
  19. // SQLite database handler
  20. db = new SQLiteHandler(getApplicationContext());
  21.  
  22. // Session manager
  23. session = new SessionManager(getApplicationContext());
  24.  
  25. // Check if user is already logged in or not
  26. if (session.isLoggedIn()) {
  27. // User is already logged in. Take him to main activity
  28. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  29. startActivity(intent);
  30. finish();
  31. }
  32.  
  33. // Login button Click Event
  34. btnLogin.setOnClickListener(new View.OnClickListener() {
  35.  
  36. @SuppressLint("NewApi")
  37. public void onClick(View view) {
  38. String email = inputEmail.getText().toString().trim();
  39. String password = inputPassword.getText().toString().trim();
  40.  
  41. // Check for empty data in the form
  42. if (!email.isEmpty() && !password.isEmpty()) {
  43. // login user
  44. checkLogin(email, password);
  45. } else {
  46. // Prompt user to enter credentials
  47. Toast.makeText(getApplicationContext(),
  48. "Please enter the credentials!", Toast.LENGTH_LONG)
  49. .show();
  50. }
  51. }
  52.  
  53. });
  54.  
  55. // Link to Register Screen
  56. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  57.  
  58. public void onClick(View view) {
  59. Intent i = new Intent(getApplicationContext(),
  60. RegisterActivity.class);
  61. startActivity(i);
  62. finish();
  63. }
  64. });
  65.  
  66. }
  67.  
  68. /**
  69. * function to verify login details in mysql db
  70. * */
  71. private void checkLogin(final String email, final String password) {
  72. // Tag used to cancel the request
  73. String tag_string_req = "req_login";
  74.  
  75. pDialog.setMessage("Logging in ...");
  76. showDialog();
  77.  
  78. StringRequest strReq = new StringRequest(Method.POST,
  79. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  80.  
  81. @Override
  82. public void onResponse(String response) {
  83. Log.d(TAG, "Login Response: " + response.toString());
  84. hideDialog();
  85.  
  86. try {
  87. JSONObject jObj = new JSONObject(response);
  88. boolean error = jObj.getBoolean("error");
  89.  
  90. // Check for error node in json
  91. if (!error) {
  92. // user successfully logged in
  93. // Create login session
  94. session.setLogin(true);
  95.  
  96. // Now store the user in SQLite
  97. String uid = jObj.getString("uid");
  98.  
  99. JSONObject user = jObj.getJSONObject("user");
  100. String name = user.getString("name");
  101. String email = user.getString("email");
  102. String phone = user.getString("phone");
  103. String created_at = user.getString("created_at");
  104.  
  105. // Inserting row in users table
  106. db.addUser(name, email,phone, uid, created_at);
  107.  
  108.  
  109. // Launch main activity
  110. Intent intent = new Intent(LoginActivity.this,
  111. MainActivity.class);
  112. startActivity(intent);
  113. finish();
  114. } else {
  115. // Error in login. Get the error message
  116. String errorMsg = jObj.getString("error_msg");
  117. Toast.makeText(getApplicationContext(),
  118. errorMsg, Toast.LENGTH_LONG).show();
  119. }
  120. } catch (JSONException e) {
  121. // JSON error
  122. e.printStackTrace();
  123. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  124. }
  125.  
  126. }
  127. }, new Response.ErrorListener() {
  128.  
  129. @Override
  130. public void onErrorResponse(VolleyError error) {
  131. Log.e(TAG, "Login Error: " + error.getMessage());
  132. Toast.makeText(getApplicationContext(),
  133. error.getMessage(), Toast.LENGTH_LONG).show();
  134. hideDialog();
  135. }
  136. }) {
  137.  
  138. @Override
  139. protected Map<String, String> getParams() {
  140. // Posting parameters to login url
  141. Map<String, String> params = new HashMap<String, String>();
  142. params.put("email", email);
  143. params.put("password", password);
  144.  
  145. return params;
  146. }
  147.  
  148. };
  149.  
  150. // Adding request to request queue
  151. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  152. }
  153.  
  154. private void showDialog() {
  155. if (!pDialog.isShowing())
  156. pDialog.show();
  157. }
  158.  
  159. private void hideDialog() {
  160. if (pDialog.isShowing())
  161. pDialog.dismiss();
  162. }
  163.  
  164. private TextView txtName;
  165. private TextView txtEmail;
  166. //private TextView txtPhone;
  167. private Button btnLogout;
  168. ImageButton button2,button1,button3;
  169.  
  170.  
  171. private SQLiteHandler db;
  172. private SessionManager session;
  173.  
  174.  
  175. protected void onCreate(Bundle savedInstanceState) {
  176. super.onCreate(savedInstanceState);
  177. setContentView(R.layout.activity_main);
  178.  
  179. txtName = (TextView) findViewById(R.id.name);
  180. txtEmail = (TextView) findViewById(R.id.email);
  181. button1 = (ImageButton) findViewById(R.id.Button);
  182.  
  183. // button3 = (ImageButton) findViewById(R.id.Button2);
  184. //txtPhone = (TextView) findViewById(R.id.phone);
  185. btnLogout = (Button) findViewById(R.id.btnLogout);
  186. // button2 = (ImageButton) findViewById(R.id.Button1);
  187. //button3 = (ImageButton) findViewById(R.id.Button2);
  188. // m = new Mail("internshipait@gmail.tld", "internshipait!!");
  189. // Add your mail Id and Password
  190.  
  191.  
  192. // SqLite database handler
  193. db = new SQLiteHandler(getApplicationContext());
  194.  
  195. // session manager
  196. session = new SessionManager(getApplicationContext());
  197.  
  198. if (!session.isLoggedIn()) {
  199. logoutUser();
  200. }
  201.  
  202. // Fetching user details from SQLite
  203. HashMap<String, String> user = db.getUserDetails();
  204.  
  205. String name = user.get("name");
  206. String email = user.get("email");
  207. //String phone=user.get("phone");
  208.  
  209. // Displaying the user details on the screen
  210. txtName.setText(name);
  211. txtEmail.setText(email);
  212. //txtPhone.setText(phone);
  213.  
  214. // Logout button click event
  215. btnLogout.setOnClickListener(new View.OnClickListener() {
  216.  
  217. @Override
  218. public void onClick(View v) {
  219.  
  220. logoutUser();
  221.  
  222. }
  223. });
  224. /*button1.setOnClickListener(new View.OnClickListener() {
  225. public void onClick(View view) {
  226. Intent myIntent = new Intent(view.getContext(), insert.class);
  227. startActivityForResult(myIntent, 0);
  228. finish();
  229. }});
  230.  
  231. button2.setOnClickListener(new View.OnClickListener() {
  232. public void onClick(View view) {
  233. Intent myIntent = new Intent(view.getContext(), insert1.class);
  234. startActivityForResult(myIntent, 0);
  235. finish();
  236. }});*/
  237.  
  238.  
  239.  
  240. /* button3.setOnClickListener(new View.OnClickListener() {
  241. public void onClick(View view) {
  242. Intent myIntent = new Intent(view.getContext(), ReadData.class);
  243. startActivityForResult(myIntent, 0);
  244. finish();
  245. }});*/
  246.  
  247. View.OnClickListener handler = new View.OnClickListener() {
  248. public void onClick(View v) {
  249. switch (v.getId()) {
  250.  
  251. case R.id.buttonShareTextUrl:
  252. shareTextUrl();
  253. break;
  254.  
  255. }
  256. }
  257. };
  258.  
  259.  
  260. // our buttons
  261. findViewById(R.id.buttonShareTextUrl).setOnClickListener(handler);
  262.  
  263. }
  264.  
  265. public void process(View view){
  266. Intent intent=null,chooser=null;
  267. if(view.getId()==R.id.send_email)
  268. {
  269. intent=new Intent(Intent.ACTION_SEND);
  270. intent.setData(Uri.parse("mailto:"));
  271. String[] to={"vidyaappait@gmail.com",};
  272. intent.putExtra(Intent.EXTRA_EMAIL,to);
  273. intent.putExtra(Intent.EXTRA_SUBJECT, "INTERESTED IN SPONSPORING A CHILD");
  274. intent.putExtra(Intent.EXTRA_TEXT, "FHHHHHHHHHHHHHHHHHHHHH");
  275. intent.setType("message/rfc822");
  276. chooser=Intent.createChooser(intent,"send email");
  277. startActivity(chooser);
  278. }
  279.  
  280. }
  281.  
  282.  
  283. private void shareTextUrl() {
  284. Intent share = new Intent(android.content.Intent.ACTION_SEND);
  285. share.setType("text/plain");
  286. share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
  287.  
  288. // Add data to the intent, the receiving app will decide
  289. // what to do with it.
  290. share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
  291. share.putExtra(Intent.EXTRA_TEXT, "please why don't u download sponsor app to sponsor amount to needy child. ");
  292. //share.putExtra(Intent.EXTRA_TEXT, "");
  293.  
  294. startActivity(Intent.createChooser(share, "Share link!"));
  295. }
  296.  
  297.  
  298. /**
  299. * Logging out the user. Will set isLoggedIn flag to false in shared
  300. * preferences Clears the user data from sqlite users table
  301. * */
  302. private void logoutUser() {
  303. session.setLogin(false);
  304.  
  305. db.deleteUsers();
  306.  
  307. // Launching the login activity
  308. Intent intent = new Intent(MainActivity.this, LoginActivity.class);
  309. startActivity(intent);
  310. finish();
  311. }
  312.  
  313.  
  314.  
  315. }
  316.  
  317. <LinearLayout
  318. android:layout_width="fill_parent"
  319. android:layout_height="48dp"
  320. android:background="#30D5C8"
  321. android:orientation="horizontal" >
  322.  
  323. <ImageView
  324. android:layout_width="40dp"
  325. android:layout_height="40dp"
  326. android:layout_gravity="center"
  327. android:layout_marginLeft="10dp"
  328. android:background="@drawable/ambu" />
  329.  
  330. <TextView
  331. android:layout_width="126dp"
  332. android:layout_height="wrap_content"
  333. android:layout_gravity="center"
  334. android:layout_marginLeft="25dp"
  335. android:text="Login"
  336. android:textColor="#00008B"
  337. android:textSize="18sp"
  338. android:textStyle="bold" />
  339. </LinearLayout>
  340.  
  341. <LinearLayout
  342. android:layout_width="fill_parent"
  343. android:layout_height="fill_parent"
  344. android:layout_marginBottom="30dp"
  345. android:layout_marginLeft="16dp"
  346. android:layout_marginRight="16dp"
  347. android:layout_marginTop="16dp"
  348. android:background="@drawable/greybackground"
  349. android:gravity="center_horizontal"
  350. android:orientation="vertical" >
  351.  
  352. <TextView
  353. android:id="@+id/textView"
  354. android:layout_width="wrap_content"
  355. android:layout_height="wrap_content"
  356. android:layout_alignParentTop="true"
  357. android:layout_centerHorizontal="true"
  358. android:layout_gravity="center"
  359. android:textStyle="bold"
  360. android:text="Welcome to Vidya app"
  361. android:textColor="#E75480" />
  362.  
  363. <ImageView
  364. android:layout_width="100dp"
  365. android:layout_height="100dp"
  366. android:layout_marginTop="15dp"
  367. android:background="@drawable/kavi" />
  368.  
  369. </LinearLayout>
  370. <EditText
  371. android:id="@+id/email"
  372. android:layout_width="280dp"
  373. android:layout_height="fill_parent"
  374. android:layout_gravity="center"
  375. android:layout_marginBottom="10dp"
  376. android:background="@color/grey"
  377. android:ems="10"
  378. android:hint="@string/hint_email"
  379. android:inputType="textEmailAddress"
  380. android:padding="10dp"
  381. android:singleLine="true"
  382. android:textColor="@drawable/cl"
  383. android:textColorHint="@color/input_login_hint" />
  384.  
  385. <EditText
  386. android:id="@+id/password"
  387. android:layout_width="280dp"
  388. android:layout_height="wrap_content"
  389. android:layout_gravity="center"
  390. android:layout_marginBottom="10dp"
  391. android:background="@color/grey"
  392. android:ems="10"
  393. android:hint="@string/hint_password"
  394. android:inputType="textPassword"
  395. android:padding="10dp"
  396. android:singleLine="true"
  397. android:textColor="@color/input_login"
  398. android:textColorHint="@color/input_login_hint" >
  399.  
  400. <requestFocus />
  401. </EditText>
  402.  
  403.  
  404.  
  405. <!-- Login Button -->
  406.  
  407. <Button
  408. android:id="@+id/btnLogin"
  409. android:layout_width="190dp"
  410. android:layout_height="38dp"
  411. android:layout_gravity="center_horizontal"
  412. android:layout_marginTop="16dip"
  413. android:background="@drawable/btn"
  414. android:text="@string/btn_login"
  415. android:textColor="@color/btn_login"
  416. android:textSize="13dp" />
  417.  
  418. <Button
  419. android:id="@+id/btnLinkToRegisterScreen"
  420. android:layout_width="293dp"
  421. android:layout_height="wrap_content"
  422. android:layout_gravity="center"
  423. android:layout_marginTop="10dip"
  424. android:background="@null"
  425. android:text="@string/btn_link_to_register"
  426. android:textAllCaps="false"
  427. android:textSize="10dp" />
  428.  
  429. <!-- Link to Login Screen -->
  430. </LinearLayout>
  431.  
  432. </ScrollView>
  433.  
  434. <LinearLayout
  435. android:layout_width="fill_parent"
  436. android:layout_height="500dp"
  437. android:background="@color/white"
  438. android:orientation="vertical" />
  439.  
  440. <LinearLayout android:id="@+id/linearLayout1"
  441. android:layout_width="fill_parent"
  442. android:layout_height="wrap_content"
  443. android:layout_centerHorizontal="true"
  444.  
  445. android:layout_marginLeft="20dp"
  446. android:layout_marginRight="20dp"
  447. android:gravity="center"
  448. android:orientation="vertical">
  449.  
  450. <ImageButton
  451. android:id="@+id/Button"
  452. android:layout_width="62dp"
  453. android:layout_height="65dp"
  454. android:adjustViewBounds="true"
  455. android:background="@null"
  456. android:scaleType="fitCenter"
  457. android:src="@drawable/search2" />
  458.  
  459.  
  460.  
  461. <Button
  462. android:id="@+id/send_email"
  463. android:layout_width="147dp"
  464. android:layout_height="wrap_content"
  465. android:layout_marginTop="20dp"
  466. android:background="@drawable/cl"
  467. android:text="@string/faid"
  468. android:onClick="process"/>
  469.  
  470. <Button
  471. android:id="@+id/buttonShareTextUrl"
  472. android:layout_width="208dp"
  473. android:layout_height="wrap_content"
  474. android:layout_marginTop="20dp"
  475. android:background="@drawable/greybackground"
  476. android:text="@string/ex" />
  477.  
  478. <RelativeLayout
  479. android:layout_width="fill_parent"
  480. android:layout_height="fill_parent"
  481. android:layout_alignParentLeft="true"
  482. android:layout_alignParentRight="true"
  483. android:layout_alignParentTop="true"
  484. android:gravity="bottom"
  485. android:orientation="vertical" >
  486. </RelativeLayout>
  487. </LinearLayout>
  488.  
  489. <ScrollView
  490. xmlns:android="http://schemas.android.com/apk/res/android"
  491. xmlns:tools="http://schemas.android.com/tools"
  492. android:id="@+id/scrollView1"
  493. android:layout_width="fill_parent"
  494. android:layout_height="fill_parent" >
  495.  
  496. <LinearLayout
  497. android:layout_width="fill_parent"
  498. android:layout_height="500dp"
  499. android:background="#89CFF0"
  500. android:orientation="vertical" >
  501.  
  502. <LinearLayout
  503. android:layout_width="fill_parent"
  504. android:layout_height="48dp"
  505. android:layout_alignParentTop="true"
  506. android:background="#89CFF0"
  507. android:orientation="horizontal" >
  508.  
  509. <ImageView
  510. android:layout_width="40dp"
  511. android:layout_height="40dp"
  512. android:layout_marginLeft="10dp"
  513. android:background="@drawable/kavi" />
  514.  
  515. <TextView
  516. android:layout_width="wrap_content"
  517. android:layout_height="wrap_content"
  518. android:layout_gravity="center_horizontal|center_vertical"
  519. android:layout_marginLeft="25dp"
  520. android:text="Registration form"
  521. android:textColor="#00008B"
  522. android:textSize="18sp"
  523. android:textStyle="bold" />
  524.  
  525. <LinearLayout
  526. android:layout_width="fill_parent"
  527. android:layout_height="48dp"
  528. android:gravity="right"
  529. android:orientation="horizontal" >
  530.  
  531. <ImageView
  532. android:layout_width="wrap_content"
  533. android:layout_height="wrap_content"
  534. android:background="@drawable/tick" />
  535. </LinearLayout>
  536. </LinearLayout>
  537.  
  538. <LinearLayout
  539. android:layout_width="match_parent"
  540. android:layout_height="520dp"
  541. android:layout_marginBottom="30dp"
  542. android:layout_marginLeft="16dp"
  543. android:layout_marginRight="18dp"
  544. android:layout_marginTop="16dp"
  545. android:layout_weight="0.01"
  546. android:background="@drawable/cl"
  547. android:orientation="vertical" >
  548.  
  549. <ImageView
  550. android:layout_width="136dp"
  551. android:layout_height="100dp"
  552. android:layout_gravity="center"
  553. android:layout_marginTop="15dp"
  554. android:background="@drawable/kavi" />
  555.  
  556. <EditText
  557. android:id="@+id/name"
  558. android:layout_width="fill_parent"
  559. android:layout_height="40dp"
  560. android:layout_marginTop="30dp"
  561. android:background="#f3f3f3"
  562. android:hint="name"
  563. android:inputType="textCapWords"
  564. android:paddingLeft="5dp" />
  565.  
  566. <EditText
  567. android:id="@+id/email"
  568. android:layout_width="fill_parent"
  569. android:layout_height="40dp"
  570. android:layout_marginTop="15dp"
  571. android:background="#f3f3f3"
  572. android:hint="email"
  573. android:inputType="textEmailAddress"
  574. android:paddingLeft="5dp" />
  575.  
  576. <EditText
  577. android:id="@+id/phone"
  578. android:layout_width="fill_parent"
  579. android:layout_height="40dp"
  580. android:layout_marginTop="15dp"
  581. android:background="#f3f3f3"
  582. android:hint="Phone"
  583. android:inputType="number"
  584. android:paddingLeft="5dp" />
  585.  
  586. <EditText
  587. android:id="@+id/password"
  588. android:layout_width="fill_parent"
  589. android:layout_height="40dp"
  590. android:layout_marginTop="15dp"
  591. android:background="#f3f3f3"
  592. android:hint="Password"
  593. android:inputType="textPassword"
  594. android:paddingLeft="5dp" />
  595.  
  596. <requestFocus />
  597.  
  598. <Button
  599. android:id="@+id/btnRegister"
  600. android:layout_width="190dp"
  601. android:layout_height="38dp"
  602. android:layout_gravity="center_horizontal"
  603. android:layout_marginTop="16dp"
  604. android:background="@drawable/btn"
  605. android:text="Register"
  606. android:textColor="#fff" />
  607.  
  608. <Button
  609. android:id="@+id/btnLinkToLoginScreen"
  610. android:layout_width="fill_parent"
  611. android:layout_height="wrap_content"
  612. android:layout_marginTop="20dip"
  613. android:background="@null"
  614. android:text="@string/btn_link_to_login"
  615. android:textAllCaps="false"
  616. android:textSize="12dp" />
  617. </LinearLayout>
  618.  
  619. <LinearLayout
  620. android:layout_width="fill_parent"
  621. android:layout_height="wrap_content"
  622. android:orientation="vertical" >
  623. </LinearLayout>
  624. </LinearLayout>
  625.  
  626. </ScrollView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement