1 package com.lasalle84.mobile_darts;
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;
25 import androidx.appcompat.app.AppCompatActivity;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
31 import java.util.Vector;
50 private final static String TAG =
"_ActiviteLancerPartie_";
51 private final static int REQUEST_CODE_ENABLE_BLUETOOTH = 0;
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");
85 protected void onCreate(Bundle savedInstanceState)
87 super.onCreate(savedInstanceState);
88 setContentView(R.layout.activity_activite_lancer_partie);
89 Log.d(TAG,
"[onCreate]");
109 Log.d(TAG,
"[onStart]");
119 Log.d(TAG,
"[onResume]");
129 Log.d(TAG,
"[onPause]");
139 Log.d(TAG,
"[onStop]");
149 Log.d(TAG,
"[onDestroy]");
159 if(cibleDarts != null)
161 Log.d(TAG,
"initialiserHandler() cibleDarts " + handler);
164 if(ecranDarts != null)
166 Log.d(TAG,
"initialiserHandler() ecranDarts " + handler);
171 Log.d(TAG,
"initialiserHandler()");
173 this.handler =
new Handler(this.getMainLooper())
176 public void handleMessage(Message msg)
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);
190 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_VERT);
194 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_VERT);
200 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
204 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
216 Log.d(TAG,
"initialiserHandler() ecranDarts " + handler);
218 if(cibleDarts != null)
220 Log.d(TAG,
"initialiserHandler() cibleDarts " + handler);
223 if(ecranDarts != null)
225 Log.d(TAG,
"initialiserHandler() ecranDarts " + handler);
235 Log.d(TAG,
"creerListeTypeJeu()");
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");
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);
253 Log.d(TAG,
"creerListeJoueurs()");
255 listeJoueur = (ListView) findViewById(R.id.ListeJoueur);
257 nomsJoueur =
new ArrayList<String>();
259 adapteurNomJoueur =
new ArrayAdapter<String>(
this, R.layout.item_joueur, R.id.texte_item,
nomsJoueur);
261 listeJoueur.setAdapter(adapteurNomJoueur);
266 AlertDialog.Builder builder =
new AlertDialog.Builder(
this);
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);
272 builder.setView(view);
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();
281 builder.setNegativeButton(R.string.annuler,
new DialogInterface.OnClickListener() {
282 public void onClick(DialogInterface dialog,
int id) {
287 dialogueAjouterJoueur = builder.create();
295 Log.d(TAG,
"initialiserWidgets()");
297 boutonCibleDarts = (ImageButton) findViewById(R.id.boutonCibleDarts);
298 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
299 boutonCibleDarts.setOnClickListener(view -> {
300 Log.d(TAG,
"onClick() boutonCibleDarts");
303 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ORANGE);
308 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
312 boutonEcranDarts = (ImageButton) findViewById(R.id.boutonEcranDarts);
313 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
314 boutonEcranDarts.setOnClickListener(v -> {
315 Log.d(TAG,
"onClick() boutonEcranDarts");
318 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ORANGE);
323 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
327 SpinnerTypeJeu = (Spinner) findViewById(R.id.spinnerTypeJeu);
336 Log.d(TAG,
"activerBluetooth()");
338 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
339 if (bluetoothAdapter == null)
341 Toast.makeText(getApplicationContext(),
"Bluetooth non activé !", Toast.LENGTH_SHORT).show();
342 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_GRIS);
343 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_GRIS);
347 if (!bluetoothAdapter.isEnabled())
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);
355 Toast.makeText(getApplicationContext(),
"Bluetooth activé", Toast.LENGTH_SHORT).show();
357 boutonCibleDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
358 boutonEcranDarts.setBackgroundColor(COULEUR_BOUTON_ROUGE);
367 Log.d(TAG,
"afficherDialogAjouterJoueur()");
368 nomJoueur.setText(
"");
369 dialogueAjouterJoueur.show();
371 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
379 WindowManager.LayoutParams wlp = window.getAttributes();
380 wlp.gravity = Gravity.BOTTOM;
381 wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
382 window.setAttributes(wlp);
390 Log.d(TAG,
"demarrerActiviteJouerPartie()");
397 Log.d(TAG,
"Cible non connectée");
407 if(cibleDarts != null)
410 Log.d(TAG,
"rechercherCibleDARTS()");
411 Set<BluetoothDevice> devices;
412 devices = bluetoothAdapter.getBondedDevices();
413 for (BluetoothDevice blueDevice : devices)
430 if(ecranDarts != null)
433 Log.d(TAG,
"rechercherEcranDARTS()");
434 Set<BluetoothDevice> devices;
435 devices = bluetoothAdapter.getBondedDevices();
436 for (BluetoothDevice blueDevice : devices)
438 Log.d(TAG,
"Device = " + blueDevice.getName());
454 if(cibleDarts != null)
456 Log.d(TAG,
"gererConnexionCibleDARTS()");
469 if(ecranDarts != null)
471 Log.d(TAG,
"gererConnexionEcranDARTS()");
494 super.onActivityResult(requestCode, resultCode, data);
495 Log.d(TAG,
"onActivityResult() Fin ActiviteCreerJoueur");
496 if(resultCode == Activity.RESULT_OK)
498 String nom = data.getStringExtra(
"retour");
499 Log.d(TAG,
"Nom du Joueur saisi" + nom);
511 Log.d(TAG,
"LancerPartie()");
512 Vector<Joueur> mesJoueurs =
new Vector<>();
513 if(nomsJoueur.size() == 0)
515 for (
int i = 1; i <= 1; i++)
517 mesJoueurs.add(
new Joueur(
"Joueur" + i));
518 Log.d(TAG,
"Joueur" + i +
" = " +
"Joueur" + i);
523 for (
int i = 0; i < nomsJoueur.size(); i++)
525 mesJoueurs.add(
new Joueur(nomsJoueur.get(i)));
526 Log.d(TAG,
"Joueur" + i +
" = " + nomsJoueur.get(i));
531 intent.putExtra(
"TypeMode", SpinnerTypeJeu.getSelectedItemPosition());
532 Log.d(TAG,
"TypeMode = " + SpinnerTypeJeu.getSelectedItemPosition());
533 intent.putExtra(
"LesJoueurs", mesJoueurs);
534 startActivityForResult(intent, 12);
543 Log.d(TAG,
"[finish]");
544 Intent intent =
new Intent();
545 setResult(RESULT_OK, intent);
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.
ImageButton boutonCibleDarts
static final int CODE_DECONNEXION
Code de Deconnexion.
L'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'état de connexion'.
static Peripherique getPeripheriqueEcranDarts()
static Peripherique cibleDarts
Le periphérique Bluetooth de la cible.
void onStop()
Méthode appelée lorsque l'activité n'est plus visible.
void initialiserWidgets()
Initialise les widgets.
static final String NOM_ECRAN
Le nom du module Bluetooth de l'écran.
ArrayAdapter< String > adapteurNomJoueur
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'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'handler.
void activerBluetooth()
Gère l'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'une boîte de dialogue s'est affichée (on reprend sur un onResume()) ou avant...
static Peripherique getPeripheriqueCibleDarts()
La classe qui contient toute la gestion des joueurs.
void onCreate(Bundle savedInstanceState)
Méthode appelée à la création de l'activité
static Peripherique ecranDarts
Le periphérique Bluetooth de l'écran.
L'activité ActiviteJouerPartie.
void gererConnexionEcranDARTS()
Gère la connexion au module Bluetooth ecran.
void finish()
Termine l'activité
void placerDialogueEnHaut(Window window)
Place la boîte de dialogue en haut pour laisser l'affichage central visible.
static final int CODE_ERREUR_DECONNECTER
Code erreur lors de la déconnexion.
void afficherDialogAjouterJoueur(View view)
Démarre l'activité pour créer les joueurs.
void initialiserDialogue()
void initialiserHandler()
Initialisation du Handler.
AlertDialog dialogueAjouterJoueur
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'activité pour jouer la partie.
ImageButton boutonEcranDarts
static final int CODE_ERREUR_CONNECTER
Code erreur lors de la connexion.
void onDestroy()
Méthode appelée à la destruction de l'application (après onStop() et détruite par le système Android)...