Commit 3b92e47d authored by cfabio's avatar cfabio

Code cleanup

parent 0067c7ae
......@@ -11,7 +11,8 @@ android {
versionName '1.6.8'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
flavorDimensions "fdroid-googlePlayStore"
/*flavorDimensions "fdroid-googlePlayStore"
productFlavors {
fdroid {
......@@ -23,8 +24,7 @@ android {
dimension "fdroid-googlePlayStore"
applicationId "eu.uwot.fabio.altcoinpricesgp"
}
}
}*/
buildTypes {
release {
......@@ -33,8 +33,6 @@ android {
}
}
productFlavors {
}
lintOptions {
disable 'MissingTranslation'
}
......
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":64,"versionName":"1.6.8","enabled":true,"outputFile":"Altcoin Prices-release.apk","fullName":"release","baseName":"release"},"path":"Altcoin Prices-release.apk","properties":{}}]
\ No newline at end of file
......@@ -26,12 +26,10 @@ public class AddCustomCoinActivity extends AppCompatActivity {
private EditText altcoinSymbol_text;
private String altcoinSymbol;
private String altcoinDescription;
private String altcoinImageURL;
private EditText tradingviewExchangeName_text;
private String tradingviewExchangeName;
private EditText tradingviewTradingPair_text;
private String tradingviewTradingPair;
private boolean done;
private boolean noErrors;
@Override
......@@ -60,27 +58,18 @@ public class AddCustomCoinActivity extends AppCompatActivity {
Thread getCoinDataTh = new Thread() {
public void run() {
getCoinData();
if (noErrors) {
addCustomCoin();
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
} else {
Toast.makeText(getApplicationContext(), R.string.errorCoinNotFound, Toast.LENGTH_SHORT).show();
}
}
};
done = false;
noErrors = true;
getCoinDataTh.start();
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!done);
if (noErrors) {
addCustomCoin();
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
} else {
Toast.makeText(getApplicationContext(), R.string.errorCoinNotFound, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_SHORT).show();
}
......@@ -151,13 +140,10 @@ public class AddCustomCoinActivity extends AppCompatActivity {
json = new JSONObject(dataSTR.toString());
JSONObject jsonNested = json.getJSONObject("Data").getJSONObject("CoinInfo");
altcoinDescription = jsonNested.getString("FullName");
altcoinImageURL = jsonNested.getString("ImageUrl");
} catch (JSONException e) {
noErrors = false;
e.printStackTrace();
}
done = true;
}
// Add custom coin //
......@@ -166,7 +152,6 @@ public class AddCustomCoinActivity extends AppCompatActivity {
SharedPreferences.Editor editor = prefs.edit();
JSONObject json = new JSONObject();
JSONObject newCustomCoin = new JSONObject();
final CoinLogo coinLogo = new CoinLogo(getApplicationContext());
// Load custom coins data
try {
......@@ -175,24 +160,6 @@ public class AddCustomCoinActivity extends AppCompatActivity {
e.printStackTrace();
}
// Get custom coin logo bitmap
Thread getLogoBitmapTh = new Thread() {
public void run() {
coinLogo.getLogoBitmap(altcoinSymbol, altcoinImageURL);
}
};
coinLogo.done = false;
getLogoBitmapTh.start();
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!coinLogo.done);
// Set empty fields to "na"
if ((tradingviewExchangeName.equals("")) || (tradingviewExchangeName == null)) {
tradingviewExchangeName = "na";
......
......@@ -36,9 +36,6 @@ public class AddNewPortfolioItemActivity extends AppCompatActivity implements Ad
// set Altcoin Description //
altcoinNameSpinner = findViewById(R.id.altcoinName_spinner);
// Create an ArrayAdapter using the string array and a default altcoinNameSpinner layout
/*ArrayAdapter<CharSequence> adapter_altcoin = ArrayAdapter.createFromResource(this,
R.array.altcoin_array, android.R.layout.simple_spinner_item);*/
coin = new Coin(getApplicationContext(), true);
ArrayAdapter<String> adapter_altcoin = new ArrayAdapter<>(this,
......@@ -138,26 +135,7 @@ public class AddNewPortfolioItemActivity extends AppCompatActivity implements Ad
long unixTimestamp = new UnixTimestamp().getUnixTimestamp(minute, hour, day, month, year);
coin.addItemThread(altcoinDescription, amountBought, unitPrice, currency, unixTimestamp);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("Settings", 0);
SharedPreferences.Editor editor = prefs.edit();
boolean res;
do {
res = prefs.getBoolean("status", false);
Toast.makeText(getApplicationContext(), R.string.titleActivityAddNewPortfolioItem, Toast.LENGTH_SHORT).show();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!res);
editor.putBoolean("status", false);
editor.apply();
startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
addItemThread(altcoinDescription, amountBought, unitPrice, currency, unixTimestamp);
} else {
Toast.makeText(getApplicationContext(), R.string.formIsIncomplete, Toast.LENGTH_SHORT).show();
}
......@@ -184,4 +162,19 @@ public class AddNewPortfolioItemActivity extends AppCompatActivity implements Ad
// do nothing
}
private void addItemThread(final String altcoinDesc, final float amountBought, final float unitPrice, final String currency, final long unixTimestamp) {
Thread addItemTh = new Thread() {
public void run() {
SharedPreferences prefs = getApplicationContext().getSharedPreferences("Settings", 0);
SharedPreferences.Editor editor = prefs.edit();
new Coin(getApplicationContext(), false).addItem(altcoinDesc, amountBought, unitPrice, currency, unixTimestamp);
editor.putBoolean("initCoinsLogos", true);
editor.apply();
startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
}
};
addItemTh.start();
}
}
......@@ -110,7 +110,7 @@ class Coin {
"SCX",
"CCP"
};
public final String[] iconomi_daa = new String[]{
private final String[] iconomi_daa = new String[]{
"BLX",
"CCC",
"SOPR",
......@@ -496,19 +496,7 @@ class Coin {
return false;
}
public void addItemThread (final String altcoinDesc, final float amountBought, final float unitPrice, final String currency, final long unixTimestamp) {
Thread addItemTh = new Thread() {
public void run() {
prefs = context.getApplicationContext().getSharedPreferences("Settings", 0);
editor = prefs.edit();
addItem(altcoinDesc, amountBought, unitPrice, currency, unixTimestamp);
}
};
addItemTh.start();
}
private void addItem(String altcoinDesc, float amountBought, float unitPrice, String currency, long unixTimestamp) {
public void addItem(String altcoinDesc, float amountBought, float unitPrice, String currency, long unixTimestamp) {
prefs = context.getApplicationContext().getSharedPreferences("Settings", 0);
editor = prefs.edit();
......@@ -549,23 +537,10 @@ class Coin {
editor.putFloat(altcoinLabel + "_p_btc", unitPriceBTC);
editor.putFloat(altcoinLabel + "_a", amountBought);
editor.putFloat(altcoinLabel + "_p", unitPrice);
editor.putBoolean("status", true);
editor.apply();
}
public void editItemThread (final String altcoinLabel, final float amountBought, final float unitPrice, final long unixTimestamp) {
Thread editItemTh = new Thread() {
public void run() {
prefs = context.getApplicationContext().getSharedPreferences("Settings", 0);
editor = prefs.edit();
editItem(altcoinLabel, amountBought, unitPrice, unixTimestamp);
}
};
editItemTh.start();
}
private void editItem(String altcoinLabel, float amountBought, float unitPrice, long unixTimestamp) {
public void editItem(String altcoinLabel, float amountBought, float unitPrice, long unixTimestamp) {
prefs = context.getApplicationContext().getSharedPreferences("Settings", 0);
editor = prefs.edit();
......@@ -577,7 +552,6 @@ class Coin {
editor.putFloat(altcoinLabel + "_a", amountBought);
editor.putFloat(altcoinLabel + "_p", unitPrice);
editor.putBoolean("status", true);
editor.apply();
} else {
Toast.makeText(context, R.string.error, Toast.LENGTH_SHORT).show();
......
......@@ -22,15 +22,13 @@ import java.net.URL;
class CoinLogo {
private final Context context;
boolean done;
public CoinLogo(Context context) {
this.context = context.getApplicationContext();
done = false;
}
// Get custom coin logo from URL an store it as Bitmap object //
public void getLogoBitmap(String altcoinSymbol, String altcoinImageURL) {
private void getLogoBitmap(String altcoinSymbol, String altcoinImageURL) {
URL url = null;
HttpURLConnection urlConnection = null;
Bitmap altcoinImageBitmap = null;
......@@ -58,8 +56,6 @@ class CoinLogo {
}
saveCustomCoinBitmapLogoToStorage(altcoinSymbol, altcoinImageBitmap);
done = true;
}
// Save custom coin bitmap logo to storage //
......@@ -78,7 +74,7 @@ class CoinLogo {
}
// Get custom coin bitmap logo from storage //
public Bitmap getCustomCoinBitmapLogoFromStorage(String altcoinName) {
public Bitmap loadCustomCoinBitmapLogoFromStorage(String altcoinName) {
String filename = "logo_" + altcoinName;
Bitmap altcoinImageBitmap = null;
......@@ -107,66 +103,90 @@ class CoinLogo {
file.delete();
}
// Download Coins Logos from cryptocompare
public void initCoinsLogos() {
Coin coin = new Coin(context, false);
for (int i = 0; i < coin.coins.length - coin.iconomi_daa.length - 1; i++) {
URL url = null;
HttpURLConnection urlConnection = null;
StringBuilder dataSTR = new StringBuilder();
String tradingPair;
String altcoinImageURL = null;
if (coin.coins[i].equals("BTC")) {
tradingPair = "USD";
} else {
tradingPair = "BTC";
}
// Get Coin Logo from cryptocompare
public void getCoinLogo(String coinSymbol) {
URL url = null;
HttpURLConnection urlConnection = null;
StringBuilder dataSTR = new StringBuilder();
String tradingPair;
String altcoinImageURL = null;
if (coinSymbol.equals("BTC")) {
tradingPair = "USD";
} else {
tradingPair = "BTC";
}
try {
try {
if (new Coin(context, false).isItIconomiDaa(coinSymbol)) {
url = new URL("https://min-api.cryptocompare.com/data/top/exchanges/full?fsym=" +
"ICN" +
"&tsym=" +
tradingPair);
} else {
url = new URL("https://min-api.cryptocompare.com/data/top/exchanges/full?fsym=" +
coin.coins[i] +
coinSymbol +
"&tsym=" +
tradingPair);
} catch (MalformedURLException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000); // 5 seconds timeout
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
dataSTR.append(Character.toString(current));
}
} catch (SocketTimeoutException e) {
dataSTR = new StringBuilder("0");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
assert urlConnection != null;
urlConnection.disconnect();
}
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000); // 5 seconds timeout
JSONObject json;
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
try {
json = new JSONObject(dataSTR.toString());
JSONObject jsonNested = json.getJSONObject("Data").getJSONObject("CoinInfo");
altcoinImageURL = jsonNested.getString("ImageUrl");
} catch (JSONException e) {
e.printStackTrace();
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
dataSTR.append(Character.toString(current));
}
} catch (SocketTimeoutException e) {
dataSTR = new StringBuilder("0");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
assert urlConnection != null;
urlConnection.disconnect();
}
JSONObject json;
getLogoBitmap(coin.coins[i], altcoinImageURL);
try {
json = new JSONObject(dataSTR.toString());
JSONObject jsonNested = json.getJSONObject("Data").getJSONObject("CoinInfo");
altcoinImageURL = jsonNested.getString("ImageUrl");
} catch (JSONException e) {
e.printStackTrace();
}
getLogoBitmap(coinSymbol, altcoinImageURL);
}
public boolean coinLogoExists(String altcoinSymbol) {
File directory = context.getFilesDir();
File file = new File(directory, "logo_" + altcoinSymbol);
if(file.exists()) {
return true;
}
else {
return false;
}
}
public void removeCachedLogos() {
File directory = context.getFilesDir();
for(File file: directory.listFiles()) {
if (!file.isDirectory()) {
file.delete();
}
}
}
......
......@@ -28,13 +28,11 @@ public class EditCustomCoinActivity extends AppCompatActivity {
private String altcoinSymbol;
private String altcoinSymbolInitial;
private String altcoinDescription;
private String altcoinImageURL;
private EditText tradingviewExchangeName_text;
private String tradingviewExchangeName;
private EditText tradingviewTradingPair_text;
private String tradingviewTradingPair;
private String key;
private boolean done;
private boolean noErrors;
@Override
......@@ -75,29 +73,20 @@ public class EditCustomCoinActivity extends AppCompatActivity {
Thread getCoinDataTh = new Thread() {
public void run() {
getCoinData();
if ((noErrors) && (!altcoinSymbol.equals(altcoinSymbolInitial))) {
editCustomCoin();
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
} else if (!noErrors) {
Toast.makeText(getApplicationContext(), R.string.errorCoinNotFound, Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
}
}
};
done = false;
noErrors = true;
getCoinDataTh.start();
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!done);
if ((noErrors) && (!altcoinSymbol.equals(altcoinSymbolInitial))) {
editCustomCoin();
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
} else if (!noErrors) {
Toast.makeText(getApplicationContext(), R.string.errorCoinNotFound, Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(getApplicationContext(), ManageCustomCoinsActivity.class));
}
} else {
Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_SHORT).show();
}
......@@ -169,13 +158,10 @@ public class EditCustomCoinActivity extends AppCompatActivity {
json = new JSONObject(dataSTR.toString());
JSONObject jsonNested = json.getJSONObject("Data").getJSONObject("CoinInfo");
altcoinDescription = jsonNested.getString("FullName");
altcoinImageURL = jsonNested.getString("ImageUrl");
} catch (JSONException e) {
noErrors = false;
e.printStackTrace();
}
done = true;
}
// Add back modified custom coin //
......@@ -184,7 +170,6 @@ public class EditCustomCoinActivity extends AppCompatActivity {
SharedPreferences.Editor editor = prefs.edit();
JSONObject json = new JSONObject();
JSONObject newCustomCoin = new JSONObject();
final CoinLogo coinLogo = new CoinLogo(getApplicationContext());
// Remove old custom coin
removeCustomCoinKey();
......@@ -196,24 +181,6 @@ public class EditCustomCoinActivity extends AppCompatActivity {
e.printStackTrace();
}
// Get custom coin logo bitmap
Thread getLogoBitmapTh = new Thread() {
public void run() {
coinLogo.getLogoBitmap(altcoinSymbol, altcoinImageURL);
}
};
coinLogo.done = false;
getLogoBitmapTh.start();
do {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!coinLogo.done);
// Set empty fields to "na"
if ((tradingviewExchangeName.equals("")) || (tradingviewExchangeName == null)) {
tradingviewExchangeName = "na";
......
package eu.uwot.fabio.altcoinprices;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -14,11 +13,10 @@ import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class EditPortfolioItemActivity extends AppCompatActivity {
private String altcoinName;
private String altcoinSymbol;
private EditText amountBought_text;
private float amountBought;
private String altcoinCurrency;
......@@ -35,7 +33,7 @@ public class EditPortfolioItemActivity extends AppCompatActivity {
TextView altcoinName_text = findViewById(R.id.altcoinSymbol);
Bundle b = getIntent().getExtras();
if(b != null) {
altcoinName = b.getString("altcoinDescription");
altcoinSymbol = b.getString("altcoinDescription");
amountBought = b.getFloat("amountBought");
unitValue = b.getFloat("unitValue");
altcoinCurrency = b.getString("altcoinCurrency");
......@@ -43,7 +41,7 @@ public class EditPortfolioItemActivity extends AppCompatActivity {
// set Altcoin name //
coin = new Coin(getApplicationContext(), false);
altcoinName_text.setText(coin.coinsLabelDescriptionHashtable.get(altcoinName));
altcoinName_text.setText(coin.coinsLabelDescriptionHashtable.get(altcoinSymbol));
// set Amount bought //
amountBought_text = findViewById(R.id.amountBought_text);
......@@ -159,26 +157,7 @@ public class EditPortfolioItemActivity extends AppCompatActivity {
unixTimestamp = new UnixTimestamp().getUnixTimestamp(minute, hour, day, month, year);
}
coin.editItemThread(altcoinName, amountBought, unitValue, unixTimestamp);
SharedPreferences prefs = getApplicationContext().getSharedPreferences("Settings", 0);
SharedPreferences.Editor editor = prefs.edit();
boolean res;
do {
res = prefs.getBoolean("status", false);
Toast.makeText(getApplicationContext(), R.string.titleActivityAddNewPortfolioItem, Toast.LENGTH_SHORT).show();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!res);
editor.putBoolean("status", false);
editor.apply();
startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
editItemThread(altcoinSymbol, amountBought, unitValue, unixTimestamp);
}
}
});
......@@ -187,11 +166,23 @@ public class EditPortfolioItemActivity extends AppCompatActivity {
final Button deleteButton = findViewById(R.id.removeButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
coin.removeItem(altcoinName);
//finish();
coin.removeItem(altcoinSymbol);
new CoinLogo(getApplicationContext()).removeCustomCoinBitmapLogoFromStorage(altcoinSymbol);
startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
}
});
}
private void editItemThread(final String altcoinSymbol, final float amountBought, final float unitPrice, final long unixTimestamp) {
Thread editItemTh = new Thread() {
public void run() {
new Coin(getApplicationContext(), false).editItem(altcoinSymbol, amountBought, unitPrice, unixTimestamp);
startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
}
};
editItemTh.start();
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class LoadingActivity extends AppCompatActivity {
......@@ -51,7 +50,8 @@ public class LoadingActivity extends AppCompatActivity {
prefs = getApplicationContext().getSharedPreferences("Settings", 0);
editor = prefs.edit();
coin = new Coin(getApplicationContext(), false);
boolean initCoinsLogos = prefs.getBoolean("initCoinsLogos", true);;
CoinLogo coinLogo = new CoinLogo(getApplicationContext());
boolean initCoinsLogos = prefs.getBoolean("initCoinsLogos", true);
int portfolioItems = 0;
for (int i = 0; i < coin.coins.length; i ++) {
......@@ -62,11 +62,7 @@ public class LoadingActivity extends AppCompatActivity {
}
}
if (initCoinsLogos) {
progressBarSlotTime = 100 / (portfolioItems + coin.coins.length);
} else {
progressBarSlotTime = 100 / (portfolioItems + 1);
}
progressBarSlotTime = 100 / (portfolioItems + 1);
progressBarProgess = 0;
initTradingPair();
......@@ -93,6 +89,12 @@ public class LoadingActivity extends AppCompatActivity {
lastDayUnitValue_btc = coin.getCoinValue(coin.coins[i], "BTC", new UnixTimestamp().getYesterdayUnixTimestamp());
}
if (initCoinsLogos) {
if (!coinLogo.coinLogoExists(coin.coins[i])) {
coinLogo.getCoinLogo(coin.coins[i]);
}
}
editor.putFloat(coin.coins[i] + "_currentUnitValue_btc", currentUnitValue_btc);
editor.putFloat(coin.coins[i] + "_lastDayUnitValue_btc", lastDayUnitValue_btc);
editor.apply();
......@@ -102,14 +104,8 @@ public class LoadingActivity extends AppCompatActivity {
}
}
// Init coins logos if needed
if (initCoinsLogos) {
Toast.makeText(getApplicationContext(), R.string.loadingCoinsLogos, Toast.LENGTH_SHORT).show();
CoinLogo coinLogo = new CoinLogo(getApplicationContext());
coinLogo.initCoinsLogos();
editor.putBoolean("initCoinsLogos", false);
editor.apply();
}
editor.putBoolean("initCoinsLogos", false);
editor.apply();
}
private void initTradingPair() {
......
......@@ -603,46 +603,11 @@ public class MainActivity extends AppCompatActivity
// Portfolio Altcoin Icon //
ImageView imageView = convertView.findViewById(R.id.portfolioAltcoinLogo);
/*if (coin.isItIconomiDaa(item.altcoinName)) {
int id = getResources().getIdentifier("eu.uwot.fabio.altcoinprices:drawable/" +
"ic_menu_icn", null, null);