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

Change completely the API protocol

Can now add easely new API
API have to dictate what to do
API can configurate 3 different responses for each request and set a special request
API can print a message or a list of information formatted
parent a63e1895
......@@ -2,7 +2,6 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/PayUTC.iml" filepath="$PROJECT_DIR$/PayUTC.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/jessy.iml" filepath="$PROJECT_DIR$/jessy.iml" />
</modules>
......
apply plugin: 'com.android.application'
android {
signingConfigs {
config {
keyAlias 'jessy'
keyPassword 'kVg6ikc+(lTkre8//m%f1'
storeFile file('/root/jessy.jks')
storePassword 'kVg6ikc+(lTkre8//m%f1'
}
}
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
......@@ -13,6 +21,10 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile '/root'
}
debug {
proguardFile '/root'
}
}
packagingOptions {
......
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":42},"path":"app-release.apk","properties":{"packageId":"fr.utc.simde.jessy","split":"","minSdkVersion":"21"}}]
\ No newline at end of file
......@@ -2,11 +2,8 @@ package fr.utc.simde.jessy;
import android.Manifest;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DownloadManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
......@@ -24,7 +21,6 @@ import android.util.Log;
import android.widget.TextView;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.ArrayList;
......@@ -33,8 +29,6 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.utc.simde.jessy.responses.BottomatikResponse;
import fr.utc.simde.jessy.tools.Bottomatik;
import fr.utc.simde.jessy.tools.CASConnexion;
import fr.utc.simde.jessy.tools.Config;
import fr.utc.simde.jessy.tools.Dialog;
......@@ -626,16 +620,41 @@ public abstract class BaseActivity extends InternetActivity {
}.start();
}
protected void setKey(final String name, final String key) {
if (name.equals("") || key.equals(""))
protected void setGingerKey(final String key) {
if (key.equals(""))
return;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key_" + name, key);
editor.apply();
dialog.startLoading(BaseActivity.this, getString(R.string.key_registration), getString(R.string.ginger_registering));
new Thread() {
@Override
public void run() {
try {
ginger.getInfo(nemopaySession.getUsername());
Thread.sleep(100);
if (name.equals("ginger"))
ginger.setKey(key);
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key_ginger", key);
editor.apply();
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(BaseActivity.this, getString(R.string.key_registration), getString(R.string.ginger_error_registering));
}
});
}
}
}.start();
}
protected boolean haveStoragePermission() {
......
......@@ -116,27 +116,27 @@ public class FoundationsOptionsActivity extends BaseActivity {
this.optionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
if (isOption(position,0)) {
if (isOption(position, 0)) {
nemopaySession.setFoundation(-1, "", -1);
startActivity(new Intent(FoundationsOptionsActivity.this, BuyerInfoActivity.class));
}
else if (isOption(position,1))
else if (isOption(position, 1))
editDialog();
else if (isOption(position,2))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.12");
else if (isOption(position,3))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.12");
else if (isOption(position,4))
else if (isOption(position, 2))
editDialog();
else if (isOption(position, 3))
startQRCodeReaderActivity(FoundationsOptionsActivity.this);
else if (isOption(position,5))
else if (isOption(position, 4))
startCardManagementActivity(FoundationsOptionsActivity.this);
else if (isOption(position,6))
else if (isOption(position, 5))
keyNemopayDialog();
else if (isOption(position,7))
else if (isOption(position, 6))
keyGingerDialog();
else if (isOption(position, 7))
keyEditDialog();
else if (isOption(position,8))
else if (isOption(position, 8))
checkUpdate();
else if (isOption(position,9))
else if (isOption(position, 9))
creditDialog();
else
configDialog();
......@@ -150,22 +150,46 @@ public class FoundationsOptionsActivity extends BaseActivity {
hasRights(getString(R.string.nemopay), new String[]{}, new Runnable(){
@Override
public void run() {
final View keyView = getLayoutInflater().inflate(R.layout.dialog_edit_key, null);
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key_edit, null);
final EditText keyInput = keyView.findViewById(R.id.input_key);
keyView.findViewById(R.id.input_name).setVisibility(View.GONE);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FoundationsOptionsActivity.this);
alertDialogBuilder
.setTitle(getString(R.string.key_registration) + " " + getString(R.string.nemopay))
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (!keyInput.getText().toString().equals(""))
setNemopayKey(keyInput.getText().toString());
}
})
.setNegativeButton(R.string.cancel, null);
.setTitle(getString(R.string.key_registration) + " " + getString(R.string.nemopay))
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (!keyInput.getText().toString().equals(""))
setNemopayKey(keyInput.getText().toString());
}
})
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder, keyInput);
}
});
}
protected void keyGingerDialog() {
hasRights(getString(R.string.nemopay), new String[]{}, new Runnable(){
@Override
public void run() {
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key_edit, null);
final EditText keyInput = keyView.findViewById(R.id.input_key);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FoundationsOptionsActivity.this);
alertDialogBuilder
.setTitle(getString(R.string.key_registration) + " " + getString(R.string.ginger))
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (!keyInput.getText().toString().equals(""))
setGingerKey(keyInput.getText().toString());
}
})
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder, keyInput);
}
......@@ -176,8 +200,9 @@ public class FoundationsOptionsActivity extends BaseActivity {
hasRights(getString(R.string.key_registration), new String[]{}, new Runnable(){
@Override
public void run() {
final View keyView = getLayoutInflater().inflate(R.layout.dialog_edit_key, null);
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key_set, null);
final EditText nameInput = keyView.findViewById(R.id.input_name);
final EditText urlInput = keyView.findViewById(R.id.input_url);
final EditText keyInput = keyView.findViewById(R.id.input_key);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FoundationsOptionsActivity.this);
......@@ -187,8 +212,8 @@ public class FoundationsOptionsActivity extends BaseActivity {
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (!nameInput.getText().toString().equals("") || !keyInput.getText().toString().equals(""))
setKey(nameInput.getText().toString(), keyInput.getText().toString());
if (!nameInput.getText().toString().equals(""))
config.setApi(nameInput.getText().toString(), urlInput.getText().toString(), keyInput.getText().toString());
}
})
.setNegativeButton(R.string.cancel, null);
......
......@@ -23,7 +23,6 @@ import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import fr.utc.simde.jessy.tools.Bottomatik;
import fr.utc.simde.jessy.tools.CASConnexion;
import fr.utc.simde.jessy.tools.Config;
import fr.utc.simde.jessy.tools.Ginger;
......@@ -449,7 +448,7 @@ public class MainActivity extends BaseActivity {
}
protected void keyDialog() {
final View keyView = getLayoutInflater().inflate(R.layout.dialog_add_key, null);
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key_add, null);
final EditText nameInput = keyView.findViewById(R.id.input_name);
final EditText descriptionInput = keyView.findViewById(R.id.input_description);
final String date = new SimpleDateFormat("yyyy/MM/dd", Locale.FRANCE).format(new Date());
......@@ -504,7 +503,7 @@ public class MainActivity extends BaseActivity {
}
protected void optionDialog() {
final View keyView = getLayoutInflater().inflate(R.layout.dialog_edit_key, null);
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key_set, null);
final EditText keyInput = keyView.findViewById(R.id.input_key);
keyView.findViewById(R.id.input_name).setVisibility(View.GONE);
......
package fr.utc.simde.jessy;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
......@@ -27,23 +27,26 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.zxing.Result;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fr.utc.simde.jessy.adapters.ListAdapater;
import fr.utc.simde.jessy.adapters.LocationsAdapter;
import fr.utc.simde.jessy.responses.APIResponse;
import fr.utc.simde.jessy.responses.ArticleResponse;
import fr.utc.simde.jessy.responses.BottomatikResponse;
import fr.utc.simde.jessy.responses.ReservationResponse;
import fr.utc.simde.jessy.responses.GingerResponse;
import fr.utc.simde.jessy.responses.QRCodeResponse;
import fr.utc.simde.jessy.tools.API;
import fr.utc.simde.jessy.tools.ExtendedScannerView;
import fr.utc.simde.jessy.tools.HTTPRequest;
import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import static android.graphics.Typeface.BOLD;
import static android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
/**
......@@ -55,57 +58,11 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
protected ZXingScannerView scannerView;
protected SharedPreferences sharedPreferences;
protected String badgeId;
protected Runnable onIdentification;
protected List<String> apiName;
protected List<String> apiUrl;
protected List<Boolean> apiNeedKey;
protected List<Boolean> apiNeedGinger;
protected List<Class> apiResponseClass;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
this.apiName = new ArrayList<String>() {{
add("bottomatik");
add("comedmus");
add("reservations");
add("fetedespartiels");
}};
this.apiUrl = new ArrayList<String>() {{
add("https://picasso.bottomatik.com/bot/transactions/");
add("https://www.lacomutc.fr/qr/" + sharedPreferences.getString("key_" + apiName.get(1), "no_key") + "/");
add("https://assos.utc.fr/simde/reservations/");
add("http://assos.utc.fr/fetedespartiels/api/");
}};
this.apiNeedKey = new ArrayList<Boolean>() {{
add(true);
add(false);
add(true);
add(true);
}};
this.apiNeedGinger = new ArrayList<Boolean>() {{
add(true);
add(false);
add(true);
add(true);
}};
this.apiResponseClass = new ArrayList<Class>() {{
add(BottomatikResponse.class);
add(ReservationResponse.class);
add(ReservationResponse.class);
add(APIResponse.class);
}};
this.scannerView = new ZXingScannerView(QRCodeReaderActivity.this) {
@Override
......@@ -121,25 +78,34 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
public boolean onLongClick(View view) {
final LayoutInflater layoutInflater = LayoutInflater.from(QRCodeReaderActivity.this);
final View popupView = layoutInflater.inflate(R.layout.dialog_tag, null);
final EditText inputApi = popupView.findViewById(R.id.input_api);
final EditText inputInfo = popupView.findViewById(R.id.input_info);
final RadioButton buttonTag = popupView.findViewById(R.id.radio_tag);
inputApi.setText(config.getCurrentApi());
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(QRCodeReaderActivity.this);
alertDialogBuilder
.setTitle(R.string.getting_informations_from)
.setView(popupView)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
new Thread(new Runnable() {
@Override
public void run() {
handleAPI(inputInfo.getText().toString(), 3, null, buttonTag.isChecked());
}
}).start();
}
})
.setCancelable(true);
.setTitle(R.string.getting_informations_from)
.setView(popupView)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
new Thread(new Runnable() {
@Override
public void run() {
Map<String, String> apiInfo = config.getApi(inputApi.getText().toString());
if (apiInfo == null) {
Log.e(LOG_TAG, getString(R.string.api_not_recognized));
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.badge_read), getString(R.string.api_not_recognized));
} else
handleAPI(buttonTag.isChecked() ? inputInfo.getText().toString().toUpperCase() : inputInfo.getText().toString(), apiInfo, null, null, buttonTag.isChecked());
}
}).start();
}
})
.setCancelable(true);
dialog.createDialog(alertDialogBuilder, inputInfo);
......@@ -160,49 +126,36 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
@Override
public void onIdentification(final String badgeId) {
if (this.onIdentification == null) {
this.scannerView.stopCamera();
final Map<String, String> apiInfo = config.getApi(config.getCurrentApi());
new Thread() {
@Override
public void run() {
Integer apiIndex = 2;
GingerResponse gingerResponse = null;
if (apiNeedGinger.get(apiIndex)) {
try {
ginger.getInfoFromBadge(badgeId);
Thread.sleep(100);
gingerResponse = new ObjectMapper().readValue(ginger.getRequest().getResponse(), GingerResponse.class);
}
catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
new Thread() {
@Override
public void run() {
try {
ginger.getInfoFromBadge(badgeId);
Thread.sleep(100);
runOnUiThread(new Runnable() {
GingerResponse gingerResponse = new ObjectMapper().readValue(ginger.getRequest().getResponse(), GingerResponse.class);
handleAPI(gingerResponse.getLogin(), apiInfo, gingerResponse, null, false);
} catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.badge_error_not_recognized), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void run() {
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.qrcode_reading), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
return;
}
}
});
handleAPI(gingerResponse.getLogin(), apiIndex, gingerResponse, false);
return;
}
}.start();
}
else {
this.badgeId = badgeId;
this.onIdentification.run();
this.onIdentification = null;
}
}
}.start();
}
protected void resumeReading() {
......@@ -214,17 +167,17 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
try {
Log.d(LOG_TAG, result.getText());
QRCodeResponse qrCodeResponse = new ObjectMapper().readValue(result.getText(), QRCodeResponse.class);
final Map<String, String> apiInfo = config.getApi(qrCodeResponse.getSystem());
if (!this.apiName.contains(qrCodeResponse.getSystem())) {
if (apiInfo == null)
dialog.infoDialog(QRCodeReaderActivity.this, result.getBarcodeFormat().toString() + ": " + getString(R.string.not_understood), result.getText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
}
else
handleQRCode(qrCodeResponse, this.apiName.indexOf(qrCodeResponse.getSystem()));
handleQRCode(qrCodeResponse, apiInfo);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
......@@ -235,26 +188,24 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
}
});
}
}
else
} else
resumeReading();
}
protected void handleQRCode(final QRCodeResponse qrCodeResponse, final Integer apiIndex) {
protected void handleQRCode(final QRCodeResponse qrCodeResponse, final Map<String, String> apiInfo) {
dialog.startLoading(QRCodeReaderActivity.this, getString(R.string.qrcode_reading), getString(R.string.user_ginger_info_collecting));
new Thread() {
@Override
public void run() {
GingerResponse gingerResponse = null;
if (apiNeedGinger.get(apiIndex)) {
if (qrCodeResponse.getUsername() != null && !qrCodeResponse.getUsername().isEmpty()) {
try {
ginger.getInfo(qrCodeResponse.getUsername());
Thread.sleep(100);
gingerResponse = new ObjectMapper().readValue(ginger.getRequest().getResponse(), GingerResponse.class);
}
catch (final Exception e) {
} catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
if (ginger.getRequest().getResponseCode() != 404) {
......@@ -275,54 +226,79 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
}
}
handleAPI(qrCodeResponse.getId(), apiIndex, gingerResponse, true);
handleAPI(qrCodeResponse.getId(), apiInfo, gingerResponse, qrCodeResponse, true);
}
}.start();
}
protected void handleAPI(final String info, final Integer apiIndex, final GingerResponse gingerResponse, final boolean byQRCode) {
protected void handleAPI(final String info, final Map<String, String> apiInfo, final GingerResponse gingerResponse, final QRCodeResponse qrCodeResponse, final boolean byTag) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getString(R.string.getting_informations_from) + " " + apiName.get(apiIndex));
dialog.changeLoading(getString(R.string.getting_informations_from) + " " + apiInfo.get("name"));
}
});
final API api = new API(QRCodeReaderActivity.this, apiName.get(apiIndex), apiUrl.get(apiIndex));
if (apiNeedKey.get(apiIndex))