Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. package com.example.lennon;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import android.support.v7.app.ActionBarActivity;
  14. import android.annotation.SuppressLint;
  15. import android.app.ProgressDialog;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.os.AsyncTask;
  19. import android.os.Bundle;
  20. import android.os.StrictMode;
  21. import android.preference.PreferenceManager;
  22. import android.util.Log;
  23. import android.view.Menu;
  24. import android.view.MenuItem;
  25. import android.view.TextureView;
  26. import android.view.View;
  27. import android.view.View.OnClickListener;
  28. import android.view.inputmethod.InputMethodManager;
  29. import android.widget.Button;
  30. import android.widget.EditText;
  31. import android.widget.TextView;
  32. import android.widget.Toast;
  33.  
  34. public class Login extends ActionBarActivity {
  35.  
  36. private Button login;
  37. private Button register;
  38. private EditText username;
  39. private EditText password;
  40. private TextView hiperlink;
  41. final Context cont=this;
  42. private UserSessionManager session;
  43. private int go=0;
  44. String name;
  45. String pass;
  46. String mail;
  47. ConnectionClass connectionclass;
  48. String usernameS;
  49. String datets;
  50. String call, db, un, passwords;
  51. Connection connect;
  52. public boolean postoi = false;
  53. ResultSet rs;
  54.  
  55. @SuppressLint("NewApi")
  56. private Connection CONN(String _user, String _pass, String _DB,
  57. String _server) {
  58. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  59. .permitAll().build();
  60. StrictMode.setThreadPolicy(policy);
  61. Connection conn = null;
  62. String ConnURL = null;
  63. try {
  64.  
  65. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  66. ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
  67. + "databaseName=" + _DB + ";user=" + _user + ";password="
  68. + _pass + ";";
  69. conn = DriverManager.getConnection(ConnURL);
  70. } catch (SQLException se) {
  71. Log.e("ERRO", se.getMessage());
  72. } catch (ClassNotFoundException e) {
  73. Log.e("ERRO", e.getMessage());
  74. } catch (Exception e) {
  75. Log.e("ERRO", e.getMessage());
  76. }
  77. return conn;
  78. }
  79. public void onClick(View v)
  80. {
  81. Intent o = new Intent(Login.this,ForgotPassword.class);
  82. startActivity(o);
  83.  
  84. }
  85. @Override
  86. protected void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88. setContentView(R.layout.activity_login);
  89.  
  90. initialize();
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. session = new UserSessionManager(getApplicationContext());
  98.  
  99. // Toast.makeText(getApplicationContext(),
  100. // "User Login Status: " + session.isUserLoggedIn(),
  101. // Toast.LENGTH_LONG).show();
  102.  
  103. Singleton.getInstance().username = PreferenceManager.getDefaultSharedPreferences(cont).getString("username", Singleton.getInstance().username);
  104. Singleton.getInstance().password = PreferenceManager.getDefaultSharedPreferences(cont).getString("password", Singleton.getInstance().password);
  105. Singleton.getInstance().email = PreferenceManager.getDefaultSharedPreferences(cont).getString("email", Singleton.getInstance().email);
  106. String userN= Singleton.getInstance().username;
  107. String pasS= Singleton.getInstance().password;
  108. String emaill= Singleton.getInstance().email;
  109. int a=userN.length();
  110. int b=pasS.length();
  111. if(a!=0 && b!=0)
  112. {
  113. session.createUserLoginSession(userN, emaill);
  114. Intent i = new Intent(getApplicationContext(), TypesActivity.class);
  115. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  116.  
  117. // Add new Flag to start new Activity
  118. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  119. startActivity(i);
  120.  
  121. finish();
  122.  
  123.  
  124. }
  125.  
  126. login.setOnClickListener(new OnClickListener() {
  127.  
  128.  
  129.  
  130. @Override
  131. public void onClick(View v) {
  132.  
  133. new LoginTask().execute();
  134. InputMethodManager imm = (InputMethodManager)Login.this.getSystemService(Context.INPUT_METHOD_SERVICE);
  135. imm.hideSoftInputFromWindow(password.getWindowToken(), 0);
  136.  
  137. }
  138.  
  139.  
  140.  
  141. });
  142.  
  143. register.setOnClickListener(new OnClickListener() {
  144.  
  145. @Override
  146. public void onClick(View v) {
  147. Intent intent = new Intent(Login.this,Register.class);
  148. startActivity(intent);
  149. }
  150. });
  151.  
  152.  
  153. }
  154. class LoginTask extends AsyncTask<String, Void, String>
  155. {
  156. private ProgressDialog pDialog;
  157. private String rez;
  158.  
  159. protected void onPreExecute (){
  160. super.onPreExecute();
  161. // Showing progress dialog
  162. pDialog = new ProgressDialog(Login.this);
  163. pDialog.setMessage("Pleace wait...");
  164. pDialog.setCancelable(false);
  165. pDialog.show();
  166.  
  167. }
  168.  
  169.  
  170.  
  171. protected void onProgressUpdate(Integer...a){
  172.  
  173. }
  174.  
  175. protected void onPostExecute(String result) {
  176.  
  177. if(result.equals("ace"))
  178. {
  179. Intent i = new Intent(getApplicationContext(), TypesActivity.class);
  180. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  181.  
  182. // Add new Flag to start new Activity
  183. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  184. startActivity(i);
  185.  
  186. finish();
  187.  
  188. }
  189. else if(!result.equals("ace"))
  190. {
  191. Toast.makeText(getApplicationContext(),
  192. "Sorry, wrong credentials!!!",
  193.  
  194. Toast.LENGTH_LONG).show();
  195. pDialog.cancel();
  196. }
  197.  
  198. }
  199.  
  200. @Override
  201. protected String doInBackground(String... params) {
  202. String usr = username.getText().toString();
  203. String psw = password.getText().toString();
  204.  
  205. connectionclass = new ConnectionClass();
  206. call = connectionclass.getip();
  207. un = connectionclass.getun();
  208. passwords = connectionclass.getpassword();
  209. db = connectionclass.getdb();
  210. connect = CONN(un, passwords, db, call);
  211.  
  212. // TODO Auto-generated method stub
  213. Log.e("NAME",usr);
  214. Log.e("PASS",psw);
  215.  
  216. String querycmd = "select * from Korisnik where username='"+usr+"' and password='"+psw+"'";
  217. // String querycmd = "select * from Korisnik";
  218. try{
  219. Statement statement = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  220. ResultSet.CONCUR_READ_ONLY);
  221.  
  222. rs = statement.executeQuery(querycmd);
  223.  
  224.  
  225. if (rs != null && rs.next()) {
  226.  
  227. name=rs.getString("username");
  228. pass=rs.getString("password");
  229. mail=rs.getString("email");
  230. session.createUserLoginSession(usr, mail);
  231. rez="ace";
  232.  
  233. Singleton.getInstance().username=usr;
  234. Singleton.getInstance().password=psw;
  235. Singleton.getInstance().email=mail;
  236. PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("username", Singleton.getInstance().username).commit();
  237. PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("password", Singleton.getInstance().password).commit();
  238. PreferenceManager.getDefaultSharedPreferences(cont).edit().putString("email", Singleton.getInstance().email).commit();
  239. // Starting MainActivity
  240.  
  241.  
  242. }
  243.  
  244. else
  245. {
  246. rez="cackoooo";
  247. // user didn't entered username or password
  248. Toast.makeText(getApplicationContext(),
  249. "Sorry, wrong credentials!!!",
  250. Toast.LENGTH_LONG).show();
  251.  
  252.  
  253. }
  254.  
  255.  
  256. }
  257. catch(Exception e){
  258.  
  259.  
  260. }
  261.  
  262.  
  263.  
  264.  
  265. return rez;
  266. }
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273. private void initialize() {
  274. login =(Button)findViewById(R.id.login);
  275. register =(Button)findViewById(R.id.activityRegister);
  276. username =(EditText)findViewById(R.id.username);
  277. password =(EditText)findViewById(R.id.password);
  278. hiperlink=(TextView)findViewById(R.id.hiperlink);
  279.  
  280. }
  281.  
  282.  
  283. @Override
  284. public boolean onCreateOptionsMenu(Menu menu) {
  285. // Inflate the menu; this adds items to the action bar if it is present.
  286. getMenuInflater().inflate(R.menu.login, menu);
  287. return true;
  288. }
  289.  
  290. @Override
  291. public boolean onOptionsItemSelected(MenuItem item) {
  292. // Handle action bar item clicks here. The action bar will
  293. // automatically handle clicks on the Home/Up button, so long
  294. // as you specify a parent activity in AndroidManifest.xml.
  295. int id = item.getItemId();
  296. if (id == R.id.action_settings) {
  297. return true;
  298. }
  299. return super.onOptionsItemSelected(item);
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement