Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. package ist.meic.sirs.securechildlocator;
  2.  
  3. import android.content.Intent;
  4. import android.os.StrictMode;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7.  
  8. import android.app.ProgressDialog;
  9. import android.telephony.TelephonyManager;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import butterknife.ButterKnife;
  18. import butterknife.InjectView;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21. private static final String TAG = "LoginActivity";
  22. private static final int REQUEST_SIGNUP = 0;
  23. private String sessionKey;
  24. private String phoneID;
  25. @InjectView(R.id.input_email) EditText _emailText;
  26. @InjectView(R.id.input_password) EditText _passwordText;
  27. @InjectView(R.id.btn_login) Button _loginButton;
  28. @InjectView(R.id.link_signup) TextView _signupLink;
  29.  
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  34. StrictMode.setThreadPolicy(policy);
  35. setContentView(R.layout.activity_main);
  36. ButterKnife.inject(this);
  37.  
  38. int PERMISSION_ALL = 1;
  39. String[] PERMISSIONS = {andorid.Manifest.permission.INTERNET, android.Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION};
  40.  
  41. if(!hasPermissions(this, PERMISSIONS)){
  42. ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
  43. }
  44.  
  45.  
  46. final String deviceId = Utils.getPhoneID((TelephonyManager) getBaseContext().getSystemService(this.TELEPHONY_SERVICE),getContentResolver());
  47. //get android unique id
  48.  
  49. phoneID = Utils.SHA256(deviceId).replace("\n","");
  50.  
  51. _loginButton.setOnClickListener(new View.OnClickListener() {
  52.  
  53. @Override
  54. public void onClick(View v) {
  55. login();
  56. }
  57. });
  58.  
  59. _signupLink.setOnClickListener(new View.OnClickListener() {
  60.  
  61. @Override
  62. public void onClick(View v) {
  63. // Start the Signup activity
  64. Intent intent = new Intent(getApplicationContext(), SignUpActivity.class);
  65. startActivityForResult(intent, REQUEST_SIGNUP);
  66. }
  67. });
  68. }
  69.  
  70. public void login(){
  71. Log.d(TAG, "Login");
  72.  
  73. if (!validate()) {
  74. onLoginFailed();
  75. return;
  76. }
  77.  
  78. _loginButton.setEnabled(false);
  79.  
  80. final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this,
  81. R.style.AppTheme);
  82. progressDialog.setIndeterminate(true);
  83. progressDialog.setMessage("Authenticating...");
  84.  
  85. try {
  86. String email= Utils.SHA256(_emailText.getText().toString()).replace("\n","");
  87. String password= Utils.SHA256(_passwordText.getText().toString()).replace("\n","");
  88. String result="2;" + email + ";" + password+";"+ phoneID + ";" + Utils.getTime();
  89. SSLClient ssl =new SSLClient(getApplicationContext());
  90. String read=Utils.readWriteSSL(getApplicationContext(),result,ssl,_loginButton);
  91. if(!read.equals("ERROR")){
  92. String tokens[]=read.split(",");
  93. sessionKey= tokens[1];
  94. ssl.closeSocket();
  95. progressDialog.show();
  96. }
  97. else
  98. return;
  99.  
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103. new android.os.Handler().postDelayed(
  104. new Runnable() {
  105. public void run() {
  106. // On complete call either onLoginSuccess or onLoginFailed
  107. onLoginSuccess();
  108. // onLoginFailed();
  109. progressDialog.dismiss();
  110. }
  111. }, 3000);
  112. }
  113.  
  114. @Override
  115. public void onBackPressed() {
  116. // disable going back to the MainActivity
  117. moveTaskToBack(true);
  118. }
  119.  
  120. public static boolean hasPermissions(Context context, String... permissions) {
  121. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
  122. for (String permission : permissions) {
  123. if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
  124. return false;
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130.  
  131.  
  132. public void onLoginSuccess() {
  133. _loginButton.setEnabled(true);
  134. try {
  135. String email = Utils.SHA256(_emailText.getText().toString()).replace("\n","");
  136. String result = "5;" +sessionKey +";"+ phoneID + ";" + email + ";" + Utils.getTime();
  137. SSLClient ssl = new SSLClient(getApplicationContext());
  138. String read=Utils.readWriteSSL(getApplicationContext(), result, ssl, _loginButton);
  139. Intent intent=null;
  140. if(read.contains("ERROR")){
  141. return;
  142. }
  143. else if (read.equals("new")) {
  144. intent = new Intent(getBaseContext(), SetupActivity.class);
  145. } else if (read.equals("legal")) {
  146. intent = new Intent(getBaseContext(), HomeActivity.class);
  147. } else if (read.equals("child")) {
  148. intent = new Intent(getBaseContext(), HomeKidActivity.class);
  149. }
  150. //send session varables
  151. if(intent!=null) {
  152. ssl.closeSocket();
  153. intent.putExtra("EMAIL", email);
  154. intent.putExtra("SESSIONKEY", sessionKey);
  155. intent.putExtra("ID",phoneID);
  156. startActivity(intent);
  157. finish();
  158. }
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. }
  162. }
  163.  
  164. public void onLoginFailed() {
  165. Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
  166. _loginButton.setEnabled(true);
  167. }
  168.  
  169. public boolean validate() {
  170. boolean valid = true;
  171.  
  172. String email = _emailText.getText().toString();
  173. String password = _passwordText.getText().toString();
  174.  
  175. if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  176. _emailText.setError("enter a valid email address");
  177. valid = false;
  178. } else {
  179. _emailText.setError(null);
  180. }
  181.  
  182. if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  183. _passwordText.setError("between 4 and 10 alphanumeric characters");
  184. valid = false;
  185. } else {
  186. _passwordText.setError(null);
  187. }
  188.  
  189. return valid;
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement