diff --git a/app/src/main/java/com/greenaddress/abcore/MainActivity.java b/app/src/main/java/com/greenaddress/abcore/MainActivity.java index e4d9377a..0476e5f5 100755 --- a/app/src/main/java/com/greenaddress/abcore/MainActivity.java +++ b/app/src/main/java/com/greenaddress/abcore/MainActivity.java @@ -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); @@ -81,7 +81,7 @@ private void refresh() { // consistent state stopDaemonAndSetStatus(); } - } else{ + } else { // switch OFF if (mDaemonStatus == DaemonStatus.STOPPING || mDaemonStatus == DaemonStatus.UNKNOWN) { //refresh @@ -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 @@ -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 diff --git a/app/src/main/java/com/greenaddress/abcore/RPCIntentService.java b/app/src/main/java/com/greenaddress/abcore/RPCIntentService.java index e8286faf..36335a9b 100755 --- a/app/src/main/java/com/greenaddress/abcore/RPCIntentService.java +++ b/app/src/main/java/com/greenaddress/abcore/RPCIntentService.java @@ -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"); @@ -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) { @@ -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(); @@ -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"); @@ -139,6 +143,7 @@ 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()); @@ -146,7 +151,7 @@ private void broadcastNetwork() throws IOException { } 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); @@ -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) { @@ -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(); @@ -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); } }