diff --git a/PremiereActivite/.idea/inspectionProfiles/Project_Default.xml b/PremiereActivite/.idea/inspectionProfiles/Project_Default.xml index 9c52622..8c798ea 100644 --- a/PremiereActivite/.idea/inspectionProfiles/Project_Default.xml +++ b/PremiereActivite/.idea/inspectionProfiles/Project_Default.xml @@ -1,6 +1,11 @@ \ No newline at end of file diff --git a/PremiereActivite/app/build.gradle b/PremiereActivite/app/build.gradle index a78f188..3b173ef 100644 --- a/PremiereActivite/app/build.gradle +++ b/PremiereActivite/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.example.app_10p5" - minSdkVersion 20 + minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.0" diff --git a/PremiereActivite/app/src/main/AndroidManifest.xml b/PremiereActivite/app/src/main/AndroidManifest.xml index fe3ce70..70529bd 100644 --- a/PremiereActivite/app/src/main/AndroidManifest.xml +++ b/PremiereActivite/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/ASyncResponse.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/ASyncResponse.java new file mode 100644 index 0000000..1e56dc5 --- /dev/null +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/ASyncResponse.java @@ -0,0 +1,10 @@ +package com.example.app_10p5; + +import org.json.JSONObject; + +/** + * Created by beaus on 30/04/2016. + */ +public interface ASyncResponse { + void processFinish(JSONObject output); +} diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java index e88ae1e..de4cbd0 100644 --- a/PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/CarteActivite.java @@ -9,20 +9,56 @@ import android.nfc.tech.MifareClassic; import android.os.Bundle; import android.widget.Toast; +import org.json.JSONObject; + import java.io.IOException; +import java.net.URL; +import java.util.HashMap; /** * Created by beaus on 24/04/2016. */ -public class CarteActivite extends Activity { +public class CarteActivite extends Activity implements ASyncResponse { private NfcAdapter mNfcAdapter; + private HashMap mParam; + private String mAPI; + + public static final String HOST = "https://10p5.clubinfo.frogeye.fr/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_carte); + switch (getIntent().getIntExtra("state", MainActivite.STATE_RIEN)) { + + case MainActivite.STATE_COMMANDE: + mParam.put("quantite", String.valueOf(getIntent().getIntExtra("quantite", -1))); + mParam.put("montant", String.valueOf(getIntent().getFloatExtra("montant", -1))); + mParam.put("token", getIntent().getStringExtra("token")); + mAPI = "api/client/payer"; + break; + case MainActivite.STATE_CREATION_COMPTE: + //TODO: param + mAPI = "api/client/ajouter"; + break; + case MainActivite.STATE_RECHARGEMENT: + //TODO: param + mAPI = "api/client/recharger"; + break; + case MainActivite.STATE_VIDANGE: + //TODO: param + mAPI = "api/client/vidange"; + break; + case MainActivite.STATE_CONNEXION: + case MainActivite.STATE_RIEN: + default: + Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); + finish(); + return; + } + mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { @@ -62,11 +98,6 @@ public class CarteActivite extends Activity { handleIntent(intent); } - public void taFonction(String id_carte, String login) - { - //code fonction - } - @Override protected void onPause() { stopForegroundDispatch(this, mNfcAdapter); @@ -77,6 +108,7 @@ public class CarteActivite extends Activity { if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Toast toast; String id_carte = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); + mParam.put("ID", id_carte); toast = Toast.makeText(getApplicationContext(), "ID Carte : " + id_carte, Toast.LENGTH_SHORT); toast.show(); @@ -137,9 +169,9 @@ public class CarteActivite extends Activity { toast.show(); //Éxécution de la fonction - taFonction(id_carte, login); + clientAPI(); + } } - } public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter){ final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); @@ -151,4 +183,24 @@ public class CarteActivite extends Activity { public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { adapter.disableForegroundDispatch(activity); } + + public void clientAPI() { + try { + URL url = new URL(HOST + mAPI); + NetworkThread nt = new NetworkThread(url, mParam); + nt.execute(); + } + catch (Throwable t){ + Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); + finish(); + return; + } + } + + + /* Retour du NetworkThread */ + @Override + public void processFinish(JSONObject output) { + //TODO: faire un retour vers l'activity parente des données reçues. + } } diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java index 924fb0b..c49a474 100644 --- a/PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/MainActivite.java @@ -1,39 +1,48 @@ package com.example.app_10p5; -import android.app.PendingIntent; import android.content.Intent; -import android.content.IntentFilter; -import android.nfc.NfcAdapter; -import android.nfc.Tag; -import android.nfc.tech.MifareClassic; -import android.nfc.tech.NfcA; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.View; +import android.widget.EditText; import android.widget.Toast; -import java.io.IOException; /** * Created by beaus on 24/04/2016. */ public class MainActivite extends FragmentActivity { + public static final int STATE_RIEN = 0; + public static final int STATE_COMMANDE = 3; + public static final int STATE_VIDANGE = 4; + public static final int STATE_RECHARGEMENT = 2; + public static final int STATE_CREATION_COMPTE = 1; + public static final int STATE_CONNEXION = 5; + public static final long EXPIRATION = 1000*60*10; + + private int mState; + private String mToken; + private long mTimeToken; + @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); + + mState = STATE_RIEN; + mTimeToken = -1; + mToken = ""; + TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); + tabLayout.addTab(tabLayout.newTab().setText("Connexion")); tabLayout.addTab(tabLayout.newTab().setText("Commande")); tabLayout.addTab(tabLayout.newTab().setText("Rechargement")); tabLayout.addTab(tabLayout.newTab().setText("Création")); tabLayout.addTab(tabLayout.newTab().setText("Vidange")); - tabLayout.addTab(tabLayout.newTab().setText("Admin")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); @@ -44,7 +53,14 @@ public class MainActivite extends FragmentActivity { tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { - viewPager.setCurrentItem(tab.getPosition()); + if(mToken != "" && System.currentTimeMillis() - mTimeToken < EXPIRATION){ + viewPager.setCurrentItem(tab.getPosition()); + } + else{ + viewPager.setCurrentItem(tab.getPosition()); //Empeche un bug graphique + viewPager.setCurrentItem(0); + Toast.makeText(MainActivite.this, "Veuillez vous connecter.", Toast.LENGTH_LONG).show(); + } } @Override @@ -61,8 +77,78 @@ public class MainActivite extends FragmentActivity { public void valideCommande(View v) { - Intent intent = new Intent(this, CarteActivite.class); - startActivity(intent); + if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) < EXPIRATION)) { + EditText champMontant = (EditText) findViewById(R.id.commande_prix); + EditText champQuantite = (EditText) findViewById(R.id.commande_quantite); + float montant = Float.parseFloat(champMontant.getText().toString()); + int quantite = Integer.parseInt(champQuantite.getText().toString()); + + if ((montant > 0.0) && (montant < 200.0)) { + mState = STATE_COMMANDE; + Intent intent = new Intent(this, CarteActivite.class); + intent.putExtra("token", mToken); + intent.putExtra("state", mState); + intent.putExtra("montant", montant); + intent.putExtra("quantite", quantite); + startActivityForResult(intent, mState); + } + } + else{ + Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show(); + final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); + viewPager.setCurrentItem(0); + } + } + + public void valideRechargement(View v) + { + if((mToken != "") && ((System.currentTimeMillis() - mTimeToken) > 0)) { + + } + else{ + Toast.makeText(this, "Veuillez vous reconnecter.", Toast.LENGTH_LONG).show(); + final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); + viewPager.setCurrentItem(0); + } + } + + public void valideConnection(View v) + { + EditText viewUser = (EditText) findViewById(R.id.connection_username); + EditText viewPsw = (EditText) findViewById(R.id.connection_password); + + String user = viewUser.toString(); + String password = viewPsw.toString(); + + if ((user != "") && (password != "")) { + mState = STATE_CONNEXION; + Intent intent = new Intent(this, CarteActivite.class); + intent.putExtra("user", user); + intent.putExtra("state", mState); + intent.putExtra("password", password); + startActivityForResult(intent, mState); + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data){ + //TODO: faire des choses avec ca + switch (requestCode) { + case STATE_COMMANDE: + break; + case STATE_CONNEXION: + break; + case STATE_CREATION_COMPTE: + break; + case STATE_RECHARGEMENT: + break; + case STATE_VIDANGE: + break; + case STATE_RIEN: + default: + Toast.makeText(this, "WTF, le cancer est dans l'application!!", Toast.LENGTH_LONG).show(); + break; + } } } diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/NetworkThread.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/NetworkThread.java new file mode 100644 index 0000000..c224939 --- /dev/null +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/NetworkThread.java @@ -0,0 +1,95 @@ +package com.example.app_10p5; + +import android.os.AsyncTask; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import javax.net.ssl.HttpsURLConnection; + +/** + * Created by beaus on 30/04/2016. + */ +public class NetworkThread extends AsyncTask { + public ASyncResponse delegate = null; + private URL mUrl; + private HashMap mParam; + + NetworkThread(URL url, HashMap param){ + + mUrl = url; + mParam = param; + } + + @Override + protected void onPreExecute() { + /** + * show dialog + */ + super.onPreExecute(); + } + + @Override + protected JSONObject doInBackground(Void ... params) { + + /** + * Do network related stuff + * return string response. + */ + + JSONObject json = new JSONObject(); + try { + HttpURLConnection httpCo = (HttpURLConnection) mUrl.openConnection(); + httpCo.setDoOutput(true); + + httpCo.connect(); + + OutputStreamWriter wr = new OutputStreamWriter(httpCo.getOutputStream()); + for (Map.Entry entry : mParam.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + wr.write(key + "=" + value); + } + wr.flush(); + + String response = ""; + + if (httpCo.getResponseCode() == HttpsURLConnection.HTTP_OK) { + String line; + BufferedReader br = new BufferedReader(new InputStreamReader(httpCo.getInputStream())); + while ((line = br.readLine()) != null) { + response += line; + } + } else { + response = "Erreur"; + } + + json = new JSONObject(response); + + System.out.println(json.getString("status")); + + httpCo.disconnect(); + } catch (Throwable e) { + System.out.println(e.toString()); + } + + return json; + } + + @Override + protected void onPostExecute(JSONObject result) { + + /** + * update ui thread and remove dialog + */ + super.onPostExecute(result); + delegate.processFinish(result); + } +} \ No newline at end of file diff --git a/PremiereActivite/app/src/main/java/com/example/app_10p5/fonction_API_client.java b/PremiereActivite/app/src/main/java/com/example/app_10p5/fonction_API_client.java new file mode 100644 index 0000000..cf65d2f --- /dev/null +++ b/PremiereActivite/app/src/main/java/com/example/app_10p5/fonction_API_client.java @@ -0,0 +1,230 @@ + +/* + +public JSON client_ajouter(String logiLille1,String soldeini, String IdCarte, String decouvert){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/client/ajouter"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("loginLille1", loginLille1)); + postParameters.add(new BasicNameValuePair("soldeInitial",soldeini)); + postParameters.add(new BasicNameValuePair("idCarte", IdCarte)); + postParameters.add(new BasicNameValuePair("decouvertAutorise",decouvert)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + + +public void client_carte(String logiLille1,String newIdCarte){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/client/carte"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("loginLille1", loginLille1)); + postParameters.add(new BasicNameValuePair("idCarte",newIdCarte)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + } + catch(Exception e) {} +} + +public JSON client_liste(){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/client/liste"); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + + +public void client_decouvert(String idCarte,String newDecouvertAutorise){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/client/decouvert"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("idCarte",idCarte)); + postParameters.add(new BasicNameValuePair("decouvertAutorise", newDecouvertAutorise)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + +public JSON utilisateur_connexion(String login, String mdp, String idCarte, String loginLille1){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/connexion"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + postParameters.add(new BasicNameValuePair("mdp",mdp)); + postParameters.add(new BasicNameValuePair("idCarte",idCarte)); + postParameters.add(new BasicNameValuePair("loginLille1", loginLille1)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + +public void utilisateur_deconnexion(){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/deconnexion"); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + +public void utilisateur_mdp(String login, String newmdp){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/mdp"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + postParameters.add(new BasicNameValuePair("mdp",newmdp)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + +public void utilisateur_droit(String login, String newdroit){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/droit"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + postParameters.add(new BasicNameValuePair("niveau de droit",newdroit)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + +public void utilisateur_ajouter(String login, String mdp, String loginLille1, String idCarte, String droit){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/ajouter"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + postParameters.add(new BasicNameValuePair("mdp",mdp)); + postParameters.add(new BasicNameValuePair("loginLille1", loginLille1)); + postParameters.add(new BasicNameValuePair("idCarte",IdCarte)); + postParameters.add(new BasicNameValuePair("niveau de droit",droit)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + + +public void utilisateur_carte(String login, String loginLille1, String idCarte){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/carte"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + postParameters.add(new BasicNameValuePair("loginLille1", loginLille1)); + postParameters.add(new BasicNameValuePair("idCarte",newIdCarte)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + +public void utilisateur_supprimer(String login){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/utilisateur/carte"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("login",idCarte)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} + + +public JSON transaction_rechargement(String idCarte, String montant){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/transaction/rechargement"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("idCarte",idCarte)); + postParameters.add(new BasicNameValuePair("montant",montant)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + + +public JSON transaction_paiementt(String idCarte, String montant, String nbConso){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/transaction/paiement"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("idCarte",idCarte)); + postParameters.add(new BasicNameValuePair("montant",montant)); + postParameters.add(new BasicNameValuePair("qte",nbConso)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + + + +public JSON transaction_vidange(String idCarte){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/transaction/vidange"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("idCarte",idCarte)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + HttpResponse responce = httpclient.execute(httppost); + return(responce); + } + catch(Exception e) {} +} + + +public void transaction_annuler(String idTransac){ + try{ + Httppost httpost = new HttpPost("10p5.clubinfo.frogeye.fr/api/transaction/annuler"); + List postParameters = new ArrayList>NameValuePair>(); + postParameters.add(new BasicNameValuePair("id",idTransac)); + httppost.setEntity(new UrlEncodedFormEntity(postParameters)); + + HttpClient httpclient = new DefaultHttpClient(); + httpclient.execute(httppost); + } + catch(Exception e) {} +} +*/ \ No newline at end of file diff --git a/PremiereActivite/app/src/main/res/layout-v23/layout_commande.xml b/PremiereActivite/app/src/main/res/layout-v23/layout_commande.xml new file mode 100644 index 0000000..f65d166 --- /dev/null +++ b/PremiereActivite/app/src/main/res/layout-v23/layout_commande.xml @@ -0,0 +1,66 @@ + + + + + + + + +