Commit 36b71c75 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'release/0.4' into develop

parents fb5a2313 3684606e
......@@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
......@@ -12,16 +13,28 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BaseActivity" />
<activity android:name=".FoundationListActivity" />
<activity android:name=".ArticleCategoryActivity" />
<activity android:name=".BaseActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".FoundationListActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".ArticleCategoryActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".BuyerInfoActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
</application>
</manifest>
\ No newline at end of file
package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import fr.utc.simde.payutc.articles.ArticleGroupFragment;
import fr.utc.simde.payutc.articles.GroupFragment;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
* Created by Samy on 27/10/2017.
......@@ -30,7 +35,7 @@ public class ArticleCategoryActivity extends BaseActivity {
private Panier panier;
private List<ArticleGroupFragment> articleGroupFragmentList;
private List<GroupFragment> groupFragmentList;
private int nbrCategories;
public class Panier {
......@@ -47,12 +52,14 @@ public class ArticleCategoryActivity extends BaseActivity {
}
public void setText() {
if (this.totalPrice == 0)
if (this.articleList.size() == 0)
this.textView.setText("Panier vide");
else
this.textView.setText("Total: " + String.format("%.2f", new Float(totalPrice) / 100.00f) + "€");
}
public List<Integer> getArticleList() { return this.articleList; }
public void addArticle(final int id, final int price) {
this.articleList.add(id);
this.totalPrice += price;
......@@ -66,6 +73,10 @@ public class ArticleCategoryActivity extends BaseActivity {
this.totalPrice = 0;
setText();
}
public Boolean isEmpty() {
return this.articleList.isEmpty();
}
}
@Override
......@@ -80,7 +91,7 @@ public class ArticleCategoryActivity extends BaseActivity {
this.tabHost = findViewById(R.id.tab_categories);
this.tabHost.setup();
this.articleGroupFragmentList = new ArrayList<ArticleGroupFragment>();
this.groupFragmentList = new ArrayList<GroupFragment>();
this.nbrCategories = 0;
try {
......@@ -104,19 +115,114 @@ public class ArticleCategoryActivity extends BaseActivity {
});
}
this.deleteButton.setOnClickListener(new View.OnClickListener() {
this.paramButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (ArticleGroupFragment articleGroupFragment : articleGroupFragmentList)
articleGroupFragment.clear();
config.setInGrid(!config.getInGrid());
startCategoryArticlesActivity(ArticleCategoryActivity.this);
}
});
panier.clear();
this.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearPanier();
}
});
}
@Override
protected void onIdentification(String idBadge) {}
protected void onIdentification(final String badgeId) {
if (this.panier.isEmpty())
startBuyerInfoActivity(ArticleCategoryActivity.this, badgeId);
else
pay(badgeId);
}
public void clearPanier() {
for (GroupFragment groupFragment : groupFragmentList)
groupFragment.clear();
panier.clear();
}
public void setBackgroundColor(int color) {
this.tabHost.setBackgroundColor(color);
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2500);
runOnUiThread(new Runnable() {
@Override
public void run() {
tabHost.setBackgroundColor(getResources().getColor(R.color.white));
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
}
}.start();
}
protected void pay(final String badgeId) {
dialog.startLoading(this, getResources().getString(R.string.paiement), getResources().getString(R.string.transaction_in_progress));
new Thread() {
@Override
public void run() {
try {
nemopaySession.setTransaction(badgeId, panier.getArticleList());
Thread.sleep(100);
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
Toast.makeText(ArticleCategoryActivity.this, "Paiement effectué", Toast.LENGTH_LONG).show();
setBackgroundColor(getResources().getColor(R.color.success));
((Vibrator) getSystemService(ArticleCategoryActivity.VIBRATOR_SERVICE)).vibrate(250);
clearPanier();
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
final JsonNode response = nemopaySession.getRequest().getJSONResponse();
if (response.has("error") && response.get("error").has("message")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(ArticleCategoryActivity.this, getString(R.string.paiement), response.get("error").get("message").textValue());
setBackgroundColor(getResources().getColor(R.color.error));
((Vibrator) getSystemService(ArticleCategoryActivity.VIBRATOR_SERVICE)).vibrate(500);
}
});
}
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(ArticleCategoryActivity.this, getString(R.string.paiement), e.getMessage());
setBackgroundColor(getResources().getColor(R.color.error));
((Vibrator) getSystemService(ArticleCategoryActivity.VIBRATOR_SERVICE)).vibrate(500);
}
});
}
}
}
}.start();
}
protected void createCategories(final JsonNode categoryList, final JsonNode articleList) throws Exception {
HashMap<Integer, ArrayList<JsonNode>> articlesPerCategory = new HashMap<Integer, ArrayList<JsonNode>>();
......@@ -143,18 +249,18 @@ public class ArticleCategoryActivity extends BaseActivity {
if (articlesForThisCategory == null || articlesForThisCategory.size() == 0)
continue;
createNewCategory(category.get("name").textValue(), new ObjectMapper().readTree(articlesForThisCategory.toString()));
createNewCategory(category.get("name").textValue(), (ArrayNode) new ObjectMapper().readTree(articlesForThisCategory.toString()));
}
}
protected void createNewCategory(final String name, final JsonNode articleList) throws Exception {
ArticleGroupFragment articleGroupFragment = new ArticleGroupFragment(ArticleCategoryActivity.this, articleList, this.panier);
protected void createNewCategory(final String name, final ArrayNode articleList) throws Exception {
GroupFragment articleGroupFragment = new GroupFragment(ArticleCategoryActivity.this, articleList, this.panier, this.config.getInGrid());
TabHost.TabSpec newTabSpec = this.tabHost.newTabSpec(name);
newTabSpec.setIndicator(name);
newTabSpec.setContent(articleGroupFragment);
this.articleGroupFragmentList.add(articleGroupFragment);
this.groupFragmentList.add(articleGroupFragment);
this.tabHost.addTab(newTabSpec);
nbrCategories++;
......
......@@ -3,6 +3,7 @@ package fr.utc.simde.payutc;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -21,6 +22,27 @@ public abstract class BaseActivity extends NFCActivity {
protected static Dialog dialog;
protected static NemopaySession nemopaySession;
protected static CASConnexion casConnexion;
protected static Config config;
protected class Config {
private SharedPreferences sharedPreferences;
private Boolean inGrid;
protected Config(final SharedPreferences sharedPreferences) {
this.sharedPreferences = sharedPreferences;
this.inGrid = sharedPreferences.getBoolean("config_in_grid", true);
}
public Boolean getInGrid() { return this.inGrid; }
public void setInGrid(final Boolean inGrid) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("config_in_grid", inGrid);
editor.apply();
this.inGrid = inGrid;
}
}
protected void disconnect() {
nemopaySession.disconnect();
......@@ -54,11 +76,11 @@ public abstract class BaseActivity extends NFCActivity {
new Thread() {
@Override
public void run() {
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getFoundations() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
try {
int responseCode = nemopaySession.getFoundations();
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode foundationList = request.getJSONResponse();
......@@ -99,20 +121,23 @@ public abstract class BaseActivity extends NFCActivity {
@Override
public void run() {
dialog.stopLoading();
if (activity.getClass().getSimpleName().equals("FoundationListActivity"))
finish();
activity.startActivity(intent);
}
});
} catch (Exception e) {
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
dialog.errorDialog(activity, getString(R.string.foundation_list_collecting), e.getMessage());
}
});
}
}
}.start();
}
......@@ -124,12 +149,11 @@ public abstract class BaseActivity extends NFCActivity {
new Thread() {
@Override
public void run() {
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getCategories() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
try {
int responseCode = nemopaySession.getCategories();
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode categoryList = request.getJSONResponse();
......@@ -155,17 +179,15 @@ public abstract class BaseActivity extends NFCActivity {
}
intent.putExtra("categoryList", request.getResponse());
} catch (Exception e) {
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.category_error_get_list));
dialog.errorDialog(activity, getString(R.string.category_list_collecting), e.getMessage());
}
});
return;
}
runOnUiThread(new Runnable() {
......@@ -175,11 +197,11 @@ public abstract class BaseActivity extends NFCActivity {
}
});
try { // Toute une série de vérifications avant de lancer l'activité
if (nemopaySession.getArticles() != 200)
throw new Exception("HTTP Error: " + Integer.toString(nemopaySession.getRequest().getResponseCode()));
try {
int responseCode = nemopaySession.getArticles();
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode articleList = request.getJSONResponse();
......@@ -208,20 +230,23 @@ public abstract class BaseActivity extends NFCActivity {
@Override
public void run() {
dialog.stopLoading();
if (activity.getClass().getSimpleName().equals("ArticleCategoryActivity"))
finish();
activity.startActivity(intent);
}
});
} catch (Exception e) {
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.article_error_get_list));
dialog.errorDialog(activity, getString(R.string.article_list_collecting), e.getMessage());
}
});
}
}
}.start();
}
......@@ -233,4 +258,64 @@ public abstract class BaseActivity extends NFCActivity {
// Plus tard, on pourra choisir quelle activité lancer
startCategoryArticlesActivity(activity);
}
protected void startBuyerInfoActivity(final Activity activity, final String badgeId) {
dialog.startLoading(activity, activity.getResources().getString(R.string.information_collection), activity.getResources().getString(R.string.buyer_info_collecting));
final Intent intent = new Intent(activity, BuyerInfoActivity.class);
new Thread() {
@Override
public void run() {
try {
nemopaySession.getBuyerInfo(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode buyerInfo = request.getJSONResponse();
if (!request.isJSONResponse())
throw new Exception("Malformed JSON");
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
intent.putExtra("badgeId", badgeId);
intent.putExtra("buyerInfo", request.getResponse());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
if (activity.getClass().getSimpleName().equals("BuyerInfoActivity"))
finish();
activity.startActivity(intent);
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 404)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(activity, getString(R.string.information_collection), e.getMessage());
}
});
}
}
}
}.start();
}
}
package fr.utc.simde.payutc;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.w3c.dom.Text;
import java.io.IOException;
import fr.utc.simde.payutc.articles.GridAdapter;
import fr.utc.simde.payutc.articles.ListAdapater;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
* Created by Samy on 29/10/2017.
*/
public class BuyerInfoActivity extends BaseActivity {
private static final String LOG_TAG = "_BuyerInfoActivity";
private String badgeId;
private String username;
private String lastname;
private String firstname;
private ArrayNode lastPurchaseList;
private ArrayNode lastArticleList;
private TextView textBuyerName;
private TextView textSolde;
private LinearLayout linearLayout;
private ListView listView;
private ListAdapater listAdapater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_info);
this.textBuyerName = findViewById(R.id.text_buyer_name);
this.textSolde = findViewById(R.id.text_solde);
this.linearLayout = findViewById(R.id.layout_articles);
try {
JsonNode buyerInfo = new ObjectMapper().readTree(getIntent().getExtras().getString("buyerInfo"));
this.badgeId = getIntent().getExtras().getString("badgeId");
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
this.username = buyerInfo.get("username").textValue();
this.lastname = buyerInfo.get("lastname").textValue();
this.firstname = buyerInfo.get("firstname").textValue();
this.lastPurchaseList = (ArrayNode) buyerInfo.get("last_purchases");
this.textBuyerName.setText(this.firstname + " " + this.lastname);
this.textSolde.setText("Solde: " + String.format("%.2f", new Float(buyerInfo.get("solde").intValue()) / 100.00f) + "€");
generatePurchases();
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.error_view), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
finish();
}
});
}
}
@Override
protected void onIdentification(final String badgeId) { }
protected void generatePurchases() throws Exception {
if (this.lastPurchaseList.size() == 0) {
String foundationName = nemopaySession.getFoundationName();
TextView noPurchase = new TextView(this);
noPurchase.setText(getString(R.string.no_purchases) + (foundationName.equals("") ? "" : "\n(" + foundationName + ")"));
noPurchase.setTextSize(24);
noPurchase.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 100, 100, 100);
noPurchase.setLayoutParams(layoutParams);
this.linearLayout.addView(noPurchase);
}
else {
generateArticleList();
this.listView = new ListView(this);