2
0

LoginActivity.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.dev.deben.implementation;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.app.ProgressDialog;
  13. import org.json.JSONException;
  14. import org.json.simple.JSONObject;
  15. import org.json.simple.JSONValue;
  16. import java.io.IOException;
  17. import java.io.StringWriter;
  18. import java.util.HashMap;
  19. import android.os.StrictMode;
  20. import com.google.android.gms.common.ConnectionResult;
  21. import com.google.android.gms.common.GoogleApiAvailability;
  22. public class LoginActivity extends AppCompatActivity {
  23. function fn = new function(this);
  24. TextView error;
  25. Button login;
  26. String regCode;
  27. EditText userField;
  28. EditText passField;
  29. EditText codeField;
  30. @Override
  31. public void onBackPressed() {
  32. }
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_login);
  37. error = (TextView) findViewById(R.id.error);
  38. error.setVisibility(View.INVISIBLE);
  39. login = (Button) findViewById(R.id.login);
  40. userField = (EditText) findViewById(R.id.username);
  41. passField = (EditText) findViewById(R.id.password);
  42. codeField = (EditText) findViewById(R.id.regCode);
  43. Uri param = getIntent().getData();
  44. if(param != null) {
  45. System.out.println("URI=" + param.toString());
  46. if (param.getQueryParameter("rid") != null) {
  47. HashMap<String, String> set = new HashMap<>();
  48. set.put("requestId", param.getQueryParameter("rid"));
  49. try {
  50. fn.writeSetting(set);
  51. } catch (IOException | JSONException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. if (param.getQueryParameter("rcd") != null) {
  56. codeField.setText(param.getQueryParameter("rcd"));
  57. }
  58. }
  59. if (android.os.Build.VERSION.SDK_INT > 9) {
  60. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  61. StrictMode.setThreadPolicy(policy);
  62. }
  63. if(checkPlayServices()) {
  64. Intent intent = new Intent(this, RegisterIntentService.class);
  65. startService(intent);
  66. }
  67. try {
  68. fn.init();
  69. if(!fn.readSetting("username").equals("") && !fn.readSetting("registerCode").equals("")) {
  70. if(fn.checkRegCode(fn.readSetting("registerCode"), fn.readSetting("username"))) {
  71. //USER ALREADY LOGGED IN
  72. fn.redirect("Main");
  73. }
  74. else {
  75. //NO LOGIN
  76. }
  77. }
  78. } catch (IOException | JSONException e) {
  79. e.printStackTrace();
  80. }
  81. Button button = (Button) findViewById(R.id.login);
  82. button.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. try {
  86. login(userField.getText().toString(), passField.getText().toString(), codeField.getText().toString());
  87. } catch (IOException | JSONException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. });
  92. }
  93. private void login(String username, String password, String code) throws IOException, JSONException {
  94. error.setVisibility(View.INVISIBLE);
  95. error.setText("");
  96. if(username == null || username.equals("") && username.length() < 5) {
  97. error.setText("Username too short");
  98. error.setVisibility(View.VISIBLE);
  99. return;
  100. }
  101. if(password == null || password.equals("") && password.length() < 5) {
  102. error.setText("Password too short");
  103. error.setVisibility(View.VISIBLE);
  104. return;
  105. }
  106. if(code == null || code.equals("") || code.length() != 4) {
  107. error.setText("Invalid register code");
  108. error.setVisibility(View.VISIBLE);
  109. return;
  110. }
  111. ProgressDialog progress = new ProgressDialog(this);
  112. progress.setTitle("Registering");
  113. progress.setMessage("Processing request...");
  114. progress.setCancelable(false);
  115. progress.show();
  116. JSONObject info = new JSONObject();
  117. info.put("serviceType", "GCM");
  118. info.put("serviceNumber", fn.readSetting("serviceNumber"));
  119. info.put("deviceId", fn.readSetting("deviceId"));
  120. info.put("notificationId", fn.readSetting("notificationId"));
  121. info.put("apiKey", fn.readSetting("apiKey"));
  122. JSONObject json = new JSONObject();
  123. json.put("function", "register");
  124. json.put("requestId", fn.readSetting("requestId"));
  125. json.put("registerCode", code);
  126. json.put("username", username);
  127. json.put("password", password);
  128. json.put("userInfo", info);
  129. StringWriter out = new StringWriter();
  130. json.writeJSONString(out);
  131. String response = fn.makeRequest("GET", fn.readSetting("serverUrl"), out.toString());
  132. System.out.println(response);
  133. if(response.length() == 0 || response == null || response == "") {
  134. progress.dismiss();
  135. error.setText("Request Failed. Try again later");
  136. error.setVisibility(View.VISIBLE);
  137. return;
  138. }
  139. else {
  140. Object obj = JSONValue.parse(response);
  141. JSONObject res = (JSONObject) obj;
  142. if(res.get("result").equals("0") || Integer.parseInt(res.get("result").toString()) == 0) {
  143. HashMap<String, String> set = new HashMap<>();
  144. set.put("username", username);
  145. set.put("password", password);
  146. set.put("registerCode", code);
  147. set.put("requestId", "0");
  148. fn.writeSetting(set);
  149. progress.dismiss();
  150. fn.redirect("Main");
  151. }
  152. else {
  153. progress.dismiss();
  154. error.setText("Error "+res.get("result")+": "+res.get("resultText"));
  155. error.setVisibility(View.VISIBLE);
  156. return;
  157. }
  158. }
  159. return;
  160. }
  161. private boolean checkPlayServices() {
  162. GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
  163. int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
  164. if (resultCode != ConnectionResult.SUCCESS) {
  165. if (apiAvailability.isUserResolvableError(resultCode)) {
  166. apiAvailability.getErrorDialog(this, resultCode, 9000)
  167. .show();
  168. error.setText("Service must be up to date in order to use this app");
  169. error.setVisibility(View.VISIBLE);
  170. login.setEnabled(false);
  171. } else {
  172. error.setText("Device not supported");
  173. error.setVisibility(View.VISIBLE);
  174. login.setEnabled(false);
  175. finish();
  176. }
  177. return false;
  178. }
  179. return true;
  180. }
  181. }