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.mms.apptests;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.telephony.TelephonyManager;
25 import android.text.TextUtils;
26 import android.view.Menu;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.widget.Button;
31 import android.widget.EditText;
32 import android.widget.Toast;
33 
34 import com.android.mms.apptests.R;
35 
36 // This is a manual test application for testing the Messaging app's ability to send Sms messages
37 // without the user having to confirm or press a send button. This app uses the intent:
38 //   com.android.mms.intent.action.SENDTO_NO_CONFIRMATION
39 // to tell the messaging app to send a message. This app tests that the required permissions
40 // are checked. It also has buttons for testing sending a long (i.e. greater than 140 char) message
41 // and for sending a number of messages in rapid fire succession.
42 
43 public class SmsSendIntentTestActivity extends Activity {
44     /** Tag string for our debug logs */
45     private static final String TAG = "SmsSendIntentTestActivity";
46     private EditText mRecipient;
47     private EditText mMessage;
48 
49     private final int NOTIFICATION_MENU = 100;
50     private final int NOTIFICATIONS_REQUEST_CODE = 101;
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         setContentView(R.layout.sms_send_intent_test);
57 
58         mRecipient = (EditText)findViewById(R.id.sms_recipient);
59         mMessage = (EditText)findViewById(R.id.sms_content);
60 
61         String line1Number = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE))
62                 .getLine1Number();
63         mRecipient.setText(line1Number); // use this to prime a number
64 
65         Button sendButton = (Button) findViewById(R.id.sms_send_message);
66         sendButton.setOnClickListener(new OnClickListener() {
67             public void onClick(View v) {
68                 sendMessage(1, 1);
69             }
70         });
71 
72         Button sendUnlockButton = (Button) findViewById(R.id.sms_send_message_unlock_screen);
73         sendUnlockButton.setOnClickListener(new OnClickListener() {
74             public void onClick(View v) {
75                 sendMessageUnlockScreen();
76             }
77         });
78 
79         Button sendMultiButton = (Button) findViewById(R.id.sms_send_multi_message);
80         sendMultiButton.setOnClickListener(new OnClickListener() {
81             public void onClick(View v) {
82                 sendMessage(5, 1);
83             }
84         });
85 
86         Button sendLongButton = (Button) findViewById(R.id.sms_send_long_message);
87         sendLongButton.setOnClickListener(new OnClickListener() {
88             public void onClick(View v) {
89                 sendMessage(1, 10);
90             }
91         });
92 
93         Button primeButton = (Button) findViewById(R.id.sms_prime_message);
94         primeButton.setOnClickListener(new OnClickListener() {
95             public void onClick(View v) {
96                 mMessage.setText(R.string.sms_long_message);
97             }
98         });
99 
100         Button notificationsButton = (Button) findViewById(R.id.turn_off_notification_message);
101         notificationsButton.setOnClickListener(new OnClickListener() {
102             public void onClick(View v) {
103                 Intent intent =
104                     new Intent("com.android.mms.intent.action.MESSAGING_APP_NOTIFICATIONS");
105                 startActivityForResult(intent, NOTIFICATIONS_REQUEST_CODE);
106             }
107         });
108     }
109 
sendMessageUnlockScreen()110     private void sendMessageUnlockScreen() {
111         String recipient = mRecipient.getText().toString();
112         if (TextUtils.isEmpty(recipient)) {
113             Toast.makeText(SmsSendIntentTestActivity.this, "Please enter a message recipient.",
114                     Toast.LENGTH_SHORT).show();
115             return;
116         }
117         String message = mMessage.getText().toString();
118         if (TextUtils.isEmpty(message)) {
119             Toast.makeText(SmsSendIntentTestActivity.this, "Please enter a message body.",
120                     Toast.LENGTH_SHORT).show();
121             return;
122         }
123 
124         Toast.makeText(SmsSendIntentTestActivity.this, "You have five seconds to lock the screen.",
125                 Toast.LENGTH_SHORT).show();
126 
127         try {
128             Thread.sleep(5000);     // yeah, yeah, it's on the UI thread
129         } catch (Exception e) {
130         }
131 
132         Uri uri = Uri.fromParts("smsto", recipient, null);
133         Intent intent = new Intent("com.android.mms.intent.action.SENDTO_NO_CONFIRMATION", uri);
134         intent.putExtra(Intent.EXTRA_TEXT, message);
135         intent.putExtra("exit_on_sent", true);
136         intent.putExtra("showUI", true);
137         startService(intent);
138     }
139 
sendMessage(int count, int dupeCount)140     private void sendMessage(int count, int dupeCount) {
141         String recipient = mRecipient.getText().toString();
142         if (TextUtils.isEmpty(recipient)) {
143             Toast.makeText(SmsSendIntentTestActivity.this, "Please enter a message recipient.",
144                     Toast.LENGTH_SHORT).show();
145             return;
146         }
147         String message = mMessage.getText().toString();
148         if (TextUtils.isEmpty(message)) {
149             Toast.makeText(SmsSendIntentTestActivity.this, "Please enter a message body.",
150                     Toast.LENGTH_SHORT).show();
151             return;
152         }
153 
154         if (dupeCount > 1) {
155             StringBuilder sb = new StringBuilder();
156             for (int i = 0; i < dupeCount; i++) {
157                 sb.append(i).append(": ").append(message).append(' ');
158             }
159             message = sb.toString();
160         }
161         Uri uri = Uri.fromParts("smsto", recipient, null);
162         for (int i = 0; i < count; i++) {
163             Intent intent = new Intent("com.android.mms.intent.action.SENDTO_NO_CONFIRMATION", uri);
164             intent.putExtra(Intent.EXTRA_TEXT, message);
165             startService(intent);
166         }
167     }
168 
169     @Override
onActivityResult(int requestCode, int resultCode, Intent data)170     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
171         if (requestCode == NOTIFICATIONS_REQUEST_CODE) {
172             String msg = "result: ";
173             if (resultCode == RESULT_OK) {
174                 msg += "ok";
175             } else if (resultCode == RESULT_CANCELED) {
176                 msg += "canceled";
177             } else {
178                 msg += resultCode;
179             }
180             Button notificationsButton = (Button) findViewById(R.id.turn_off_notification_message);
181             notificationsButton.setText(msg);
182         }
183     }
184 
185     @Override
onCreateOptionsMenu(Menu menu)186     public boolean onCreateOptionsMenu(Menu menu) {
187         menu.add(Menu.NONE, NOTIFICATION_MENU, Menu.NONE, R.string.menu_notifications);
188         return true;
189     }
190 
191     @Override
onOptionsItemSelected(MenuItem item)192     public boolean onOptionsItemSelected(MenuItem item) {
193         switch (item.getItemId()) {
194             case NOTIFICATION_MENU:
195                 Intent intent =
196                     new Intent("com.android.mms.intent.action.MESSAGING_APP_NOTIFICATIONS");
197                 startActivity(intent);
198                 break;
199         }
200         return true;
201     }
202 }
203