summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllog/datasources/voicemail/VoicemailDataSource.java
blob: 75c05139d42bd23e6c0652bf315d3347edf6c78a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.calllog.datasources.voicemail;

import android.content.ContentValues;
import android.content.Context;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.AnnotatedCallLog;
import com.android.dialer.calllog.datasources.CallLogDataSource;
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.calllog.datasources.util.RowCombiner;
import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.compat.telephony.TelephonyManagerCompat;
import com.android.dialer.telecom.TelecomUtil;
import com.android.dialer.util.PermissionsUtil;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import java.util.Map.Entry;
import javax.inject.Inject;

/** Provide information for whether the call is a call to the voicemail inbox. */
public class VoicemailDataSource implements CallLogDataSource {

  private final ListeningExecutorService backgroundExecutor;

  @Inject
  VoicemailDataSource(@BackgroundExecutor ListeningExecutorService backgroundExecutor) {
    this.backgroundExecutor = backgroundExecutor;
  }

  @Override
  public ListenableFuture<Boolean> isDirty(Context appContext) {
    // The isVoicemail status is immutable and permanent. The call will always show as "Voicemail"
    // even if the SIM is swapped. Dialing the row will result in some unexpected number after a SIM
    // swap but this is deemed acceptable.
    return Futures.immediateFuture(false);
  }

  @Override
  @SuppressWarnings("missingPermission")
  public ListenableFuture<Void> fill(Context appContext, CallLogMutations mutations) {
    if (!PermissionsUtil.hasReadPhoneStatePermissions(appContext)) {
      return Futures.immediateFuture(null);
    }
    return backgroundExecutor.submit(
        () -> {
          TelecomManager telecomManager = appContext.getSystemService(TelecomManager.class);
          for (Entry<Long, ContentValues> insert : mutations.getInserts().entrySet()) {
            ContentValues values = insert.getValue();
            PhoneAccountHandle phoneAccountHandle =
                TelecomUtil.composePhoneAccountHandle(
                    values.getAsString(AnnotatedCallLog.PHONE_ACCOUNT_COMPONENT_NAME),
                    values.getAsString(AnnotatedCallLog.PHONE_ACCOUNT_ID));
            DialerPhoneNumber dialerPhoneNumber;
            try {
              dialerPhoneNumber =
                  DialerPhoneNumber.parseFrom(values.getAsByteArray(AnnotatedCallLog.NUMBER));
            } catch (InvalidProtocolBufferException e) {
              throw new IllegalStateException(e);
            }

            if (telecomManager.isVoiceMailNumber(
                phoneAccountHandle, dialerPhoneNumber.getNormalizedNumber())) {
              values.put(AnnotatedCallLog.IS_VOICEMAIL_CALL, 1);
              TelephonyManager telephonyManager =
                  TelephonyManagerCompat.getTelephonyManagerForPhoneAccountHandle(
                      appContext, phoneAccountHandle);
              values.put(
                  AnnotatedCallLog.VOICEMAIL_CALL_TAG, telephonyManager.getVoiceMailAlphaTag());
            }
          }
          return null;
        });
  }

  @Override
  public ListenableFuture<Void> onSuccessfulFill(Context appContext) {
    return Futures.immediateFuture(null);
  }

  @Override
  public ContentValues coalesce(List<ContentValues> individualRowsSortedByTimestampDesc) {
    return new RowCombiner(individualRowsSortedByTimestampDesc)
        .useMostRecentInt(AnnotatedCallLog.IS_VOICEMAIL_CALL)
        .useMostRecentString(AnnotatedCallLog.VOICEMAIL_CALL_TAG)
        .combine();
  }

  @Override
  public void registerContentObservers(Context appContext) {}

  @Override
  public void unregisterContentObservers(Context appContext) {}

  @Override
  public ListenableFuture<Void> clearData() {
    return Futures.immediateFuture(null);
  }
}