Android Open Source - foodroid Login






From Project

Back to project page foodroid.

License

The source code is released under:

GNU General Public License

If you think the Android project foodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wmc.ReservationClient;
/*w  ww .j  a  v  a 2  s  . c o m*/
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {
  TextView msg;
  CheckBox chkSaveInput;
  boolean registerMode = false;
  String Server;
  DatabaseHelper db;
  EditText username;
  SharedPreferences.OnSharedPreferenceChangeListener listener;
  SharedPreferences prefs;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("loginpref", "false");
    editor.commit();

    listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
      public void onSharedPreferenceChanged(
          SharedPreferences sharedPreferences, String key) {
        if (key.equals("loginpref")) {
          if (sharedPreferences.getString("loginpref", "").equals(
              "true")) {
            SharedPreferences.Editor editor = sharedPreferences
                .edit();
            editor.putBoolean("SaveInput", chkSaveInput.isChecked());
            editor.putString("login", username.getText().toString());
            editor.putString("loginpref", "");
            editor.commit();

            EndLogin();
          }
        }

      }
    };
    prefs.registerOnSharedPreferenceChangeListener(listener);
    EditText name = (EditText) findViewById(R.id.txtName);
    name.setVisibility(View.INVISIBLE);

    TextView lblName = (TextView) findViewById(R.id.TextViewName);
    lblName.setVisibility(View.INVISIBLE);

    // Typeface font = Typeface.createFromAsset(getAssets(),
    // "BNAZANIN.TTF");
    // lblName.setTypeface(font);

    TextView lblMessage = (TextView) findViewById(R.id.txtMessage);
    lblMessage.setTextColor(Color.BLACK);

    AbsoluteLayout loginLayout = (AbsoluteLayout) findViewById(R.id.loginLayout);
    loginLayout.setBackgroundColor(Color.RED);

    chkSaveInput = (CheckBox) findViewById(R.id.checkBoxSaveInput);

    Resources res = getResources();
    Server = res.getString(R.string.Server);

    db = new DatabaseHelper(this);
  }

  @Override
  protected void onPause() {

    super.onPause();
    prefs.unregisterOnSharedPreferenceChangeListener(listener);
  }

  public void EndLogin() {

    Intent intent = this.getIntent();
    this.setResult(RESULT_OK, intent);
    this.finish();
  }

  public void login(View v) throws Exception {
    if (!registerMode) {
      EditText username = (EditText) findViewById(R.id.txtUserName);
      EditText password = (EditText) findViewById(R.id.txtPassword);

      if (Utility.isNetworkAvailable(this)) {
        HttpClient client = new DefaultHttpClient();
        HttpGet getMethod = new HttpGet("http://" + Server
            + "/RestaurantReservation/resources/login/"
            + username.getText().toString() + "/"
            + password.getText().toString());
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
          String responseBody = client.execute(getMethod,
              responseHandler);
          String result = getWebServiceResult(responseBody);

          if (result.equals("success")) {

            SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("SaveInput", chkSaveInput.isChecked());
            editor.putString("login", username.getText().toString());
            editor.commit();

            Intent intent = this.getIntent();
            // intent.putExtra("SOMETHING", "EXTRAS");
            this.setResult(RESULT_OK, intent);
            this.finish();
          } else {
            msg = (TextView) findViewById(R.id.txtMessage);
            msg.setText("??? ?????? ?? ??? ???? ??? ???? ??? ????");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      } else {
        Utility.sendSMS("log/" + username.getText().toString() + "/"
            + password.getText().toString(), this);
      }
    }
    // register mode
    else {
      if (Utility.isNetworkAvailable(this)) {
        EditText name = (EditText) findViewById(R.id.txtName);
        EditText username = (EditText) findViewById(R.id.txtUserName);
        EditText password = (EditText) findViewById(R.id.txtPassword);

        HttpClient client = new DefaultHttpClient();
        HttpGet getMethod = new HttpGet("http://" + Server
            + "/RestaurantReservation/resources/register/"
            + username.getText().toString() + "/"
            + password.getText().toString() + "/"
            + name.getText().toString());
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
          String responseBody = client.execute(getMethod,
              responseHandler);
          responseBody = responseBody.replace("&lt;", "<")
              .replace("&gt;", ">").replace("&amp;", "&");
          String result = getWebServiceResult(responseBody);

          if (result.equals("success")) {
            String[] fields = { "Username", "Password", "Name" };
            String[] values = { username.getText().toString(),
                password.getText().toString(),
                name.getText().toString() };
            db.InsertRow("user", fields, values);

            SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("SaveInput", chkSaveInput.isChecked());
            editor.putString("login", username.getText().toString());
            editor.commit();

            Intent intent = this.getIntent();
            this.setResult(RESULT_OK, intent);
            this.finish();
          } else if (result.equals("Duplicate")) {
            msg = (TextView) findViewById(R.id.txtMessage);
            msg.setText("??? ?????? ?????? ?? ????");
          } else {
            msg = (TextView) findViewById(R.id.txtMessage);
            msg.setText("??? ?? ?????? ?? ????");
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      } else
        Toast.makeText(this, "??? ?????? ?? ???????", Toast.LENGTH_LONG)
            .show();
    }

  }

  public void register(View v) {
    registerMode = true;

    msg = (TextView) findViewById(R.id.txtMessage);
    msg.setText("");

    EditText name = (EditText) findViewById(R.id.txtName);
    name.setVisibility(View.VISIBLE);
    TextView lblName = (TextView) findViewById(R.id.TextViewName);
    lblName.setVisibility(View.VISIBLE);

    Button btnLogin = (Button) findViewById(R.id.buttonLogin);
    btnLogin.setText("??? ???");

    Button btnRegister = (Button) findViewById(R.id.buttonRegister);
    btnRegister.setVisibility(View.INVISIBLE);
  }

  String getWebServiceResult(String raw) throws Exception {
    String value = "";
    String result = "";
    DocumentBuilder builder = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));
    NodeList res = doc.getElementsByTagName("string");

    for (int i = 0; i < res.getLength(); i++) {
      Element re = (Element) res.item(i);
      value = "";
      for (int j = 0; j < re.getChildNodes().getLength(); j++)
        value += re.getChildNodes().item(j).getNodeValue();
      result = value;
    }
    return result;
  }

}




