Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. package com.example.slema.barber_schedule;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.net.ConnectivityManager;
  9. import android.net.NetworkInfo;
  10. import android.support.v4.content.LocalBroadcastManager;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.widget.EditText;
  15. import android.widget.ProgressBar;
  16. import android.widget.Toast;
  17. import com.example.slema.barber_schedule.networking.sha256;
  18. import android.os.Handler;
  19.  
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22. private ProgressBar spinner;
  23. EditText email,password;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. email=(EditText) findViewById(R.id.email);
  29. password=(EditText) findViewById(R.id.pass);
  30. spinner = (ProgressBar) findViewById(R.id.progressBar);
  31. spinner.setVisibility(View.GONE);
  32.  
  33. }
  34. //------------------------------------------------------------------------------------------------------
  35. public void login(View view)
  36. {
  37. if(isNetworkAvailable())// check for network
  38. {
  39. if (!(isempty(email.getEditableText().toString(), password.getText().toString()))) {
  40. this.login();
  41. spinner.setVisibility(View.VISIBLE);
  42.  
  43. }
  44. else
  45. {
  46. Toast.makeText(getApplicationContext(),"Please Fill Missing Spaces",Toast.LENGTH_LONG).show();
  47.  
  48. }
  49. }
  50. else
  51. {
  52. Toast.makeText(getApplicationContext(),"No Network available",Toast.LENGTH_LONG).show();
  53. }
  54. }
  55. //--------------------------------------------------------------------------------------------------------
  56. public void signup(View view)
  57. {
  58.  
  59. if(isNetworkAvailable()) { // check for network
  60. Intent intent = new Intent(this, Signup.class);
  61. startActivity(intent);
  62. }
  63. else
  64. {
  65. Toast.makeText(getApplicationContext(),"No Network available",Toast.LENGTH_LONG).show();
  66. }
  67. }
  68. //--------------------------------------------------------------------------------------------------------
  69. public void recover_account(View view){
  70. Intent intent =new Intent(this,RecoveryEmail.class);
  71. startActivity(intent);
  72. }
  73. //--------------------------------------------------------------------------------------------------------
  74. private boolean isNetworkAvailable() {
  75. ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
  76. NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  77. return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  78. }
  79. //---------------------------------------------------------------------------------------------------------
  80. public boolean isempty(String email ,String password)
  81. {
  82. if(email.equals("") || password.equals(""))
  83. {
  84. return true;
  85. }
  86. return false;
  87. }
  88. //-------------------------------------------------------------------------------------------------------------
  89. /**calls the nethread class to send the client's
  90. email;the recived list of favourite barbers data
  91. is handled by the onReceive function which updates the
  92. listview respectively.
  93. */
  94. public void login()
  95. {
  96. Intent network=new Intent(this,com.example.slema.barber_schedule.networking.nethread.class);
  97. String[] credentials=new String[]{"login",email.getText().toString(),sha256.sha256(password.getText().toString())};
  98. network.putExtra("cred", credentials);
  99. startService(network);
  100.  
  101. }
  102. //--------------------------------------------------------------------------------------------------------------
  103. /**
  104. * the following three function i.e (onResume,onPause,onReceive)
  105. * are responsible for handeling the broadcast service which
  106. * in turn makes it possible to communicate with the nethread class.
  107. *
  108. * the onReceive method is responsible for receiving and also
  109. * updating the listview whith new data.
  110. */
  111. @Override
  112. protected void onResume() {
  113. super.onResume();
  114. // Register for the particular broadcast based on ACTION string
  115. IntentFilter filter = new IntentFilter(com.example.slema.barber_schedule.networking.nethread.ACTION);
  116. LocalBroadcastManager.getInstance(this).registerReceiver(testReceiver, filter);
  117. // or `registerReceiver(testReceiver, filter)` for a normal broadcast
  118. }
  119.  
  120. @Override
  121. protected void onPause() {
  122. super.onPause();
  123. // Unregister the listener when the application is paused
  124. LocalBroadcastManager.getInstance(this).unregisterReceiver(testReceiver);
  125. // or `unregisterReceiver(testReceiver)` for a normal broadcast
  126. }
  127.  
  128. // Define the callback for what to do when data is received
  129. private BroadcastReceiver testReceiver = new BroadcastReceiver() {
  130. @Override
  131. public void onReceive(Context context, Intent intent) {
  132. int resultCode = intent.getIntExtra("resultCode", RESULT_CANCELED);
  133. if (resultCode == RESULT_OK) {
  134. String resultValue = intent.getStringExtra("resultValue");
  135. if (resultValue.equals("client_1")) {
  136. Intent intent_client = new Intent(getApplicationContext(), com.example.slema.barber_schedule.client_gui.client_interface.class);
  137. startActivity(intent_client);
  138. spinner.setVisibility(View.GONE);
  139. } else if (resultValue.equals("barber_1")) {
  140. Intent intent_barber = new Intent(getApplicationContext(), com.example.slema.barber_schedule.client_gui.client_interface.class);
  141. startActivity(intent_barber);
  142. spinner.setVisibility(View.GONE);
  143. } else {
  144. Toast.makeText(getApplicationContext(), (resultValue), Toast.LENGTH_LONG).show();
  145. spinner.setVisibility(View.GONE);
  146. }
  147. }
  148. }
  149. };
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement