Java tutorial
/* * Copyright (C) 2014 Sam Higginbotham * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.zapto.samhippiemiddlepoolchecker; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.content.SharedPreferences; import android.text.format.DateFormat; import android.text.format.Time; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; public class Values { //the values float accepted = 0; float rejected = 0; float immature = 0; float unexchanged = 0; float balance = 0; float paid = 0; //required to save the values SharedPreferences settings; Context context; public Values(SharedPreferences settings, Context context) { this.settings = settings; this.context = context; accepted = settings.getFloat("accepted", 0); rejected = settings.getFloat("rejected", 0); immature = settings.getFloat("immature", 0); unexchanged = settings.getFloat("unexchanged", 0); balance = settings.getFloat("balance", 0); paid = settings.getFloat("paid", 0); } static public String valueToString(float value, int scale) { BigDecimal decimal = new BigDecimal(value); decimal = decimal.setScale(scale, BigDecimal.ROUND_HALF_UP); return decimal.toPlainString(); } //can't be run in main thread //use asynctask for activities and runnables for services PoolError update(String address, URL... urls) { //error is returned at the end PoolError error = PoolError.NONE; //Aborting the http to save data throws an error. This says if we should cover it up. boolean actualNetworkError = true; //reset the values. If they are never changed, then 0 is the most accurate number accepted = 0; rejected = 0; immature = 0; unexchanged = 0; balance = 0; paid = 0; try { //streaming the json HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(urls[0].toString()); HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(in); boolean addressFound = false;//see if we need to return and address not found error mainParse: //label for breaking when address is found while (parser.nextToken() != JsonToken.END_OBJECT)//finding "report" { if ("report".equals(parser.getCurrentName()))//beginning of report { boolean firstRun = true; while (parser.nextToken() == JsonToken.START_ARRAY)//each address has its own array { if (firstRun)//this jumps over some junk at the begining { parser.nextToken(); firstRun = false; } parser.nextToken();//have to skip some junk each time if (address.equals(parser.getText()))//we have found our address { addressFound = true;//this prevents an address not found error from being returned while (parser.nextToken() != JsonToken.END_ARRAY) { //getting each of our values from the array. //having -420 as a default lets us see if the value is there while not using up 0 if ("megahashesPerSecond".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { accepted = value; } } if ("rejectedMegahashesPerSecond".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { rejected = value; } } if ("immatureBalance".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { immature = value; } } if ("unexchangedBalance".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { unexchanged = value; } } if ("bitcoinBalance".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { balance = value; } } if ("paidOut".equals(parser.getCurrentName())) { float value = (float) parser.getValueAsDouble(-420); if (value > 0)//negative means wrong value { paid = value; } } } break mainParse;//no need to download any more addresses } else { while (parser.nextToken() != JsonToken.END_ARRAY) { } //skipping over an unwanted address } } } } if (!addressFound)//we never found an address { error = PoolError.ADDRESS; } actualNetworkError = false; request.abort();//should stop any extra data usage, also forces ioexception (which is ignored) parser.close(); in.close(); } catch (MalformedURLException e) { if (actualNetworkError) { error = PoolError.NETWORK; } } catch (IOException e) { if (actualNetworkError) { error = PoolError.NETWORK; } } return error; } public void save() { //stores everything as sharedpreferences so the they will be shown when the app is opened SharedPreferences.Editor editor = settings.edit(); editor.putFloat("accepted", accepted); editor.putFloat("rejected", rejected); editor.putFloat("immature", immature); editor.putFloat("unexchanged", unexchanged); editor.putFloat("balance", balance); editor.putFloat("paid", paid); //have to make sure that the user's time and date preferences are respected Date date = new Date(); java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context); Time time = new Time(); time.setToNow(); String formattedTime; if (DateFormat.is24HourFormat(context)) { formattedTime = time.format("%k:%M"); } else { formattedTime = time.format("%l:%M %p"); } editor.putString("lastUpdatedTime", dateFormat.format(date) + " " + formattedTime); editor.commit(); //update widget here MainActivity.updateWidget(context); } }