Java Source Code List

com.adp.ADPWsSample.java
com.adp.BalanceResult.java
com.adp.ChangePasswordResponse.java
com.adp.ChangePassword.java
com.adp.GetBalanceResponse.java
com.adp.GetBalance.java
com.adp.GetStatusResponse.java
com.adp.GetStatus.java
com.adp.IncomingMessage.java
com.adp.JaxRpcMessagingServiceServiceLocator.java
com.adp.JaxRpcMessagingServiceService.java
com.adp.JaxRpcMessagingService.java
com.adp.MessageObject.java
com.adp.MessagingServiceSoapBindingStub.java
com.adp.MultiAddressMessageObject.java
com.adp.OutgoingMessage.java
com.adp.ReceiveResponse.java
com.adp.ReceiveResult.java
com.adp.Receive.java
com.adp.ReportResponse.java
com.adp.ReportResult.java
com.adp.Report.java
com.adp.Result.java
com.adp.SendMultipleResponse.java
com.adp.SendMultiple.java
com.adp.SendResponse.java
com.adp.SendResult.java
com.adp.Send.java
com.adp.StatusReportResponse.java
com.adp.StatusReportResult.java
com.adp.StatusReportType0.java
com.adp.StatusReport.java
com.sba.util.DateFields.java
com.sba.util.PersianCalendar.java
com.wmc.Registration.BranchlistResource.java
com.wmc.Registration.CommentListBean.java
com.wmc.Registration.CommentResource.java
com.wmc.Registration.CommentlistResource.java
com.wmc.Registration.FoodListResource.java
com.wmc.Registration.LoginResource.java
com.wmc.Registration.OrderBean.java
com.wmc.Registration.OrderListBean.java
com.wmc.Registration.OrderResource.java
com.wmc.Registration.RegisterResource.java
com.wmc.Registration.ReserveListBean.java
com.wmc.Registration.Settings.java
com.wmc.ReservationClient.Account.java
com.wmc.ReservationClient.BranchList.java
com.wmc.ReservationClient.BranchPage.java
com.wmc.ReservationClient.Branch.java
com.wmc.ReservationClient.Comment.java
com.wmc.ReservationClient.DatabaseHelper.java
com.wmc.ReservationClient.Favorite.java
com.wmc.ReservationClient.FoodList.java
com.wmc.ReservationClient.FoodPage.java
com.wmc.ReservationClient.Food.java
com.wmc.ReservationClient.Login.java
com.wmc.ReservationClient.Main.java
com.wmc.ReservationClient.OrderList.java
com.wmc.ReservationClient.Order.java
com.wmc.ReservationClient.Search.java
com.wmc.ReservationClient.SmsMessageReceiver.java
com.wmc.ReservationClient.Table.java
com.wmc.ReservationClient.Update.java
com.wmc.ReservationClient.Utility.java
smsserver.CommentSMS.java
smsserver.LoginSMS.java
smsserver.OrderSMS.java
smsserver.Settings.java
smsserver.SmsServer.java