Advertisement
w1ntz

login

Jul 10th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.89 KB | None | 0 0
  1. package com.example.win8pro.globish;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.os.StrictMode;
  9. import android.support.v4.app.FragmentActivity;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.view.View;
  14. import android.widget.EditText;
  15.  
  16. import org.apache.http.NameValuePair;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25.  
  26.  
  27. public class login extends FragmentActivity{
  28.  
  29. public static int nAllClass;
  30.  
  31. public static DatabaseHandler dbHandler;
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35.  
  36. final String PREFS_NAME = "MyPrefsFile";
  37.  
  38.  
  39.  
  40. SharedPreferences settings = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE,Context.MODE_PRIVATE);
  41. SharedPreferences.Editor editor = settings.edit();
  42.  
  43. boolean checkFirst = settings.getBoolean("first_time", true);
  44.  
  45. if (checkFirst) {
  46. Log.d("Comments", "First time");
  47.  
  48. editor.putBoolean("first_time", false);
  49. editor.commit();
  50.  
  51. Intent newActivity = new Intent(this, AboutGlobish.class);
  52. startActivity(newActivity);
  53.  
  54. }
  55.  
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_login);
  58.  
  59. if (android.os.Build.VERSION.SDK_INT > 9) {
  60. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  61. StrictMode.setThreadPolicy(policy);
  62. }
  63.  
  64. final EditText loginText = (EditText) findViewById(R.id.email_white_edit);
  65. final EditText loginPassword = (EditText) findViewById(R.id.password_edit);
  66.  
  67. }
  68.  
  69. private boolean executeCammand(){
  70. System.out.println(" executeCammand");
  71. Runtime runtime = Runtime.getRuntime();
  72. try
  73. {
  74. Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
  75. int mExitValue = mIpAddrProcess.waitFor();
  76. System.out.println(" mExitValue "+mExitValue);
  77. boolean netstat= InternetCheck.isInternetAvailable();
  78. System.out.println("netstat="+netstat);
  79. if(mExitValue==0&&netstat==true){
  80.  
  81.  
  82. return true;
  83. }else{
  84. return false;
  85. }
  86. }
  87. catch (InterruptedException ignore)
  88. {
  89. ignore.printStackTrace();
  90. System.out.println(" Exception:"+ignore);
  91. }
  92. catch (IOException e)
  93. {
  94. e.printStackTrace();
  95. System.out.println(" Exception:"+e);
  96. }
  97. return false;
  98. }
  99. public void loginfb(View v) throws JSONException {
  100. Intent newActivity = new Intent(this, drawer.class);
  101. startActivity(newActivity);
  102. }
  103. public void goToAbout(View v) throws JSONException{
  104. Intent newActivity = new Intent(this, AboutGlobish.class);
  105. startActivity(newActivity);
  106. }
  107.  
  108.  
  109. public void login(View v) throws JSONException {
  110. final EditText loginText = (EditText) findViewById(R.id.email_white_edit);
  111. final EditText loginPassword = (EditText) findViewById(R.id.password_edit);
  112.  
  113. boolean net= executeCammand();
  114. boolean netstat= InternetCheck.isInternetAvailable();
  115. if (net==true&&netstat==true) {
  116. System.out.println("net available");
  117. }
  118. else if (net==false) {
  119. System.out.println("net unavailable");
  120. final AlertDialog.Builder ad = new AlertDialog.Builder(this);
  121. ad.setTitle("Error! ");
  122. // ad.setIcon(android.R.drawable.btn_star_big_on);
  123. ad.setPositiveButton("Close", null);
  124. ad.setMessage("No internet connection");
  125.  
  126. ad.show();
  127. finishActivity(0);
  128. }
  129.  
  130. // username: yustemarianecel@gmail.com
  131. // password: maewnam
  132. //
  133. // username: maxnight007@hotmail.com
  134. // password: liverpool
  135.  
  136. String username = loginText.getText().toString();
  137. String password = loginPassword.getText().toString();
  138.  
  139. String urlLogin = "http://ga.aquawams.com/api/xhr_request_user_info.php?username=" + username + "&password=" + password;
  140. String urlSchedule = "http://ga.aquawams.com/api/xhr_request_schedule.php?username=" + username + "&password=" + password;
  141.  
  142. List<NameValuePair> params = new ArrayList<NameValuePair>();
  143. Log.i("URL Login", urlLogin);
  144.  
  145. System.out.println("======================================");
  146. Log.i("Username", username);
  147. Log.i("Password", password);
  148. System.out.println("======================================");
  149.  
  150. params.add(new BasicNameValuePair("strUser", username));
  151. params.add(new BasicNameValuePair("strPass", password));
  152.  
  153. String JSONScheduleString = gethttp.getHttpPost(urlSchedule, params);
  154. Log.i("JSON Schedule", JSONScheduleString);
  155.  
  156. String resultServer = gethttp.getHttpPost(urlLogin, params);
  157. String strStatusID = "0";
  158. System.out.println(resultServer);
  159. String result= "0";
  160. JSONObject info;
  161. String type;
  162.  
  163. String name_parsed;
  164. String skype;
  165. String dob;
  166. String timezone;
  167. String chechResult;
  168.  
  169. String strError = "incorrect username and/or password";
  170. JSONObject JSONUserInfo;
  171. JSONObject JSONSchedule;
  172.  
  173. try {
  174. SharedPreferences sharedPref = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE,Context.MODE_PRIVATE);
  175. SharedPreferences.Editor editor = sharedPref.edit();
  176.  
  177. JSONSchedule = new JSONObject(JSONScheduleString);
  178.  
  179. chechResult = JSONSchedule.getString("result");
  180. Log.i("Check Result",chechResult);
  181.  
  182. editor.putString("resultbubu", chechResult);
  183. editor.commit();
  184.  
  185. JSONUserInfo = new JSONObject(resultServer);
  186.  
  187. result = JSONUserInfo.getString("result");
  188. Log.i("Result", result);
  189. editor.putString("result", result);
  190. editor.commit();
  191.  
  192. info = JSONUserInfo.getJSONObject("info");
  193. System.out.println(info);
  194.  
  195. type = info.getString("type");
  196. System.out.println(type);
  197. editor.putString( "type", type);
  198. editor.commit();
  199.  
  200. name_parsed = info.getString("name");
  201. System.out.println(name_parsed);
  202. Log.i("Test Name", "Start Login");
  203. name_parsed ="\n"+name_parsed;
  204. editor.putString(Constant.NAME, name_parsed);
  205. editor.commit();
  206.  
  207. skype = info.getString("skype");
  208. System.out.println(skype);
  209. editor.putString( "skype", skype);
  210. editor.commit();
  211.  
  212. dob = info.getString("dob");
  213. System.out.println(dob);
  214. editor.putString( "dob", dob);
  215. editor.commit();
  216.  
  217. timezone = info.getString("timezone");
  218. System.out.println(timezone);
  219. editor.putString( "timezone", timezone);
  220. editor.commit();
  221.  
  222. } catch (JSONException e) {
  223. // TODO Auto-generated catch block
  224. e.printStackTrace();
  225. }
  226.  
  227. // Prepare Login
  228. if(result.equals("false"))
  229. {
  230. // Dialog
  231. final AlertDialog.Builder ad = new AlertDialog.Builder(this);
  232. ad.setTitle("Error! ");
  233. // ad.setIcon(android.R.drawable.btn_star_big_on);
  234. ad.setPositiveButton("Close", null);
  235. ad.setMessage(strError);
  236. ad.show();
  237. // loginText.setText("");
  238. loginPassword.setText("");
  239.  
  240. }else if (result.equals("true")){
  241.  
  242. //parse json here
  243. try {
  244.  
  245. JSONSchedule = new JSONObject(JSONScheduleString);
  246. System.out.println("-------------printJSONObject-------------");
  247. Log.i("JSON Object - Schedule", ("" + JSONSchedule) );
  248. System.out.println("-------------printJSONObject-------------");
  249.  
  250. chechResult = JSONSchedule.getString("result");
  251. Log.i("Result", chechResult);
  252.  
  253. SharedPreferences sharedPref = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE, Context.MODE_PRIVATE);
  254. SharedPreferences.Editor editor = sharedPref.edit();
  255.  
  256. editor.putString("resultbubu", chechResult);
  257. editor.commit();
  258.  
  259. // ========================================================
  260. JSONArray aClass = JSONSchedule.getJSONArray("class");
  261. Log.i("JSON Array - Class", ("" + aClass) );
  262. // a = array
  263.  
  264. int nClass = aClass.length();
  265. Log.i("Number of classes", ("" + nClass));
  266. // n = number of
  267.  
  268. JSONArray aSchedule;
  269. nAllClass = 0;
  270.  
  271. for(int i=0; i<nClass; i++){
  272. aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
  273. nAllClass += aSchedule.length();
  274. }
  275.  
  276. //===========IndiClass START====================
  277.  
  278. IndiClass[] classes = new IndiClass[nAllClass];
  279. for(int i = 0; i < nAllClass; i++){
  280. classes[i] = new IndiClass();
  281. }
  282.  
  283. int classTracker = 0;
  284.  
  285. //TODO: If have time, this can be change into 2 loop and one method
  286. /*
  287. From OriGinAl:
  288. The method setClassInfo can just pass the array of JSON("schedule")
  289. to setSchedule within itself.
  290. */
  291.  
  292. for(int i=0; i<nClass; i++){
  293. aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
  294.  
  295. for(int j=0; j<aSchedule.length(); j++){
  296. classes[classTracker].setScheduleInfo(aSchedule.getJSONObject(j));
  297. classTracker++;
  298. }
  299.  
  300. }
  301.  
  302. classTracker = 0;
  303. Log.i("Class tracker", ("" + classTracker));
  304. int nSchedule;
  305.  
  306. for(int i=0; i<nClass; i++){
  307. aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
  308. nSchedule = aSchedule.length();
  309. Log.i("Class no." + i, ("" + nSchedule));
  310.  
  311. String aName = aClass.getJSONObject(i).getJSONArray("course_name").getString(0);
  312.  
  313. for(int j=0; j<nSchedule; j++){
  314. classes[classTracker].setClassInfo(aClass.getJSONObject(i), aName);
  315. classTracker++;
  316. }
  317.  
  318. }
  319.  
  320. IndiClass.sortByDate(classes);
  321.  
  322. for(int i=0; i<nAllClass; i++){
  323. Log.i("Schedule no." + (i+1), classes[i].toString());
  324. }
  325.  
  326.  
  327. //for loop here
  328. // } for (int j=0;j<schedulenum;j++) {
  329. // JSONArray f[] = new JSONArray[100];
  330. // f[j] = ee[i].getJSONArray("schedule");
  331. // System.out.println(f[j] + "got array f"+j);
  332. // int appointnum = f[j].length();
  333.  
  334. // //for loop here
  335. // for (int k=0;k<appointnum;k++) {
  336. // JSONObject g[] = new JSONObject[100];
  337. // g[k] = f[j].getJSONObject(i);
  338. // System.out.println(g[k] + "got object g"+g);
  339. //// int stdytime =
  340. //
  341. // }
  342. // }
  343.  
  344.  
  345.  
  346. // int len = f.length();
  347. // System.out.println(len);
  348.  
  349. // JSONObject g= f.getJSONObject(0);
  350. //
  351. // System.out.println(g+"got object g");
  352.  
  353.  
  354. // JSONArray mArray = new JSONArray(f);
  355. // int id_count = 0;
  356. // int devices_count = 0;
  357. // int tmp = 0;
  358. //
  359. // for (int i = 0; i<mArray.length(); i++) {
  360. // try {
  361. // tmp = mArray.getJSONObject(i).getInt("id");
  362. // id_count = id_count + 1;
  363. // }
  364. // catch (JSONException e) {
  365. // // If id doesn't exist, this exception is thrown
  366. // }
  367. // try {
  368. // tmp = mArray.getJSONObject(i).getInt("time");
  369. // devices_count = devices_count + 1;
  370. // }
  371. // catch (JSONException e) {
  372. // // If number_of_devices doesn't exist, this exception is thrown
  373. // }
  374. // }
  375. // System.out.println(devices_count);
  376. // System.out.println(id_count);
  377.  
  378. // Iterator<Object> keys = f.keys();
  379. // while (keys.hasNext()) {
  380. // String key = keys.next();
  381. // try{
  382. // JSONArray array = jObject.getJSONArray(key);
  383. // //count objects values using array length.
  384. // }
  385. // catch(Exception e){
  386. // e.printStackTrace()
  387. // }
  388. // }
  389. //
  390. //
  391. //
  392.  
  393.  
  394.  
  395. dbHandler = new DatabaseHandler(getApplicationContext());
  396.  
  397. dbHandler.deleteAll();
  398.  
  399. for(IndiClass classObj: classes) {
  400. dbHandler.createClassData(classObj);
  401. }
  402.  
  403. Log.i("Done DataBase Creation", "SQLite");
  404.  
  405. }catch (JSONException e){
  406. e.printStackTrace();
  407. }
  408.  
  409. // //==========Test Notification===========
  410. // // get a Calendar object with current time
  411. // Calendar cal = Calendar.getInstance();
  412. // // add 5 minutes to the calendar object
  413. // cal.add(Calendar.MINUTE, 1);
  414. // Intent intent = new Intent(this, AlarmReceiver.class);
  415. // intent.putExtra("alarm_message", "O'Doyle Rules!");
  416. // // In reality, you would want to have a static variable for the request code instead of 192837
  417. // PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  418. //
  419. // // Get the AlarmManager service
  420. // AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  421. // am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
  422. // //======================================
  423.  
  424. // String[] timeARR = {"2015-04-12 14:50:00", "2015-04-12 15:54:00", "2015-04-12 15:15:00", "2015-04-13 19:54:00", "2015-04-13 19:59:00"};
  425. // String[] nameARR = {"TEST1", "TEST2", "TEST3", "TEST4", "TEST5"};
  426. // for (int i = 0; i < timeARR.length; i++){
  427. // IndiClass.setLocalNoti(login.this, nameARR[i], timeARR[i], i);
  428. // }
  429.  
  430.  
  431.  
  432. for (int i = 1; i<=login.nAllClass; i++) {
  433. IndiClass classObj = login.dbHandler.getIndiClassBySQLid(i);
  434. IndiClass.setLocalNoti(login.this, classObj._course_name, classObj._time_text, Integer.parseInt(classObj.scID));
  435. }
  436.  
  437.  
  438.  
  439. Intent newActivity = new Intent(this, drawer.class);
  440. startActivity(newActivity);
  441.  
  442.  
  443. }else if (result.equals("null")){
  444.  
  445. }
  446.  
  447. }
  448.  
  449.  
  450. @Override
  451. public boolean onCreateOptionsMenu(Menu menu) {
  452. // Inflate the menu; this adds items to the action bar if it is present.
  453. getMenuInflater().inflate(R.menu.menu_login, menu);
  454.  
  455. return true;
  456. }
  457.  
  458.  
  459. @Override
  460. public boolean onOptionsItemSelected(MenuItem item) {
  461. // Handle action bar item clicks here. The action bar will
  462. // automatically handle clicks on the Home/Up button, so long
  463. // as you specify a parent activity in AndroidManifest.xml.
  464. int id = item.getItemId();
  465.  
  466. //noinspection SimplifiableIfStatement
  467. if (id == R.id.action_settings) {
  468. return true;
  469. }
  470.  
  471. return super.onOptionsItemSelected(item);
  472. }
  473.  
  474. public void userPassForDebug(View v){
  475.  
  476. EditText username = (EditText) findViewById(R.id.email_white_edit);
  477. EditText password = (EditText) findViewById(R.id.password_edit);
  478.  
  479. username.setText("maxnight007@hotmail.com");
  480. password.setText("liverpool");
  481.  
  482. Log.i("Set text", "clicked");
  483.  
  484. }
  485.  
  486.  
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement