Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lennon;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.support.v7.app.ActionBarActivity;
- import android.annotation.SuppressLint;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.TextureView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Login extends ActionBarActivity {
- private Button login;
- private Button register;
- private EditText username;
- private EditText password;
- private TextView hiperlink;
- final Context cont=this;
- private UserSessionManager session;
- private int go=0;
- String name;
- String pass;
- String mail;
- ConnectionClass connectionclass;
- String usernameS;
- String datets;
- String call, db, un, passwords;
- Connection connect;
- public boolean postoi = false;
- ResultSet rs;
- @SuppressLint("NewApi")
- private Connection CONN(String _user, String _pass, String _DB,
- String _server) {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection conn = null;
- String ConnURL = null;
- try {
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
- + "databaseName=" + _DB + ";user=" + _user + ";password="
- + _pass + ";";
- conn = DriverManager.getConnection(ConnURL);
- } catch (SQLException se) {
- Log.e("ERRO", se.getMessage());
- } catch (ClassNotFoundException e) {
- Log.e("ERRO", e.getMessage());
- } catch (Exception e) {
- Log.e("ERRO", e.getMessage());
- }
- return conn;
- }
- public void onClick(View v)
- {
- Intent o = new Intent(Login.this,ForgotPassword.class);
- startActivity(o);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- initialize();
- session = new UserSessionManager(getApplicationContext());
- // Toast.makeText(getApplicationContext(),
- // "User Login Status: " + session.isUserLoggedIn(),
- // Toast.LENGTH_LONG).show();
- Singleton.getInstance().username = PreferenceManager.getDefaultSharedPreferences(cont).getString("username", Singleton.getInstance().username);
- Singleton.getInstance().password = PreferenceManager.getDefaultSharedPreferences(cont).getString("password", Singleton.getInstance().password);
- Singleton.getInstance().email = PreferenceManager.getDefaultSharedPreferences(cont).getString("email", Singleton.getInstance().email);
- String userN= Singleton.getInstance().username;
- String pasS= Singleton.getInstance().password;
- String emaill= Singleton.getInstance().email;
- int a=userN.length();
- int b=pasS.length();
- if(a!=0 && b!=0)
- {
- session.createUserLoginSession(userN, emaill);
- Intent i = new Intent(getApplicationContext(), TypesActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- // Add new Flag to start new Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(i);
- finish();
- }
- login.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- new LoginTask().execute();
- InputMethodManager imm = (InputMethodManager)Login.this.getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(password.getWindowToken(), 0);
- }
- });
- register.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Login.this,Register.class);
- startActivity(intent);
- }
- });
- }
- class LoginTask extends AsyncTask<String, Void, String>
- {
- private ProgressDialog pDialog;
- private String rez;
- protected void onPreExecute (){
- super.onPreExecute();
- // Showing progress dialog
- pDialog = new ProgressDialog(Login.this);
- pDialog.setMessage("Pleace wait...");
- pDialog.setCancelable(false);
- pDialog.show();
- }
- protected void onProgressUpdate(Integer...a){
- }
- protected void onPostExecute(String result) {
- if(result.equals("ace"))
- {
- Intent i = new Intent(getApplicationContext(), TypesActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- // Add new Flag to start new Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(i);
- finish();
- }
- else if(!result.equals("ace"))
- {
- Toast.makeText(getApplicationContext(),
- "Sorry, wrong credentials!!!",
- Toast.LENGTH_LONG).show();
- pDialog.cancel();
- }
- }
- @Override
- protected String doInBackground(String... params) {
- String usr = username.getText().toString();
- String psw = password.getText().toString();
- connectionclass = new ConnectionClass();
- call = connectionclass.getip();
- un = connectionclass.getun();
- passwords = connectionclass.getpassword();
- db = connectionclass.getdb();
- connect = CONN(un, passwords, db, call);
- // TODO Auto-generated method stub
- Log.e("NAME",usr);
- Log.e("PASS",psw);
- String querycmd = "select * from Korisnik where username='"+usr+"' and password='"+psw+"'";
- // String querycmd = "select * from Korisnik";
- try{
- Statement statement = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs = statement.executeQuery(querycmd);
- if (rs != null && rs.next()) {
- name=rs.getString("username");
- pass=rs.getString("password");
- mail=rs.getString("email");
- session.createUserLoginSession(usr, mail);
- rez="ace";
- Singleton.getInstance().username=usr;
- Singleton.getInstance().password=psw;
- Singleton.getInstance().email=mail;
- PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("username", Singleton.getInstance().username).commit();
- PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("password", Singleton.getInstance().password).commit();
- PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("email", Singleton.getInstance().email).commit();
- // Starting MainActivity
- }
- else
- {
- rez="cackoooo";
- // user didn't entered username or password
- Toast.makeText(getApplicationContext(),
- "Sorry, wrong credentials!!!",
- Toast.LENGTH_LONG).show();
- }
- }
- catch(Exception e){
- }
- return rez;
- }
- }
- private void initialize() {
- login =(Button)findViewById(R.id.login);
- register =(Button)findViewById(R.id.activityRegister);
- username =(EditText)findViewById(R.id.username);
- password =(EditText)findViewById(R.id.password);
- hiperlink=(TextView)findViewById(R.id.hiperlink);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.login, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement