12 package com.google.appinventor.components.runtime.util;
14 import android.app.Notification;
15 import android.app.NotificationManager;
16 import android.app.PendingIntent;
17 import android.content.BroadcastReceiver;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.telephony.PhoneNumberUtils;
21 import android.telephony.SmsMessage;
22 import android.util.Log;
23 import androidx.core.app.NotificationCompat;
27 import java.util.List;
54 @SuppressWarnings(
"deprecation")
57 public static final String TAG =
"SmsBroadcastReceiver";
58 public static final int NOTIFICATION_ID = 8647;
66 public void onReceive(Context context, Intent intent) {
67 Log.i(TAG,
"onReceive");
70 String phone = getPhoneNumber(intent);
71 String msg = getMessage(intent);
73 Log.i(TAG,
"Received " + phone +
" : " + msg);
82 Log.i(TAG, context.getApplicationInfo().packageName +
83 " Receiving is not enabled, ignoring message.");
90 isRepl(context)) && !
Texting.isRunning()) {
91 Log.i(TAG, context.getApplicationInfo().packageName +
92 " Texting isn't running, and either receivingEnabled is FOREGROUND or we are the repl.");
100 Log.i(TAG, context.getApplicationInfo().packageName +
101 " App in Foreground, delivering message.");
103 Log.i(TAG, context.getApplicationInfo().packageName +
104 " Texting isn't running, but receivingEnabled == 2, sending notification.");
105 sendNotification(context, phone, msg);
115 private String getPhoneNumber(Intent intent) {
119 if (intent.getAction().equals(
"com.google.android.apps.googlevoice.SMS_RECEIVED")) {
123 phone = PhoneNumberUtils.formatNumber(phone);
128 for (SmsMessage smsMsg : messages) {
129 if (smsMsg !=
null) {
132 phone = smsMsg.getOriginatingAddress();
134 phone = LollipopUtil.formatNumber(phone);
136 phone = PhoneNumberUtils.formatNumber(phone);
142 Object[] pdus = (Object[]) intent.getExtras().get(
"pdus");
143 for (Object pdu : pdus) {
144 SmsMessage smsMsg = SmsMessage.createFromPdu((
byte[]) pdu);
145 phone = smsMsg.getOriginatingAddress();
146 phone = PhoneNumberUtils.formatNumber(phone);
149 }
catch(NullPointerException e) {
150 Log.w(TAG,
"Unable to retrieve originating address from SmsMessage", e);
160 private String getMessage(Intent intent) {
164 if (intent.getAction().equals(
"com.google.android.apps.googlevoice.SMS_RECEIVED")) {
167 msg = intent.getExtras().getString(Texting.MESSAGE_TAG);
169 }
else if (SdkLevel.getLevel() >= SdkLevel.LEVEL_KITKAT) {
171 StringBuilder sb =
new StringBuilder();
172 List<SmsMessage> messages = KitkatUtil.getMessagesFromIntent(intent);
173 for (SmsMessage smsMsg : messages) {
174 if (smsMsg !=
null) {
175 sb.append(smsMsg.getMessageBody());
181 StringBuilder sb =
new StringBuilder();
182 Object[] pdus = (Object[]) intent.getExtras().get(
"pdus");
183 for (Object pdu : pdus) {
184 SmsMessage smsMsg = SmsMessage.createFromPdu((
byte[]) pdu);
185 sb.append(smsMsg.getMessageBody());
189 }
catch(NullPointerException e) {
192 Log.w(TAG,
"Unable to retrieve message body from SmsMessage", e);
203 private void sendNotification(Context context, String phone, String msg) {
204 Log.i(TAG,
"sendingNotification " + phone +
":" + msg);
207 String packageName = context.getPackageName();
208 Log.i(TAG,
"Package name : " + packageName);
210 Intent newIntent =
null;
214 String classname = packageName +
".Screen1";
215 newIntent =
new Intent(context, Class.forName(classname));
216 newIntent.setAction(Intent.ACTION_MAIN);
217 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
222 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
225 PendingIntent activity = PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
226 NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
227 Notification note =
new NotificationCompat.Builder(context)
228 .setSmallIcon(android.R.drawable.sym_call_incoming)
229 .setTicker(phone +
" : " + msg)
230 .setWhen(System.currentTimeMillis())
232 .setDefaults(Notification.DEFAULT_SOUND)
233 .setContentTitle(
"Sms from " + phone)
235 .setContentIntent(activity)
236 .setNumber(Texting.getCachedMsgCount())
239 nm.notify(
null, NOTIFICATION_ID, note);
240 Log.i(TAG,
"Notification sent, classname: " + classname);
242 }
catch (ClassNotFoundException e) {
247 private boolean isRepl(Context context) {
249 String packageName = context.getPackageName();
250 String classname = packageName +
".Screen1";
251 Class appClass = Class.forName(classname);
252 Class superClass = appClass.getSuperclass();
253 if (superClass.equals(ReplForm.class))
257 }
catch (ClassNotFoundException e) {