Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.win8pro.globish;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.support.v4.app.FragmentActivity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class login extends FragmentActivity{
- public static int nAllClass;
- public static DatabaseHandler dbHandler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- final String PREFS_NAME = "MyPrefsFile";
- SharedPreferences settings = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = settings.edit();
- boolean checkFirst = settings.getBoolean("first_time", true);
- if (checkFirst) {
- Log.d("Comments", "First time");
- editor.putBoolean("first_time", false);
- editor.commit();
- Intent newActivity = new Intent(this, AboutGlobish.class);
- startActivity(newActivity);
- }
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- if (android.os.Build.VERSION.SDK_INT > 9) {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- }
- final EditText loginText = (EditText) findViewById(R.id.email_white_edit);
- final EditText loginPassword = (EditText) findViewById(R.id.password_edit);
- }
- private boolean executeCammand(){
- System.out.println(" executeCammand");
- Runtime runtime = Runtime.getRuntime();
- try
- {
- Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
- int mExitValue = mIpAddrProcess.waitFor();
- System.out.println(" mExitValue "+mExitValue);
- boolean netstat= InternetCheck.isInternetAvailable();
- System.out.println("netstat="+netstat);
- if(mExitValue==0&&netstat==true){
- return true;
- }else{
- return false;
- }
- }
- catch (InterruptedException ignore)
- {
- ignore.printStackTrace();
- System.out.println(" Exception:"+ignore);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- System.out.println(" Exception:"+e);
- }
- return false;
- }
- public void loginfb(View v) throws JSONException {
- Intent newActivity = new Intent(this, drawer.class);
- startActivity(newActivity);
- }
- public void goToAbout(View v) throws JSONException{
- Intent newActivity = new Intent(this, AboutGlobish.class);
- startActivity(newActivity);
- }
- public void login(View v) throws JSONException {
- final EditText loginText = (EditText) findViewById(R.id.email_white_edit);
- final EditText loginPassword = (EditText) findViewById(R.id.password_edit);
- boolean net= executeCammand();
- boolean netstat= InternetCheck.isInternetAvailable();
- if (net==true&&netstat==true) {
- System.out.println("net available");
- }
- else if (net==false) {
- System.out.println("net unavailable");
- final AlertDialog.Builder ad = new AlertDialog.Builder(this);
- ad.setTitle("Error! ");
- // ad.setIcon(android.R.drawable.btn_star_big_on);
- ad.setPositiveButton("Close", null);
- ad.setMessage("No internet connection");
- ad.show();
- finishActivity(0);
- }
- // username: yustemarianecel@gmail.com
- // password: maewnam
- //
- // username: maxnight007@hotmail.com
- // password: liverpool
- String username = loginText.getText().toString();
- String password = loginPassword.getText().toString();
- String urlLogin = "http://ga.aquawams.com/api/xhr_request_user_info.php?username=" + username + "&password=" + password;
- String urlSchedule = "http://ga.aquawams.com/api/xhr_request_schedule.php?username=" + username + "&password=" + password;
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- Log.i("URL Login", urlLogin);
- System.out.println("======================================");
- Log.i("Username", username);
- Log.i("Password", password);
- System.out.println("======================================");
- params.add(new BasicNameValuePair("strUser", username));
- params.add(new BasicNameValuePair("strPass", password));
- String JSONScheduleString = gethttp.getHttpPost(urlSchedule, params);
- Log.i("JSON Schedule", JSONScheduleString);
- String resultServer = gethttp.getHttpPost(urlLogin, params);
- String strStatusID = "0";
- System.out.println(resultServer);
- String result= "0";
- JSONObject info;
- String type;
- String name_parsed;
- String skype;
- String dob;
- String timezone;
- String chechResult;
- String strError = "incorrect username and/or password";
- JSONObject JSONUserInfo;
- JSONObject JSONSchedule;
- try {
- SharedPreferences sharedPref = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- JSONSchedule = new JSONObject(JSONScheduleString);
- chechResult = JSONSchedule.getString("result");
- Log.i("Check Result",chechResult);
- editor.putString("resultbubu", chechResult);
- editor.commit();
- JSONUserInfo = new JSONObject(resultServer);
- result = JSONUserInfo.getString("result");
- Log.i("Result", result);
- editor.putString("result", result);
- editor.commit();
- info = JSONUserInfo.getJSONObject("info");
- System.out.println(info);
- type = info.getString("type");
- System.out.println(type);
- editor.putString( "type", type);
- editor.commit();
- name_parsed = info.getString("name");
- System.out.println(name_parsed);
- Log.i("Test Name", "Start Login");
- name_parsed ="\n"+name_parsed;
- editor.putString(Constant.NAME, name_parsed);
- editor.commit();
- skype = info.getString("skype");
- System.out.println(skype);
- editor.putString( "skype", skype);
- editor.commit();
- dob = info.getString("dob");
- System.out.println(dob);
- editor.putString( "dob", dob);
- editor.commit();
- timezone = info.getString("timezone");
- System.out.println(timezone);
- editor.putString( "timezone", timezone);
- editor.commit();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Prepare Login
- if(result.equals("false"))
- {
- // Dialog
- final AlertDialog.Builder ad = new AlertDialog.Builder(this);
- ad.setTitle("Error! ");
- // ad.setIcon(android.R.drawable.btn_star_big_on);
- ad.setPositiveButton("Close", null);
- ad.setMessage(strError);
- ad.show();
- // loginText.setText("");
- loginPassword.setText("");
- }else if (result.equals("true")){
- //parse json here
- try {
- JSONSchedule = new JSONObject(JSONScheduleString);
- System.out.println("-------------printJSONObject-------------");
- Log.i("JSON Object - Schedule", ("" + JSONSchedule) );
- System.out.println("-------------printJSONObject-------------");
- chechResult = JSONSchedule.getString("result");
- Log.i("Result", chechResult);
- SharedPreferences sharedPref = this.getSharedPreferences(Constant.MYSHAREDPREFERENCE, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.putString("resultbubu", chechResult);
- editor.commit();
- // ========================================================
- JSONArray aClass = JSONSchedule.getJSONArray("class");
- Log.i("JSON Array - Class", ("" + aClass) );
- // a = array
- int nClass = aClass.length();
- Log.i("Number of classes", ("" + nClass));
- // n = number of
- JSONArray aSchedule;
- nAllClass = 0;
- for(int i=0; i<nClass; i++){
- aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
- nAllClass += aSchedule.length();
- }
- //===========IndiClass START====================
- IndiClass[] classes = new IndiClass[nAllClass];
- for(int i = 0; i < nAllClass; i++){
- classes[i] = new IndiClass();
- }
- int classTracker = 0;
- //TODO: If have time, this can be change into 2 loop and one method
- /*
- From OriGinAl:
- The method setClassInfo can just pass the array of JSON("schedule")
- to setSchedule within itself.
- */
- for(int i=0; i<nClass; i++){
- aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
- for(int j=0; j<aSchedule.length(); j++){
- classes[classTracker].setScheduleInfo(aSchedule.getJSONObject(j));
- classTracker++;
- }
- }
- classTracker = 0;
- Log.i("Class tracker", ("" + classTracker));
- int nSchedule;
- for(int i=0; i<nClass; i++){
- aSchedule = aClass.getJSONObject(i).getJSONArray("schedule");
- nSchedule = aSchedule.length();
- Log.i("Class no." + i, ("" + nSchedule));
- String aName = aClass.getJSONObject(i).getJSONArray("course_name").getString(0);
- for(int j=0; j<nSchedule; j++){
- classes[classTracker].setClassInfo(aClass.getJSONObject(i), aName);
- classTracker++;
- }
- }
- IndiClass.sortByDate(classes);
- for(int i=0; i<nAllClass; i++){
- Log.i("Schedule no." + (i+1), classes[i].toString());
- }
- //for loop here
- // } for (int j=0;j<schedulenum;j++) {
- // JSONArray f[] = new JSONArray[100];
- // f[j] = ee[i].getJSONArray("schedule");
- // System.out.println(f[j] + "got array f"+j);
- // int appointnum = f[j].length();
- // //for loop here
- // for (int k=0;k<appointnum;k++) {
- // JSONObject g[] = new JSONObject[100];
- // g[k] = f[j].getJSONObject(i);
- // System.out.println(g[k] + "got object g"+g);
- //// int stdytime =
- //
- // }
- // }
- // int len = f.length();
- // System.out.println(len);
- // JSONObject g= f.getJSONObject(0);
- //
- // System.out.println(g+"got object g");
- // JSONArray mArray = new JSONArray(f);
- // int id_count = 0;
- // int devices_count = 0;
- // int tmp = 0;
- //
- // for (int i = 0; i<mArray.length(); i++) {
- // try {
- // tmp = mArray.getJSONObject(i).getInt("id");
- // id_count = id_count + 1;
- // }
- // catch (JSONException e) {
- // // If id doesn't exist, this exception is thrown
- // }
- // try {
- // tmp = mArray.getJSONObject(i).getInt("time");
- // devices_count = devices_count + 1;
- // }
- // catch (JSONException e) {
- // // If number_of_devices doesn't exist, this exception is thrown
- // }
- // }
- // System.out.println(devices_count);
- // System.out.println(id_count);
- // Iterator<Object> keys = f.keys();
- // while (keys.hasNext()) {
- // String key = keys.next();
- // try{
- // JSONArray array = jObject.getJSONArray(key);
- // //count objects values using array length.
- // }
- // catch(Exception e){
- // e.printStackTrace()
- // }
- // }
- //
- //
- //
- dbHandler = new DatabaseHandler(getApplicationContext());
- dbHandler.deleteAll();
- for(IndiClass classObj: classes) {
- dbHandler.createClassData(classObj);
- }
- Log.i("Done DataBase Creation", "SQLite");
- }catch (JSONException e){
- e.printStackTrace();
- }
- // //==========Test Notification===========
- // // get a Calendar object with current time
- // Calendar cal = Calendar.getInstance();
- // // add 5 minutes to the calendar object
- // cal.add(Calendar.MINUTE, 1);
- // Intent intent = new Intent(this, AlarmReceiver.class);
- // intent.putExtra("alarm_message", "O'Doyle Rules!");
- // // In reality, you would want to have a static variable for the request code instead of 192837
- // PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- //
- // // Get the AlarmManager service
- // AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
- // am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
- // //======================================
- // 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"};
- // String[] nameARR = {"TEST1", "TEST2", "TEST3", "TEST4", "TEST5"};
- // for (int i = 0; i < timeARR.length; i++){
- // IndiClass.setLocalNoti(login.this, nameARR[i], timeARR[i], i);
- // }
- for (int i = 1; i<=login.nAllClass; i++) {
- IndiClass classObj = login.dbHandler.getIndiClassBySQLid(i);
- IndiClass.setLocalNoti(login.this, classObj._course_name, classObj._time_text, Integer.parseInt(classObj.scID));
- }
- Intent newActivity = new Intent(this, drawer.class);
- startActivity(newActivity);
- }else if (result.equals("null")){
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.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();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void userPassForDebug(View v){
- EditText username = (EditText) findViewById(R.id.email_white_edit);
- EditText password = (EditText) findViewById(R.id.password_edit);
- username.setText("maxnight007@hotmail.com");
- password.setText("liverpool");
- Log.i("Set text", "clicked");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement