1 package com.example.groom;
3 import android.annotation.SuppressLint;
4 import android.app.NotificationChannel;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
7 import android.bluetooth.BluetoothDevice;
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.content.SharedPreferences;
12 import android.os.Build;
13 import android.os.Bundle;
15 import androidx.appcompat.app.AlertDialog;
16 import androidx.appcompat.app.AppCompatActivity;
17 import androidx.appcompat.widget.Toolbar;
18 import androidx.core.app.NotificationCompat;
19 import androidx.core.content.ContextCompat;
20 import androidx.webkit.WebSettingsCompat;
21 import androidx.webkit.WebViewFeature;
23 import android.os.CountDownTimer;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.util.Base64;
27 import android.util.Log;
28 import android.view.Gravity;
29 import android.view.LayoutInflater;
30 import android.view.View;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.webkit.WebSettings;
35 import android.webkit.WebView;
36 import android.widget.AdapterView;
37 import android.widget.ArrayAdapter;
38 import android.widget.Button;
39 import android.widget.EditText;
40 import android.widget.ImageView;
41 import android.widget.RadioButton;
42 import android.widget.Spinner;
43 import android.widget.TextView;
44 import android.widget.Toast;
46 import java.util.ArrayList;
60 public class IHMGroom extends AppCompatActivity
63 private static final String
TAG =
"_IHMGroom";
130 SharedPreferences sharedPreferences;
142 super.onCreate(savedInstanceState);
143 setContentView(R.layout.activity_main);
144 Log.d(
TAG,
"onCreate()");
147 setTitle(getString(R.string.app_name) +
" v" + BuildConfig.VERSION_NAME);
150 sharedPreferences = getBaseContext().getSharedPreferences(
PREFERENCES, MODE_PRIVATE);
162 Toolbar toolbar = findViewById(R.id.toolbar);
163 setSupportActionBar(toolbar);
165 nouvelUrl =
"<iframe src=\"https://calendar.google.com/calendar/embed?src=robertutilisateur84%40gmail.com&ctz=Europe%2FParis\" style=\"border: 0\" width=\"760\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe>";
176 Log.d(
TAG,
"onStart()");
186 Log.d(
TAG,
"onResume()");
197 Log.d(
TAG,
"onPause()");
207 Log.d(
TAG,
"onStop()");
218 Log.d(
TAG,
"onDestroy()");
261 public void onClick(View v)
280 etatConnexion = (RadioButton) findViewById(R.id.etatConnexion);
286 public void onClick(View v)
288 Log.d(
TAG,
"onClick() boutonAjoutOccupant");
296 public void onClick(View v)
300 Log.d(
TAG,
"onClick() boutonEnvoieOccupant");
315 public void onClick(View v)
319 Log.d(
TAG,
"onClick() envoyerMessageOccupant");
324 titreEtat = (TextView) findViewById(R.id.titreEtat);
329 public void onClick(View v)
331 Log.d(
TAG,
"onClick() boutonRadioLibre");
343 public void onClick(View v)
345 Log.d(
TAG,
"onClick() boutonRadioOccupe");
359 public void onClick(View v)
361 Log.d(
TAG,
"onClick() boutonRadioAbsent");
376 boutonEntrer = (Button) findViewById(R.id.boutonEntrer);
377 boutonEntrer.setOnClickListener(
new View.OnClickListener()
380 public void onClick(View v)
382 Log.d(
TAG,
"onClick() boutonEntrer");
395 public void onClick(View v)
397 if(
boutonSonnette.getText().toString().equals(
"Activer Sonnette"))
399 Log.d(
TAG,
"onClick() boutonSonnette");
408 Log.d(
TAG,
"onClick() boutonSonnette");
424 public void onClick(View v)
453 public void onClick(View v)
455 Log.d(
TAG,
"onClick() boutonAjoutUrl");
470 ArrayList<String> listePeripheriques =
new ArrayList<String>();
473 listePeripheriques.add(device.getName());
475 ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, listePeripheriques);
481 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
483 Log.d(TAG,
"onItemSelected() position : " + position +
" -> " + CommunicationGroom.getListeGrooms().get(position).getName() +
" [" + CommunicationGroom.getListeGrooms().get(position).getAddress() +
"]");
484 if(choixGroom != position)
486 choixGroom = position;
487 communicationGroom = new CommunicationGroom(CommunicationGroom.getListeGrooms().get(position), handler);
492 public void onNothingSelected(AdapterView<?> parent)
504 Log.d(TAG,
"modifierOccupant()");
505 modificationOccupant =
new AlertDialog.Builder(
this);
507 modificationOccupant.setTitle(
"GROOM");
508 modificationOccupant.setMessage(
"Modifier les informations de l'occupannt : ");
511 LayoutInflater factory = LayoutInflater.from(
this);
512 final View ajoutJoueurView = factory.inflate(R.layout.dialogue_modification_occupant,
null);
513 modificationOccupant.setView(ajoutJoueurView);
515 modificationOccupant.setPositiveButton(
"Modifier",
new DialogInterface.OnClickListener()
518 public void onClick(DialogInterface dialog, int which)
521 EditText editNomOccupant =
522 (EditText) ((AlertDialog) dialog).findViewById(R.id.nomOccupant);
523 EditText editPrenomOccupant =
524 (EditText) ((AlertDialog) dialog).findViewById(R.id.prenomOccupant);
525 EditText editFonctionOccupant =
526 (EditText) ((AlertDialog) dialog).findViewById(R.id.fonctionOccupant);
528 "Modification occupant : " +
"Nom = " + editNomOccupant.getText().toString() +
" - " +
"Prénom = " + editPrenomOccupant.getText().toString() +
" - " +
"Fonction = " + editFonctionOccupant.getText().toString());
531 occupant.setNom(editNomOccupant.getText().toString());
532 occupant.setPrenom(editPrenomOccupant.getText().toString());
533 occupant.setFonction(editFonctionOccupant.getText().toString());
534 texteNomFonction.setText(occupant.getNom() +
" " + occupant.getPrenom() +
" - " + occupant.getFonction());
537 sharedPreferences.edit().putString(PREFERENCES_NOM_OCCUPANT, occupant.getNom()).apply();
538 sharedPreferences.edit().putString(PREFERENCES_PRENOM_OCCUPANT, occupant.getPrenom()).apply();
539 sharedPreferences.edit().putString(PREFERENCES_FONCTION, occupant.getFonction()).apply();
542 modificationOccupant.setNegativeButton(
"Annuler",
new DialogInterface.OnClickListener()
545 public void onClick(DialogInterface dialog, int which)
550 AlertDialog dialogueModificationOccupant = modificationOccupant.create();
551 dialogueModificationOccupant.show();
555 (EditText) ((AlertDialog) dialogueModificationOccupant).findViewById(R.id.nomOccupant);
556 nom.setText(occupant.getNom());
558 (EditText) ((AlertDialog) dialogueModificationOccupant).findViewById(R.id.prenomOccupant);
559 prenom.setText(occupant.getPrenom());
561 (EditText) ((AlertDialog) dialogueModificationOccupant).findViewById(R.id.fonctionOccupant);
562 fonction.setText(occupant.getFonction());
568 Log.d(TAG,
"modifierUrl()");
569 modificationUrl =
new AlertDialog.Builder(
this);
571 modificationUrl.setTitle(
"GROOM");
572 modificationUrl.setMessage(
"Modifier l'agenda");
575 LayoutInflater factory = LayoutInflater.from(
this);
576 final View ajoutModificationUrlView = factory.inflate(R.layout.dialogue_modification_calendrier,
null);
577 modificationUrl.setView(ajoutModificationUrlView);
579 modificationUrl.setPositiveButton(
"Modifier",
new DialogInterface.OnClickListener()
582 public void onClick(DialogInterface dialog, int which)
585 EditText editEntreeUrl =
586 (EditText) ((AlertDialog) dialog).findViewById(R.id.entreeUrl);
588 editEntreeUrl.getText().toString();
590 nouvelUrl = editEntreeUrl.getText().toString();
592 WebView myWebView = (WebView) findViewById(R.id.calendrierGroom);
593 WebSettings webSettings = myWebView.getSettings();
594 webSettings.setJavaScriptEnabled(true);
596 String unencodedHtml = nouvelUrl;
597 String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
598 myWebView.loadData(encodedHtml,
"text/html",
"base64");
599 if(WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK))
601 WebSettingsCompat.setForceDark(myWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
605 modificationUrl.setNegativeButton(
"Annuler",
new DialogInterface.OnClickListener()
608 public void onClick(DialogInterface dialog, int which)
613 AlertDialog dialogueModificationUrl = modificationUrl.create();
614 dialogueModificationUrl.show();
621 Log.d(TAG,
"onCreateOptionsMenu()");
622 getMenuInflater().inflate(R.menu.menu_main, menu);
629 int id = item.getItemId();
630 Log.d(TAG,
"onOptionsItemSelected() " +
"id = " +
id);
637 return super.onOptionsItemSelected(item);
643 final private Handler handler =
new Handler()
645 @SuppressLint(
"HandlerLeak")
646 public void handleMessage(Message msg)
648 super.handleMessage(msg);
653 Log.d(TAG,
"ERREUR_GROOM");
657 Log.d(TAG,
"CONNEXION_GROOM");
658 afficherConnexionGroom();
659 initialiserEtatGroom();
662 Log.d(TAG,
"DECONNEXION_GROOM");
663 afficherDeconnexionGroom();
666 String trame = msg.obj.toString();
667 Log.d(TAG,
"RECEPTION_GROOM -> " + trame);
669 String champs[] = trame.split(
";");
671 for(
int i=0;i<champs.length;++i)
673 Log.d(TAG,
" champ " + i +
" -> " + champs[i]);
681 Log.d(TAG,
" ETAT_LIBRE");
682 boutonRadioLibre.setChecked(
true);
683 boutonSonnette.setEnabled(
true);
687 Log.d(TAG,
" ETAT_ABSENT");
688 boutonRadioAbsent.setChecked(
true);
689 afficherEtatAbsent();
692 Log.d(TAG,
" ETAT_OCCUPE");
693 boutonRadioOccupe.setChecked(
true);
694 afficherEtatOccupe();
697 Log.d(TAG,
"ETAT_ENTRER");
698 afficherEtatEntrer();
704 Log.d(TAG,
" Appui sonnette !");
706 afficherNotification();
710 Log.d(TAG,
" Pas d'appui sonnette !");
715 Log.d(TAG,
" Présence personne détectée !");
720 Log.d(TAG,
" Pas de présence personne détectée !");
721 desactiverPresence();
726 Log.d(TAG,
" Détecteur_de présence activé !");
727 boutonPresence.setText(
"Desactiver");
728 affichageActiverModePresence();
733 Log.d(TAG,
" Détecteur_de présence désactivé !");
734 boutonPresence.setText(
"Activer");
735 affichageDesactiverModePresence();
741 Log.d(TAG,
" Sonnette activée !");
742 boutonSonnette.setText(
"Désactiver Sonnette");
746 Log.d(TAG,
" Sonnette désactivée !");
747 boutonSonnette.setText(
"Activer Sonnette");
764 Context context = getApplicationContext();
765 CharSequence text =
"Erreur communication !\nVérifier l'appairage et le branchement du groom.";
766 int duration = Toast.LENGTH_LONG;
767 Toast toast = Toast.makeText(context, text, duration);
769 toast.setGravity(Gravity.LEFT,600,-600);
777 Context context = getApplicationContext();
778 CharSequence text =
"Libre !";
779 int duration = Toast.LENGTH_LONG;
780 Toast toast = Toast.makeText(context, text, duration);
782 toast.setGravity(Gravity.LEFT,300,-150);
790 Context context = getApplicationContext();
791 CharSequence text =
"Occupé !";
792 int duration = Toast.LENGTH_LONG;
793 Toast toast = Toast.makeText(context, text, duration);
795 toast.setGravity(Gravity.LEFT,300,-70);
803 Context context = getApplicationContext();
804 CharSequence text =
"Absent !";
805 int duration = Toast.LENGTH_SHORT;
806 Toast toast = Toast.makeText(context, text, duration);
808 toast.setGravity(Gravity.LEFT,300,0);
816 Context context = getApplicationContext();
817 CharSequence text =
"Entrer !";
818 int duration = Toast.LENGTH_SHORT;
819 Toast toast = Toast.makeText(context, text, duration);
829 notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
837 CharSequence name = getString(R.string.app_name);
838 String description =
"Gestion GROOM";
839 int importance = NotificationManager.IMPORTANCE_DEFAULT;
840 NotificationChannel channel =
new NotificationChannel(
"GROOM", name, importance);
841 channel.setDescription(description);
842 notificationManager.createNotificationChannel(channel);
845 String titreNotification = getString(R.string.app_name);
846 Log.d(TAG,
"afficherNotification() titre = " + titreNotification +
" - message = " + messageSonnette +
" - numeroNotification = " + numeroNotification);
849 NotificationCompat.Builder notification =
new NotificationCompat.Builder(
this,
"GROOM")
850 .setSmallIcon(R.mipmap.ic_launcher)
851 .setContentTitle(titreNotification)
852 .setContentText(messageSonnette)
853 .setPriority(NotificationCompat.PRIORITY_DEFAULT);
855 PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0,
new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
856 notification.setContentIntent(pendingIntent);
857 notification.setAutoCancel(
true);
860 notificationManager.notify(numeroNotification++, notification.build());
870 if(communicationGroom !=
null)
871 communicationGroom.envoyer(
"$ETAT\r\n");
879 boutonRadioEtatPresence.setText(
"Il y a quelqu'un à l'entrée !");
880 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
882 boutonRadioEtatPresence.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.vert));
884 LayoutInflater inflater = this.getLayoutInflater();
886 layout = inflater.inflate(R.layout.toast_presence, findViewById(R.id.layout_toast_presence));
888 imagePresence = (ImageView) findViewById(R.id.imagePresence);
890 toast =
new Toast(getApplicationContext());
891 toast.setDuration(Toast.LENGTH_LONG);
892 toast.setGravity(Gravity.TOP,-130,280);
893 toast.setView(layout);
903 boutonRadioEtatPresence.setText(
"Il n'y a personne à l'entrée");
904 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
906 boutonRadioEtatPresence.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.rouge));
916 boutonRadioActivationPresence.setText(
"Désactivé");
917 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
919 boutonRadioActivationPresence.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.rouge));
929 boutonRadioActivationPresence.setText(
"Activé");
930 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
932 boutonRadioActivationPresence.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.vert));
941 LayoutInflater inflater = this.getLayoutInflater();
943 layout = inflater.inflate(R.layout.toast_image, findViewById(R.id.layout_image_clochette));
945 imageClochette = (ImageView) findViewById(R.id.imageClochette);
947 toast =
new Toast(getApplicationContext());
948 toast.setDuration(Toast.LENGTH_LONG);
949 toast.setGravity(Gravity.RIGHT,100,-30);
950 toast.setView(layout);
959 boutonPresence.setText(
"Activer");
967 boutonPresence.setText(
"Désactiver");
968 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
970 boutonRadioActivationPresence.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.vert));
979 Log.d(TAG,
"afficherDeconnexionGroom()");
983 choixGroom = (int)listeAppareilsBluetooth.getSelectedItemId();
985 boutonConnexion.setEnabled(
true);
986 listeAppareilsBluetooth.setEnabled(
true);
990 boutonConnexion.setEnabled(
false);
991 listeAppareilsBluetooth.setEnabled(
false);
994 boutonConnexion.setText(
"Connecter");
995 etatConnexion.setText(
"Déconnecté");
996 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
998 etatConnexion.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.rouge));
1000 boutonRadioLibre.setEnabled(
false);
1001 boutonRadioAbsent.setEnabled(
false);
1002 boutonRadioOccupe.setEnabled(
false);
1003 boutonEntrer.setEnabled(
false);
1004 boutonSonnette.setEnabled(
false);
1005 boutonAjoutOccupant.setEnabled(
true);
1006 envoyerMessageOccupant.setEnabled(
false);
1007 texteMessageOccupant.setEnabled(
false);
1008 boutonEnvoieOccupant.setEnabled(
true);
1009 boutonEnvoieOccupant.setEnabled(
false);
1010 boutonPresence.setEnabled(
false);
1011 afficherPresenceDesactiveGroom();
1012 boutonSonnette.setText(
"Activer Sonnette");
1013 boutonRadioAbsent.setChecked(
false);
1014 boutonRadioOccupe.setChecked(
false);
1015 boutonRadioLibre.setChecked(
false);
1023 Log.d(TAG,
"afficherConnexionGroom()");
1025 listeAppareilsBluetooth.setEnabled(
false);
1026 boutonConnexion.setEnabled(
true);
1027 boutonConnexion.setText(
"Déconnecter");
1028 etatConnexion.setText(
"Connecté");
1029 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
1031 etatConnexion.setButtonTintList(ContextCompat.getColorStateList(
this, R.color.vert));
1033 boutonRadioLibre.setEnabled(
true);
1034 boutonRadioAbsent.setEnabled(
true);
1035 boutonRadioOccupe.setEnabled(
true);
1036 boutonEntrer.setEnabled(
true);
1037 boutonSonnette.setEnabled(
false);
1038 boutonAjoutOccupant.setEnabled(
true);
1039 envoyerMessageOccupant.setEnabled(
true);
1040 texteMessageOccupant.setEnabled(
true);
1041 boutonEnvoieOccupant.setEnabled(
true);
1042 boutonEnvoieOccupant.setEnabled(
true);
1043 boutonPresence.setEnabled(
true);
1044 boutonSonnette.setText(
"Activer Sonnette");
1052 WebView myWebView = (WebView) findViewById(R.id.calendrierGroom);
1053 WebSettings webSettings = myWebView.getSettings();
1054 webSettings.setJavaScriptEnabled(
true);
1056 String unencodedHtml =
"<iframe src=\"https://calendar.google.com/calendar/embed?src=robertutilisateur84%40gmail.com&ctz=Europe%2FParis\" style=\"border: 0\" width=\"760\" height=\"495\" frameborder=\"0\" scrolling=\"no\"></iframe>";;
1057 String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
1058 myWebView.loadData(encodedHtml,
"text/html",
"base64");
1059 if(WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK))
1061 WebSettingsCompat.setForceDark(myWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
1064 new CountDownTimer(300000, 1000)
1066 public void onTick(
long millisUntilFinished)
1069 public void onFinish()
1071 String encodedHtml = Base64.encodeToString(nouvelUrl.getBytes(), Base64.NO_PADDING);
1072 myWebView.loadData(encodedHtml,
"text/html",
"base64");
1073 if(WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK))
1075 WebSettingsCompat.setForceDark(myWebView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);