diff options
author | Tom Taylor <tomtaylor@google.com> | 2011-02-25 10:58:34 -0800 |
---|---|---|
committer | Tom Taylor <tomtaylor@google.com> | 2011-02-25 10:58:34 -0800 |
commit | 58a104a69871bd7e93afd768ceca5df1eac32ff9 (patch) | |
tree | 21d0673a9a516c1cacb6508f362469aedd0dcd29 /src/com | |
parent | c50682dac4ffdccc763c3886bafaa734796d166d (diff) | |
download | BasicSmsReceiver-58a104a69871bd7e93afd768ceca5df1eac32ff9.tar.gz |
Add basic SMS functionality in Honeycomb to support EU roaming requirements
First cut. Bug 3444951
Change-Id: Iec634a8d0a784972b4b1eb57047dca36135a147d
Diffstat (limited to 'src/com')
3 files changed, 253 insertions, 0 deletions
diff --git a/src/com/android/basicsmsreceiver/BasicSmsReceiverApp.java b/src/com/android/basicsmsreceiver/BasicSmsReceiverApp.java new file mode 100755 index 0000000..2db846b --- /dev/null +++ b/src/com/android/basicsmsreceiver/BasicSmsReceiverApp.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.basicsmsreceiver; + +import android.app.Activity; +import android.app.Application; +import android.content.SharedPreferences; +import android.util.Log; + +public class BasicSmsReceiverApp extends Application { + public static final String LOG_TAG = "BasicSmsReceiverApp"; + public static final String SMS_LITE_PREFS_KEY = "sms_lite_prefs"; + public static final String PREF_KEY_NOTIFICATION_ID = "notification_id"; + + static BasicSmsReceiverApp gBasicSmsReceiverApp; + + @Override + public void onCreate() { + super.onCreate(); + + gBasicSmsReceiverApp = this; + } + + public static BasicSmsReceiverApp getBasicSmsReceiverApp() { + return gBasicSmsReceiverApp; + } + + // Each incoming sms gets its own notification. We have to use a new unique notification id + // for each one. + public int getNextNotificationId() { + SharedPreferences prefs = getSharedPreferences(SMS_LITE_PREFS_KEY, + Activity.MODE_PRIVATE); + int notificationId = prefs.getInt(PREF_KEY_NOTIFICATION_ID, 0); + ++notificationId; + if (notificationId > 32765) { + notificationId = 1; // wrap around before it gets dangerous + } + + // Save the updated notificationId in SharedPreferences + SharedPreferences.Editor editor = prefs.edit(); + editor.putInt(PREF_KEY_NOTIFICATION_ID, notificationId); + editor.apply(); + + Log.d(LOG_TAG, "getNextNotificationId: " + notificationId); + + return notificationId; + } +} diff --git a/src/com/android/basicsmsreceiver/DialogSmsDisplay.java b/src/com/android/basicsmsreceiver/DialogSmsDisplay.java new file mode 100755 index 0000000..a6bb19b --- /dev/null +++ b/src/com/android/basicsmsreceiver/DialogSmsDisplay.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.basicsmsreceiver; + +import java.util.Locale; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.NotificationManager; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.KeyEvent; +import android.view.View; +import android.view.Window; + +public class DialogSmsDisplay extends Activity { + private static final String LOG_TAG = "SmsReceivedDialog"; + + private static final int DIALOG_SHOW_MESSAGE = 1; + + public static final String SMS_FROM_ADDRESS_EXTRA = + "com.android.basicsmsreceiver.SMS_FROM_ADDRESS"; + public static final String SMS_MESSAGE_EXTRA = + "com.android.basicsmsreceiver.SMS_MESSAGE"; + public static final String SMS_NOTIFICATION_ID_EXTRA = + "com.android.basicsmsreceiver.NOTIFICATION_ID"; + + private String mFromAddress; + private String mMessage; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Bundle extras = getIntent().getExtras(); + mFromAddress = extras.getString(SMS_FROM_ADDRESS_EXTRA); + mMessage = extras.getString(SMS_MESSAGE_EXTRA); + int notificationId = extras.getInt(SMS_NOTIFICATION_ID_EXTRA); + + Log.i(LOG_TAG, "notificationId: " + notificationId); + + // Dismiss the notification that brought us here. + NotificationManager notificationManager = + (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(notificationId); + + showDialog(DIALOG_SHOW_MESSAGE); + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case DIALOG_SHOW_MESSAGE: + return new AlertDialog.Builder(this) + .setTitle(String.format(getString(R.string.sms_message_from_format), + mFromAddress)) + .setMessage(mMessage) + .setCancelable(true) + .setOnCancelListener(new AlertDialog.OnCancelListener() { + public void onCancel(DialogInterface dialog) { + dialog.dismiss(); + finish(); + } + }) + .setNeutralButton(R.string.sms_done_button, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + dialog.dismiss(); + finish(); + } + }) + .create(); + } + return null; + } +} diff --git a/src/com/android/basicsmsreceiver/SmsMessageReceiver.java b/src/com/android/basicsmsreceiver/SmsMessageReceiver.java new file mode 100755 index 0000000..b3c86c7 --- /dev/null +++ b/src/com/android/basicsmsreceiver/SmsMessageReceiver.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.basicsmsreceiver; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.ContactsContract; +import android.telephony.SmsMessage; +import android.util.Log; +import android.widget.RemoteViews; + +public class SmsMessageReceiver extends BroadcastReceiver { + /** Tag string for our debug logs */ + private static final String LOG_TAG = "SmsMessageReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + Bundle extras = intent.getExtras(); + Log.i(LOG_TAG, "onReceive"); + if (extras == null) + return; + + Object[] pdus = (Object[]) extras.get("pdus"); + + for (int i = 0; i < pdus.length; i++) { + SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]); + String fromAddress = message.getOriginatingAddress(); + String messageBody = message.getMessageBody().toString(); + + Log.i(LOG_TAG, "From: " + fromAddress + " message: " + messageBody); + + addNotification(context, fromAddress, messageBody); + } + } + + private void addNotification(Context context, String fromAddress, String message) { + int notificationId = BasicSmsReceiverApp.getBasicSmsReceiverApp().getNextNotificationId(); + + Notification.Builder notification = new Notification.Builder(context) + .setTicker(message) + .setWhen(System.currentTimeMillis()) + .setContentTitle(fromAddress) + .setContentText(message) + .setSmallIcon(R.drawable.stat_notify_sms) + .setContentIntent(createDisplayMessageIntent(context, fromAddress, message, + notificationId)); + + Log.i(LOG_TAG, "addNotification notificationId: " + notificationId); + + NotificationManager notificationManager = + (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); + + notificationManager.notify(notificationId, notification.getNotification()); + } + + private PendingIntent createDisplayMessageIntent(Context context, String fromAddress, + String message, int notificationId) { + // Trigger the main activity to fire up a dialog that shows the received messages + Intent di = new Intent(); + di.setClass(context, DialogSmsDisplay.class); + di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | + Intent.FLAG_ACTIVITY_CLEAR_TOP); + di.putExtra(DialogSmsDisplay.SMS_FROM_ADDRESS_EXTRA, fromAddress); + di.putExtra(DialogSmsDisplay.SMS_MESSAGE_EXTRA, message); + di.putExtra(DialogSmsDisplay.SMS_NOTIFICATION_ID_EXTRA, notificationId); + + // This line is needed to make this intent compare differently than the other intents + // created here for other messages. Without this line, the PendingIntent always gets the + // intent of a previous message and notification. + di.setType(Integer.toString(notificationId)); + + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, di, 0); + return pendingIntent; + } + +} |