1/*
2 * Copyright (C) 2016 Google Inc. All Rights Reserved.
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
17package com.android.phone.vvm.omtp.sms;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Bundle;
22import android.provider.VoicemailContract;
23import android.telecom.PhoneAccountHandle;
24
25import com.android.internal.telephony.Phone;
26import com.android.phone.PhoneUtils;
27import com.android.phone.vvm.omtp.OmtpConstants;
28import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
29import com.android.phone.vvm.omtp.VvmLog;
30
31/**
32 * Class ot handle voicemail SMS under legacy mode
33 *
34 * @see OmtpVvmCarrierConfigHelper#isLegacyModeEnabled()
35 */
36public class LegacyModeSmsHandler {
37
38    private static final String TAG = "LegacyModeSmsHandler";
39
40    public static void handle(Context context, Intent intent, PhoneAccountHandle handle) {
41        VvmLog.v(TAG, "processing VVM SMS on legacy mode");
42        String eventType = intent.getExtras()
43                .getString(VoicemailContract.EXTRA_VOICEMAIL_SMS_PREFIX);
44        Bundle data = intent.getExtras().getBundle(VoicemailContract.EXTRA_VOICEMAIL_SMS_FIELDS);
45
46        if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) {
47            SyncMessage message = new SyncMessage(data);
48            VvmLog.v(TAG, "Received SYNC sms for " + handle.getId() +
49                    " with event " + message.getSyncTriggerEvent());
50
51            switch (message.getSyncTriggerEvent()) {
52                case OmtpConstants.NEW_MESSAGE:
53                case OmtpConstants.MAILBOX_UPDATE:
54                    // The user has called into the voicemail and the new message count could
55                    // change.
56                    // For some carriers new message count could be set to 0 even if there are still
57                    // unread messages, to clear the message waiting indicator.
58                    VvmLog.v(TAG, "updating MWI");
59                    Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(handle);
60                    // Setting voicemail message count to non-zero will show the telephony voicemail
61                    // notification, and zero will clear it.
62                    phone.setVoiceMessageCount(message.getNewMessageCount());
63                    break;
64                default:
65                    break;
66            }
67        }
68    }
69}
70