Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions app/src/main/java/com/greenaddress/abcore/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void run() {
};

private void refresh() {
if (mSwitchOn){
if (mSwitchOn) {
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING || mDaemonStatus == DaemonStatus.UNKNOWN) {
//refresh
final Intent i = new Intent(this, RPCIntentService.class);
Expand All @@ -81,7 +81,7 @@ private void refresh() {
// consistent state
stopDaemonAndSetStatus();
}
} else{
} else {
// switch OFF
if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN) {
//refresh
Expand Down Expand Up @@ -260,17 +260,15 @@ else if (mDaemonStatus == DaemonStatus.STOPPED ){
// the right status will get reflected
break;
case "exception":
if (intent.hasExtra("exception")) {
final String exe = intent.getStringExtra("exception");
if (exe != null)
Log.i(TAG, exe);
final String exe = intent.getStringExtra("exception");
if (exe != null) {
Log.i(TAG, exe);
}

if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN){
if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN) {
mDaemonStatus = DaemonStatus.STOPPED;
mTvStatus.setText(getString(R.string.status_header, mDaemonStatus.toString()));
}
else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING){
} else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.RUNNING) {
// if we get here it means that the daemon is *actually not* running but the screen is reflecting
// as if its running or we are trying to start it. This is a bad state and we will simply
// try to stop the daemon and get back to a consistent state
Expand All @@ -279,11 +277,11 @@ else if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus
//for mDaemonStatus = STOPPED we don't have to do anything
break;
case "localonion":
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.UNKNOWN){
if (mDaemonStatus == DaemonStatus.STARTING || mDaemonStatus == DaemonStatus.UNKNOWN) {
mDaemonStatus = DaemonStatus.RUNNING;
mTvStatus.setText(getString(R.string.status_header, mDaemonStatus.toString()));
}
else if (mDaemonStatus == DaemonStatus.STOPPED ){
else if (mDaemonStatus == DaemonStatus.STOPPED) {
// if we get here it means that the daemon is *actually* running but the screen is reflecting
// as OFF. This is a bad state and we will simply try to stop the daemon and get
// back to a consistent state
Expand Down
48 changes: 29 additions & 19 deletions app/src/main/java/com/greenaddress/abcore/RPCIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ private String getRpcUrl() throws IOException {
String password = p.getProperty("rpcpassword");
final String testnet = p.getProperty("testnet");
final String nonMainnet = testnet == null || !testnet.equals("1") ? p.getProperty("regtest") : testnet;

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final String useDistribution = prefs.getString("usedistribution", "core");

if (user == null || password == null) {
final String cookie = String.format("%s/%s", p.getProperty("datadir"), ".cookie");
final String cookieTestnet = String.format("%s/%s", p.getProperty("datadir"), "testnet3/.cookie");
Expand All @@ -68,12 +70,12 @@ private String getRpcUrl() throws IOException {
final String daemon = "liquid".equals(useDistribution) ? cookieLiquid : cookie;

final String fCookie = nonMainnet == null || !nonMainnet.equals("1") ? daemon : cookieTestnet;
final File file = new File(fCookie);
final File cookieFile = new File(fCookie);

final StringBuilder text = new StringBuilder();

try {
final BufferedReader br = new BufferedReader(new FileReader(file));
final BufferedReader br = new BufferedReader(new FileReader(cookieFile));
String line;

while ((line = br.readLine()) != null) {
Expand All @@ -100,7 +102,7 @@ private BitcoindRpcClient getRpc() throws IOException {
return new BitcoinJSONRPCClient(getRpcUrl());
}

private void broadcastPeerlist() throws IOException {
private void broadcastPeerList() throws IOException {
final BitcoindRpcClient bitcoin = getRpc();

final Intent broadcastIntent = new Intent();
Expand All @@ -119,15 +121,17 @@ private void broadcastPeerlist() throws IOException {

}

private void broadcastNetwork() throws IOException {
private void broadcastNetwork() throws IOException, BitcoinRPCException {
Log.d(TAG, "broadcastNetwork");
final BitcoindRpcClient bitcoin = getRpc();
final Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(PARAM_OUT_MSG, "localonion");

final BitcoindRpcClient.NetworkInfo info = bitcoin.getNetworkInfo();
for (final Object addrs : info.localAddresses()) {
final Map data = (Map) addrs;
for (final Object address : info.localAddresses()) {
final Map data = (Map) address;
final String host = (String) data.get("address");
if (host != null && host.endsWith(".onion")) {
final Long port = (Long) data.get("port");
Expand All @@ -139,14 +143,15 @@ private void broadcastNetwork() throws IOException {
break;
}
}

final BitcoindRpcClient.BlockChainInfo blockChainInfo = bitcoin.getBlockChainInfo();
broadcastIntent.putExtra("sync", blockChainInfo.verificationProgress().multiply(BigDecimal.valueOf(100)).intValue());
broadcastIntent.putExtra("blocks", blockChainInfo.blocks());
sendBroadcast(broadcastIntent);
}

private void broadcastError(final Exception e) {
Log.e(TAG, e.getClass().getName());
Log.e(TAG, "broadcastError - " + e.getClass().getName());
final Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
Expand All @@ -169,7 +174,7 @@ protected void onHandleIntent(final Intent intent) {
conn.authenticate(IOUtils.toByteArray(new FileInputStream(cookie_path)));
conn.shutdownTor("HALT");
} catch (final IOException e) {
e.printStackTrace();
Log.w(TAG, "Error stopping TOR", e);
}

while (true) {
Expand Down Expand Up @@ -231,7 +236,7 @@ protected void onHandleIntent(final Intent intent) {

if (request != null)
if (request.equals("peerlist")) {
broadcastPeerlist();
broadcastPeerList();
return;
} else if (request.equals("localonion")) {
broadcastNetwork();
Expand All @@ -250,17 +255,22 @@ protected void onHandleIntent(final Intent intent) {
sendBroadcast(broadcastIntent);

} catch (final BitcoinRPCException | IOException i) {
Log.i(TAG, "EXE", i);

if (i instanceof BitcoinRPCException && (((BitcoinRPCException) i).getResponseCode() == 500)) {
final Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(PARAM_OUT_MSG, "OK");
sendBroadcast(broadcastIntent);
return;
if (i instanceof BitcoinRPCException) {
final BitcoinRPCException bitcoinRPCException = (BitcoinRPCException) i;
if (bitcoinRPCException.getResponseCode() == 500) {
Log.d(TAG, "BitcoinRPCException - Internal Server Error");
final Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.RPCResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(PARAM_OUT_MSG, "OK");
sendBroadcast(broadcastIntent);
return;
} else {
Log.e(TAG, "BitcoinRPCException", i);
}
} else {
Log.e(TAG, "IOException", i);
}

broadcastError(i);
}
}
Expand Down