Commit d625bbe4 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'release/0.3'

parents af5a4c5d 6e29c9f4
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.utc.simde.payutc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
......@@ -20,6 +21,7 @@
</activity>
<activity android:name=".BaseActivity" />
<activity android:name=".FoundationListActivity" />
<activity android:name=".ArticleCategoryActivity" />
</application>
</manifest>
\ No newline at end of file
package fr.utc.simde.payutc;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import fr.utc.simde.payutc.fragments.ArticleGroupFragment;
/**
* Created by Samy on 27/10/2017.
*/
public class ArticleCategoryActivity extends BaseActivity {
private static final String LOG_TAG = "_ArticleCategoryActivit";
private TabHost categoryTabList;
private int nbrCategories;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_articles_category);
this.nbrCategories = 0;
this.categoryTabList = findViewById(R.id.tab_categories);
this.categoryTabList.setup();
try {
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList")));
} 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();
}
});
}
if (nbrCategories == 0) {
dialog.errorDialog(this, getResources().getString(R.string.information_collection), getResources().getString(R.string.article_error_0_categorie_not_0), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
finish();
}
});
}
}
@Override
protected void onIdentification(String idBadge) {}
protected void createCategories(final JsonNode categoryList, final JsonNode articleList) throws Exception {
HashMap<Integer, ArrayList<JsonNode>> articlesPerCategory = new HashMap<Integer, ArrayList<JsonNode>>();
final int foundationId = nemopaySession.getFoundationId();
for (final JsonNode article : articleList) {
if (!article.has("id") || !article.has("price") || !article.has("name") || !article.has("active") || !article.has("cotisant") || !article.has("alcool") || !article.has("categorie_id") || !article.has("image_url") || !article.has("fundation_id") || article.get("fundation_id").intValue() != foundationId)
throw new Exception("Unexpected JSON");
if (!article.get("active").booleanValue())
continue;
if (articlesPerCategory.containsKey(article.get("categorie_id").intValue()))
articlesPerCategory.get(article.get("categorie_id").intValue()).add(article);
else
articlesPerCategory.put(article.get("categorie_id").intValue(), new ArrayList<JsonNode>(){{ add(article); }});
}
for (JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != foundationId)
throw new Exception("Unexpected JSON");
ArrayList<JsonNode> articlesForThisCategory = articlesPerCategory.get(category.get("id").intValue());
if (articlesForThisCategory == null || articlesForThisCategory.size() == 0)
continue;
createNewCategory(category.get("name").textValue(), new ObjectMapper().readTree(articlesForThisCategory.toString()));
}
}
protected void createNewCategory(final String name, final JsonNode articleList) throws Exception {
TabHost.TabSpec newTabSpec = this.categoryTabList.newTabSpec(name);
newTabSpec.setIndicator(name);
newTabSpec.setContent(new ArticleGroupFragment(ArticleCategoryActivity.this, articleList));
this.categoryTabList.addTab(newTabSpec);
nbrCategories++;
}
}
......@@ -49,62 +49,188 @@ public abstract class BaseActivity extends NFCActivity {
protected void startFoundationListActivity(final Activity activity) {
dialog.startLoading(activity, getString(R.string.information_collection), getString(R.string.foundation_list_collecting));
final Intent intent = new Intent(activity, FoundationListActivity.class);
new Thread() {
@Override
public void run() {
try {
nemopaySession.getFoundations();
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()));
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode foundationList = request.getJSONResponse();
if (!request.isJSONResponse() || !foundationList.isArray())
throw new Exception("Malformed JSON");
if (foundationList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
fatal(activity, getString(R.string.information_collection), nemopaySession.getUsername() + " " + getString(R.string.user_no_rights));
}
});
return;
}
for (final JsonNode foundation : foundationList) {
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
}
if (foundationList.size() == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
startArticlesActivity(activity, foundationList.get(0).get("fun_id").intValue(), foundationList.get(0).get("name").textValue());
}
});
return;
}
intent.putExtra("foundationList", request.getResponse());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
activity.startActivity(intent);
}
});
} catch (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));
}
});
}
runOnUiThread(new Runnable() {
@Override
public void run() {
HTTPRequest request = nemopaySession.getRequest();
}
}.start();
}
try {
// Tout une série de vérifications avant de lancer l'activité
if (request.getResponseCode() != 200)
throw new Exception("Malformed JSON");
protected void startCategoryArticlesActivity(final Activity activity) {
dialog.startLoading(activity, activity.getResources().getString(R.string.information_collection), activity.getResources().getString(R.string.category_list_collecting));
final Intent intent = new Intent(activity, ArticleCategoryActivity.class);
JsonNode foundationList = request.getJsonResponse();
String response = request.getResponse();
new Thread() {
@Override
public void run() {
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("JSON unexpected");
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()));
if (foundationList.size() == 0) {
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode categoryList = request.getJSONResponse();
if (!request.isJSONResponse() || !categoryList.isArray())
throw new Exception("Malformed JSON");
if (categoryList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
fatal(activity, getString(R.string.information_collection), nemopaySession.getUsername() + " " + getString(R.string.user_no_rights));
return;
dialog.errorDialog(activity, getString(R.string.information_collection), nemopaySession.getFoundationName() + " " + getString(R.string.category_error_0));
}
});
for (final JsonNode foundation : foundationList) {
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
}
return;
}
for (final JsonNode category : categoryList) {
if (!category.has("id") || !category.has("name") || !category.has("fundation_id") || category.get("fundation_id").intValue() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
intent.putExtra("categoryList", request.getResponse());
} catch (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));
}
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.article_list_collecting));
}
});
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()));
Thread.sleep(100);
final HTTPRequest request = nemopaySession.getRequest();
final JsonNode articleList = request.getJSONResponse();
if (!request.isJSONResponse() || !articleList.isArray())
throw new Exception("Malformed JSON");
if (foundationList.size() == 1) {
if (articleList.size() == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
nemopaySession.setFoundation(foundationList.get(0).get("fun_id").intValue());
Log.d(LOG_TAG, String.valueOf(foundationList.get(0).get("fun_id").intValue()));
return;
dialog.errorDialog(activity, getString(R.string.information_collection), nemopaySession.getFoundationName() + " " + getString(R.string.article_error_0));
}
});
Intent intent = new Intent(activity, FoundationListActivity.class);
intent.putExtra("foundationList", response);
return;
}
for (final JsonNode article : articleList) {
if (!article.has("id") || !article.has("price") || !article.has("name") || !article.has("active") || !article.has("cotisant") || !article.has("alcool") || !article.has("categorie_id") || !article.has("image_url") || !article.has("fundation_id") || article.get("fundation_id").intValue() != nemopaySession.getFoundationId())
throw new Exception("Unexpected JSON");
}
intent.putExtra("articleList", request.getResponse());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
activity.startActivity(intent);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(activity, getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
}
}
});
});
} catch (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));
}
});
}
}
}.start();
}
protected void startArticlesActivity(final Activity activity, final int foundationId, final String foundationName) {
nemopaySession.setFoundation(foundationId, foundationName);
Log.d(LOG_TAG, String.valueOf(foundationId));
// Plus tard, on pourra choisir quelle activité lancer
startCategoryArticlesActivity(activity);
}
}
......@@ -24,11 +24,15 @@ import fr.utc.simde.payutc.tools.HTTPRequest;
public class FoundationListActivity extends BaseActivity {
private static final String LOG_TAG = "_FoundationListActivity";
LinearLayout listLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foundation_list);
listLayout = findViewById(R.id.foundationList);
try {
setFoundationList(new ObjectMapper().readTree(getIntent().getExtras().getString("foundationList")));
} catch (Exception e) {
......@@ -53,8 +57,6 @@ public class FoundationListActivity extends BaseActivity {
}
protected void setFoundationList(final JsonNode foundationList) throws Exception {
LinearLayout linearLayout = findViewById(R.id.foundationList);
for (final JsonNode foundation : foundationList) {
Button foundationButton = new Button(this);
......@@ -62,23 +64,24 @@ public class FoundationListActivity extends BaseActivity {
throw new Exception("Unexpected JSON");
foundationButton.setText(foundation.get("name").textValue());
foundationButton.setOnClickListener(new onClickFoundation(foundation.get("fun_id").intValue()));
foundationButton.setOnClickListener(new onClickFoundation(foundation.get("fun_id").intValue(), foundation.get("name").textValue()));
linearLayout.addView(foundationButton);
this.listLayout.addView(foundationButton);
}
}
public class onClickFoundation implements View.OnClickListener {
final int idFoundation;
final int foundationId;
final String foundationName;
public onClickFoundation(int idFoundation) {
this.idFoundation = idFoundation;
public onClickFoundation(final int foundationId, final String foundationName) {
this.foundationId = foundationId;
this.foundationName = foundationName;
}
@Override
public void onClick(View view) {
nemopaySession.setFoundation(this.idFoundation);
Log.d(LOG_TAG, String.valueOf(this.idFoundation));
startArticlesActivity(FoundationListActivity.this, this.foundationId, this.foundationName);
}
};
......
package fr.utc.simde.payutc;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
......
package fr.utc.simde.payutc.fragments;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import fr.utc.simde.payutc.R;
import fr.utc.simde.payutc.tools.HTTPRequest;
import static android.content.ContentValues.TAG;
/**
* Created by Samy on 28/10/2017.
*/
public class ArticleFragment extends View {
private static final String LOG_TAG = "_ArticleFragment";
private int id;
private int price;
private String name;
private String imageUrl;
private LayoutInflater layoutInflater;
private View view;
private TextView textView;
private ImageView iv;
private HTTPRequest request;
private LinearLayout linearLayout;
public ArticleFragment(final Activity activity, final JsonNode article) {
super(activity);
/*
this.layoutInflater = LayoutInflater.from(activity);
this.view = this.layoutInflater.inflate(R.layout.fragment_article, null);
this.textView = view.findViewById(R.id.text_article);
this.textView.setText(article.get("name").textValue());*/
this.id = article.get("id").intValue();
this.price = article.get("price").intValue();
this.name = article.get("name").textValue();
this.imageUrl = article.get("image_url").textValue();
setView(activity);
}
public void setView(final Activity activity) {
TextView t = new TextView(activity);
iv = new ImageView(activity);
iv.setTag("image_article_" + Integer.toString(this.id));
iv.setImageResource(R.mipmap.ic_launcher);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200,200);
iv.setLayoutParams(parms);
t.setText(this.name + ": ");
TextView t2 = new TextView(activity);
t2.setText((this.price / 100) + "€" + ((this.price % 100) == 0 ? "" : (((this.price % 100) < 10 ? "0" : "") + (this.price % 100))));
this.linearLayout = new LinearLayout(activity);
linearLayout.addView(iv);
linearLayout.addView(t);
linearLayout.addView(t2);
if (this.imageUrl != null && !this.imageUrl.equals("")) {
new Thread(){
@Override
public void run() {
request = new HTTPRequest(imageUrl);
if (request.get() == 200) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
iv.setImageBitmap(request.getImageResponse());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}.start();
//new DownloadImageTask(iv).execute(this.imageUrl);
}
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Bitmap image;
public DownloadImageTask(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
try {
URL url = new URL(urldisplay);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
image = BitmapFactory.decodeStream(input);
} catch (Exception e) {
image = null;
}
return image;
}
@SuppressLint("NewApi")
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
}
}
}
public View getView() {
return this.linearLayout;
}
}
package fr.utc.simde.payutc.fragments;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;