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