Mobile-DARTS  1.1
BTS SNIR LaSalle Avignon 2021
ActiviteLancerPartie.java
Aller à la documentation de ce fichier.
1 package com.lasalle84.mobile_darts;
2 
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.bluetooth.BluetoothAdapter;
6 import android.bluetooth.BluetoothDevice;
7 import android.content.DialogInterface;
8 import android.content.Intent;
9 import android.graphics.Color;
10 import android.os.Bundle;
11 import android.os.Handler;
12 import android.os.Message;
13 import android.util.Log;
14 import android.view.Gravity;
15 import android.view.View;
16 import android.view.Window;
17 import android.view.WindowManager;
18 import android.widget.ArrayAdapter;
19 import android.widget.EditText;
20 import android.widget.ImageButton;
21 import android.widget.ListView;
22 import android.widget.Spinner;
23 import android.widget.Toast;
24 
25 import androidx.appcompat.app.AppCompatActivity;
26 
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.Vector;
32 
45 public class ActiviteLancerPartie extends AppCompatActivity
46 {
50  private final static String TAG = "_ActiviteLancerPartie_";
51  private final static int REQUEST_CODE_ENABLE_BLUETOOTH = 0;
52  // Différentes couleurs des boutons
53  private final static int COULEUR_BOUTON_ROUGE = Color.parseColor("#ee5e5e");
54  private final static int COULEUR_BOUTON_ORANGE = Color.parseColor("#f7bb31");
55  private final static int COULEUR_BOUTON_GRIS = Color.parseColor("#c0c5c6");
56  private final static int COULEUR_BOUTON_VERT = Color.parseColor("#5eed7b");
57 
61  private BluetoothAdapter bluetoothAdapter = null;
62  public static Peripherique cibleDarts= null;
63  public static Peripherique ecranDarts = null;
64  private Handler handler = null;
65  private List<String> nomsJoueur;
66  private ArrayAdapter<String> adapteurNomJoueur;
67 
71  private Spinner SpinnerTypeJeu;
72  private ListView listeJoueur;
73  private ImageButton boutonCibleDarts;
74  private ImageButton boutonEcranDarts;
75  private EditText nomJoueur;
76  private AlertDialog dialogueAjouterJoueur;
77 
84  @Override
85  protected void onCreate(Bundle savedInstanceState)
86  {
87  super.onCreate(savedInstanceState);
88  setContentView(R.layout.activity_activite_lancer_partie);
89  Log.d(TAG, "[onCreate]");
90 
92 
94 
96 
98 
100  }
101 
105  @Override
106  protected void onStart()
107  {
108  super.onStart();
109  Log.d(TAG, "[onStart]");
110  }
111 
115  @Override
116  protected void onResume()
117  {
118  super.onResume();
119  Log.d(TAG, "[onResume]");
120  }
121 
125  @Override
126  protected void onPause()
127  {
128  super.onPause();
129  Log.d(TAG, "[onPause]");
130  }
131 
135  @Override
136  protected void onStop()
137  {
138  super.onStop();
139  Log.d(TAG, "[onStop]");
140  }
141 
145  @Override
146  protected void onDestroy()
147  {
148  super.onDestroy();
149  Log.d(TAG, "[onDestroy]");
150  }
151 
155  private void initialiserHandler()
156  {
157  if(handler != null)
158  {
159  if(cibleDarts != null)
160  {
161  Log.d(TAG,"initialiserHandler() cibleDarts " + handler);
162  cibleDarts.setHandlerActivite(handler);
163  }
164  if(ecranDarts != null)
165  {
166  Log.d(TAG,"initialiserHandler() ecranDarts " + handler);
167  ecranDarts.setHandlerActivite(handler);
168  }
169  return;
170  }
171  Log.d(TAG,"initialiserHandler()");
172 
173  this.handler = new Handler(this.getMainLooper())
174  {
175  @Override
176  public void handleMessage(Message msg)
177  {
178  super.handleMessage(msg);
179  Bundle b = msg.getData();
180  Log.d(TAG,"ActiviteLancerPartie:handleMessage() Nom : " + b.getString("nom") + " - Adresse : " + b.getString("adresse") + " -> " + msg.what);
181 
185  switch(msg.what)
186  {
188  if(b.getString("nom").equals(Peripherique.NOM_CIBLE))
189  {
190  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_VERT);
191  }
192  else if(b.getString("nom").equals(Peripherique.NOM_ECRAN))
193  {
194  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_VERT);
195  }
196  break;
198  if(b.getString("nom").equals(Peripherique.NOM_CIBLE))
199  {
200  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
201  }
202  else if(b.getString("nom").equals(Peripherique.NOM_ECRAN))
203  {
204  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
205  }
206  break;
208  break;
210  break;
212  break;
213  }
214  }
215  };
216  Log.d(TAG,"initialiserHandler() ecranDarts " + handler);
217 
218  if(cibleDarts != null)
219  {
220  Log.d(TAG,"initialiserHandler() cibleDarts " + handler);
221  cibleDarts.setHandlerActivite(handler);
222  }
223  if(ecranDarts != null)
224  {
225  Log.d(TAG,"initialiserHandler() ecranDarts " + handler);
226  ecranDarts.setHandlerActivite(handler);
227  }
228  }
229 
233  private void creerListeTypeJeu()
234  {
235  Log.d(TAG, "creerListeTypeJeu()");
236 
237  List<String> listeTypeJeu = new ArrayList<String>();
238  listeTypeJeu.add("301");
239  listeTypeJeu.add("301 Double Out");
240  listeTypeJeu.add("501");
241  listeTypeJeu.add("501 Double Out");
242 
243  ArrayAdapter<String> adapteurTypeJeu = new ArrayAdapter<String>(this, R.layout.item_type_jeu, R.id.texte_type_jeu, listeTypeJeu);
244  adapteurTypeJeu.setDropDownViewResource(R.layout.spinner_dropdown_item);
245  SpinnerTypeJeu.setAdapter(adapteurTypeJeu);
246  }
247 
251  private void creerListeJoueurs()
252  {
253  Log.d(TAG, "creerListeJoueurs()");
254 
255  listeJoueur = (ListView) findViewById(R.id.ListeJoueur);
256 
257  nomsJoueur = new ArrayList<String>();
258 
259  adapteurNomJoueur = new ArrayAdapter<String>(this, R.layout.item_joueur, R.id.texte_item, nomsJoueur);
260 
261  listeJoueur.setAdapter(adapteurNomJoueur);
262  }
263 
264  private void initialiserDialogue()
265  {
266  AlertDialog.Builder builder = new AlertDialog.Builder(this);
267 
268  builder.setTitle(R.string.title);
269  View view = getLayoutInflater().inflate(R.layout.dialog_nouveau_joueur,null);
270  nomJoueur = (EditText) view.findViewById(R.id.nomJoueur);
271 
272  builder.setView(view);
273 
274  builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
275  public void onClick(DialogInterface dialog, int id) {
276  Log.d(TAG, "onClick() nom joueur : " + nomJoueur.getText().toString());
277  nomsJoueur.add(nomJoueur.getText().toString());
278  adapteurNomJoueur.notifyDataSetChanged();
279  }
280  });
281  builder.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
282  public void onClick(DialogInterface dialog, int id) {
283  // User cancelled the dialog
284  }
285  });
286 
287  dialogueAjouterJoueur = builder.create();
288  }
289 
293  private void initialiserWidgets()
294  {
295  Log.d(TAG, "initialiserWidgets()");
296 
297  boutonCibleDarts = (ImageButton) findViewById(R.id.boutonCibleDarts);
298  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
299  boutonCibleDarts.setOnClickListener(view -> {
300  Log.d(TAG, "onClick() boutonCibleDarts");
302  {
303  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ORANGE);
305  }
306  else
307  {
308  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
309  }
310  });
311 
312  boutonEcranDarts = (ImageButton) findViewById(R.id.boutonEcranDarts);
313  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
314  boutonEcranDarts.setOnClickListener(v -> {
315  Log.d(TAG, "onClick() boutonEcranDarts");
317  {
318  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ORANGE);
320  }
321  else
322  {
323  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
324  }
325  });
326 
327  SpinnerTypeJeu = (Spinner) findViewById(R.id.spinnerTypeJeu);
329  }
330 
334  public void activerBluetooth()
335  {
336  Log.d(TAG, "activerBluetooth()");
337 
338  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
339  if (bluetoothAdapter == null)
340  {
341  Toast.makeText(getApplicationContext(), "Bluetooth non activé !", Toast.LENGTH_SHORT).show();
342  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_GRIS);
343  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_GRIS);
344  }
345  else
346  {
347  if (!bluetoothAdapter.isEnabled())
348  {
349  Toast.makeText(getApplicationContext(), "Bluetooth non activé !", Toast.LENGTH_SHORT).show();
350  Intent activeBlueTooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
351  startActivityForResult(activeBlueTooth, REQUEST_CODE_ENABLE_BLUETOOTH);
352  }
353  else
354  {
355  Toast.makeText(getApplicationContext(), "Bluetooth activé", Toast.LENGTH_SHORT).show();
356  }
357  boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
358  boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
359  }
360  }
361 
365  public void afficherDialogAjouterJoueur(View view)
366  {
367  Log.d(TAG, "afficherDialogAjouterJoueur()");
368  nomJoueur.setText("");
369  dialogueAjouterJoueur.show();
370  placerDialogueEnHaut(dialogueAjouterJoueur.getWindow());
371  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
372  }
373 
377  private void placerDialogueEnHaut(Window window)
378  {
379  WindowManager.LayoutParams wlp = window.getAttributes();
380  wlp.gravity = Gravity.BOTTOM;
381  wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
382  window.setAttributes(wlp);
383  }
384 
388  public void demarrerActiviteJouerPartie(View view)
389  {
390  Log.d(TAG, "demarrerActiviteJouerPartie()");
391  if(cibleDarts.estConnecte())
392  {
393  LancerPartie();
394  }
395  else
396  {
397  Log.d(TAG,"Cible non connectée");
398  return;
399  }
400  }
401 
405  public boolean rechercherCibleDARTS()
406  {
407  if(cibleDarts != null)
408  return true;
409 
410  Log.d(TAG,"rechercherCibleDARTS()");
411  Set<BluetoothDevice> devices;
412  devices = bluetoothAdapter.getBondedDevices();
413  for (BluetoothDevice blueDevice : devices)
414  {
415  if(blueDevice.getName().equals(Peripherique.NOM_CIBLE))
416  {
417  cibleDarts = new Peripherique(blueDevice, handler);
418  return true;
419  }
420  }
421 
422  return false;
423  }
424 
428  public boolean rechercherEcranDARTS()
429  {
430  if(ecranDarts != null)
431  return true;
432 
433  Log.d(TAG,"rechercherEcranDARTS()");
434  Set<BluetoothDevice> devices;
435  devices = bluetoothAdapter.getBondedDevices();
436  for (BluetoothDevice blueDevice : devices)
437  {
438  Log.d(TAG,"Device = " + blueDevice.getName());
439  if(blueDevice.getName().equals(Peripherique.NOM_ECRAN))
440  {
441  ecranDarts = new Peripherique(blueDevice, handler);
442  return true;
443  }
444  }
445 
446  return false;
447  }
448 
453  {
454  if(cibleDarts != null)
455  {
456  Log.d(TAG,"gererConnexionCibleDARTS()");
457  if(!cibleDarts.estConnecte())
458  cibleDarts.connecter();
459  else
460  cibleDarts.deconnecter();
461  }
462  }
463 
468  {
469  if(ecranDarts != null)
470  {
471  Log.d(TAG,"gererConnexionEcranDARTS()");
472  if(!ecranDarts.estConnecte())
473  ecranDarts.connecter();
474  else
475  ecranDarts.deconnecter();
476  }
477  }
478 
480  {
481  return cibleDarts;
482  }
483 
485  {
486  return ecranDarts;
487  }
488 
492  protected void onActivityResult(int requestCode, int resultCode, Intent data)
493  {
494  super.onActivityResult(requestCode, resultCode, data);
495  Log.d(TAG, "onActivityResult() Fin ActiviteCreerJoueur");
496  if(resultCode == Activity.RESULT_OK)
497  {
498  String nom = data.getStringExtra("retour");
499  Log.d(TAG, "Nom du Joueur saisi" + nom);
500  nomsJoueur.add(nom);
501  }
502  }
503 
504 
509  public void LancerPartie()
510  {
511  Log.d(TAG, "LancerPartie()");
512  Vector<Joueur> mesJoueurs = new Vector<>();
513  if(nomsJoueur.size() == 0)
514  {
515  for (int i = 1; i <= 1; i++)
516  {
517  mesJoueurs.add(new Joueur("Joueur" + i));
518  Log.d(TAG, "Joueur" + i + " = " + "Joueur" + i);
519  }
520  }
521  else
522  {
523  for (int i = 0; i < nomsJoueur.size(); i++)
524  {
525  mesJoueurs.add(new Joueur(nomsJoueur.get(i)));
526  Log.d(TAG, "Joueur" + i + " = " + nomsJoueur.get(i));
527  }
528  }
529 
530  Intent intent = new Intent(ActiviteLancerPartie.this, ActiviteJouerPartie.class);
531  intent.putExtra("TypeMode", SpinnerTypeJeu.getSelectedItemPosition());
532  Log.d(TAG,"TypeMode = " + SpinnerTypeJeu.getSelectedItemPosition());
533  intent.putExtra("LesJoueurs", mesJoueurs);
534  startActivityForResult(intent, 12);
535  finish();
536  }
540  @Override
541  public void finish()
542  {
543  Log.d(TAG, "[finish]");
544  Intent intent = new Intent();
545  setResult(RESULT_OK, intent);
546  super.finish();
547  }
548 }
void onStart()
Méthode appelée au démarrage après le onCreate() ou un restart après un onStop()
void creerListeJoueurs()
Crée la liste des joueurs.
static final int CODE_DECONNEXION
Code de Deconnexion.
L&#39;activité ActiviteLancerPartie.
BluetoothAdapter bluetoothAdapter
Adaptateur Bluetooth de la tablette.
void onResume()
Méthode appelée après onStart() ou après onPause()
La classe qui contient toute la gestion des periphériques.
boolean estConnecte()
Méthode qui retourne l&#39;état de connexion&#39;.
static Peripherique cibleDarts
Le periphérique Bluetooth de la cible.
void onStop()
Méthode appelée lorsque l&#39;activité n&#39;est plus visible.
static final String NOM_ECRAN
Le nom du module Bluetooth de l&#39;écran.
List< String > nomsJoueur
Conteneur de noms des Joueurs.
void onActivityResult(int requestCode, int resultCode, Intent data)
Traite le retour des activités.
void deconnecter()
Méthode pour se deconnecter sur le periphérique en Bluetooth.
static final int CODE_ERREUR_ENVOYER
Code erreur lors de l&#39;envoi.
static final int CODE_CONNEXION
Code de Connection.
boolean rechercherEcranDARTS()
Démarre la recherche du module Bluetooth ecran.
boolean rechercherCibleDARTS()
Démarre la recherche du module Bluetooth cible.
void setHandlerActivite(Handler handler)
Méthode qui permet de changer d&#39;handler.
void activerBluetooth()
Gère l&#39;activation du Bluetooth.
Handler handler
Le Handler qui permet de communiquer avec le thread.
void creerListeTypeJeu()
Crée la liste des types de partie.
void gererConnexionCibleDARTS()
Gère la connexion au module Bluetooth cible.
void onPause()
Méthode appelée après qu&#39;une boîte de dialogue s&#39;est affichée (on reprend sur un onResume()) ou avant...
La classe qui contient toute la gestion des joueurs.
Definition: Joueur.java:19
void onCreate(Bundle savedInstanceState)
Méthode appelée à la création de l&#39;activité
static Peripherique ecranDarts
Le periphérique Bluetooth de l&#39;écran.
L&#39;activité ActiviteJouerPartie.
void gererConnexionEcranDARTS()
Gère la connexion au module Bluetooth ecran.
void placerDialogueEnHaut(Window window)
Place la boîte de dialogue en haut pour laisser l&#39;affichage central visible.
static final int CODE_ERREUR_DECONNECTER
Code erreur lors de la déconnexion.
void afficherDialogAjouterJoueur(View view)
Démarre l&#39;activité pour créer les joueurs.
void initialiserHandler()
Initialisation du Handler.
static final String NOM_CIBLE
Le nom du module Bluetooth de la cible-darts 2021.
void connecter()
Méthode pour se connecter sur le periphérique en Bluetooth.
void demarrerActiviteJouerPartie(View view)
Démarre l&#39;activité pour jouer la partie.
static final int CODE_ERREUR_CONNECTER
Code erreur lors de la connexion.
void onDestroy()
Méthode appelée à la destruction de l&#39;application (après onStop() et détruite par le système Android)...