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