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      * Clear internal cache data.
38      */
clear()39     public void clear() {
40         mMessages.clear();
41         mSentMessageResults.clear();
42         mDeliveredMessageResults.clear();
43     }
44 
45     /**
46      * Offer a SMS message to the queue of SMS messages waiting to be processed.
47      */
offerSmsMessage(String smsMessage)48     public void offerSmsMessage(String smsMessage) {
49         mMessages.offer(smsMessage);
50     }
51 
52     /**
53      * Wait for timeoutMs for a incoming SMS message to be received and return that SMS message,
54      * or null if the SMS message was not received before the timeout.
55      */
waitForSmsMessage(int timeoutMs)56     public String waitForSmsMessage(int timeoutMs) {
57         try {
58             return mMessages.poll(timeoutMs, TimeUnit.MILLISECONDS);
59         } catch (InterruptedException e) {
60             return null;
61         }
62     }
63 
offerMessageSentIntent(Intent intent)64     public void offerMessageSentIntent(Intent intent) {
65         mSentMessageResults.offer(intent);
66     }
67 
waitForMessageSentIntent(int timeoutMs)68     public Intent waitForMessageSentIntent(int timeoutMs) {
69         try {
70             return mSentMessageResults.poll(timeoutMs, TimeUnit.MILLISECONDS);
71         } catch (InterruptedException e) {
72             return null;
73         }
74     }
75 
offerMessageDeliveredIntent(Intent intent)76     public void offerMessageDeliveredIntent(Intent intent) {
77         mDeliveredMessageResults.offer(intent);
78     }
79 
waitForMessageDeliveredIntent(int timeoutMs)80     public Intent waitForMessageDeliveredIntent(int timeoutMs) {
81         try {
82             return mDeliveredMessageResults.poll(timeoutMs, TimeUnit.MILLISECONDS);
83         } catch (InterruptedException e) {
84             return null;
85         }
86     }
87 }
88