Home | History | Annotate | Download | only in basicsmsreceiver
      1 /*
      2  * Copyright (C) 2011 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.android.basicsmsreceiver;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.NotificationManager;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.content.Intent;
     26 import android.os.Bundle;
     27 import android.util.Log;
     28 
     29 public class DialogSmsDisplay extends Activity {
     30     private static final String LOG_TAG = "SmsReceivedDialog";
     31 
     32     private static final int DIALOG_SHOW_MESSAGE = 1;
     33 
     34     public static final String SMS_FROM_ADDRESS_EXTRA =
     35                                                 "com.android.basicsmsreceiver.SMS_FROM_ADDRESS";
     36     public static final String SMS_MESSAGE_EXTRA =
     37                                                 "com.android.basicsmsreceiver.SMS_MESSAGE";
     38     public static final String SMS_NOTIFICATION_ID_EXTRA =
     39                                                 "com.android.basicsmsreceiver.NOTIFICATION_ID";
     40 
     41     // Visible to unit tests
     42     String mFromAddress;
     43     String mMessage;
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48 
     49         parseIntent(getIntent());
     50     }
     51 
     52     private void parseIntent(Intent intent) {
     53         if (intent == null) {
     54             return;
     55         }
     56         Bundle extras = intent.getExtras();
     57         if (extras == null) {
     58             return;
     59         }
     60         mFromAddress = extras.getString(SMS_FROM_ADDRESS_EXTRA);
     61         mMessage = extras.getString(SMS_MESSAGE_EXTRA);
     62         int notificationId = extras.getInt(SMS_NOTIFICATION_ID_EXTRA);
     63 
     64         Log.i(LOG_TAG, "notificationId: " + notificationId);
     65 
     66         // Dismiss the notification that brought us here.
     67         NotificationManager notificationManager =
     68             (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
     69         notificationManager.cancel(notificationId);
     70 
     71         showDialog(DIALOG_SHOW_MESSAGE);
     72     }
     73 
     74     @Override
     75     protected void onNewIntent(Intent intent) {
     76         removeDialog(DIALOG_SHOW_MESSAGE);
     77 
     78         parseIntent(intent);
     79     }
     80 
     81     @Override
     82     protected Dialog onCreateDialog(int id) {
     83         switch (id) {
     84             case DIALOG_SHOW_MESSAGE:
     85                 return new AlertDialog.Builder(this)
     86                         .setTitle(String.format(getString(R.string.sms_message_from_format),
     87                                 mFromAddress))
     88                         .setMessage(mMessage)
     89                         .setCancelable(true)
     90                         .setOnCancelListener(new AlertDialog.OnCancelListener() {
     91                             public void onCancel(DialogInterface dialog) {
     92                                 dialog.dismiss();
     93                                 finish();
     94                             }
     95                         })
     96                         .setNeutralButton(R.string.sms_done_button,
     97                                 new DialogInterface.OnClickListener() {
     98                             public void onClick(DialogInterface dialog, int whichButton) {
     99                                 dialog.dismiss();
    100                                 finish();
    101                             }
    102                         })
    103                         .create();
    104         }
    105         return null;
    106     }
    107 }
    108