Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stellent.canntiva;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.json.JSONArray;
- import org.json.JSONException;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import com.stellent.canntiva.utils.DataUrls;
- import com.stellent.canntiva.utils.MyProgressDialog;
- import com.stellent.canntiva.utils.convertget;
- //sahil/9533180031
- public class Login extends Activity implements OnClickListener
- {
- EditText loginid,password;
- ImageView login,signup;
- public static String logintext,passwordtext;
- public static String message,id,name;
- String l,p;
- MyProgressDialog dialog;
- Handler mHandler;
- AlertDialog alertDialog;
- public static String Country_iso[];
- public static String Country[];
- public static String Country1[];
- String user="";
- public static int count=0;
- private static String KEY="login";
- private static String LOGIN_KEY="loginkey";
- private static String Loginid;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- loginid=(EditText)findViewById(R.id.loginid);
- password=(EditText)findViewById(R.id.password);
- login=(ImageView)findViewById(R.id.login);
- signup=(ImageView)findViewById(R.id.signup);
- login.setOnClickListener(this);
- signup.setOnClickListener(this);
- boolean s=isOnline();
- Log.e("net connect", ""+s);
- if(s)
- {
- Log.d("net","yes" );
- }
- else
- {
- Log.d("net","no" );
- Toast.makeText(this,"NetWork Connection Failed",Toast.LENGTH_LONG).show();
- finish();
- }
- }
- private void JsonParsing(String response)
- {
- try
- {
- JSONArray array=new JSONArray(response);
- Country=new String[array.length()];
- Country1=new String[array.length()+1];
- Country_iso=new String[array.length()];
- //Country[0]="Country";
- for(int i=0;i<array.length();i++)
- {
- Country[i]=array.getJSONObject(i).getString("name").toString();
- }
- Country1[0]="Country";
- for(int i=0;i<array.length();i++)
- {
- Country1[i+1]=Country[i];
- Log.i("Country",Country1[i]);
- }
- for(int i=0;i<array.length();i++)
- {
- Country_iso[i]=array.getJSONObject(i).getString("country_iso").toString();
- Log.i("Country",Country_iso[i]);
- }
- Log.e("Country", Country[1]);
- Log.e("Country_iso", Country_iso[0]);
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onResume(){
- super.onResume();
- InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(loginid.getWindowToken(), 0);
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
- }
- public boolean isOnline()
- {
- ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = cm.getActiveNetworkInfo();
- if (netInfo != null && netInfo.isConnectedOrConnecting())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- @Override
- public void onClick(View v)
- {
- if(v.equals(login))
- {
- // Intent in=new Intent(Login.this,MenuPage.class);
- // startActivity(in);
- // finish();
- InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(loginid.getWindowToken(), 0);
- MenuPage.user=loginid.getText().toString();
- user=loginid.getText().toString();
- logintext=loginid.getText().toString();
- name=loginid.getText().toString();
- passwordtext=password.getText().toString();
- if(logintext.contains(" "))
- {
- l=logintext.replace(" ", "");
- }
- else
- {
- l=logintext;
- }
- if(passwordtext.contains(" "))
- {
- p=passwordtext.replace(" ", "");
- }
- else
- {
- p=passwordtext;
- }
- Log.i("logintext", logintext);
- Log.i("passwordtext", passwordtext);
- boolean check=checkEmailCorrect(logintext);
- if(logintext.length()==0)
- {
- Toast.makeText(Login.this,"Please Enter Login ID",Toast.LENGTH_SHORT).show();
- }
- else if(passwordtext.length()==0)
- {
- Toast.makeText(Login.this,"Please Enter Password",Toast.LENGTH_SHORT).show();
- }
- else
- {
- alertDialog = new AlertDialog.Builder(Login.this).create();
- dialog=MyProgressDialog.show(Login.this, null,null);
- new Thread(){
- public void run(){
- try{
- Thread.sleep(5000);
- }
- catch(Exception ex){
- Log.e("Welcome Exception :",ex.toString());
- }
- try{
- Message msg=mHandler.obtainMessage();
- mHandler.sendMessage(msg);
- }
- catch(NullPointerException ex){
- Log.e("Handler Exception :",ex.toString());
- }
- }
- }.start();
- mHandler=new Handler(){
- public void handleMessage(Message msg)
- {
- super.handleMessage(msg);
- String response=convertget.getValuefromUrl(DataUrls.loginurl+"?username="+l+"&password="+p);
- Log.e("response", response);
- if(response.equals("zero"))
- {
- Toast.makeText(Login.this, "Network Error", Toast.LENGTH_LONG).show();
- //finish();
- }
- else
- {
- sampleJSonParser(response);
- String res=convertget.getValuefromUrl(DataUrls.getcountry);
- JsonParsing(res);
- if(message.contains("success"))
- {
- // SharedPreferences preferences = Login.this.getSharedPreferences(KEY,0);
- // SharedPreferences.Editor editor = preferences.edit();
- // editor.putString(LOGIN_KEY,"success");
- // editor.putString(Loginid, id);
- // editor.commit();
- Intent in=new Intent(Login.this,MenuPage.class);
- startActivity(in);
- finish();
- }
- else
- {
- Log.v("no else","no else");
- alertDialog.setTitle("LoginID or Password Doesn't Exist");
- Dismiss();
- alertDialog.show();
- }
- }
- if(dialog!=null && dialog.isShowing())
- {
- dialog.dismiss();
- }
- }
- };
- }
- }
- if(v.equals(signup))
- {
- //
- Intent in=new Intent(Login.this,Signup.class);
- startActivity(in);
- finish();
- }
- }
- boolean checkEmailCorrect(String Email)
- {
- if(Email.length() == 0)
- {
- return false;
- }
- String pttn = "^\\D.+@.+\\.[a-z]+";
- Pattern p = Pattern.compile(pttn);
- Matcher m = p.matcher(Email);
- if(m.matches()) {
- return true;
- }
- return false;
- }
- private void sampleJSonParser(String url)
- {
- // TODO Auto-generated method stub
- try
- {
- JSONArray array= new JSONArray("["+url+"]");
- // Log.d("id:",array.getJSONObject(0).getString("Id").toString());
- Log.d("message :",array.getJSONObject(0).getString("message").toString());
- message=array.getJSONObject(0).getString("message").toString();
- if(message.equals("login success"))
- {
- id=array.getJSONObject(0).getString("Id").toString();
- }
- }
- catch(Exception ex)
- {
- //Log.e("Exception in Class",ex.toString());
- ex.printStackTrace();
- }
- }
- private void Dismiss()
- {
- // TODO Auto-generated method stub
- alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- alertDialog.dismiss();
- } });
- }
- public void onActivityResult(int requestcode,int resultcode ,Intent data)
- {
- switch(requestcode)
- {
- case 0: if(resultcode==RESULT_OK)
- {
- //Toast.makeText(this, "came back", Toast.LENGTH_LONG).show();
- break;
- }
- case 1: if(resultcode==RESULT_OK)
- {
- loginid.setText(user);
- password.setText("");
- break;
- }
- }
- }
- protected void onPause()
- {
- super.onPause();
- dialog.dismiss();
- }
- }
Add Comment
Please, Sign In to add comment