Wong Joon Hui
3 years ago
15 changed files with 305 additions and 77 deletions
@ -0,0 +1,18 @@ |
|||||||
|
# Echo Socket |
||||||
|
SERVER_URL="http://hello-world-rewards-booking-api-build.testpigeon.net" |
||||||
|
CLIENT_ID="3" |
||||||
|
CLIENT_SECRET="w5RJ7bMF6NH39aDVyJJV0M1gOg0nZQcO2atVcXyF" |
||||||
|
ECHO_SERVER_URL="http://hello-world-rewards-booking-api-build.testpigeon.net" |
||||||
|
ECHO_SERVER_PORT="6001" |
||||||
|
KIOSK_CODE="KIOSK002" |
||||||
|
SOCKET_PREFIX="HelloBookingApi_" |
||||||
|
PAYMENT_CHANNEL_ID="payment#" |
||||||
|
PAYMENT_EVENT_TYPE=".makepayment" |
||||||
|
IC_CHANNEL_ID="verification#" |
||||||
|
IC_EVENT_TYPE=".guestverification" |
||||||
|
|
||||||
|
# AWS |
||||||
|
AWS_ACCESS_KEY="AKIAXHWEPPPSPOMSLL5R" |
||||||
|
AWS_SECRET_KEY="NhO7IGhwcTdWO41gS0KTEKgiZ7mikmZ4HdMG1W2R" |
||||||
|
|
||||||
|
LOG_GROUP_NAME="hotel-kiosk" |
@ -0,0 +1 @@ |
|||||||
|
IyBFY2hvIFNvY2tldApTRVJWRVJfVVJMPSJodHRwOi8vaGVsbG8td29ybGQtcmV3YXJkcy1ib29raW5nLWFwaS1idWlsZC50ZXN0cGlnZW9uLm5ldCIKQ0xJRU5UX0lEPSIzIgpDTElFTlRfU0VDUkVUPSJ3NVJKN2JNRjZOSDM5YURWeUpKVjBNMWdPZzBuWlFjTzJhdFZjWHlGIgpFQ0hPX1NFUlZFUl9VUkw9Imh0dHA6Ly9oZWxsby13b3JsZC1yZXdhcmRzLWJvb2tpbmctYXBpLWJ1aWxkLnRlc3RwaWdlb24ubmV0IgpFQ0hPX1NFUlZFUl9QT1JUPSI2MDAxIgpLSU9TS19DT0RFPSJLSU9TSzAwMiIKU09DS0VUX1BSRUZJWD0iSGVsbG9Cb29raW5nQXBpXyIKUEFZTUVOVF9DSEFOTkVMX0lEPSJwYXltZW50IyIKUEFZTUVOVF9FVkVOVF9UWVBFPSIubWFrZXBheW1lbnQiCklDX0NIQU5ORUxfSUQ9InZlcmlmaWNhdGlvbiMiCklDX0VWRU5UX1RZUEU9Ii5ndWVzdHZlcmlmaWNhdGlvbiIKCiMgQVdTCkFXU19BQ0NFU1NfS0VZPSJBS0lBWEhXRVBQUFNQT01TTEw1UiIKQVdTX1NFQ1JFVF9LRVk9Ik5oTzdJR2h3Y1RkV080MWdTMEtURUtnaVo3bWlrbVo0SGRNRzFXMlIiCgpMT0dfR1JPVVBfTkFNRT0iaG90ZWwta2lvc2si |
@ -0,0 +1,28 @@ |
|||||||
|
package com.cst.im30.common; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
import lombok.Builder; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
@Builder |
||||||
|
@Data |
||||||
|
public class AppConfig implements Serializable { |
||||||
|
|
||||||
|
private String |
||||||
|
serverURL, |
||||||
|
clientId, |
||||||
|
clientSecret, |
||||||
|
echoServerUrl, |
||||||
|
echoServerPort, |
||||||
|
kioskCode, |
||||||
|
socketPrefix, |
||||||
|
paymentChannelId, |
||||||
|
paymentEventType, |
||||||
|
icChannelId, |
||||||
|
icEventType, |
||||||
|
awsAccessKey, |
||||||
|
awsSecretKey, |
||||||
|
logGroupName; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,198 @@ |
|||||||
|
package com.cst.im30.utility; |
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.os.Environment; |
||||||
|
import android.util.Base64; |
||||||
|
|
||||||
|
import com.cst.im30.MainApplication; |
||||||
|
import com.cst.im30.common.AppConfig; |
||||||
|
|
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.FileNotFoundException; |
||||||
|
import java.io.FileReader; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.nio.file.Files; |
||||||
|
import java.nio.file.Path; |
||||||
|
import java.nio.file.Paths; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
public class SetupUtils { |
||||||
|
|
||||||
|
public boolean readEnvFile() { |
||||||
|
if (isExternalStorageReadable()) { |
||||||
|
File configFile = getFile(); |
||||||
|
if (configFile == null) { return false; } |
||||||
|
|
||||||
|
long numberOfLines = countLines(configFile); |
||||||
|
|
||||||
|
if (numberOfLines == 1) { |
||||||
|
decodeAndParseFile(configFile); |
||||||
|
} else { |
||||||
|
parseFile(configFile); |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} else { |
||||||
|
Logger.logD("External Storage Not Readable"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private File getFile() { |
||||||
|
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/CST"); |
||||||
|
File[] files = appDirectory.listFiles(); |
||||||
|
|
||||||
|
if (files == null || files.length == 0) { |
||||||
|
Logger.logD("ENV File Not Found! (Folder Empty)"); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
for (File file : files) { |
||||||
|
String fileName = file.getName().toLowerCase(); |
||||||
|
if (fileName.endsWith(".env")) { |
||||||
|
Logger.logD("Found: " + file.getAbsolutePath()); |
||||||
|
return file; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Logger.logD("ENV File Not Found! (Got Files but no ENV)"); |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean decodeAndParseFile(File file) { |
||||||
|
try { |
||||||
|
FileInputStream is = new FileInputStream(file); |
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
||||||
|
|
||||||
|
String config; |
||||||
|
|
||||||
|
String base64Line = reader.readLine(); |
||||||
|
byte[] decodedString = Base64.decode(base64Line.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT); |
||||||
|
config = new String(decodedString); |
||||||
|
Map<String, String> map = parseDecodedFile(config); |
||||||
|
readConfiguration(map); |
||||||
|
|
||||||
|
return true; |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
private long countLines(File file) { |
||||||
|
long lines = 0; |
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { |
||||||
|
String line; |
||||||
|
while ((line = reader.readLine()) != null) { |
||||||
|
if (line.trim().isEmpty()) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
lines++; |
||||||
|
} |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return lines; |
||||||
|
} |
||||||
|
|
||||||
|
private Map<String, String> parseDecodedFile(String config) { |
||||||
|
String[] lines = config.split(Pattern.quote("\n")); |
||||||
|
|
||||||
|
LinkedHashMap<String, String> infoMap = new LinkedHashMap<>(); |
||||||
|
for (String line : lines) { |
||||||
|
if (line.length() > 0 && !line.startsWith("#")) { |
||||||
|
String[] value = line.split("="); |
||||||
|
String k, v; |
||||||
|
k = value[0]; |
||||||
|
if (value.length == 1) { |
||||||
|
v = ""; |
||||||
|
} else { |
||||||
|
// Filter In-Line # Comments
|
||||||
|
String vStr = value[1]; |
||||||
|
int commentIndex = vStr.indexOf("#"); |
||||||
|
if (commentIndex != -1) { |
||||||
|
vStr = vStr.substring(0, commentIndex); |
||||||
|
} |
||||||
|
v = vStr; |
||||||
|
} |
||||||
|
String key = k.trim().replaceAll("^\"|\"$", ""); |
||||||
|
String valueMap = v.trim().replaceAll("^\"|\"$", ""); |
||||||
|
infoMap.put(key, valueMap); |
||||||
|
} |
||||||
|
} |
||||||
|
return infoMap; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean parseFile(File file) { |
||||||
|
try { |
||||||
|
FileInputStream is = new FileInputStream(file); |
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
||||||
|
|
||||||
|
LinkedHashMap<String, String> infoMap = new LinkedHashMap<>(); |
||||||
|
String line, k, v; |
||||||
|
String[] value; |
||||||
|
while ((line = reader.readLine()) != null) { |
||||||
|
if (line.length() > 0 && !line.startsWith("#")) { |
||||||
|
value = line.split("="); |
||||||
|
k = value[0]; |
||||||
|
if (value.length == 1) { |
||||||
|
v = ""; |
||||||
|
} else { |
||||||
|
// Allows # Comments in the line
|
||||||
|
String vStr = value[1]; |
||||||
|
int commentIndex = vStr.indexOf("#"); |
||||||
|
if (commentIndex != -1) { |
||||||
|
vStr = vStr.substring(0, commentIndex); |
||||||
|
} |
||||||
|
v = vStr; |
||||||
|
} |
||||||
|
String key = k.trim().replaceAll("^\"|\"$", ""); |
||||||
|
String valueMap = v.trim().replaceAll("^\"|\"$", ""); |
||||||
|
infoMap.put(key, valueMap); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (infoMap.isEmpty()) { return false; } |
||||||
|
|
||||||
|
readConfiguration(infoMap); |
||||||
|
|
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private void readConfiguration(Map<String, String> map) { |
||||||
|
MainApplication.config = AppConfig.builder() |
||||||
|
.serverURL(map.get("SERVER_URL")) |
||||||
|
.clientId(map.get("CLIENT_ID")) |
||||||
|
.clientSecret(map.get("CLIENT_SECRET")) |
||||||
|
.echoServerUrl(map.get("ECHO_SERVER_URL")) |
||||||
|
.echoServerPort(map.get("ECHO_SERVER_PORT")) |
||||||
|
.kioskCode(map.get("KIOSK_CODE")) |
||||||
|
.socketPrefix(map.get("SOCKET_PREFIX")) |
||||||
|
.paymentChannelId(map.get("PAYMENT_CHANNEL_ID")) |
||||||
|
.paymentEventType(map.get("PAYMENT_EVENT_TYPE")) |
||||||
|
.icChannelId(map.get("IC_CHANNEL_ID")) |
||||||
|
.icEventType(map.get("IC_EVENT_TYPE")) |
||||||
|
.awsAccessKey(map.get("AWS_ACCESS_KEY")) |
||||||
|
.awsSecretKey(map.get("AWS_SECRET_KEY")) |
||||||
|
.logGroupName(map.get("LOG_GROUP_NAME")) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
/* Checks if external storage is available to at least read */ |
||||||
|
public boolean isExternalStorageReadable() { |
||||||
|
String state = Environment.getExternalStorageState(); |
||||||
|
return Environment.MEDIA_MOUNTED.equals(state) || |
||||||
|
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue