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.Notification; 20 import android.app.Service; 21 import android.content.Intent; 22 import android.graphics.Bitmap; 23 import android.os.Binder; 24 import android.os.IBinder; 25 import android.support.v4.app.NotificationManagerCompat; 26 import android.support.v4.app.NotificationCompat; 27 28 import java.util.ArrayList; 29 30 public class RecipeService extends Service { 31 private NotificationManagerCompat mNotificationManager; 32 private Binder mBinder = new LocalBinder(); 33 private Recipe mRecipe; 34 35 public class LocalBinder extends Binder { getService()36 RecipeService getService() { 37 return RecipeService.this; 38 } 39 } 40 41 @Override onCreate()42 public void onCreate() { 43 mNotificationManager = NotificationManagerCompat.from(this); 44 } 45 46 @Override onBind(Intent intent)47 public IBinder onBind(Intent intent) { 48 return mBinder; 49 } 50 51 @Override onStartCommand(Intent intent, int flags, int startId)52 public int onStartCommand(Intent intent, int flags, int startId) { 53 if (intent.getAction().equals(Constants.ACTION_START_COOKING)) { 54 createNotification(intent); 55 return START_STICKY; 56 } 57 return START_NOT_STICKY; 58 } 59 createNotification(Intent intent)60 private void createNotification(Intent intent) { 61 mRecipe = Recipe.fromBundle(intent.getBundleExtra(Constants.EXTRA_RECIPE)); 62 ArrayList<Notification> notificationPages = new ArrayList<Notification>(); 63 64 int stepCount = mRecipe.recipeSteps.size(); 65 66 for (int i = 0; i < stepCount; ++i) { 67 Recipe.RecipeStep recipeStep = mRecipe.recipeSteps.get(i); 68 NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(); 69 style.bigText(recipeStep.stepText); 70 style.setBigContentTitle(String.format( 71 getResources().getString(R.string.step_count), i + 1, stepCount)); 72 style.setSummaryText(""); 73 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 74 builder.setStyle(style); 75 notificationPages.add(builder.build()); 76 } 77 78 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 79 80 if (mRecipe.recipeImage != null) { 81 Bitmap recipeImage = Bitmap.createScaledBitmap( 82 AssetUtils.loadBitmapAsset(this, mRecipe.recipeImage), 83 Constants.NOTIFICATION_IMAGE_WIDTH, Constants.NOTIFICATION_IMAGE_HEIGHT, false); 84 builder.setLargeIcon(recipeImage); 85 } 86 builder.setContentTitle(mRecipe.titleText); 87 builder.setContentText(mRecipe.summaryText); 88 builder.setSmallIcon(R.mipmap.ic_notification_recipe); 89 90 Notification notification = builder 91 .extend(new NotificationCompat.WearableExtender() 92 .addPages(notificationPages)) 93 .build(); 94 mNotificationManager.notify(Constants.NOTIFICATION_ID, notification); 95 } 96 } 97