Guest User

Untitled

a guest
Jun 22nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. package com.stellent.canntiva;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8.  
  9. import android.app.Activity;
  10. import android.app.AlertDialog;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.net.ConnectivityManager;
  15. import android.net.NetworkInfo;
  16. import android.os.Bundle;
  17. import android.os.Handler;
  18. import android.os.Message;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.view.WindowManager;
  22. import android.view.View.OnClickListener;
  23. import android.view.inputmethod.InputMethodManager;
  24. import android.widget.EditText;
  25. import android.widget.ImageView;
  26. import android.widget.Toast;
  27.  
  28. import com.stellent.canntiva.utils.DataUrls;
  29. import com.stellent.canntiva.utils.MyProgressDialog;
  30. import com.stellent.canntiva.utils.convertget;
  31.  
  32. //sahil/9533180031
  33. public class Login extends Activity implements OnClickListener
  34. {
  35.  
  36. EditText loginid,password;
  37.  
  38. ImageView login,signup;
  39.  
  40. public static String logintext,passwordtext;
  41.  
  42. public static String message,id,name;
  43.  
  44. String l,p;
  45. MyProgressDialog dialog;
  46. Handler mHandler;
  47. AlertDialog alertDialog;
  48.  
  49. public static String Country_iso[];
  50. public static String Country[];
  51. public static String Country1[];
  52. String user="";
  53.  
  54. public static int count=0;
  55.  
  56.  
  57. private static String KEY="login";
  58. private static String LOGIN_KEY="loginkey";
  59. private static String Loginid;
  60. @Override
  61. public void onCreate(Bundle savedInstanceState)
  62. {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.login);
  65.  
  66. loginid=(EditText)findViewById(R.id.loginid);
  67. password=(EditText)findViewById(R.id.password);
  68.  
  69.  
  70. login=(ImageView)findViewById(R.id.login);
  71. signup=(ImageView)findViewById(R.id.signup);
  72.  
  73. login.setOnClickListener(this);
  74. signup.setOnClickListener(this);
  75.  
  76. boolean s=isOnline();
  77. Log.e("net connect", ""+s);
  78.  
  79. if(s)
  80. {
  81. Log.d("net","yes" );
  82. }
  83. else
  84. {
  85. Log.d("net","no" );
  86. Toast.makeText(this,"NetWork Connection Failed",Toast.LENGTH_LONG).show();
  87. finish();
  88. }
  89.  
  90. }
  91. private void JsonParsing(String response)
  92. {
  93. try
  94. {
  95. JSONArray array=new JSONArray(response);
  96.  
  97. Country=new String[array.length()];
  98. Country1=new String[array.length()+1];
  99. Country_iso=new String[array.length()];
  100. //Country[0]="Country";
  101.  
  102. for(int i=0;i<array.length();i++)
  103. {
  104. Country[i]=array.getJSONObject(i).getString("name").toString();
  105.  
  106.  
  107.  
  108. }
  109. Country1[0]="Country";
  110. for(int i=0;i<array.length();i++)
  111. {
  112. Country1[i+1]=Country[i];
  113. Log.i("Country",Country1[i]);
  114. }
  115.  
  116. for(int i=0;i<array.length();i++)
  117. {
  118. Country_iso[i]=array.getJSONObject(i).getString("country_iso").toString();
  119.  
  120.  
  121. Log.i("Country",Country_iso[i]);
  122. }
  123.  
  124. Log.e("Country", Country[1]);
  125. Log.e("Country_iso", Country_iso[0]);
  126.  
  127. }
  128. catch (JSONException e) {
  129.  
  130. e.printStackTrace();
  131. }
  132.  
  133. }
  134. @Override
  135. public void onResume(){
  136. super.onResume();
  137. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  138. imm.hideSoftInputFromWindow(loginid.getWindowToken(), 0);
  139.  
  140. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  141. }
  142.  
  143. public boolean isOnline()
  144. {
  145. ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  146. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  147.  
  148. if (netInfo != null && netInfo.isConnectedOrConnecting())
  149. {
  150. return true;
  151. }
  152. else
  153. {
  154. return false;
  155. }
  156.  
  157. }
  158. @Override
  159. public void onClick(View v)
  160. {
  161. if(v.equals(login))
  162. {
  163. // Intent in=new Intent(Login.this,MenuPage.class);
  164. // startActivity(in);
  165. // finish();
  166. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  167. imm.hideSoftInputFromWindow(loginid.getWindowToken(), 0);
  168.  
  169. MenuPage.user=loginid.getText().toString();
  170.  
  171. user=loginid.getText().toString();
  172.  
  173. logintext=loginid.getText().toString();
  174. name=loginid.getText().toString();
  175. passwordtext=password.getText().toString();
  176. if(logintext.contains(" "))
  177. {
  178. l=logintext.replace(" ", "");
  179. }
  180. else
  181. {
  182. l=logintext;
  183. }
  184. if(passwordtext.contains(" "))
  185. {
  186. p=passwordtext.replace(" ", "");
  187. }
  188. else
  189. {
  190. p=passwordtext;
  191. }
  192. Log.i("logintext", logintext);
  193. Log.i("passwordtext", passwordtext);
  194.  
  195. boolean check=checkEmailCorrect(logintext);
  196.  
  197. if(logintext.length()==0)
  198. {
  199. Toast.makeText(Login.this,"Please Enter Login ID",Toast.LENGTH_SHORT).show();
  200. }
  201. else if(passwordtext.length()==0)
  202. {
  203. Toast.makeText(Login.this,"Please Enter Password",Toast.LENGTH_SHORT).show();
  204. }
  205. else
  206. {
  207. alertDialog = new AlertDialog.Builder(Login.this).create();
  208.  
  209. dialog=MyProgressDialog.show(Login.this, null,null);
  210. new Thread(){
  211. public void run(){
  212. try{
  213. Thread.sleep(5000);
  214. }
  215.  
  216. catch(Exception ex){
  217.  
  218. Log.e("Welcome Exception :",ex.toString());
  219. }
  220. try{
  221. Message msg=mHandler.obtainMessage();
  222. mHandler.sendMessage(msg);
  223. }
  224. catch(NullPointerException ex){
  225. Log.e("Handler Exception :",ex.toString());
  226.  
  227. }
  228.  
  229. }
  230. }.start();
  231.  
  232. mHandler=new Handler(){
  233. public void handleMessage(Message msg)
  234. {
  235. super.handleMessage(msg);
  236.  
  237. String response=convertget.getValuefromUrl(DataUrls.loginurl+"?username="+l+"&password="+p);
  238. Log.e("response", response);
  239.  
  240. if(response.equals("zero"))
  241. {
  242. Toast.makeText(Login.this, "Network Error", Toast.LENGTH_LONG).show();
  243. //finish();
  244.  
  245. }
  246. else
  247. {
  248.  
  249. sampleJSonParser(response);
  250. String res=convertget.getValuefromUrl(DataUrls.getcountry);
  251.  
  252. JsonParsing(res);
  253.  
  254. if(message.contains("success"))
  255. {
  256.  
  257. // SharedPreferences preferences = Login.this.getSharedPreferences(KEY,0);
  258. // SharedPreferences.Editor editor = preferences.edit();
  259. // editor.putString(LOGIN_KEY,"success");
  260. // editor.putString(Loginid, id);
  261. // editor.commit();
  262.  
  263. Intent in=new Intent(Login.this,MenuPage.class);
  264. startActivity(in);
  265.  
  266. finish();
  267. }
  268. else
  269. {
  270. Log.v("no else","no else");
  271. alertDialog.setTitle("LoginID or Password Doesn't Exist");
  272.  
  273. Dismiss();
  274. alertDialog.show();
  275. }
  276. }
  277. if(dialog!=null && dialog.isShowing())
  278. {
  279. dialog.dismiss();
  280. }
  281. }
  282.  
  283. };
  284.  
  285.  
  286. }
  287. }
  288. if(v.equals(signup))
  289. {
  290. //
  291.  
  292. Intent in=new Intent(Login.this,Signup.class);
  293. startActivity(in);
  294. finish();
  295.  
  296. }
  297.  
  298. }
  299. boolean checkEmailCorrect(String Email)
  300. {
  301. if(Email.length() == 0)
  302. {
  303. return false;
  304. }
  305.  
  306. String pttn = "^\\D.+@.+\\.[a-z]+";
  307. Pattern p = Pattern.compile(pttn);
  308. Matcher m = p.matcher(Email);
  309.  
  310. if(m.matches()) {
  311. return true;
  312. }
  313.  
  314. return false;
  315. }
  316. private void sampleJSonParser(String url)
  317. {
  318. // TODO Auto-generated method stub
  319. try
  320. {
  321. JSONArray array= new JSONArray("["+url+"]");
  322.  
  323.  
  324.  
  325. // Log.d("id:",array.getJSONObject(0).getString("Id").toString());
  326. Log.d("message :",array.getJSONObject(0).getString("message").toString());
  327. message=array.getJSONObject(0).getString("message").toString();
  328.  
  329.  
  330. if(message.equals("login success"))
  331. {
  332. id=array.getJSONObject(0).getString("Id").toString();
  333. }
  334.  
  335.  
  336.  
  337. }
  338.  
  339.  
  340. catch(Exception ex)
  341. {
  342. //Log.e("Exception in Class",ex.toString());
  343. ex.printStackTrace();
  344. }
  345. }
  346. private void Dismiss()
  347. {
  348. // TODO Auto-generated method stub
  349. alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  350. public void onClick(DialogInterface dialog, int which) {
  351.  
  352.  
  353. alertDialog.dismiss();
  354.  
  355. } });
  356. }
  357. public void onActivityResult(int requestcode,int resultcode ,Intent data)
  358. {
  359. switch(requestcode)
  360. {
  361. case 0: if(resultcode==RESULT_OK)
  362. {
  363. //Toast.makeText(this, "came back", Toast.LENGTH_LONG).show();
  364. break;
  365. }
  366. case 1: if(resultcode==RESULT_OK)
  367. {
  368. loginid.setText(user);
  369. password.setText("");
  370.  
  371. break;
  372. }
  373.  
  374. }
  375. }
  376. protected void onPause()
  377. {
  378. super.onPause();
  379.  
  380. dialog.dismiss();
  381. }
  382. }
Add Comment
Please, Sign In to add comment