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.cellbroadcastreceiver.tests;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.os.Handler;
22import android.os.Message;
23import android.util.Log;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.widget.Button;
27import android.widget.CheckBox;
28import android.widget.EditText;
29
30import java.util.Random;
31
32/**
33 * Activity to send test cell broadcast messages from GUI.
34 */
35public class SendTestBroadcastActivity extends Activity {
36    private static final String TAG = "SendTestBroadcastActivity";
37
38    /** Whether to delay before sending test message. */
39    private boolean mDelayBeforeSending;
40
41    /** Delay time before sending test message (when box is checked). */
42    private static final int DELAY_BEFORE_SENDING_MSEC = 5000;
43
44    private final Handler mDelayHandler = new Handler() {
45        @Override
46        public void handleMessage(Message msg) {
47            // call the onClick() method again, passing null View.
48            // The callback will ignore mDelayBeforeSending when the View is null.
49            OnClickListener pendingButtonClick = (OnClickListener) msg.obj;
50            pendingButtonClick.onClick(null);
51        }
52    };
53
54    /**
55     * Increment the message ID field and return the previous value.
56     * @return the current value of the message ID text field
57     */
58    private int getMessageId() {
59        EditText messageIdField = (EditText) findViewById(R.id.message_id);
60        int messageId = 0;
61        try {
62            messageId = Integer.parseInt(messageIdField.getText().toString());
63        } catch (NumberFormatException ignored) {
64            Log.e(TAG, "Invalid message ID");
65        }
66        int newMessageId = (messageId + 1) % 65536;
67        if (newMessageId == 0) {
68            newMessageId = 1;
69        }
70        messageIdField.setText(String.valueOf(newMessageId));
71        return messageId;
72    }
73
74    /**
75     * Return the value of the category field.
76     * @return the current value of the category text field
77     */
78    private int getCategory() {
79        EditText categoryField = (EditText) findViewById(R.id.category_id);
80        return Integer.parseInt(categoryField.getText().toString());
81    }
82
83    /**
84     * Initialization of the Activity after it is first created.  Must at least
85     * call {@link android.app.Activity#setContentView(int)} to
86     * describe what is to be displayed in the screen.
87     */
88    @Override
89    protected void onCreate(Bundle savedInstanceState) {
90        super.onCreate(savedInstanceState);
91
92        setContentView(R.layout.test_buttons);
93
94        /* Set message ID to a random value from 1-65535. */
95        EditText messageIdField = (EditText) findViewById(R.id.message_id);
96        messageIdField.setText(String.valueOf(new Random().nextInt(65535) + 1));
97
98        /* When category ID is non-zero, use it for the GSM/UMTS message identifier. */
99        EditText categoryIdField = (EditText) findViewById(R.id.category_id);
100        categoryIdField.setText("0");
101
102        /* Send an ETWS earthquake broadcast message to app. */
103        Button etwsEarthquakeTypeButton = (Button) findViewById(R.id.button_etws_earthquake_type);
104        etwsEarthquakeTypeButton.setOnClickListener(new OnClickListener() {
105            public void onClick(View v) {
106                if (mDelayBeforeSending && v != null) {
107                    Message msg = mDelayHandler.obtainMessage(0, this);
108                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
109                } else {
110                    SendTestMessages.testSendEtwsMessageEarthquake(SendTestBroadcastActivity.this,
111                            getMessageId());
112                }
113            }
114        });
115
116        /* Send an ETWS tsunami broadcast message to app. */
117        Button etwsTsunamiTypeButton = (Button) findViewById(R.id.button_etws_tsunami_type);
118        etwsTsunamiTypeButton.setOnClickListener(new OnClickListener() {
119            public void onClick(View v) {
120                if (mDelayBeforeSending && v != null) {
121                    Message msg = mDelayHandler.obtainMessage(0, this);
122                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
123                } else {
124                    SendTestMessages.testSendEtwsMessageTsunami(SendTestBroadcastActivity.this,
125                            getMessageId());
126                }
127            }
128        });
129
130        /* Send an ETWS earthquake and tsunami broadcast message to app. */
131        Button etwsEarthquakeTsunamiTypeButton = (Button)
132                findViewById(R.id.button_etws_earthquake_tsunami_type);
133        etwsEarthquakeTsunamiTypeButton.setOnClickListener(new OnClickListener() {
134            public void onClick(View v) {
135                if (mDelayBeforeSending && v != null) {
136                    Message msg = mDelayHandler.obtainMessage(0, this);
137                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
138                } else {
139                    SendTestMessages.testSendEtwsMessageEarthquakeTsunami(
140                            SendTestBroadcastActivity.this, getMessageId());
141                }
142            }
143        });
144
145        /* Send an ETWS other emergency broadcast message to app. */
146        Button etwsOtherTypeButton = (Button) findViewById(R.id.button_etws_other_type);
147        etwsOtherTypeButton.setOnClickListener(new OnClickListener() {
148            public void onClick(View v) {
149                if (mDelayBeforeSending && v != null) {
150                    Message msg = mDelayHandler.obtainMessage(0, this);
151                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
152                } else {
153                    SendTestMessages.testSendEtwsMessageOther(
154                            SendTestBroadcastActivity.this, getMessageId());
155                }
156            }
157        });
158
159        /* Send an ETWS cancel broadcast message to app. */
160        Button etwsCancelTypeButton = (Button) findViewById(R.id.button_etws_cancel_type);
161        etwsCancelTypeButton.setOnClickListener(new OnClickListener() {
162            public void onClick(View v) {
163                if (mDelayBeforeSending && v != null) {
164                    Message msg = mDelayHandler.obtainMessage(0, this);
165                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
166                } else {
167                    SendTestMessages.testSendEtwsMessageCancel(SendTestBroadcastActivity.this,
168                            getMessageId());
169                }
170            }
171        });
172
173        /* Send an ETWS test broadcast message to app. */
174        Button etwsTestTypeButton = (Button) findViewById(R.id.button_etws_test_type);
175        etwsTestTypeButton.setOnClickListener(new OnClickListener() {
176            public void onClick(View v) {
177                if (mDelayBeforeSending && v != null) {
178                    Message msg = mDelayHandler.obtainMessage(0, this);
179                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
180                } else {
181                    SendTestMessages.testSendEtwsMessageTest(SendTestBroadcastActivity.this,
182                            getMessageId());
183                }
184            }
185        });
186
187        /* Send a CMAS presidential alert to app. */
188        Button cmasPresAlertButton = (Button) findViewById(R.id.button_cmas_pres_alert);
189        cmasPresAlertButton.setOnClickListener(new OnClickListener() {
190            public void onClick(View v) {
191                if (mDelayBeforeSending && v != null) {
192                    Message msg = mDelayHandler.obtainMessage(0, this);
193                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
194                } else {
195                    SendCdmaCmasMessages.testSendCmasPresAlert(SendTestBroadcastActivity.this,
196                            getMessageId());
197                }
198            }
199        });
200
201        /* Send a CMAS extreme alert to app. */
202        Button cmasExtremeAlertButton = (Button) findViewById(R.id.button_cmas_extreme_alert);
203        cmasExtremeAlertButton.setOnClickListener(new OnClickListener() {
204            public void onClick(View v) {
205                if (mDelayBeforeSending && v != null) {
206                    Message msg = mDelayHandler.obtainMessage(0, this);
207                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
208                } else {
209                    SendCdmaCmasMessages.testSendCmasExtremeAlert(SendTestBroadcastActivity.this,
210                            getMessageId());
211                }
212            }
213        });
214
215        /* Send a CMAS severe alert to app. */
216        Button cmasSevereAlertButton = (Button) findViewById(R.id.button_cmas_severe_alert);
217        cmasSevereAlertButton.setOnClickListener(new OnClickListener() {
218            public void onClick(View v) {
219                if (mDelayBeforeSending && v != null) {
220                    Message msg = mDelayHandler.obtainMessage(0, this);
221                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
222                } else {
223                    SendCdmaCmasMessages.testSendCmasSevereAlert(SendTestBroadcastActivity.this,
224                            getMessageId());
225                }
226            }
227        });
228
229        /* Send a CMAS AMBER alert to app. */
230        Button cmasAmberAlertButton = (Button) findViewById(R.id.button_cmas_amber_alert);
231        cmasAmberAlertButton.setOnClickListener(new OnClickListener() {
232            public void onClick(View v) {
233                if (mDelayBeforeSending && v != null) {
234                    Message msg = mDelayHandler.obtainMessage(0, this);
235                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
236                } else {
237                    SendCdmaCmasMessages.testSendCmasAmberAlert(SendTestBroadcastActivity.this,
238                            getMessageId());
239                }
240            }
241        });
242
243        /* Send a CMAS monthly test alert to app. */
244        Button cmasMonthlyTestButton = (Button) findViewById(R.id.button_cmas_monthly_test);
245        cmasMonthlyTestButton.setOnClickListener(new OnClickListener() {
246            public void onClick(View v) {
247                if (mDelayBeforeSending && v != null) {
248                    Message msg = mDelayHandler.obtainMessage(0, this);
249                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
250                } else {
251                    SendCdmaCmasMessages.testSendCmasMonthlyTest(SendTestBroadcastActivity.this,
252                            getMessageId());
253                }
254            }
255        });
256
257        /* Send a GSM 7-bit broadcast message to app. */
258        Button gsm7bitTypeButton = (Button) findViewById(R.id.button_gsm_7bit_type);
259        gsm7bitTypeButton.setOnClickListener(new OnClickListener() {
260            public void onClick(View v) {
261                if (mDelayBeforeSending && v != null) {
262                    Message msg = mDelayHandler.obtainMessage(0, this);
263                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
264                } else {
265                    SendTestMessages.testSendMessage7bit(SendTestBroadcastActivity.this,
266                            getMessageId(), getCategory());
267                }
268            }
269        });
270
271        /* Send a UMTS 7-bit broadcast message to app. */
272        Button gsm7bitUmtsTypeButton = (Button) findViewById(R.id.button_gsm_7bit_umts_type);
273        gsm7bitUmtsTypeButton.setOnClickListener(new OnClickListener() {
274            public void onClick(View v) {
275                if (mDelayBeforeSending && v != null) {
276                    Message msg = mDelayHandler.obtainMessage(0, this);
277                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
278                } else {
279                    SendTestMessages.testSendMessage7bitUmts(SendTestBroadcastActivity.this,
280                            getMessageId(), getCategory());
281                }
282            }
283        });
284
285        /* Send a GSM 7-bit no padding broadcast message to app. */
286        Button gsm7bitNoPaddingButton = (Button) findViewById(R.id.button_gsm_7bit_nopadding_type);
287        gsm7bitNoPaddingButton.setOnClickListener(new OnClickListener() {
288            public void onClick(View v) {
289                if (mDelayBeforeSending && v != null) {
290                    Message msg = mDelayHandler.obtainMessage(0, this);
291                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
292                } else {
293                    SendTestMessages.testSendMessage7bitNoPadding(SendTestBroadcastActivity.this,
294                            getMessageId(), getCategory());
295                }
296            }
297        });
298
299        /* Send a UMTS 7-bit no padding broadcast message to app. */
300        Button gsm7bitNoPaddingUmtsTypeButton =
301                (Button) findViewById(R.id.button_gsm_7bit_nopadding_umts_type);
302        gsm7bitNoPaddingUmtsTypeButton.setOnClickListener(new OnClickListener() {
303            public void onClick(View v) {
304                if (mDelayBeforeSending && v != null) {
305                    Message msg = mDelayHandler.obtainMessage(0, this);
306                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
307                } else {
308                    SendTestMessages.testSendMessage7bitNoPaddingUmts(
309                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
310                }
311            }
312        });
313
314        /* Send a UMTS 7-bit multi-page broadcast message to app. */
315        Button gsm7bitMultipageButton =
316                (Button) findViewById(R.id.button_gsm_7bit_multipage_type);
317        gsm7bitMultipageButton.setOnClickListener(new OnClickListener() {
318            public void onClick(View v) {
319                if (mDelayBeforeSending && v != null) {
320                    Message msg = mDelayHandler.obtainMessage(0, this);
321                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
322                } else {
323                    SendTestMessages.testSendMessage7bitMultipageGsm(SendTestBroadcastActivity.this,
324                            getMessageId(), getCategory());
325                }
326            }
327        });
328
329        /* Send a UMTS 7-bit multi-page broadcast message to app. */
330        Button gsm7bitMultipageUmtsButton =
331                (Button) findViewById(R.id.button_gsm_7bit_multipage_umts_type);
332        gsm7bitMultipageUmtsButton.setOnClickListener(new OnClickListener() {
333            public void onClick(View v) {
334                if (mDelayBeforeSending && v != null) {
335                    Message msg = mDelayHandler.obtainMessage(0, this);
336                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
337                } else {
338                    SendTestMessages.testSendMessage7bitMultipageUmts(
339                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
340                }
341            }
342        });
343
344        /* Send a GSM 7-bit broadcast message with language to app. */
345        Button gsm7bitWithLanguageButton =
346                (Button) findViewById(R.id.button_gsm_7bit_with_language_type);
347        gsm7bitWithLanguageButton.setOnClickListener(new OnClickListener() {
348            public void onClick(View v) {
349                if (mDelayBeforeSending && v != null) {
350                    Message msg = mDelayHandler.obtainMessage(0, this);
351                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
352                } else {
353                    SendTestMessages.testSendMessage7bitWithLanguage(SendTestBroadcastActivity.this,
354                            getMessageId(), getCategory());
355                }
356            }
357        });
358
359        /* Send a GSM 7-bit broadcast message with language to app. */
360        Button gsm7bitWithLanguageInBodyButton =
361                (Button) findViewById(R.id.button_gsm_7bit_with_language_body_gsm_type);
362        gsm7bitWithLanguageInBodyButton.setOnClickListener(new OnClickListener() {
363            public void onClick(View v) {
364                if (mDelayBeforeSending && v != null) {
365                    Message msg = mDelayHandler.obtainMessage(0, this);
366                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
367                } else {
368                    SendTestMessages.testSendMessage7bitWithLanguageInBody(
369                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
370                }
371            }
372        });
373
374        /* Send a UMTS 7-bit broadcast message with language to app. */
375        Button gsm7bitWithLanguageUmtsButton =
376                (Button) findViewById(R.id.button_gsm_7bit_with_language_body_umts_type);
377        gsm7bitWithLanguageUmtsButton.setOnClickListener(new OnClickListener() {
378            public void onClick(View v) {
379                if (mDelayBeforeSending && v != null) {
380                    Message msg = mDelayHandler.obtainMessage(0, this);
381                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
382                } else {
383                    SendTestMessages.testSendMessage7bitWithLanguageInBodyUmts(
384                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
385                }
386            }
387        });
388
389        /* Send a GSM UCS-2 broadcast message to app. */
390        Button gsmUcs2TypeButton = (Button) findViewById(R.id.button_gsm_ucs2_type);
391        gsmUcs2TypeButton.setOnClickListener(new OnClickListener() {
392            public void onClick(View v) {
393                if (mDelayBeforeSending && v != null) {
394                    Message msg = mDelayHandler.obtainMessage(0, this);
395                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
396                } else {
397                    SendTestMessages.testSendMessageUcs2(SendTestBroadcastActivity.this,
398                            getMessageId(), getCategory());
399                }
400            }
401        });
402
403        /* Send a UMTS UCS-2 broadcast message to app. */
404        Button gsmUcs2UmtsTypeButton = (Button) findViewById(R.id.button_gsm_ucs2_umts_type);
405        gsmUcs2UmtsTypeButton.setOnClickListener(new OnClickListener() {
406            public void onClick(View v) {
407                if (mDelayBeforeSending && v != null) {
408                    Message msg = mDelayHandler.obtainMessage(0, this);
409                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
410                } else {
411                    SendTestMessages.testSendMessageUcs2Umts(SendTestBroadcastActivity.this,
412                            getMessageId(), getCategory());
413                }
414            }
415        });
416
417        /* Send a UMTS UCS-2 multipage broadcast message to app. */
418        Button gsmUcs2MultipageUmtsTypeButton =
419                (Button) findViewById(R.id.button_gsm_ucs2_multipage_umts_type);
420        gsmUcs2MultipageUmtsTypeButton.setOnClickListener(new OnClickListener() {
421            public void onClick(View v) {
422                if (mDelayBeforeSending && v != null) {
423                    Message msg = mDelayHandler.obtainMessage(0, this);
424                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
425                } else {
426                    SendTestMessages.testSendMessageUcs2MultipageUmts(
427                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
428                }
429            }
430        });
431
432        /* Send a GSM UCS-2 broadcast message with language to app. */
433        Button gsmUcs2WithLanguageTypeButton =
434                (Button) findViewById(R.id.button_gsm_ucs2_with_language_type);
435        gsmUcs2WithLanguageTypeButton.setOnClickListener(new OnClickListener() {
436            public void onClick(View v) {
437                if (mDelayBeforeSending && v != null) {
438                    Message msg = mDelayHandler.obtainMessage(0, this);
439                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
440                } else {
441                    SendTestMessages.testSendMessageUcs2WithLanguageInBody(
442                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
443                }
444            }
445        });
446
447        /* Send a UMTS UCS-2 broadcast message with language to app. */
448        Button gsmUcs2WithLanguageUmtsTypeButton =
449                (Button) findViewById(R.id.button_gsm_ucs2_with_language_umts_type);
450        gsmUcs2WithLanguageUmtsTypeButton.setOnClickListener(new OnClickListener() {
451            public void onClick(View v) {
452                if (mDelayBeforeSending && v != null) {
453                    Message msg = mDelayHandler.obtainMessage(0, this);
454                    mDelayHandler.sendMessageDelayed(msg, DELAY_BEFORE_SENDING_MSEC);
455                } else {
456                    SendTestMessages.testSendMessageUcs2WithLanguageUmts(
457                            SendTestBroadcastActivity.this, getMessageId(), getCategory());
458                }
459            }
460        });
461
462        /* Update boolean to delay before sending when box is checked. */
463        final CheckBox delayCheckbox = (CheckBox) findViewById(R.id.button_delay_broadcast);
464        delayCheckbox.setOnClickListener(new OnClickListener() {
465            public void onClick(View v) {
466                mDelayBeforeSending = delayCheckbox.isChecked();
467            }
468        });
469    }
470}
471