Home | History | Annotate | Download | only in com.example.android.messagingservice
      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.messagingservice;
     18 
     19 import android.app.Notification;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.graphics.BitmapFactory;
     24 import android.os.Bundle;
     25 import android.support.v4.app.NotificationManagerCompat;
     26 import android.support.v4.app.RemoteInput;
     27 import android.support.v4.text.TextUtilsCompat;
     28 import android.support.v4.app.NotificationCompat;
     29 import android.text.TextUtils;
     30 import android.util.Log;
     31 
     32 /**
     33  * A receiver that gets called when a reply is sent to a given conversationId.
     34  */
     35 public class MessageReplyReceiver extends BroadcastReceiver {
     36 
     37     private static final String TAG = MessageReplyReceiver.class.getSimpleName();
     38 
     39     @Override
     40     public void onReceive(Context context, Intent intent) {
     41         if (MessagingService.REPLY_ACTION.equals(intent.getAction())) {
     42             int conversationId = intent.getIntExtra(MessagingService.CONVERSATION_ID, -1);
     43             CharSequence reply = getMessageText(intent);
     44             if (conversationId != -1) {
     45                 Log.d(TAG, "Got reply (" + reply + ") for ConversationId " + conversationId);
     46                 MessageLogger.logMessage(context, "ConversationId: " + conversationId +
     47                         " received a reply: [" + reply + "]");
     48 
     49                 // Update the notification to stop the progress spinner.
     50                 NotificationManagerCompat notificationManager =
     51                         NotificationManagerCompat.from(context);
     52                 Notification repliedNotification = new NotificationCompat.Builder(context)
     53                         .setSmallIcon(R.drawable.notification_icon)
     54                         .setLargeIcon(BitmapFactory.decodeResource(
     55                                 context.getResources(), R.drawable.android_contact))
     56                         .setContentText(context.getString(R.string.replied))
     57                         .build();
     58                 notificationManager.notify(conversationId, repliedNotification);
     59             }
     60         }
     61     }
     62 
     63     /**
     64      * Get the message text from the intent.
     65      * Note that you should call {@code RemoteInput#getResultsFromIntent(intent)} to process
     66      * the RemoteInput.
     67      */
     68     private CharSequence getMessageText(Intent intent) {
     69         Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
     70         if (remoteInput != null) {
     71             return remoteInput.getCharSequence(
     72                     MessagingService.EXTRA_REMOTE_REPLY);
     73         }
     74         return null;
     75     }
     76 }
     77