• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.vvm.omtp.sms;
18 
19 import android.annotation.MainThread;
20 import android.annotation.WorkerThread;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.provider.VoicemailContract;
27 import com.android.phone.Assert;
28 import com.android.phone.vvm.omtp.OmtpConstants;
29 import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
30 import com.android.phone.vvm.omtp.VvmLog;
31 import com.android.phone.vvm.omtp.protocol.VisualVoicemailProtocol;
32 import java.io.Closeable;
33 import java.io.IOException;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.TimeoutException;
38 
39 /**
40  * Intercepts a incoming STATUS SMS with a blocking call.
41  */
42 public class StatusSmsFetcher extends BroadcastReceiver implements Closeable {
43 
44     private static final String TAG = "VvmStatusSmsFetcher";
45 
46     private static final long STATUS_SMS_TIMEOUT_MILLIS = 60_000;
47 
48     private CompletableFuture<Bundle> mFuture = new CompletableFuture<>();
49 
50     private final Context mContext;
51     private final int mSubId;
52 
StatusSmsFetcher(Context context, int subId)53     public StatusSmsFetcher(Context context, int subId) {
54         mContext = context;
55         mSubId = subId;
56         IntentFilter filter = new IntentFilter(VoicemailContract.ACTION_VOICEMAIL_SMS_RECEIVED);
57         context.registerReceiver(this, filter);
58     }
59 
60     @Override
close()61     public void close() throws IOException {
62         mContext.unregisterReceiver(this);
63     }
64 
65     @WorkerThread
get()66     public Bundle get()
67             throws InterruptedException, ExecutionException, TimeoutException {
68         Assert.isNotMainThread();
69         return mFuture.get(STATUS_SMS_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
70     }
71 
72     @Override
73     @MainThread
onReceive(Context context, Intent intent)74     public void onReceive(Context context, Intent intent) {
75         Assert.isMainThread();
76         int subId = intent.getExtras().getInt(VoicemailContract.EXTRA_VOICEMAIL_SMS_SUBID);
77 
78         if (mSubId != subId) {
79             return;
80         }
81         String eventType = intent.getExtras()
82                 .getString(VoicemailContract.EXTRA_VOICEMAIL_SMS_PREFIX);
83 
84         if (eventType.equals(OmtpConstants.STATUS_SMS_PREFIX)) {
85             mFuture.complete(intent.getBundleExtra(VoicemailContract.EXTRA_VOICEMAIL_SMS_FIELDS));
86             return;
87         }
88 
89         if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) {
90             return;
91         }
92 
93         VvmLog.i(TAG, "VVM SMS with event " + eventType
94                 + " received, attempting to translate to STATUS SMS");
95         OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(context, subId);
96         VisualVoicemailProtocol protocol = helper.getProtocol();
97         if (protocol == null) {
98             return;
99         }
100         Bundle translatedBundle = protocol.translateStatusSmsBundle(helper, eventType,
101                 intent.getBundleExtra(VoicemailContract.EXTRA_VOICEMAIL_SMS_FIELDS));
102 
103         if (translatedBundle != null) {
104             VvmLog.i(TAG, "Translated to STATUS SMS");
105             mFuture.complete(translatedBundle);
106         }
107     }
108 }
109