1 /* 2 * Copyright (C) 2019 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.phone.testapps.smsmanagertestapp; 18 19 import android.app.IntentService; 20 import android.app.PendingIntent; 21 import android.content.Intent; 22 import android.os.AsyncTask; 23 import android.telephony.SmsManager; 24 import android.util.Log; 25 26 /** 27 * IntentService whose purpose is to handle outgoing SMS intents for this application and schedule 28 * them onto a AsyncTask to sleep for 5 seconds. This allows us to simulate SMS messages being sent 29 * from background services. 30 */ 31 public class SmsManagerTestService extends IntentService { 32 33 private static final String LOG_TAG = "smsmanagertestservice"; 34 35 private static class SendSmsJob extends AsyncTask<Intent, Void, Void> { 36 37 @Override doInBackground(Intent... intents)38 protected Void doInBackground(Intent... intents) { 39 Intent intent = intents[0]; 40 try { 41 Thread.sleep(5000); 42 } catch (InterruptedException e) { 43 // testing 44 } 45 46 String text = intent.getStringExtra(EXTRA_SEND_TEXT); 47 String phoneNumber = intent.getStringExtra(EXTRA_SEND_NUMBER); 48 PendingIntent sendIntent = intent.getParcelableExtra(EXTRA_SEND_INTENT); 49 sendSms(phoneNumber, text, sendIntent); 50 return null; 51 } 52 53 @Override onPostExecute(Void aVoid)54 protected void onPostExecute(Void aVoid) { 55 Log.i(LOG_TAG, "SMS sent"); 56 } 57 58 } 59 60 public static final String SEND_SMS = "com.android.phone.testapps.smsmanagertestapp.send_sms"; 61 public static final String EXTRA_SEND_TEXT = "text"; 62 public static final String EXTRA_SEND_NUMBER = "number"; 63 public static final String EXTRA_SEND_INTENT = "sendIntent"; 64 SmsManagerTestService()65 public SmsManagerTestService() { 66 super("SmsManagerTestService"); 67 } 68 69 70 @Override onHandleIntent(Intent intent)71 protected void onHandleIntent(Intent intent) { 72 switch (intent.getAction()) { 73 case SEND_SMS : { 74 new SendSmsJob().execute(intent); 75 break; 76 } 77 } 78 } 79 sendSms(String phoneNumber, String text, PendingIntent sendIntent)80 private static void sendSms(String phoneNumber, String text, PendingIntent sendIntent) { 81 SmsManager m = SmsManager.getDefault(); 82 m.sendTextMessage(phoneNumber, null, text, sendIntent, null); 83 } 84 } 85