Back to project page foodroid.
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.
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("<", "<") .replace(">", ">").replace("&", "&"); 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; } }