1 /* 2 Copyright 2016 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 package com.example.android.wearable.wear.wearnotifications; 17 18 import android.support.v7.app.NotificationCompat; 19 20 /** 21 * We use a Singleton for a global copy of the NotificationCompat.Builder to update active 22 * Notifications from other Services/Activities. 23 * 24 * You have two options for updating your notifications: 25 * 26 * 1. Use a new NotificationCompatBuilder to create the Notification. This approach requires you 27 * to get *ALL* the information and pass it to the builder. We get all the information from a Mock 28 * Database and this is the approach used in the MainActivity. 29 * 30 * 2. Use an existing NotificationCompatBuilder to create a Notification. This approach requires 31 * you to store a reference to the original builder. The benefit is you only need the new/updated 32 * information for an existing notification. We use this approach in the IntentService handlers to 33 * update existing notifications. 34 * 35 * IMPORTANT NOTE 1: You shouldn't save/modify the resulting Notification object using 36 * its member variables and/or legacy APIs. If you want to retain anything from update 37 * to update, retain the Builder as option 2 outlines. 38 * 39 * IMPORTANT NOTE 2: If the global Notification Builder is lost because the process is killed, you 40 * should have a way to recreate the Notification Builder from a persistent state. (We do this as 41 * well in the sample, check the IntentServices.) 42 */ 43 public final class GlobalNotificationBuilder { 44 45 private static NotificationCompat.Builder sGlobalNotificationCompatBuilder = null; 46 47 /* 48 * Empty constructor - We don't initialize builder because we rely on a null state to let us 49 * know the Application's process was killed. 50 */ GlobalNotificationBuilder()51 private GlobalNotificationBuilder () { } 52 setNotificationCompatBuilderInstance(NotificationCompat.Builder builder)53 public static void setNotificationCompatBuilderInstance (NotificationCompat.Builder builder) { 54 sGlobalNotificationCompatBuilder = builder; 55 } 56 getNotificationCompatBuilderInstance()57 public static NotificationCompat.Builder getNotificationCompatBuilderInstance(){ 58 return sGlobalNotificationCompatBuilder; 59 } 60 }