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 package android.telephony.cts; 17 18 import android.content.Intent; 19 20 import java.util.concurrent.LinkedBlockingQueue; 21 import java.util.concurrent.TimeUnit; 22 23 public class AsyncSmsMessageListener { 24 25 private static final AsyncSmsMessageListener sInstance = new AsyncSmsMessageListener(); 26 getInstance()27 public static final AsyncSmsMessageListener getInstance() { 28 return sInstance; 29 } 30 31 private final LinkedBlockingQueue<String> mMessages = new LinkedBlockingQueue<>(1); 32 private final LinkedBlockingQueue<Intent> mSentMessageResults = new LinkedBlockingQueue<>(1); 33 private final LinkedBlockingQueue<Intent> mDeliveredMessageResults = 34 new LinkedBlockingQueue<>(1); 35 36 /** 37 * Offer a SMS message to the queue of SMS messages waiting to be processed. 38 */ offerSmsMessage(String smsMessage)39 public void offerSmsMessage(String smsMessage) { 40 mMessages.offer(smsMessage); 41 } 42 43 /** 44 * Wait for timeoutMs for a incoming SMS message to be received and return that SMS message, 45 * or null if the SMS message was not received before the timeout. 46 */ waitForSmsMessage(int timeoutMs)47 public String waitForSmsMessage(int timeoutMs) { 48 try { 49 return mMessages.poll(timeoutMs, TimeUnit.MILLISECONDS); 50 } catch (InterruptedException e) { 51 return null; 52 } 53 } 54 offerMessageSentIntent(Intent intent)55 public void offerMessageSentIntent(Intent intent) { 56 mSentMessageResults.offer(intent); 57 } 58 waitForMessageSentIntent(int timeoutMs)59 public Intent waitForMessageSentIntent(int timeoutMs) { 60 try { 61 return mSentMessageResults.poll(timeoutMs, TimeUnit.MILLISECONDS); 62 } catch (InterruptedException e) { 63 return null; 64 } 65 } 66 offerMessageDeliveredIntent(Intent intent)67 public void offerMessageDeliveredIntent(Intent intent) { 68 mDeliveredMessageResults.offer(intent); 69 } 70 waitForMessageDeliveredIntent(int timeoutMs)71 public Intent waitForMessageDeliveredIntent(int timeoutMs) { 72 try { 73 return mDeliveredMessageResults.poll(timeoutMs, TimeUnit.MILLISECONDS); 74 } catch (InterruptedException e) { 75 return null; 76 } 77 } 78 } 79