• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.voicemail.impl;
18 
19 import android.annotation.TargetApi;
20 import android.content.Intent;
21 import android.os.Build.VERSION_CODES;
22 import android.os.UserManager;
23 import android.telecom.PhoneAccountHandle;
24 import android.telephony.VisualVoicemailService;
25 import android.telephony.VisualVoicemailSms;
26 import com.android.dialer.logging.DialerImpression;
27 import com.android.dialer.logging.Logger;
28 import com.android.voicemail.VoicemailComponent;
29 import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
30 import com.android.voicemail.impl.sms.LegacyModeSmsHandler;
31 import com.android.voicemail.impl.sync.VvmAccountManager;
32 
33 /** Implements {@link VisualVoicemailService} to receive visual voicemail events */
34 @TargetApi(VERSION_CODES.O)
35 public class OmtpService extends VisualVoicemailService {
36 
37   private static final String TAG = "VvmOmtpService";
38 
39   public static final String ACTION_SMS_RECEIVED = "com.android.vociemailomtp.sms.sms_received";
40 
41   public static final String EXTRA_VOICEMAIL_SMS = "extra_voicemail_sms";
42 
43   @Override
onCellServiceConnected( VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle)44   public void onCellServiceConnected(
45       VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle) {
46     VvmLog.i(TAG, "onCellServiceConnected");
47     if (!isModuleEnabled()) {
48       VvmLog.e(TAG, "onCellServiceConnected received when module is disabled");
49       task.finish();
50       return;
51     }
52 
53     if (!isUserUnlocked()) {
54       VvmLog.i(TAG, "onCellServiceConnected: user locked");
55       task.finish();
56       return;
57     }
58 
59     if (!isServiceEnabled(phoneAccountHandle)) {
60       task.finish();
61       return;
62     }
63 
64     Logger.get(this).logImpression(DialerImpression.Type.VVM_UNBUNDLED_EVENT_RECEIVED);
65     ActivationTask.start(OmtpService.this, phoneAccountHandle, null);
66     task.finish();
67   }
68 
69   @Override
onSmsReceived(VisualVoicemailTask task, final VisualVoicemailSms sms)70   public void onSmsReceived(VisualVoicemailTask task, final VisualVoicemailSms sms) {
71     VvmLog.i(TAG, "onSmsReceived");
72     if (!isModuleEnabled()) {
73       VvmLog.e(TAG, "onSmsReceived received when module is disabled");
74       task.finish();
75       return;
76     }
77 
78     if (!isUserUnlocked()) {
79       LegacyModeSmsHandler.handle(this, sms);
80       return;
81     }
82 
83     if (!isServiceEnabled(sms.getPhoneAccountHandle())) {
84       task.finish();
85       return;
86     }
87 
88     // isUserUnlocked() is not checked. OmtpMessageReceiver will handle the locked case.
89 
90     Logger.get(this).logImpression(DialerImpression.Type.VVM_UNBUNDLED_EVENT_RECEIVED);
91     Intent intent = new Intent(ACTION_SMS_RECEIVED);
92     intent.setPackage(getPackageName());
93     intent.putExtra(EXTRA_VOICEMAIL_SMS, sms);
94     sendBroadcast(intent);
95     task.finish();
96   }
97 
98   @Override
onSimRemoved( final VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle)99   public void onSimRemoved(
100       final VisualVoicemailTask task, final PhoneAccountHandle phoneAccountHandle) {
101     VvmLog.i(TAG, "onSimRemoved");
102     if (!isModuleEnabled()) {
103       VvmLog.e(TAG, "onSimRemoved called when module is disabled");
104       task.finish();
105       return;
106     }
107 
108     if (!isUserUnlocked()) {
109       VvmLog.i(TAG, "onSimRemoved: user locked");
110       task.finish();
111       return;
112     }
113 
114     Logger.get(this).logImpression(DialerImpression.Type.VVM_UNBUNDLED_EVENT_RECEIVED);
115     VvmAccountManager.removeAccount(this, phoneAccountHandle);
116     task.finish();
117   }
118 
119   @Override
onStopped(VisualVoicemailTask task)120   public void onStopped(VisualVoicemailTask task) {
121     VvmLog.i(TAG, "onStopped");
122     if (!isModuleEnabled()) {
123       VvmLog.e(TAG, "onStopped called when module is disabled");
124       task.finish();
125       return;
126     }
127     if (!isUserUnlocked()) {
128       VvmLog.i(TAG, "onStopped: user locked");
129       task.finish();
130       return;
131     }
132     Logger.get(this).logImpression(DialerImpression.Type.VVM_UNBUNDLED_EVENT_RECEIVED);
133   }
134 
isModuleEnabled()135   private boolean isModuleEnabled() {
136     return VoicemailComponent.get(this).getVoicemailClient().isVoicemailModuleEnabled();
137   }
138 
isServiceEnabled(PhoneAccountHandle phoneAccountHandle)139   private boolean isServiceEnabled(PhoneAccountHandle phoneAccountHandle) {
140     OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(this, phoneAccountHandle);
141     if (!config.isValid()) {
142       VvmLog.i(TAG, "VVM not supported on " + phoneAccountHandle);
143       return false;
144     }
145     if (!VisualVoicemailSettingsUtil.isEnabled(this, phoneAccountHandle)
146         && !config.isLegacyModeEnabled()) {
147       VvmLog.i(TAG, "VVM is disabled");
148       return false;
149     }
150     return true;
151   }
152 
isUserUnlocked()153   private boolean isUserUnlocked() {
154     UserManager userManager = getSystemService(UserManager.class);
155     return userManager.isUserUnlocked();
156   }
157 }
158