• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.apis.os;
18 
19 import java.util.Locale;
20 
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.speech.tts.TextToSpeech;
28 import android.speech.tts.TextToSpeech.OnInitListener;
29 import android.util.Log;
30 
31 import com.example.android.apis.R;
32 
33 public class SmsReceivedDialog extends Activity implements OnInitListener {
34     private static final String TAG = "SmsReceivedDialog";
35 
36     private static final int DIALOG_SHOW_MESSAGE = 1;
37 
38     public static final String SMS_FROM_ADDRESS_EXTRA = "com.example.android.apis.os.SMS_FROM_ADDRESS";
39     public static final String SMS_FROM_DISPLAY_NAME_EXTRA = "com.example.android.apis.os.SMS_FROM_DISPLAY_NAME";
40     public static final String SMS_MESSAGE_EXTRA = "com.example.android.apis.os.SMS_MESSAGE";
41 
42     private TextToSpeech mTts;
43 
44     private String mFromDisplayName;
45     private String mFromAddress;
46     private String mMessage;
47     private String mFullBodyString;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         mFromAddress = getIntent().getExtras().getString(SMS_FROM_ADDRESS_EXTRA);
54         mFromDisplayName = getIntent().getExtras().getString(SMS_FROM_DISPLAY_NAME_EXTRA);
55         mMessage = getIntent().getExtras().getString(SMS_MESSAGE_EXTRA);
56 
57         mFullBodyString = String.format(
58                 getResources().getString(R.string.sms_speak_string_format),
59                 mFromDisplayName,
60                 mMessage);
61 
62         showDialog(DIALOG_SHOW_MESSAGE);
63         mTts = new TextToSpeech(this, this);
64     }
65 
onInit(int status)66     public void onInit(int status) {
67         if (status == TextToSpeech.SUCCESS) {
68             int result = mTts.setLanguage(Locale.US);
69             if (result == TextToSpeech.LANG_MISSING_DATA
70                     || result == TextToSpeech.LANG_NOT_SUPPORTED) {
71                 Log.e(TAG, "TTS language is not available.");
72             } else {
73                 mTts.speak(mFullBodyString, TextToSpeech.QUEUE_ADD, null);
74             }
75         } else {
76             // Initialization failed.
77             Log.e(TAG, "Could not initialize TTS.");
78         }
79     }
80 
81     @Override
onCreateDialog(int id)82     protected Dialog onCreateDialog(int id) {
83         switch (id) {
84         case DIALOG_SHOW_MESSAGE:
85             return new AlertDialog.Builder(this)
86                     .setIcon(android.R.drawable.ic_dialog_email)
87                     .setTitle("Message Received")
88                     .setMessage(mFullBodyString)
89                     .setPositiveButton(R.string.reply, new DialogInterface.OnClickListener() {
90                         public void onClick(DialogInterface dialog, int whichButton) {
91                             // Begin creating the reply with the SmsMessagingDemo activity
92                             Intent i = new Intent();
93                             i.setClass(SmsReceivedDialog.this, SmsMessagingDemo.class);
94                             i.putExtra(SmsMessagingDemo.SMS_RECIPIENT_EXTRA, mFromAddress);
95                             startActivity(i);
96 
97                             dialog.dismiss();
98                             finish();
99                         }
100                     })
101                     .setNegativeButton(R.string.dismiss, new DialogInterface.OnClickListener() {
102                         public void onClick(DialogInterface dialog, int whichButton) {
103                             dialog.dismiss();
104                             finish();
105                         }
106                     })
107                     .setOnCancelListener(new DialogInterface.OnCancelListener() {
108                         public void onCancel(DialogInterface dialog) {
109                             finish();
110                         }
111                     }).create();
112         }
113         return null;
114     }
115 }
116