Java tutorial
/* * Copyright (C) 2011 - Gareth Llewellyn * * This file is part of DediPortal - http://blog.NetworksAreMadeOfString.co.uk/DediPortal/ * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/> */ package com.dedipower.portal.android; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.dedipower.portal.android.unofficial.R; public class Launcher extends Activity { PortalAPI API = new PortalAPI(); ProgressDialog dialog; String APIResponse = ""; String AcctCode, Email, Password = ""; private SharedPreferences settings = null; Thread peformLogin; Handler handler; List<NameValuePair> Credentials = new ArrayList<NameValuePair>(3); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.launcher); dialog = new ProgressDialog(this); dialog.setMessage("Logging in....."); //Get our settings settings = getSharedPreferences("Dedipower", 0); ManageSettings(); handler = new Handler() { public void handleMessage(Message msg) { dialog.dismiss(); //Apparantly stopping threads is no longer supported //peformLogin.stop(); if (APIResponse == "true") { //Tell the user Toast.makeText(Launcher.this, "Login Successful", Toast.LENGTH_SHORT).show(); //Check the "remember details checkbox" CheckBox Remember = (CheckBox) findViewById(R.id.RememberDetailsCheckBox); SharedPreferences.Editor editor = settings.edit(); if (Remember.isChecked() == true) { editor.putString("AcctCode", AcctCode); editor.putString("Email", Email); editor.putString("Password", Password); editor.putBoolean("Remember", true); editor.commit(); } else { editor.putString("AcctCode", ""); editor.putString("Email", ""); editor.putString("Password", ""); editor.putBoolean("Remember", false); editor.commit(); } //Do extent stuff Intent LauncherIntent = new Intent(Launcher.this, PortalLanding.class); LauncherIntent.putExtra("sessionid", API.SessionID); Launcher.this.startActivity(LauncherIntent); finish(); } else { Toast.makeText(Launcher.this, "Login Failed: " + APIResponse, Toast.LENGTH_SHORT).show(); } } }; Button LoginButton = (Button) findViewById(R.id.LoginButton); LoginButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { dialog.show(); EditText AcctCodeEditText = (EditText) findViewById(R.id.AcctCodeInput); EditText EmailEditText = (EditText) findViewById(R.id.EmailInput); EditText PasswordEditText = (EditText) findViewById(R.id.PasswordInput); AcctCode = AcctCodeEditText.getText().toString(); Email = EmailEditText.getText().toString(); Password = PasswordEditText.getText().toString(); Credentials.add(new BasicNameValuePair("account", AcctCode)); Credentials.add(new BasicNameValuePair("username", Email)); Credentials.add(new BasicNameValuePair("password", Password)); CreateThread(); peformLogin.start(); /*String APIResponse = API.Login(Credentials); if(APIResponse == "true") { //Tell the user Toast.makeText(Launcher.this, "Login Successful", Toast.LENGTH_SHORT).show(); //Check the "remember details checkbox" CheckBox Remember = (CheckBox) findViewById(R.id.RememberDetailsCheckBox); SharedPreferences.Editor editor = settings.edit(); if(Remember.isChecked() == true) { editor.putString("AcctCode", AcctCodeEditText.getText().toString()); editor.putString("Email", EmailEditText.getText().toString()); editor.putString("Password", PasswordEditText.getText().toString()); editor.putBoolean("Remember", true); editor.commit(); } else { editor.putString("AcctCode", ""); editor.putString("Email", ""); editor.putString("Password", ""); editor.putBoolean("Remember", false); editor.commit(); } //Do extent stuff Intent LauncherIntent = new Intent(Launcher.this, PortalLanding.class); LauncherIntent.putExtra("sessionid", API.SessionID); Launcher.this.startActivity(LauncherIntent); finish(); } else { Toast.makeText(Launcher.this, "Login Failed: " + APIResponse, Toast.LENGTH_SHORT).show(); }*/ } }); } public void CreateThread() { peformLogin = new Thread() { public void run() { APIResponse = API.Login(Credentials); handler.sendEmptyMessage(0); return; } }; } public void ManageSettings() { EditText AcctCodeEditText = (EditText) findViewById(R.id.AcctCodeInput); EditText EmailEditText = (EditText) findViewById(R.id.EmailInput); EditText PasswordEditText = (EditText) findViewById(R.id.PasswordInput); CheckBox Remember = (CheckBox) findViewById(R.id.RememberDetailsCheckBox); AcctCodeEditText.setText(settings.getString("AcctCode", "")); EmailEditText.setText(settings.getString("Email", "")); PasswordEditText.setText(settings.getString("Password", "")); Remember.setChecked(settings.getBoolean("Remember", false)); } }