From 7c08da98e2a9f2b907cc199389dfdccc8baee905 Mon Sep 17 00:00:00 2001 From: Reynaldo Reyes Date: Mon, 11 Apr 2016 00:07:15 -0430 Subject: [PATCH] Create M.A.S.A. Mobile version 1.0 --- app/app.iml | 2 - .../CourseActivity.java | 37 ++- .../MainActivity.java | 21 +- .../SectionActivity.java | 55 ++-- .../Services/ServiceHandler.java | 37 +-- .../Services/TransmitterTask.java | 45 +++ .../StudentActivity.java | 303 ++++++++++++++++-- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/activity_scan.xml | 9 +- .../main/res/layout/list_item_students.xml | 14 +- app/src/main/res/layout/student_activity.xml | 28 +- app/src/main/res/values/strings.xml | 3 +- 12 files changed, 431 insertions(+), 125 deletions(-) create mode 100644 app/src/main/java/massiveattendancescannerapplication/Services/TransmitterTask.java diff --git a/app/app.iml b/app/app.iml index 4667ff9..3647b8d 100644 --- a/app/app.iml +++ b/app/app.iml @@ -64,13 +64,11 @@ - - diff --git a/app/src/main/java/massiveattendancescannerapplication/CourseActivity.java b/app/src/main/java/massiveattendancescannerapplication/CourseActivity.java index b7cf8f5..b3e68f3 100644 --- a/app/src/main/java/massiveattendancescannerapplication/CourseActivity.java +++ b/app/src/main/java/massiveattendancescannerapplication/CourseActivity.java @@ -23,12 +23,16 @@ public class CourseActivity extends ListActivity { private ProgressDialog pDialog; ArrayList> courseList; - JSONArray course = null; - private static final String URL = "http://192.168.0.101:3000/courses"; + JSONArray courses; + JSONObject professor; + String JSONString; + private static final String COURSE = "courses"; + private static final String URL = "http://192.168.0.106:3000/professors/56f5fd3a20047f3c15b05f0e"; private static final String TAG_ID = "_id"; private static final String TAG_NAME = "name"; private static final String TAG_CODE = "code"; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -66,7 +70,6 @@ public class CourseActivity extends ListActivity { protected String doInBackground(String... args) { ServiceHandler sh = new ServiceHandler(); - String JSONString = null; try { JSONString = sh.getServiceCall(URL); } catch (IOException e) { @@ -74,20 +77,20 @@ public class CourseActivity extends ListActivity { } try { - course = new JSONArray(JSONString); - - for (int i = 0; i < course.length(); i++) { - JSONObject c = course.getJSONObject(i); - String id = c.getString(TAG_ID); - String name = c.getString(TAG_NAME); - String code = c.getString(TAG_CODE); - - HashMap map = new HashMap(); - map.put(TAG_ID, id); - map.put(TAG_NAME, name); - map.put(TAG_CODE, code); - - courseList.add(map); + professor = new JSONObject(JSONString); + if (professor.has(COURSE)) { + courses = professor.getJSONArray(COURSE); + for (int i = 0; i < courses.length(); i++) { + JSONObject c = courses.getJSONObject(i); + String id = c.getString(TAG_ID); + String name = c.getString(TAG_NAME); + String code = c.getString(TAG_CODE); + HashMap map = new HashMap(); + map.put(TAG_ID, id); + map.put(TAG_NAME, name); + map.put(TAG_CODE, code); + courseList.add(map); + } } } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/massiveattendancescannerapplication/MainActivity.java b/app/src/main/java/massiveattendancescannerapplication/MainActivity.java index ee431dc..cd249be 100644 --- a/app/src/main/java/massiveattendancescannerapplication/MainActivity.java +++ b/app/src/main/java/massiveattendancescannerapplication/MainActivity.java @@ -6,13 +6,9 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.content.Intent; -import android.bluetooth.BluetoothAdapter; - public class MainActivity extends ActionBarActivity { - public static final int REQUEST_ENABLE_BT = 1; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -38,23 +34,8 @@ public class MainActivity extends ActionBarActivity { return super.onOptionsItemSelected(item); } - //Called when the user clicks on Scan - public void scanPhones (View view) { - - BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (mBluetoothAdapter == null) { - // Device does not support Bluetooth - } - - if (!mBluetoothAdapter.isEnabled()) { - Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); - } - Intent intent = new Intent(this, ScanActivity.class); - startActivity(intent); - } //Called when the user clicks on Listar Materias - public void adminScreen (View view) { + public void courseScreen (View view) { Intent intent = new Intent(this, CourseActivity.class); startActivity(intent); } diff --git a/app/src/main/java/massiveattendancescannerapplication/SectionActivity.java b/app/src/main/java/massiveattendancescannerapplication/SectionActivity.java index cecf80c..e51864f 100644 --- a/app/src/main/java/massiveattendancescannerapplication/SectionActivity.java +++ b/app/src/main/java/massiveattendancescannerapplication/SectionActivity.java @@ -23,8 +23,12 @@ public class SectionActivity extends ListActivity { private ProgressDialog pDialog; ArrayList> sectionList; - String course_id, course_name; - private static final String URL = "http://192.168.0.101:3000/sections"; + JSONArray courses,sections; + JSONObject professor; + String JSONString, course_id, course_name; + private static final String COURSE = "courses"; + private static final String SECTION = "sections"; + private static final String URL = "http://192.168.0.106:3000/professors/56f5fd3a20047f3c15b05f0e"; private static final String TAG_ID = "_id"; private static final String TAG_NAME = "name"; private static final String TAG_SEMESTER = "semester"; @@ -72,7 +76,6 @@ public class SectionActivity extends ListActivity { protected String doInBackground(String... args) { ServiceHandler sh = new ServiceHandler(); - String JSONString = null; try { JSONString = sh.getServiceCall(URL); } catch (IOException e) { @@ -80,29 +83,40 @@ public class SectionActivity extends ListActivity { } try { - JSONArray sections = new JSONArray(JSONString); - for (int i = 0; i < sections.length(); i++) { - JSONObject c = sections.getJSONObject(i); + professor = new JSONObject(JSONString); + if (professor.has(COURSE)) { + courses = professor.getJSONArray(COURSE); + for (int i = 0; i < courses.length(); i++) { + JSONObject c = courses.getJSONObject(i); + String course = c.getString(TAG_ID); + assert course != null; + if (course.equals(course_id)){ + if (c.has(SECTION)){ + sections = c.getJSONArray(SECTION); + for (int j = 0; j < sections.length(); j++) { + JSONObject s = sections.getJSONObject(j); - String section_id = c.getString(TAG_ID); - String section_no = String.valueOf(i + 1); - String name = c.getString(TAG_NAME); - String semester = c.getString(TAG_SEMESTER); + String section_id = s.getString(TAG_ID); + String section_no = String.valueOf(j + 1); + String name = s.getString(TAG_NAME); + String semester = s.getString(TAG_SEMESTER); - - HashMap map = new HashMap(); - map.put("course_id", course_id); - map.put(TAG_ID, section_id); - map.put("section_no", section_no + "."); - map.put(TAG_NAME, name); - map.put(TAG_SEMESTER, semester); - - sectionList.add(map); + HashMap map = new HashMap(); + map.put("course_id", course_id); + map.put(TAG_ID, section_id); + map.put("section_no", section_no + "."); + map.put(TAG_NAME, name); + map.put(TAG_SEMESTER, semester); + sectionList.add(map); + } + } + } + } } - } catch (JSONException e) { e.printStackTrace(); } + return null; } @@ -110,7 +124,6 @@ public class SectionActivity extends ListActivity { pDialog.dismiss(); runOnUiThread(new Runnable() { public void run() { - ListAdapter adapter = new SimpleAdapter( SectionActivity.this, sectionList, R.layout.list_item_sections, new String[] { "course_id", TAG_ID, "track_no", diff --git a/app/src/main/java/massiveattendancescannerapplication/Services/ServiceHandler.java b/app/src/main/java/massiveattendancescannerapplication/Services/ServiceHandler.java index 3fe6d48..990bcb3 100644 --- a/app/src/main/java/massiveattendancescannerapplication/Services/ServiceHandler.java +++ b/app/src/main/java/massiveattendancescannerapplication/Services/ServiceHandler.java @@ -5,9 +5,10 @@ import android.util.Log; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; -import org.json.JSONArray; +import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; @@ -70,31 +71,25 @@ public class ServiceHandler { * @author Reynaldo */ - public boolean postServiceCall(String url, JSONArray jsonArray) throws IOException { + public boolean postServiceCall(String url, JSONObject jsonobject) throws IOException { int inputStream; response=""; - try { - HttpClient httpclient = new DefaultHttpClient(); - HttpPost httpPost = new HttpPost(url); - String json = jsonArray.toString(); - StringEntity se = new StringEntity(json); - httpPost.setEntity(se); - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-type", "application/json"); - HttpResponse httpResponse = httpclient.execute(httpPost); - inputStream = httpResponse.getStatusLine().getStatusCode(); - if (inputStream == 200){ - return true; - }else { - return false; - } + HttpClient httpclient = new DefaultHttpClient(); + HttpPut httpPut = new HttpPut(url); - - } catch (Exception e) { - Log.d("InputStream", e.getLocalizedMessage()); + String json = jsonobject.toString(); + StringEntity se = new StringEntity(json); + httpPut.setEntity(se); + httpPut.setHeader("Accept", "application/json"); + httpPut.setHeader("Content-type", "application/json"); + HttpResponse httpResponse = httpclient.execute(httpPut); + inputStream = httpResponse.getStatusLine().getStatusCode(); + if (inputStream == 200){ + return true; + }else { + return false; } - return false; } private static String convertInputStreamToString(InputStream inputStream) throws IOException{ diff --git a/app/src/main/java/massiveattendancescannerapplication/Services/TransmitterTask.java b/app/src/main/java/massiveattendancescannerapplication/Services/TransmitterTask.java new file mode 100644 index 0000000..09cd35d --- /dev/null +++ b/app/src/main/java/massiveattendancescannerapplication/Services/TransmitterTask.java @@ -0,0 +1,45 @@ +package massiveattendancescannerapplication.Services; + +import org.json.JSONObject; + + +import android.os.AsyncTask; +import android.os.Handler; + +/** + *

+ * This is an AsyncTask that handles the construction of the Answer object and the AnswerArray + * which is the JSON object containing a JSON array of JSON objects. After the final JSON object + * is created this function invokes the POST method to send the information o the corresponding + * service. + *

+ * + * @author Reynaldo + */ + +public class TransmitterTask extends AsyncTask { + + private static final String URL = "http://192.168.0.106:3000/professors/56f5fd3a20047f3c15b05f0e"; + boolean result; + + final Handler myHandler = new Handler(); + public TransmitterTask() { + + } + + protected Void doInBackground(JSONObject... professor) { + + while (true){ + ServiceHandler pt = new ServiceHandler(); + try{ + result = pt.postServiceCall(URL,professor[0]); + if(result){ + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/app/src/main/java/massiveattendancescannerapplication/StudentActivity.java b/app/src/main/java/massiveattendancescannerapplication/StudentActivity.java index 3a69a47..eaa6346 100644 --- a/app/src/main/java/massiveattendancescannerapplication/StudentActivity.java +++ b/app/src/main/java/massiveattendancescannerapplication/StudentActivity.java @@ -1,45 +1,100 @@ package massiveattendancescannerapplication; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.app.ProgressDialog; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.AsyncTask; import android.os.Bundle; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; +import android.widget.TextView; +import android.widget.Toast; + import massiveattendancescannerapplication.Services.ServiceHandler; +import massiveattendancescannerapplication.Services.TransmitterTask; public class StudentActivity extends ListActivity { private ProgressDialog pDialog; ArrayList> studentList; - private static final String URL = "http://192.168.0.101:3000/sections"; + JSONArray courses,sections,students; + JSONObject professor; + String course_id, section_name, section_id, currentDate; + private static final String COURSE = "courses"; + private static final String SECTION = "sections"; + private static final String STUDENT = "students"; + private static final String URL = "http://192.168.0.106:3000/professors/56f5fd3a20047f3c15b05f0e"; private static final String TAG_ID = "_id"; private static final String TAG_NO = "id"; private static final String TAG_NAME = "name"; private static final String TAG_LASTNAME = "lastname"; private static final String TAG_STUDENTS = "students"; - JSONArray students = null; - String section_id = null; - String section_name = null; + private static final String BT_ADDRESS = "btaddress"; + private static final int REQUEST_ENABLE_BT = 1; + SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy"); + TextView stateBluetooth; + BluetoothAdapter bluetoothAdapter; + ListView listDevicesFound; + Button btnScanDevice,btnEndClass; + TransmitterTask transmitterTask; + ArrayAdapter btArrayAdapter; + int assist = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.student_activity); Intent i = getIntent(); + + stateBluetooth = (TextView)findViewById(R.id.bluetoothstate); + bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + + listDevicesFound = (ListView)findViewById(R.id.devicesfound); + btArrayAdapter = new ArrayAdapter<>(StudentActivity.this, + android.R.layout.simple_list_item_1); + listDevicesFound.setAdapter(btArrayAdapter); + + btnScanDevice = (Button)findViewById(R.id.scandevice); + btnScanDevice.setOnClickListener(btnScanDeviceOnClickListener); + + btnEndClass = (Button)findViewById(R.id.terminate); + btnEndClass.setOnClickListener(btnEndClassOnClickListener); + + CheckBlueToothState(); + + registerReceiver(ActionFoundReceiver, + new IntentFilter(BluetoothDevice.ACTION_FOUND)); + + course_id = i.getStringExtra("course_id"); section_id = i.getStringExtra("section_id"); studentList = new ArrayList<>(); new LoadStudents().execute(); - ListView lv = getListView(); + } + + @Override + protected void onStop() + { + unregisterReceiver(ActionFoundReceiver); + super.onStop(); } class LoadStudents extends AsyncTask { @@ -67,49 +122,61 @@ public class StudentActivity extends ListActivity { } try { - JSONArray sections = new JSONArray(JSONString); - - for (int i = 0; i < sections.length(); i++) { - JSONObject c = sections.getJSONObject(i); - String section = c.getString(TAG_ID); - assert section != null; - if (section.equals(section_id)){ - section_name = c.getString(TAG_NAME); - students = c.getJSONArray(TAG_STUDENTS); - for (int x = 0; x < students.length(); x++) { - JSONObject s = students.getJSONObject(x); - String student_id = String.valueOf(x + 1); - String student_no = s.getString(TAG_NO); - String name = s.getString(TAG_NAME); - String lastname = s.getString(TAG_LASTNAME); - - HashMap map = new HashMap(); - map.put("student_id", student_id); - map.put(TAG_ID, student_id); - map.put("student_no", student_no); - map.put(TAG_NAME, name); - map.put(TAG_LASTNAME, lastname); - - studentList.add(map); + professor = new JSONObject(JSONString); + if (professor.has(COURSE)) { + courses = professor.getJSONArray(COURSE); + for (int i = 0; i < courses.length(); i++) { + JSONObject c = courses.getJSONObject(i); + String course = c.getString(TAG_ID); + assert course != null; + if (course.equals(course_id)){ + if (c.has(SECTION)) { + sections = c.getJSONArray(SECTION); + for (int j = 0; j < sections.length(); j++) { + JSONObject s = sections.getJSONObject(j); + String section = s.getString(TAG_ID); + assert section != null; + if (section.equals(section_id)) { + if(s.has(STUDENT)) { + section_name = s.getString(TAG_NAME); + students = s.getJSONArray(TAG_STUDENTS); + for (int x = 0; x < students.length(); x++) { + JSONObject st = students.getJSONObject(x); + String student_id = String.valueOf(x + 1); + String student_no = st.getString(TAG_NO); + String name = st.getString(TAG_NAME); + String lastname = st.getString(TAG_LASTNAME); + HashMap map = new HashMap<>(); + map.put("student_id", student_id); + map.put(TAG_ID, student_id); + map.put("student_no", student_no); + map.put(TAG_NAME, name); + map.put(TAG_LASTNAME, lastname); + studentList.add(map); + } + } + } + } + } } } } } catch (JSONException e) { e.printStackTrace(); } + return null; } protected void onPostExecute(String file_url) { - pDialog.dismiss(); runOnUiThread(new Runnable() { public void run() { ListAdapter adapter = new SimpleAdapter( StudentActivity.this, studentList, - R.layout.list_item_students, new String[] { "section_id", TAG_ID, TAG_NO, - TAG_NAME, TAG_LASTNAME }, new int[] { - R.id.section_id, R.id.student_id, R.id.student_no, R.id.student_name, R.id.student_lastname }); + R.layout.list_item_students, new String[]{"section_id", TAG_ID, TAG_NO, + TAG_NAME, TAG_LASTNAME}, new int[]{ + R.id.section_id, R.id.student_id, R.id.student_no, R.id.student_name, R.id.student_lastname}); setListAdapter(adapter); setTitle(section_name); @@ -117,4 +184,174 @@ public class StudentActivity extends ListActivity { }); } } + + private void CheckBlueToothState(){ + if (bluetoothAdapter == null){ + stateBluetooth.setText("Bluetooth NOT supported"); + }else{ + if (bluetoothAdapter.isEnabled()){ + if(bluetoothAdapter.isDiscovering()){ + stateBluetooth.setText("Bluetooth is currently in device discovery process."); + btnScanDevice.setEnabled(false); + + }else{ + stateBluetooth.setText("Bluetooth is Enabled."); + btnScanDevice.setEnabled(true); + } + }else{ + stateBluetooth.setText("Bluetooth is NOT Enabled!"); + Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); + } + } + } + + private Button.OnClickListener btnScanDeviceOnClickListener + = new Button.OnClickListener(){ + @Override + public void onClick(View arg0) { + btArrayAdapter.clear(); + bluetoothAdapter.startDiscovery(); + }}; + + private Button.OnClickListener btnEndClassOnClickListener + = new Button.OnClickListener(){ + + @Override + public void onClick(View arg0) { + try { + for (int x = 0; x < students.length(); x++) { + JSONObject st; + boolean scanned = false; + st = students.getJSONObject(x); + JSONArray assistance; + assistance = st.getJSONArray("assistanceTotal"); + assert assistance != null; + currentDate = SDF.format(new Date()); + for(int g=0; g diff --git a/app/src/main/res/layout/activity_scan.xml b/app/src/main/res/layout/activity_scan.xml index 88d099e..a6d1829 100644 --- a/app/src/main/res/layout/activity_scan.xml +++ b/app/src/main/res/layout/activity_scan.xml @@ -16,9 +16,16 @@ android:text="@string/scanning" android:enabled="false" /> +