1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.wearable.recipeassistant;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.os.Parcelable;
22 import android.util.Log;
23 
24 import org.json.JSONArray;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 
28 import java.util.ArrayList;
29 
30 public class Recipe {
31     private static final String TAG = "RecipeAssistant";
32 
33     public String titleText;
34     public String summaryText;
35     public String recipeImage;
36     public String ingredientsText;
37 
38     public static class RecipeStep {
RecipeStep()39         RecipeStep() { }
40         public String stepImage;
41         public String stepText;
42 
toBundle()43         public Bundle toBundle() {
44             Bundle bundle = new Bundle();
45             bundle.putString(Constants.RECIPE_FIELD_STEP_TEXT, stepText);
46             bundle.putString(Constants.RECIPE_FIELD_STEP_IMAGE, stepImage);
47             return bundle;
48         }
49 
fromBundle(Bundle bundle)50         public static RecipeStep fromBundle(Bundle bundle) {
51             RecipeStep recipeStep = new RecipeStep();
52             recipeStep.stepText = bundle.getString(Constants.RECIPE_FIELD_STEP_TEXT);
53             recipeStep.stepImage = bundle.getString(Constants.RECIPE_FIELD_STEP_IMAGE);
54             return recipeStep;
55         }
56     }
57     ArrayList<RecipeStep> recipeSteps;
58 
Recipe()59     public Recipe() {
60         recipeSteps = new ArrayList<RecipeStep>();
61     }
62 
fromJson(Context context, JSONObject json)63     public static Recipe fromJson(Context context, JSONObject json) {
64         Recipe recipe = new Recipe();
65         try {
66             recipe.titleText = json.getString(Constants.RECIPE_FIELD_TITLE);
67             recipe.summaryText = json.getString(Constants.RECIPE_FIELD_SUMMARY);
68             if (json.has(Constants.RECIPE_FIELD_IMAGE)) {
69                 recipe.recipeImage = json.getString(Constants.RECIPE_FIELD_IMAGE);
70             }
71             JSONArray ingredients = json.getJSONArray(Constants.RECIPE_FIELD_INGREDIENTS);
72             recipe.ingredientsText = "";
73             for (int i = 0; i < ingredients.length(); i++) {
74                 recipe.ingredientsText += " - "
75                         + ingredients.getJSONObject(i).getString(Constants.RECIPE_FIELD_TEXT) + "\n";
76             }
77 
78             JSONArray steps = json.getJSONArray(Constants.RECIPE_FIELD_STEPS);
79             for (int i = 0; i < steps.length(); i++) {
80                 JSONObject step = steps.getJSONObject(i);
81                 RecipeStep recipeStep = new RecipeStep();
82                 recipeStep.stepText = step.getString(Constants.RECIPE_FIELD_TEXT);
83                 if (step.has(Constants.RECIPE_FIELD_IMAGE)) {
84                     recipeStep.stepImage = step.getString(Constants.RECIPE_FIELD_IMAGE);
85                 }
86                 recipe.recipeSteps.add(recipeStep);
87             }
88         } catch (JSONException e) {
89             Log.e(TAG, "Error loading recipe: " + e);
90             return null;
91         }
92         return recipe;
93     }
94 
toBundle()95     public Bundle toBundle() {
96         Bundle bundle = new Bundle();
97         bundle.putString(Constants.RECIPE_FIELD_TITLE, titleText);
98         bundle.putString(Constants.RECIPE_FIELD_SUMMARY, summaryText);
99         bundle.putString(Constants.RECIPE_FIELD_IMAGE, recipeImage);
100         bundle.putString(Constants.RECIPE_FIELD_INGREDIENTS, ingredientsText);
101         if (recipeSteps != null) {
102             ArrayList<Parcelable> stepBundles = new ArrayList<Parcelable>(recipeSteps.size());
103             for (RecipeStep recipeStep : recipeSteps) {
104                 stepBundles.add(recipeStep.toBundle());
105             }
106             bundle.putParcelableArrayList(Constants.RECIPE_FIELD_STEPS, stepBundles);
107         }
108         return bundle;
109     }
110 
fromBundle(Bundle bundle)111     public static Recipe fromBundle(Bundle bundle) {
112         Recipe recipe = new Recipe();
113         recipe.titleText = bundle.getString(Constants.RECIPE_FIELD_TITLE);
114         recipe.summaryText = bundle.getString(Constants.RECIPE_FIELD_SUMMARY);
115         recipe.recipeImage = bundle.getString(Constants.RECIPE_FIELD_IMAGE);
116         recipe.ingredientsText = bundle.getString(Constants.RECIPE_FIELD_INGREDIENTS);
117         ArrayList<Parcelable> stepBundles =
118                 bundle.getParcelableArrayList(Constants.RECIPE_FIELD_STEPS);
119         if (stepBundles != null) {
120             for (Parcelable stepBundle : stepBundles) {
121                 recipe.recipeSteps.add(RecipeStep.fromBundle((Bundle) stepBundle));
122             }
123         }
124         return recipe;
125     }
126 }
127