Home | History | Annotate | Download | only in os
      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.List;
     20 
     21 import android.app.Activity;
     22 import android.app.PendingIntent;
     23 import android.content.BroadcastReceiver;
     24 import android.content.ComponentName;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.content.pm.PackageManager;
     29 import android.graphics.Color;
     30 import android.os.Bundle;
     31 import android.telephony.SmsManager;
     32 import android.text.TextUtils;
     33 import android.util.Log;
     34 import android.view.View;
     35 import android.view.View.OnClickListener;
     36 import android.widget.Button;
     37 import android.widget.CheckBox;
     38 import android.widget.CompoundButton;
     39 import android.widget.EditText;
     40 import android.widget.TextView;
     41 import android.widget.Toast;
     42 import android.widget.CompoundButton.OnCheckedChangeListener;
     43 
     44 import com.example.android.apis.R;
     45 
     46 public class SmsMessagingDemo extends Activity {
     47     /** Tag string for our debug logs */
     48     private static final String TAG = "SmsMessagingDemo";
     49 
     50     public static final String SMS_RECIPIENT_EXTRA = "com.example.android.apis.os.SMS_RECIPIENT";
     51 
     52     public static final String ACTION_SMS_SENT = "com.example.android.apis.os.SMS_SENT_ACTION";
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57 
     58         setContentView(R.layout.sms_demo);
     59 
     60         if (getIntent().hasExtra(SMS_RECIPIENT_EXTRA)) {
     61             ((TextView) findViewById(R.id.sms_recipient)).setText(getIntent().getExtras()
     62                     .getString(SMS_RECIPIENT_EXTRA));
     63             ((TextView) findViewById(R.id.sms_content)).requestFocus();
     64         }
     65 
     66         // Enable or disable the broadcast receiver depending on the checked
     67         // state of the checkbox.
     68         CheckBox enableCheckBox = (CheckBox) findViewById(R.id.sms_enable_receiver);
     69 
     70         final PackageManager pm = this.getPackageManager();
     71         final ComponentName componentName = new ComponentName("com.example.android.apis",
     72                 "com.example.android.apis.os.SmsMessageReceiver");
     73 
     74         enableCheckBox.setChecked(pm.getComponentEnabledSetting(componentName) ==
     75                                   PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
     76 
     77         enableCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     78             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     79                 Log.d(TAG, (isChecked ? "Enabling" : "Disabling") + " SMS receiver");
     80 
     81                 pm.setComponentEnabledSetting(componentName,
     82                         isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
     83                                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
     84                         PackageManager.DONT_KILL_APP);
     85             }
     86         });
     87 
     88         final EditText recipientTextEdit = (EditText) SmsMessagingDemo.this
     89                 .findViewById(R.id.sms_recipient);
     90         final EditText contentTextEdit = (EditText) SmsMessagingDemo.this
     91                 .findViewById(R.id.sms_content);
     92         final TextView statusView = (TextView) SmsMessagingDemo.this.findViewById(R.id.sms_status);
     93 
     94         // Watch for send button clicks and send text messages.
     95         Button sendButton = (Button) findViewById(R.id.sms_send_message);
     96         sendButton.setOnClickListener(new OnClickListener() {
     97             public void onClick(View v) {
     98                 if (TextUtils.isEmpty(recipientTextEdit.getText())) {
     99                     Toast.makeText(SmsMessagingDemo.this, "Please enter a message recipient.",
    100                             Toast.LENGTH_SHORT).show();
    101                     return;
    102                 }
    103 
    104                 if (TextUtils.isEmpty(contentTextEdit.getText())) {
    105                     Toast.makeText(SmsMessagingDemo.this, "Please enter a message body.",
    106                             Toast.LENGTH_SHORT).show();
    107                     return;
    108                 }
    109 
    110                 recipientTextEdit.setEnabled(false);
    111                 contentTextEdit.setEnabled(false);
    112 
    113                 SmsManager sms = SmsManager.getDefault();
    114 
    115                 List<String> messages = sms.divideMessage(contentTextEdit.getText().toString());
    116 
    117                 String recipient = recipientTextEdit.getText().toString();
    118                 for (String message : messages) {
    119                     sms.sendTextMessage(recipient, null, message, PendingIntent.getBroadcast(
    120                             SmsMessagingDemo.this, 0, new Intent(ACTION_SMS_SENT), 0), null);
    121                 }
    122             }
    123         });
    124 
    125         // Register broadcast receivers for SMS sent and delivered intents
    126         registerReceiver(new BroadcastReceiver() {
    127             @Override
    128             public void onReceive(Context context, Intent intent) {
    129                 String message = null;
    130                 boolean error = true;
    131                 switch (getResultCode()) {
    132                 case Activity.RESULT_OK:
    133                     message = "Message sent!";
    134                     error = false;
    135                     break;
    136                 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
    137                     message = "Error.";
    138                     break;
    139                 case SmsManager.RESULT_ERROR_NO_SERVICE:
    140                     message = "Error: No service.";
    141                     break;
    142                 case SmsManager.RESULT_ERROR_NULL_PDU:
    143                     message = "Error: Null PDU.";
    144                     break;
    145                 case SmsManager.RESULT_ERROR_RADIO_OFF:
    146                     message = "Error: Radio off.";
    147                     break;
    148                 }
    149 
    150                 recipientTextEdit.setEnabled(true);
    151                 contentTextEdit.setEnabled(true);
    152                 contentTextEdit.setText("");
    153 
    154                 statusView.setText(message);
    155                 statusView.setTextColor(error ? Color.RED : Color.GREEN);
    156             }
    157         }, new IntentFilter(ACTION_SMS_SENT));
    158     }
    159 }
    160