Skip to content

Most visited

Recently visited

navigation
RecipeAssistant / Application / src / com.example.android.wearable.recipeassistant /

RecipeActivity.java

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.app.Activity;
20
import android.app.PendingIntent;
21
import android.content.Intent;
22
import android.graphics.Bitmap;
23
import android.os.Bundle;
24
import android.util.Log;
25
import android.view.LayoutInflater;
26
import android.view.Menu;
27
import android.view.MenuItem;
28
import android.view.View;
29
import android.view.animation.Animation;
30
import android.view.animation.AnimationUtils;
31
import android.widget.ImageView;
32
import android.widget.LinearLayout;
33
import android.widget.TextView;
34
 
35
import org.json.JSONObject;
36
 
37
public class RecipeActivity extends Activity {
38
    private static final String TAG = "RecipeAssistant";
39
    private String mRecipeName;
40
    private Recipe mRecipe;
41
    private ImageView mImageView;
42
    private TextView mTitleTextView;
43
    private TextView mSummaryTextView;
44
    private TextView mIngredientsTextView;
45
    private LinearLayout mStepsLayout;
46
 
47
    @Override
48
    protected void onStart() {
49
        super.onStart();
50
        Intent intent = getIntent();
51
        mRecipeName = intent.getStringExtra(Constants.RECIPE_NAME_TO_LOAD);
52
        if (Log.isLoggable(TAG, Log.DEBUG)) {
53
            Log.d(TAG, "Intent: " + intent.toString() + " " + mRecipeName);
54
        }
55
        loadRecipe();
56
    }
57
 
58
    @Override
59
    protected void onCreate(Bundle savedInstanceState) {
60
        super.onCreate(savedInstanceState);
61
        setContentView(R.layout.recipe);
62
        mTitleTextView = (TextView) findViewById(R.id.recipeTextTitle);
63
        mSummaryTextView = (TextView) findViewById(R.id.recipeTextSummary);
64
        mImageView = (ImageView) findViewById(R.id.recipeImageView);
65
        mIngredientsTextView = (TextView) findViewById(R.id.textIngredients);
66
        mStepsLayout = (LinearLayout) findViewById(R.id.layoutSteps);
67
    }
68
 
69
    @Override
70
    public boolean onCreateOptionsMenu(Menu menu) {
71
        // Inflate the menu; this adds items to the action bar if it is present.
72
        getMenuInflater().inflate(R.menu.main, menu);
73
        return true;
74
    }
75
 
76
    @Override
77
    public boolean onOptionsItemSelected(MenuItem item) {
78
        switch(item.getItemId()) {
79
            case R.id.action_cook:
80
                startCooking();
81
                return true;
82
        }
83
        return super.onOptionsItemSelected(item);
84
    }
85
 
86
    private void loadRecipe() {
87
        JSONObject jsonObject = AssetUtils.loadJSONAsset(this, mRecipeName);
88
        if (jsonObject != null) {
89
            mRecipe = Recipe.fromJson(this, jsonObject);
90
            if (mRecipe != null) {
91
                displayRecipe(mRecipe);
92
            }
93
        }
94
    }
95
 
96
    private void displayRecipe(Recipe recipe) {
97
        Animation fadeIn = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
98
        mTitleTextView.setAnimation(fadeIn);
99
        mTitleTextView.setText(recipe.titleText);
100
        mSummaryTextView.setText(recipe.summaryText);
101
        if (recipe.recipeImage != null) {
102
            mImageView.setAnimation(fadeIn);
103
            Bitmap recipeImage = AssetUtils.loadBitmapAsset(this, recipe.recipeImage);
104
            mImageView.setImageBitmap(recipeImage);
105
        }
106
        mIngredientsTextView.setText(recipe.ingredientsText);
107
 
108
        findViewById(R.id.ingredientsHeader).setAnimation(fadeIn);
109
        findViewById(R.id.ingredientsHeader).setVisibility(View.VISIBLE);
110
        findViewById(R.id.stepsHeader).setAnimation(fadeIn);
111
 
112
        findViewById(R.id.stepsHeader).setVisibility(View.VISIBLE);
113
 
114
        LayoutInflater inf = LayoutInflater.from(this);
115
        mStepsLayout.removeAllViews();
116
        int stepNumber = 1;
117
        for (Recipe.RecipeStep step : recipe.recipeSteps) {
118
            View view = inf.inflate(R.layout.step_item, null);
119
            ImageView iv = (ImageView) view.findViewById(R.id.stepImageView);
120
            if (step.stepImage == null) {
121
                iv.setVisibility(View.GONE);
122
            } else {
123
                Bitmap stepImage = AssetUtils.loadBitmapAsset(this, step.stepImage);
124
                iv.setImageBitmap(stepImage);
125
            }
126
            ((TextView) view.findViewById(R.id.textStep)).setText(
127
                    (stepNumber++) + ". " + step.stepText);
128
            mStepsLayout.addView(view);
129
        }
130
    }
131
 
132
    private void startCooking() {
133
        Intent intent = new Intent(this, RecipeService.class);
134
        intent.setAction(Constants.ACTION_START_COOKING);
135
        intent.putExtra(Constants.EXTRA_RECIPE, mRecipe.toBundle());
136
        startService(intent);
137
    }
138
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.