Advertisement
newkidd

Signin activity

Mar 27th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URI;
  3. import java.net.URISyntaxException;
  4. import java.net.URL;
  5.  
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.params.CoreProtocolPNames;
  13. import org.apache.http.util.EntityUtils;
  14.  
  15. import com.stemsoftware.reap.R;
  16.  
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.net.wifi.WifiInfo;
  20. import android.net.wifi.WifiManager;
  21. import android.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.telephony.TelephonyManager;
  24. import android.util.Log;
  25. import android.view.Menu;
  26. import android.view.View;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.Toast;
  30.  
  31. public class SwitchUserLogin extends Activity implements View.OnClickListener {
  32.  
  33. private EditText etUser, etPassword;
  34. private Button bLogin;
  35. private String u, p, a, b, c, d, mLogin, urlLogin, devLogin, dLogin;
  36. private final String KEY_LOGIN_URL = "https://..../api.php";
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.server_auth_dialog);
  42. etUser = (EditText) findViewById(R.id.username_edit);
  43. etPassword = (EditText) findViewById(R.id.password_edit);
  44. bLogin = (Button) findViewById(R.id.buttonLogin);
  45. bLogin.setOnClickListener(this);
  46. }
  47. @Override
  48. public void onClick(View v) {
  49. bLogin.setEnabled(false);
  50. startLoginCrap();
  51. }
  52.  
  53. private void startLoginCrap(){
  54. TelephonyManager d = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  55. String Device=d.getDeviceId();
  56. if (Device == null){
  57. WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  58. WifiInfo wInfo = wifiManager.getConnectionInfo();
  59. String c = wInfo.getMacAddress();
  60. //Log.w("StringC", Device);
  61. Log.w("MacAddress", wInfo.getMacAddress());
  62. }else{
  63. c = d.getDeviceId();
  64. Log.w("DeviceID", c);
  65. }
  66.  
  67. u = etUser.getText().toString();
  68. p = etPassword.getText().toString();
  69. a = "?action=login";
  70. b = "?action=SetDeviceID";
  71. mLogin = "false";
  72. dLogin = "false";
  73. //Log.w("DeviceID", c);
  74. urlLogin= (KEY_LOGIN_URL + a + "&user=" + u + "&pass=" + p);
  75. devLogin= (KEY_LOGIN_URL + b + "&user=" + u + "&device=" + c);
  76. new VerifyLogin().execute(urlLogin);
  77. //Log.w("LoginStatus", mLogin);
  78. new setDeviceID().execute(devLogin);
  79. Log.w("DeviceStatus", devLogin);
  80.  
  81. }
  82.  
  83. private class VerifyLogin extends AsyncTask<String, Void, String> {
  84. String msgl = "";
  85. HttpClient httpClientl;
  86. HttpGet httpGetl;
  87. HttpResponse responsel;
  88. HttpEntity entityl;
  89.  
  90. @Override
  91. protected String doInBackground(String... urls) {
  92. try {
  93. httpClientl = new DefaultHttpClient();
  94. URI loginl = new URI(urls[0]);
  95. httpGetl = new HttpGet(loginl);
  96. responsel = httpClientl.execute(httpGetl);
  97. entityl = responsel.getEntity();
  98. msgl = EntityUtils.toString(entityl);
  99.  
  100. } catch (ClientProtocolException e) {
  101. e.printStackTrace();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. } catch (URISyntaxException e) {
  105. e.printStackTrace();
  106. }
  107.  
  108. Log.v("msgl", msgl);
  109. if (msgl.equals("\"incorrect!\"")){
  110. mLogin="false";
  111. }
  112. else{
  113. mLogin="true";
  114. }
  115. return mLogin;
  116. }
  117.  
  118. @Override
  119. protected void onPostExecute(String result) {
  120. //super.onPostExecute(result);
  121.  
  122. if (mLogin == "true") {
  123. Toast.makeText(getApplicationContext(), "Login Successful",
  124. Toast.LENGTH_SHORT).show();
  125. //mLogin="true";
  126. Log.v("LoginPass", mLogin);
  127. Log.v("LoginResult", result);
  128. bLogin.setEnabled(false);
  129.  
  130. } else {
  131. Toast.makeText(getApplicationContext(), "Invalid Password",
  132. Toast.LENGTH_LONG).show();
  133. bLogin.setEnabled(true);
  134. Log.v("mLoginResult", mLogin);
  135. Log.w("LoginFail", result);
  136. }
  137. }
  138.  
  139. }
  140. private class setDeviceID extends AsyncTask<String, Void, String> {
  141. String msgD = "";
  142. HttpClient httpClientD;
  143. HttpGet httpGetD;
  144. HttpResponse responseD;
  145. HttpEntity entityD;
  146.  
  147. @Override
  148. protected String doInBackground(String... urls) {
  149. try {
  150. httpClientD = new DefaultHttpClient();
  151. URI loginD = new URI(urls[0]);
  152. httpGetD = new HttpGet(loginD);
  153. responseD = httpClientD.execute(httpGetD);
  154. entityD = responseD.getEntity();
  155. msgD = EntityUtils.toString(entityD);
  156.  
  157. } catch (ClientProtocolException e) {
  158. e.printStackTrace();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. } catch (URISyntaxException e) {
  162. e.printStackTrace();
  163. }
  164. //Log.v("deviceLoginMSGd", msgD);
  165. if (msgD.equals("\"Device ID NOT Set\"")){
  166. dLogin="false";
  167. }
  168. else{
  169. dLogin="true";
  170. }
  171. return dLogin;
  172. }
  173.  
  174. @Override
  175. protected void onPostExecute(String result) {
  176.  
  177. if (dLogin == "true") {
  178. Toast.makeText(getApplicationContext(), "Device ID Set",
  179. Toast.LENGTH_SHORT).show();
  180. Log.v("deviceLoginPass", dLogin);
  181. Log.v("deviceLoginResult", result);
  182. bLogin.setEnabled(false);
  183.  
  184. } else {
  185. Toast.makeText(getApplicationContext(), "Device ID NOT Set",
  186. Toast.LENGTH_LONG).show();
  187. Log.v("deviceLoginResult", dLogin);
  188. Log.w("deviceLoginFail", result);
  189. bLogin.setEnabled(true);
  190. }
  191. }
  192.  
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement