package com.ata.Ashpazsho;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class login extends ActionBarActivity {
private EditText usertext,passtext;
private ImageView login,exit;
private CheckBox cb;
public static String res="";
private int count=0;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usertext =(EditText) findViewById(R.id.login_user);
passtext =(EditText) findViewById(R.id.login_pass);
login =(ImageView) findViewById(R.id.login_login);
exit =(ImageView) findViewById(R.id.login_cancel);
cb=(CheckBox) findViewById(R.id.login_cb);
sp=getApplicationContext().getSharedPreferences("userP", 0);
if(sp.getInt("status", 0)==1){
Intent in=new Intent(login.this,MainActivity.class);
f();
startActivity(in);
}
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*Intent in=new Intent(login.this,MainActivity.class);
startActivity(in);*/
if(!usertext.getText().toString().equals("")&& !passtext.getText().toString().equals("")){
if(cb.isChecked()){
login1(usertext.getText().toString(),passtext.getText().toString(),1);
}else{
login1(usertext.getText().toString(),passtext.getText().toString(),0);
}
}else{
Toast.makeText(getApplicationContext(), "نام کاربری و پسورد را وارد نمایید!", Toast.LENGTH_SHORT).show();
}
}
});
exit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
finish();
}
});
}
private void login1(final String user,final String pass,final int status){
new LoginServer("http://ata.xzn.ir/logindf.php",user,pass).execute();
final ProgressDialog pd=new ProgressDialog(login.this);
pd.setMessage("لطفا صبر کنید");
pd.show();
final Timer tm=new Timer();
tm.scheduleAtFixedRate(new TimerTask(){
public void run() {
runOnUiThread(new Runnable(){
public void run() {
count++;
if(count==40){
pd.cancel();
tm.cancel();
new LoginServer("http://ata.xzn.ir/logindf.php",user,pass).cancel(true);
Toast.makeText(getApplicationContext(), "خطا در برقراری ارتباط", Toast.LENGTH_LONG).show();
}
if(res.equals("ok")){
pd.cancel();
sp=getApplicationContext().getSharedPreferences("userP", 0);
Editor edit=sp.edit();
edit.putString("username", user);
edit.putInt("status", status);
edit.commit();
//Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
Intent in=new Intent(login.this,MainActivity.class);
res="";
tm.cancel();
f();
startActivity(in);
}else if(res.equals("wrong password")){
pd.cancel();
Toast.makeText(getApplicationContext(), "پسورد اشتباه است!!!", Toast.LENGTH_LONG).show();
res="";
tm.cancel();
}else if(res.equals("no user")){
pd.cancel();
Toast.makeText(getApplicationContext(), "نام کاربری وجود ندارد!!!", Toast.LENGTH_LONG).show();
res="";
tm.cancel();
}
}
});
}
}, 1, 1000);
}
private void f(){
this.finish();
}
@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();
switch(id){
case R.id.action_favorite:
return true;
case R.id.action_help:
AlertDialog.Builder ad=new AlertDialog.Builder(this);
ad.setTitle("راهنما");
ad.setIcon(R.drawable.help);
ad.setMessage("فیلدهای ستاره دار باید حتما کامل شوند.\n در صورتی که دانشجو هستید \n سایر فیلدها را نیز کامل نمایید.!");
ad.setCancelable(true);
ad.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog alert=ad.create();
alert.show();
return true;
case R.id.action_cancel:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}