Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.CoreProtocolPNames;
- import org.apache.http.util.EntityUtils;
- import com.stemsoftware.reap.R;
- import android.app.Activity;
- import android.content.Context;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class SwitchUserLogin extends Activity implements View.OnClickListener {
- private EditText etUser, etPassword;
- private Button bLogin;
- private String u, p, a, b, c, d, mLogin, urlLogin, devLogin, dLogin;
- private final String KEY_LOGIN_URL = "https://..../api.php";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.server_auth_dialog);
- etUser = (EditText) findViewById(R.id.username_edit);
- etPassword = (EditText) findViewById(R.id.password_edit);
- bLogin = (Button) findViewById(R.id.buttonLogin);
- bLogin.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- bLogin.setEnabled(false);
- startLoginCrap();
- }
- private void startLoginCrap(){
- TelephonyManager d = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- String Device=d.getDeviceId();
- if (Device == null){
- WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- WifiInfo wInfo = wifiManager.getConnectionInfo();
- String c = wInfo.getMacAddress();
- //Log.w("StringC", Device);
- Log.w("MacAddress", wInfo.getMacAddress());
- }else{
- c = d.getDeviceId();
- Log.w("DeviceID", c);
- }
- u = etUser.getText().toString();
- p = etPassword.getText().toString();
- a = "?action=login";
- b = "?action=SetDeviceID";
- mLogin = "false";
- dLogin = "false";
- //Log.w("DeviceID", c);
- urlLogin= (KEY_LOGIN_URL + a + "&user=" + u + "&pass=" + p);
- devLogin= (KEY_LOGIN_URL + b + "&user=" + u + "&device=" + c);
- new VerifyLogin().execute(urlLogin);
- //Log.w("LoginStatus", mLogin);
- new setDeviceID().execute(devLogin);
- Log.w("DeviceStatus", devLogin);
- }
- private class VerifyLogin extends AsyncTask<String, Void, String> {
- String msgl = "";
- HttpClient httpClientl;
- HttpGet httpGetl;
- HttpResponse responsel;
- HttpEntity entityl;
- @Override
- protected String doInBackground(String... urls) {
- try {
- httpClientl = new DefaultHttpClient();
- URI loginl = new URI(urls[0]);
- httpGetl = new HttpGet(loginl);
- responsel = httpClientl.execute(httpGetl);
- entityl = responsel.getEntity();
- msgl = EntityUtils.toString(entityl);
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- Log.v("msgl", msgl);
- if (msgl.equals("\"incorrect!\"")){
- mLogin="false";
- }
- else{
- mLogin="true";
- }
- return mLogin;
- }
- @Override
- protected void onPostExecute(String result) {
- //super.onPostExecute(result);
- if (mLogin == "true") {
- Toast.makeText(getApplicationContext(), "Login Successful",
- Toast.LENGTH_SHORT).show();
- //mLogin="true";
- Log.v("LoginPass", mLogin);
- Log.v("LoginResult", result);
- bLogin.setEnabled(false);
- } else {
- Toast.makeText(getApplicationContext(), "Invalid Password",
- Toast.LENGTH_LONG).show();
- bLogin.setEnabled(true);
- Log.v("mLoginResult", mLogin);
- Log.w("LoginFail", result);
- }
- }
- }
- private class setDeviceID extends AsyncTask<String, Void, String> {
- String msgD = "";
- HttpClient httpClientD;
- HttpGet httpGetD;
- HttpResponse responseD;
- HttpEntity entityD;
- @Override
- protected String doInBackground(String... urls) {
- try {
- httpClientD = new DefaultHttpClient();
- URI loginD = new URI(urls[0]);
- httpGetD = new HttpGet(loginD);
- responseD = httpClientD.execute(httpGetD);
- entityD = responseD.getEntity();
- msgD = EntityUtils.toString(entityD);
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- //Log.v("deviceLoginMSGd", msgD);
- if (msgD.equals("\"Device ID NOT Set\"")){
- dLogin="false";
- }
- else{
- dLogin="true";
- }
- return dLogin;
- }
- @Override
- protected void onPostExecute(String result) {
- if (dLogin == "true") {
- Toast.makeText(getApplicationContext(), "Device ID Set",
- Toast.LENGTH_SHORT).show();
- Log.v("deviceLoginPass", dLogin);
- Log.v("deviceLoginResult", result);
- bLogin.setEnabled(false);
- } else {
- Toast.makeText(getApplicationContext(), "Device ID NOT Set",
- Toast.LENGTH_LONG).show();
- Log.v("deviceLoginResult", dLogin);
- Log.w("deviceLoginFail", result);
- bLogin.setEnabled(true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement