Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  1. package com.example.bluetooth;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import androidx.annotation.Nullable;
  14. import androidx.appcompat.app.AppCompatActivity;
  15. import com.android.volley.Request;
  16. import com.android.volley.RequestQueue;
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.android.volley.toolbox.Volley;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Calendar;
  28. import java.util.Hashtable;
  29. import java.util.Map;
  30. import java.util.Set;
  31.  
  32. public class MainActivity extends AppCompatActivity {
  33. /**
  34. * Variables FINAL
  35. */
  36. private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("d-M-yyyy");
  37. /**
  38. private static final String URL_TEST = "https://firestore.googleapis.com/v1/projects/attendancelistapp/databases/(default)/documents/attendance/5-7-2019/students/201212651";
  39. */
  40. private static final String URL = "https://firestore.googleapis.com/v1/projects/attendancelistapp/databases/(default)/documents/attendance/";
  41. private static final String STUDENTS_PATH = "/students";
  42. private static final String ID_STUDENTS = "/201327471";
  43. private static final String URL_STUDENT_LIST = "https://firestore.googleapis.com/v1/projects/attendancelistapp/databases/(default)/documents/studentsList?pageSize=100";
  44.  
  45. private static final String TAG = "MainActivity";
  46. private static final String TAG_DATE = "FormatDate";
  47. private static final String TAG_INTERVAL = "Date Interval";
  48. private static final String TAG_ABSCENCE = "Abscence";
  49. private static final String TAG_STUDENT_LIST = "Student List";
  50. private static final String TAG_NORMAL = "Normal";
  51.  
  52. private static final int ENABLE_BT = 0;
  53. private static final int DISCOVERY_BT = 1;
  54.  
  55. /**
  56. * Botones, texto, que van ha estar en la interfaz del app movil
  57. */
  58. private TextView mStatusBlueTv, mStatusBlueMAC, mAssistanceMov;
  59. private ImageView mBlueTv;
  60. private Button mOnBtn, mOffBtn, mDiscoverBtn, mTryAgain;
  61.  
  62. private BluetoothAdapter mBlueAdapter;
  63.  
  64. private Map<String, String> students = new Hashtable<>();
  65.  
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. this.getStudentList();
  70. setContentView(R.layout.activity_main);
  71.  
  72. mStatusBlueTv = findViewById(R.id.statusBluetoothTv);
  73. mStatusBlueMAC = findViewById(R.id.statusBluetoothMAC);
  74. mAssistanceMov = findViewById(R.id.statusAssistanceMov);
  75. mBlueTv = findViewById(R.id.bluetoothTv);
  76. mOnBtn = findViewById(R.id.onBtn);
  77. mOffBtn = findViewById(R.id.offBtn);
  78. mDiscoverBtn = findViewById(R.id.discoverableBtn);
  79. mTryAgain = findViewById(R.id.tryagain);
  80.  
  81. /*
  82. Obtiene la MAC del dispositivo
  83. */
  84. this.getMAC();
  85.  
  86. /*
  87. Obtiene si el bluetooth esta disponible
  88. */
  89. if (getBluetooth()) return;
  90.  
  91. /*
  92. Pinta la imagen dependiendo de si el bluetooth esta prendido
  93. */
  94. this.paintBluetooth();
  95.  
  96. /*
  97. Obtiene si estas inscrito o no en firestore
  98. */
  99. this.getAttendance();
  100.  
  101. /*
  102. Prende el bluetooth
  103. */
  104. this.getBluetoothOn();
  105.  
  106. /*
  107. Apaga el bluetooth
  108. */
  109. this.getBluetoothOff();
  110.  
  111. /*
  112. Hace que el dispositivo sea visible
  113. */
  114. this.getDiscoveryOn();
  115.  
  116. /*
  117. Busca de nuevo en la base de datos
  118. */
  119. mTryAgain.setOnClickListener(new View.OnClickListener() {
  120. @Override
  121. public void onClick(View view) {
  122. getAttendance();
  123. }
  124. });
  125. Log.w("AQUI!", students.isEmpty() + "");
  126. this.getIntervalDates();
  127. }
  128.  
  129. private void getIntervalDates() {
  130.  
  131. Calendar initCalendar = Calendar.getInstance();
  132. initCalendar.set(2019, 8, 5);
  133. initCalendar.add(Calendar.MONTH, -1);
  134.  
  135. Calendar endCalendar = Calendar.getInstance();
  136. endCalendar.set(2019, 12, 31);
  137. endCalendar.add(Calendar.MONTH, -1);
  138.  
  139. for (Calendar init = initCalendar; !endCalendar.before(initCalendar) ; init.add(Calendar.DATE, 1)){
  140. if(init.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY || init.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY || init.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
  141. Log.w(TAG_INTERVAL, SIMPLE_DATE_FORMAT.format(init.getTime()));
  142. try {
  143. RequestQueue que = Volley.newRequestQueue(this);
  144. Log.w(TAG_INTERVAL, students.isEmpty() + "");
  145. Set<String> studentsID = students.keySet();
  146. Log.w(TAG_INTERVAL, studentsID.toString());
  147. for (String studentID: studentsID) {
  148. Log.w(TAG_INTERVAL, studentID);
  149. String url = URL + SIMPLE_DATE_FORMAT.format(init.getTime()) + STUDENTS_PATH + studentID;
  150. Log.w(TAG_INTERVAL, url);
  151. JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  152. @SuppressLint("SetTextI18n")
  153. @Override
  154. public void onResponse(JSONObject response) {}
  155. }, new Response.ErrorListener() {
  156. @SuppressLint("SetTextI18n")
  157. @Override
  158. public void onErrorResponse(VolleyError e) {
  159. Log.w(TAG_ABSCENCE, "NO ESTA INSCRITO");
  160. Log.w(TAG_ABSCENCE, e.toString());
  161. }
  162. });
  163. que.add(getRequest);
  164. }
  165. } catch(Exception e){
  166. Log.w(TAG_INTERVAL, e.getMessage());
  167. }
  168. }
  169. }
  170.  
  171.  
  172. }
  173.  
  174. private void getStudentList() {
  175. try{
  176. RequestQueue requestQueue = Volley.newRequestQueue(this);
  177. JsonObjectRequest getList = new JsonObjectRequest(Request.Method.GET, URL_STUDENT_LIST, null, new Response.Listener<JSONObject>() {
  178. @SuppressLint("SetTextI18n")
  179. @Override
  180. public void onResponse(JSONObject response) {
  181. Map<String, String> temp = new Hashtable<>();
  182. try {
  183. JSONArray document = response.getJSONArray("documents");
  184. for (int i = 0; i < document.length() ;i++){
  185. JSONObject jsonObject = document.getJSONObject(i);
  186. JSONObject fields = null;
  187. try {
  188. fields = jsonObject.getJSONObject("fields");
  189. } catch (Exception e){
  190. Log.w(TAG_NORMAL, e.toString());
  191. }
  192. if(fields != null){
  193. String name = fields.getJSONObject("name").getString("stringValue");
  194. String id = fields.getJSONObject("code").getString("stringValue");
  195. temp.put(id, name);
  196. }
  197. }
  198. students.clear();
  199. students.putAll(temp);
  200.  
  201. Log.w(TAG_NORMAL, students.size() + "");
  202. } catch (JSONException e) {
  203. Log.w(TAG_STUDENT_LIST, e.toString());
  204. }
  205. }
  206. }, new Response.ErrorListener() {
  207. @SuppressLint("SetTextI18n")
  208. @Override
  209. public void onErrorResponse(VolleyError e) {
  210. Log.w(TAG_STUDENT_LIST, "NO ESTA INSCRITO");
  211. Log.w(TAG_STUDENT_LIST, e.toString());
  212. }
  213. });
  214. requestQueue.add(getList);
  215. Log.w(TAG_NORMAL, students.size() + "");
  216. } catch(Exception e){
  217. Log.w(TAG_STUDENT_LIST, e.toString());
  218. }
  219. }
  220.  
  221. private void getDiscoveryOn() {
  222. mDiscoverBtn.setOnClickListener(new View.OnClickListener() {
  223. @Override
  224. public void onClick(View view) {
  225. if(!mBlueAdapter.isDiscovering()){
  226. showMessage("Making Your Device Discoverable");
  227. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  228. startActivityForResult(intent, DISCOVERY_BT);
  229. mBlueTv.setImageResource(R.drawable.ic_action_bluetooth);
  230. }else{
  231. showMessage("Bluetooth is already Discoverable");
  232. }
  233. }
  234. });
  235. }
  236.  
  237. private void getBluetoothOff() {
  238. mOffBtn.setOnClickListener(new View.OnClickListener() {
  239. @Override
  240. public void onClick(View view) {
  241. if(mBlueAdapter.isEnabled()){
  242. mBlueAdapter.disable();
  243. showMessage("Turning Bluetooth Off");
  244. mBlueTv.setImageResource(R.drawable.ic_action_off);
  245. }else{
  246. showMessage("Bluetooth is already off");
  247. }
  248. }
  249. });
  250. }
  251.  
  252. private void getBluetoothOn() {
  253. mOnBtn.setOnClickListener(new View.OnClickListener() {
  254. @Override
  255. public void onClick(View view) {
  256. if(!mBlueAdapter.isEnabled()){
  257. showMessage("Turning on Bluetooth");
  258. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  259. startActivityForResult(intent, ENABLE_BT);
  260. }else{
  261. showMessage("Bluetooth is already on");
  262. }
  263. }
  264. });
  265. }
  266.  
  267. @SuppressLint("SetTextI18n")
  268. private void getAttendance() {
  269. /*
  270. Crea el formato de la fecha del dia
  271. */
  272. String dateFormat = getDate();
  273.  
  274. /*
  275. Genera la URL de la base de datos
  276. */
  277. String urlCompleted = URL + dateFormat + STUDENTS_PATH + ID_STUDENTS;
  278. Log.w(TAG, urlCompleted);
  279.  
  280. /*
  281. Busca el ID de estudiante con la fecha establecida
  282. */
  283. try {
  284. RequestQueue que = Volley.newRequestQueue(this);
  285. JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, urlCompleted, null, new Response.Listener<JSONObject>() {
  286. @SuppressLint("SetTextI18n")
  287. @Override
  288. public void onResponse(JSONObject response) {
  289. Log.w(TAG, "FOUND");
  290. Log.w(TAG, response.toString());
  291. mAssistanceMov.setText("You are Registered");
  292. mTryAgain.setText("YEI");
  293. }
  294. }, new Response.ErrorListener() {
  295. @SuppressLint("SetTextI18n")
  296. @Override
  297. public void onErrorResponse(VolleyError e) {
  298. Log.w(TAG, "NO ESTA INSCRITO");
  299. Log.w(TAG, e.toString());
  300. mAssistanceMov.setError(e.toString());
  301. mAssistanceMov.setText("Not Registered");
  302. mTryAgain.setText("TELL TO MARIO");
  303. }
  304. });
  305. que.add(getRequest);
  306. } catch(Exception e){
  307. mAssistanceMov.setError(e.toString());
  308. mAssistanceMov.setText("ERROR IN EXCEPTION");
  309. Log.w(TAG, e.getMessage());
  310. }
  311. }
  312.  
  313. private String getDate() {
  314. String dateFormat = "";
  315. try{
  316. Calendar calendar = Calendar.getInstance();
  317. calendar.add(Calendar.MONTH, -1);
  318. dateFormat = SIMPLE_DATE_FORMAT.format(calendar.getTime());
  319. Log.w(TAG_DATE, dateFormat);
  320. }catch(Exception e){
  321. Log.w(TAG_DATE, e.toString());
  322. }
  323. return dateFormat;
  324. }
  325.  
  326. private void paintBluetooth() {
  327. if(mBlueAdapter.isEnabled()){
  328. mBlueTv.setImageResource(R.drawable.ic_action_bluetooth);
  329. }else{
  330. mBlueTv.setImageResource(R.drawable.ic_action_off);
  331. }
  332. }
  333.  
  334. @SuppressLint("SetTextI18n")
  335. private boolean getBluetooth() {
  336. mBlueAdapter = BluetoothAdapter.getDefaultAdapter();
  337. if(mBlueAdapter == null){
  338. mStatusBlueTv.setText("Bluetooth is not available");
  339. mBlueTv.setImageResource(R.drawable.ic_action_warning);
  340. return true;
  341. }else{
  342. mStatusBlueTv.setText("Bluetooth is available");
  343. }
  344. return false;
  345. }
  346.  
  347. @SuppressLint("SetTextI18n")
  348. private void getMAC() {
  349. String macAddress = android.provider.Settings.Secure.getString(getContentResolver(), "bluetooth_address");
  350. if(macAddress != null){
  351. mStatusBlueMAC.setText(macAddress);
  352. }else{
  353. mStatusBlueMAC.setText("It is not possible to find the Bluetooth MAC");
  354. }
  355. }
  356.  
  357.  
  358. @Override
  359. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  360. if (requestCode == ENABLE_BT) {
  361. if (resultCode == RESULT_OK) {
  362. mBlueTv.setImageResource(R.drawable.ic_action_bluetooth);
  363. showMessage("Bluetooth is on");
  364. } else {
  365. showMessage("couldn't on bluetooth");
  366. }
  367. }
  368. super.onActivityResult(requestCode, resultCode, data);
  369. }
  370.  
  371. private void showMessage(String msg){
  372. Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  373. }
  374.  
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement