summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/call/ExternalCallList.java
blob: f104dfe9f9808267b5004d9966a5451cd1e1dc16 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2017 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.incallui.call;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.telecom.Call;
import android.util.ArraySet;
import com.android.contacts.common.compat.CallCompat;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Tracks the external calls known to the InCall UI.
 *
 * <p>External calls are those with {@code android.telecom.Call.Details#PROPERTY_IS_EXTERNAL_CALL}.
 */
public class ExternalCallList {

  private final Set<Call> mExternalCalls = new ArraySet<>();
  private final Set<ExternalCallListener> mExternalCallListeners =
      Collections.newSetFromMap(new ConcurrentHashMap<ExternalCallListener, Boolean>(8, 0.9f, 1));
  /** Handles {@link android.telecom.Call.Callback} callbacks. */
  private final Call.Callback mTelecomCallCallback =
      new Call.Callback() {
        @Override
        public void onDetailsChanged(Call call, Call.Details details) {
          notifyExternalCallUpdated(call);
        }
      };

  /** Begins tracking an external call and notifies listeners of the new call. */
  public void onCallAdded(Call telecomCall) {
    Assert.checkArgument(
        telecomCall.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL));
    mExternalCalls.add(telecomCall);
    telecomCall.registerCallback(mTelecomCallCallback, new Handler(Looper.getMainLooper()));
    notifyExternalCallAdded(telecomCall);
  }

  /** Stops tracking an external call and notifies listeners of the removal of the call. */
  public void onCallRemoved(Call telecomCall) {
    if (!mExternalCalls.contains(telecomCall)) {
      // This can happen on M for external calls from blocked numbers
      LogUtil.i("ExternalCallList.onCallRemoved", "attempted to remove unregistered call");
      return;
    }
    mExternalCalls.remove(telecomCall);
    telecomCall.unregisterCallback(mTelecomCallCallback);
    notifyExternalCallRemoved(telecomCall);
  }

  /** Adds a new listener to external call events. */
  public void addExternalCallListener(@NonNull ExternalCallListener listener) {
    mExternalCallListeners.add(listener);
  }

  /** Removes a listener to external call events. */
  public void removeExternalCallListener(@NonNull ExternalCallListener listener) {
    if (!mExternalCallListeners.contains(listener)) {
      LogUtil.i(
          "ExternalCallList.removeExternalCallListener",
          "attempt to remove unregistered listener.");
    }
    mExternalCallListeners.remove(listener);
  }

  public boolean isCallTracked(@NonNull android.telecom.Call telecomCall) {
    return mExternalCalls.contains(telecomCall);
  }

  /** Notifies listeners of the addition of a new external call. */
  private void notifyExternalCallAdded(Call call) {
    for (ExternalCallListener listener : mExternalCallListeners) {
      listener.onExternalCallAdded(call);
    }
  }

  /** Notifies listeners of the removal of an external call. */
  private void notifyExternalCallRemoved(Call call) {
    for (ExternalCallListener listener : mExternalCallListeners) {
      listener.onExternalCallRemoved(call);
    }
  }

  /** Notifies listeners of changes to an external call. */
  private void notifyExternalCallUpdated(Call call) {
    if (!call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
      // A previous external call has been pulled and is now a regular call, so we will remove
      // it from the external call listener and ensure that the CallList is informed of the
      // change.
      onCallRemoved(call);

      for (ExternalCallListener listener : mExternalCallListeners) {
        listener.onExternalCallPulled(call);
      }
    } else {
      for (ExternalCallListener listener : mExternalCallListeners) {
        listener.onExternalCallUpdated(call);
      }
    }
  }

  /**
   * Defines events which the {@link ExternalCallList} exposes to interested components (e.g. {@link
   * com.android.incallui.ExternalCallNotifier ExternalCallNotifier}).
   */
  public interface ExternalCallListener {

    void onExternalCallAdded(Call call);

    void onExternalCallRemoved(Call call);

    void onExternalCallUpdated(Call call);

    void onExternalCallPulled(Call call);
  }
}