LoginActivity.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Feel free to copy/use it for your own project.
  3. * Keep in mind that it took me several days/weeks, beers and asperines to make this.
  4. * So be nice, and give me some credit, I won't bite and it won't hurt you.
  5. *
  6. * Created by Deben Oldert
  7. */
  8. package com.dev.deben.implementation;
  9. import android.app.ProgressDialog;
  10. import android.content.Intent;
  11. import android.graphics.Color;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.os.StrictMode;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.TextView;
  20. import com.google.android.gms.common.ConnectionResult;
  21. import com.google.android.gms.common.GoogleApiAvailability;
  22. import com.loopj.android.http.AsyncHttpClient;
  23. import com.loopj.android.http.AsyncHttpResponseHandler;
  24. import org.json.JSONException;
  25. import org.json.simple.JSONObject;
  26. import org.json.simple.JSONValue;
  27. import java.io.IOException;
  28. import java.util.HashMap;
  29. import cz.msebera.android.httpclient.Header;
  30. import cz.msebera.android.httpclient.entity.StringEntity;
  31. public class LoginActivity extends AppCompatActivity {
  32. function fn = new function(this);
  33. TextView err;
  34. Button login;
  35. EditText userField;
  36. EditText passField;
  37. EditText codeField;
  38. @Override
  39. public void onBackPressed() {
  40. }
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_login);
  45. err = (TextView) findViewById(R.id.error);
  46. err.setVisibility(View.INVISIBLE);
  47. login = (Button) findViewById(R.id.login);
  48. userField = (EditText) findViewById(R.id.username);
  49. passField = (EditText) findViewById(R.id.password);
  50. codeField = (EditText) findViewById(R.id.regCode);
  51. Uri param = getIntent().getData();
  52. if(param != null) {
  53. System.out.println("URI=" + param.toString());
  54. if (param.getQueryParameter("rid") != null) {
  55. HashMap<String, String> set = new HashMap<>();
  56. set.put("requestId", param.getQueryParameter("rid"));
  57. try {
  58. fn.writeSetting(set);
  59. } catch (IOException | JSONException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. if (param.getQueryParameter("rcd") != null) {
  64. codeField.setText(param.getQueryParameter("rcd"));
  65. }
  66. }
  67. if (android.os.Build.VERSION.SDK_INT > 9) {
  68. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  69. StrictMode.setThreadPolicy(policy);
  70. }
  71. if(checkPlayServices()) {
  72. Intent intent = new Intent(this, RegisterIntentService.class);
  73. startService(intent);
  74. }
  75. try {
  76. fn.init();
  77. if(!fn.readSetting("username").equals("") && !fn.readSetting("registerCode").equals("")) {
  78. if(fn.checkRegCode(fn.readSetting("registerCode"), fn.readSetting("username"))) {
  79. //USER ALREADY LOGGED IN
  80. fn.redirect("Main");
  81. }
  82. else {
  83. //NO LOGIN
  84. }
  85. }
  86. } catch (IOException | JSONException e) {
  87. e.printStackTrace();
  88. }
  89. Button button = (Button) findViewById(R.id.login);
  90. button.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. try {
  94. login(userField.getText().toString(), passField.getText().toString(), codeField.getText().toString());
  95. } catch (IOException | JSONException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. });
  100. }
  101. private void login(String usr, String pass, String cod) throws IOException, JSONException {
  102. final String username = usr;
  103. final String password = pass;
  104. final String reqCode = cod;
  105. final ProgressDialog progress = new ProgressDialog(this);
  106. err.setVisibility(View.INVISIBLE);
  107. err.setText("");
  108. if(username == null || username.equals("") && username.length() < 5) {
  109. err.setText("Username too short");
  110. err.setVisibility(View.VISIBLE);
  111. return;
  112. }
  113. if(password == null || password.equals("") && password.length() < 5) {
  114. err.setText("Password too short");
  115. err.setVisibility(View.VISIBLE);
  116. return;
  117. }
  118. if(reqCode == null || reqCode.equals("") || reqCode.length() != 4) {
  119. err.setText("Invalid register code");
  120. err.setVisibility(View.VISIBLE);
  121. return;
  122. }
  123. progress.setTitle("Registering your device");
  124. progress.setMessage("Processing...");
  125. progress.setCancelable(false);
  126. progress.show();
  127. JSONObject info = new JSONObject();
  128. info.put("serviceType", "GCM");
  129. info.put("serviceNumber", fn.readSetting("serviceNumber"));
  130. info.put("deviceId", fn.readSetting("deviceId"));
  131. info.put("notificationId", fn.readSetting("notificationId"));
  132. info.put("apiKey", fn.readSetting("apiKey"));
  133. JSONObject json = new JSONObject();
  134. json.put("function", "register");
  135. json.put("requestId", fn.readSetting("requestId"));
  136. json.put("registerCode", reqCode);
  137. json.put("username", username);
  138. json.put("password", password);
  139. json.put("userInfo", info);
  140. AsyncHttpClient client = new AsyncHttpClient();
  141. client.post(this, fn.readSetting("serverUrl"), new StringEntity(json.toJSONString()), "application/json", new AsyncHttpResponseHandler() {
  142. @Override
  143. public void onFailure(int code, Header[] headers, byte[] responseBody, Throwable error) {
  144. err.setTextColor(Color.parseColor("#ff1700"));
  145. err.setText("HTTP ERROR: " + code);
  146. err.setVisibility(View.VISIBLE);
  147. progress.dismiss();
  148. }
  149. @Override
  150. public void onSuccess(int code, Header[] headers, byte[] responseBody) {
  151. String res = new String(responseBody);
  152. Object obj = JSONValue.parse(res);
  153. JSONObject response = (JSONObject) obj;
  154. if (response.get("result") != null && response.get("result").equals("0") || Integer.parseInt(response.get("result").toString()) == 0) {
  155. HashMap<String, String> set = new HashMap<>();
  156. set.put("username", username);
  157. set.put("password", password);
  158. set.put("registerCode", reqCode);
  159. set.put("requestId", "0");
  160. try {
  161. fn.writeSetting(set);
  162. } catch (IOException | JSONException e) {
  163. e.printStackTrace();
  164. }
  165. progress.dismiss();
  166. fn.redirect("Main");
  167. err.setVisibility(View.VISIBLE);
  168. progress.dismiss();
  169. } else {
  170. progress.dismiss();
  171. err.setText("Error " + response.get("result") + ": " + response.get("resultText"));
  172. err.setVisibility(View.VISIBLE);
  173. return;
  174. }
  175. }
  176. });
  177. return;
  178. }
  179. private boolean checkPlayServices() {
  180. GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
  181. int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
  182. if (resultCode != ConnectionResult.SUCCESS) {
  183. if (apiAvailability.isUserResolvableError(resultCode)) {
  184. apiAvailability.getErrorDialog(this, resultCode, 9000)
  185. .show();
  186. err.setText("Service must be up to date in order to use this app");
  187. err.setVisibility(View.VISIBLE);
  188. login.setEnabled(false);
  189. } else {
  190. err.setText("Device not supported");
  191. err.setVisibility(View.VISIBLE);
  192. login.setEnabled(false);
  193. finish();
  194. }
  195. return false;
  196. }
  197. return true;
  198. }
  199. }