Android Open Source - SmartCocktailShaker Json Drink Provider






From Project

Back to project page SmartCocktailShaker.

License

The source code is released under:

MIT License

If you think the Android project SmartCocktailShaker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tonydicola.smartshaker;
/*from  w w  w. j  a va 2 s.  com*/
import android.content.Context;

import com.tonydicola.smartshaker.interfaces.Drink;
import com.tonydicola.smartshaker.interfaces.DrinkProvider;
import com.tonydicola.smartshaker.interfaces.PreparationStep;

import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;

public class JsonDrinkProvider implements DrinkProvider {

    private TreeMap<String, JSONObject> drinks = new TreeMap<String, JSONObject>();

    public JsonDrinkProvider(String data) {
        try {
            // Parse JSON data.
            JSONObject json = new JSONObject(data);
            // Load drinks into sorted map in memory.
            JSONArray drinkArray = json.getJSONArray("drinks");
            for (int i = 0; i < drinkArray.length(); ++i) {
                JSONObject drink = drinkArray.getJSONObject(i);
                drinks.put(drink.getString("name"), drink);
            }

        }
        catch (JSONException e) {
            // Failed to parse document, move on with no data loaded.
        }

    }

    @Override
    public Collection<String> getDrinkNames() {
        // Return all the drink names in sorted order.
        return drinks.keySet();
    }

    @Override
    public Drink getDrink(String name) {
        return new JSONDrink(drinks.get(name));
    }

    public static DrinkProvider loadFromResource(Context context, int resourceId) {
        // Load drink JSON data from a resource file.
        try {
            InputStream drinkResource = context.getResources().openRawResource(resourceId);
            String data = IOUtils.toString(drinkResource);
            IOUtils.closeQuietly(drinkResource);
            return new JsonDrinkProvider(data);
        }
        catch (IOException e) {
            // Do nothing, no drink provider loaded.
        }
        return null;
    }

    public class JSONDrink implements Drink {
        private JSONObject drink;
        private ArrayList<PreparationStep> steps = new ArrayList<PreparationStep>();

        public JSONDrink(JSONObject drink) {
            this.drink = drink;
            // Parse preparation steps
            JSONArray stepArray = drink.optJSONArray("preparation");
            if (stepArray != null) {
                for (int i = 0; i < stepArray.length(); ++i) {
                    steps.add(new JSONPreparationStep(stepArray.optJSONObject(i)));
                }
            }
        }

        @Override
        public String getName() {
            return drink.optString("name");
        }

        @Override
        public String getDescription() {
            return drink.optString("description");
        }

        @Override
        public List<PreparationStep> getPreparation() {
            return steps;
        }
    }

    public class JSONPreparationStep implements PreparationStep {
        private JSONObject step;

        public JSONPreparationStep(JSONObject step) {
            this.step = step;
        }

        @Override
        public String getInstructions() {
            return step.optString("instructions");
        }

        @Override
        public Double getMeasureGrams() {
            try {
                return step.getDouble("measure_grams");
            }
            catch (JSONException e) {
                // No measure value, return null.
                return null;
            }
        }

        @Override
        public Double getGramsPerOz() {
            try {
                return step.getDouble("grams_per_oz");
            }
            catch (JSONException e) {
                // No measure value, return null.
                return null;
            }
        }
    }
}




Java Source Code List

com.hoho.android.usbserial.driver.CdcAcmSerialDriver.java
com.hoho.android.usbserial.driver.CommonUsbSerialDriver.java
com.hoho.android.usbserial.driver.Cp2102SerialDriver.java
com.hoho.android.usbserial.driver.FtdiSerialDriver.java
com.hoho.android.usbserial.driver.ProlificSerialDriver.java
com.hoho.android.usbserial.driver.UsbId.java
com.hoho.android.usbserial.driver.UsbSerialDriver.java
com.hoho.android.usbserial.driver.UsbSerialProber.java
com.hoho.android.usbserial.driver.UsbSerialRuntimeException.java
com.hoho.android.usbserial.util.HexDump.java
com.hoho.android.usbserial.util.SerialInputOutputManager.java
com.hoho.android.usbserial.util.UsbSerialInputStream.java
com.hoho.android.usbserial.util.UsbSerialOutputStream.java
com.tonydicola.smartshaker.BluetoothSppProvider.java
com.tonydicola.smartshaker.JsonDrinkProvider.java
com.tonydicola.smartshaker.MockConnectionProvider.java
com.tonydicola.smartshaker.PrepareDrinkModel.java
com.tonydicola.smartshaker.StepListAdapter.java
com.tonydicola.smartshaker.UsbSerialProvider.java
com.tonydicola.smartshaker.activities.ChooseConnection.java
com.tonydicola.smartshaker.activities.DrinkDetails.java
com.tonydicola.smartshaker.activities.DrinkList.java
com.tonydicola.smartshaker.activities.PrepareDrink.java
com.tonydicola.smartshaker.factories.ConnectionFactory.java
com.tonydicola.smartshaker.factories.DrinkFactory.java
com.tonydicola.smartshaker.interfaces.ConnectionProvider.java
com.tonydicola.smartshaker.interfaces.DeviceConnection.java
com.tonydicola.smartshaker.interfaces.DrinkProvider.java
com.tonydicola.smartshaker.interfaces.Drink.java
com.tonydicola.smartshaker.interfaces.PreparationStep.java