1 /* 2 * Copyright (C) 2012 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.threadsample; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.support.v4.content.LocalBroadcastManager; 22 23 public class BroadcastNotifier { 24 25 private LocalBroadcastManager mBroadcaster; 26 27 /** 28 * Creates a BroadcastNotifier containing an instance of LocalBroadcastManager. 29 * LocalBroadcastManager is more efficient than BroadcastManager; because it only 30 * broadcasts to components within the app, it doesn't have to do parceling and so forth. 31 * 32 * @param context a Context from which to get the LocalBroadcastManager 33 */ BroadcastNotifier(Context context)34 public BroadcastNotifier(Context context) { 35 36 // Gets an instance of the support library local broadcastmanager 37 mBroadcaster = LocalBroadcastManager.getInstance(context); 38 39 } 40 41 /** 42 * 43 * Uses LocalBroadcastManager to send an {@link Intent} containing {@code status}. The 44 * {@link Intent} has the action {@code BROADCAST_ACTION} and the category {@code DEFAULT}. 45 * 46 * @param status {@link Integer} denoting a work request status 47 */ broadcastIntentWithState(int status)48 public void broadcastIntentWithState(int status) { 49 50 Intent localIntent = new Intent(); 51 52 // The Intent contains the custom broadcast action for this app 53 localIntent.setAction(Constants.BROADCAST_ACTION); 54 55 // Puts the status into the Intent 56 localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, status); 57 localIntent.addCategory(Intent.CATEGORY_DEFAULT); 58 59 // Broadcasts the Intent 60 mBroadcaster.sendBroadcast(localIntent); 61 62 } 63 64 /** 65 * Uses LocalBroadcastManager to send an {@link String} containing a logcat message. 66 * {@link Intent} has the action {@code BROADCAST_ACTION} and the category {@code DEFAULT}. 67 * 68 * @param logData a {@link String} to insert into the log. 69 */ notifyProgress(String logData)70 public void notifyProgress(String logData) { 71 72 Intent localIntent = new Intent(); 73 74 // The Intent contains the custom broadcast action for this app 75 localIntent.setAction(Constants.BROADCAST_ACTION); 76 77 localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, -1); 78 79 // Puts log data into the Intent 80 localIntent.putExtra(Constants.EXTENDED_STATUS_LOG, logData); 81 localIntent.addCategory(Intent.CATEGORY_DEFAULT); 82 83 // Broadcasts the Intent 84 mBroadcaster.sendBroadcast(localIntent); 85 86 } 87 } 88