summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/NewReturnToCallController.java
blob: c588dc4e1eaf071accfb4835529c1908a684a533 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * 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;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.telecom.CallAudioState;
import android.text.TextUtils;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.android.dialer.lettertile.LetterTileDrawable;
import com.android.dialer.telecom.TelecomUtil;
import com.android.incallui.ContactInfoCache.ContactCacheEntry;
import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
import com.android.incallui.InCallPresenter.InCallUiListener;
import com.android.incallui.audiomode.AudioModeProvider;
import com.android.incallui.audiomode.AudioModeProvider.AudioModeListener;
import com.android.incallui.call.CallList;
import com.android.incallui.call.CallList.Listener;
import com.android.incallui.call.DialerCall;
import com.android.incallui.speakerbuttonlogic.SpeakerButtonInfo;
import com.android.incallui.speakerbuttonlogic.SpeakerButtonInfo.IconSize;
import com.android.newbubble.NewBubble;
import com.android.newbubble.NewBubbleComponent;
import com.android.newbubble.NewBubbleInfo;
import com.android.newbubble.NewBubbleInfo.Action;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Listens for events relevant to the return-to-call bubble and updates the bubble's state as
 * necessary
 */
public class NewReturnToCallController implements InCallUiListener, Listener, AudioModeListener {

  private final Context context;

  @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
  NewBubble bubble;

  private static Boolean canShowBubblesForTesting = null;

  private CallAudioState audioState;

  private final PendingIntent toggleSpeaker;
  private final PendingIntent showSpeakerSelect;
  private final PendingIntent toggleMute;
  private final PendingIntent endCall;
  private final PendingIntent fullScreen;

  private final ContactInfoCache contactInfoCache;

  public static boolean isEnabled(Context context) {
    return ConfigProviderBindings.get(context).getBoolean("enable_return_to_call_bubble_v2", false);
  }

  public NewReturnToCallController(Context context, ContactInfoCache contactInfoCache) {
    this.context = context;
    this.contactInfoCache = contactInfoCache;

    toggleSpeaker = createActionIntent(NewReturnToCallActionReceiver.ACTION_TOGGLE_SPEAKER);
    showSpeakerSelect =
        createActionIntent(NewReturnToCallActionReceiver.ACTION_SHOW_AUDIO_ROUTE_SELECTOR);
    toggleMute = createActionIntent(NewReturnToCallActionReceiver.ACTION_TOGGLE_MUTE);
    endCall = createActionIntent(NewReturnToCallActionReceiver.ACTION_END_CALL);
    fullScreen = createActionIntent(NewReturnToCallActionReceiver.ACTION_RETURN_TO_CALL);

    InCallPresenter.getInstance().addInCallUiListener(this);
    CallList.getInstance().addListener(this);
    AudioModeProvider.getInstance().addListener(this);
    audioState = AudioModeProvider.getInstance().getAudioState();
  }

  public void tearDown() {
    hide();
    InCallPresenter.getInstance().removeInCallUiListener(this);
    CallList.getInstance().removeListener(this);
    AudioModeProvider.getInstance().removeListener(this);
  }

  @Override
  public void onUiShowing(boolean showing) {
    if (showing) {
      hide();
    } else {
      if (getCall() != null) {
        show();
      }
    }
  }

  private void hide() {
    if (bubble != null) {
      bubble.hide();
    } else {
      LogUtil.i("ReturnToCallController.hide", "hide() called without calling show()");
    }
  }

  private void show() {
    if (bubble == null) {
      bubble = startBubble();
    } else {
      bubble.show();
    }
    startContactInfoSearch();
  }

  /**
   * Determines whether bubbles can be shown based on permissions obtained. This should be checked
   * before attempting to create a Bubble.
   *
   * @return true iff bubbles are able to be shown.
   * @see Settings#canDrawOverlays(Context)
   */
  private static boolean canShowBubbles(@NonNull Context context) {
    return canShowBubblesForTesting != null
        ? canShowBubblesForTesting
        : Settings.canDrawOverlays(context);
  }

  @VisibleForTesting(otherwise = VisibleForTesting.NONE)
  static void setCanShowBubblesForTesting(boolean canShowBubbles) {
    canShowBubblesForTesting = canShowBubbles;
  }

  @VisibleForTesting
  public NewBubble startBubble() {
    if (!canShowBubbles(context)) {
      LogUtil.i("ReturnToCallController.startNewBubble", "can't show bubble, no permission");
      return null;
    }
    NewBubble returnToCallBubble = NewBubbleComponent.get(context).getNewBubble();
    returnToCallBubble.setBubbleInfo(generateBubbleInfo());
    returnToCallBubble.show();
    return returnToCallBubble;
  }

  @Override
  public void onIncomingCall(DialerCall call) {}

  @Override
  public void onUpgradeToVideo(DialerCall call) {}

  @Override
  public void onSessionModificationStateChange(DialerCall call) {}

  @Override
  public void onCallListChange(CallList callList) {}

  @Override
  public void onDisconnect(DialerCall call) {
    LogUtil.enterBlock("ReturnToCallController.onDisconnect");
    if (bubble != null && bubble.isVisible() && (getCall() == null)) {
      // Show "Call ended" and hide bubble when there is no outgoing, active or background call
      LogUtil.i("ReturnToCallController.onDisconnect", "show call ended and hide bubble");
      // Don't show text if it's Duo upgrade
      // It doesn't work for Duo fallback upgrade since we're not considered in call
      if (!TelecomUtil.isInCall(context) || CallList.getInstance().getIncomingCall() != null) {
        bubble.showText(context.getText(R.string.incall_call_ended));
      }
      hide();
    } else {
      startContactInfoSearch();
    }
  }

  @Override
  public void onWiFiToLteHandover(DialerCall call) {}

  @Override
  public void onHandoverToWifiFailed(DialerCall call) {}

  @Override
  public void onInternationalCallOnWifi(@NonNull DialerCall call) {}

  @Override
  public void onAudioStateChanged(CallAudioState audioState) {
    this.audioState = audioState;
    if (bubble != null) {
      bubble.updateActions(generateActions());
    }
  }

  private void startContactInfoSearch() {
    DialerCall dialerCall = getCall();
    if (dialerCall != null) {
      contactInfoCache.findInfo(
          dialerCall, false /* isIncoming */, new ReturnToCallContactInfoCacheCallback(this));
    }
  }

  private DialerCall getCall() {
    DialerCall dialerCall = CallList.getInstance().getOutgoingCall();
    if (dialerCall == null) {
      dialerCall = CallList.getInstance().getActiveOrBackgroundCall();
    }
    return dialerCall;
  }

  private void onPhotoAvatarReceived(@NonNull Drawable photo) {
    if (bubble != null) {
      bubble.updatePhotoAvatar(photo);
    }
  }

  private void onLetterTileAvatarReceived(@NonNull Drawable photo) {
    if (bubble != null) {
      bubble.updateAvatar(photo);
    }
  }

  private NewBubbleInfo generateBubbleInfo() {
    return NewBubbleInfo.builder()
        .setPrimaryColor(context.getResources().getColor(R.color.dialer_theme_color, null))
        .setPrimaryIcon(Icon.createWithResource(context, R.drawable.on_going_call))
        .setStartingYPosition(
            context.getResources().getDimensionPixelOffset(R.dimen.return_to_call_initial_offset_y))
        .setActions(generateActions())
        .build();
  }

  @NonNull
  private List<Action> generateActions() {
    List<Action> actions = new ArrayList<>();
    SpeakerButtonInfo speakerButtonInfo = new SpeakerButtonInfo(audioState, IconSize.SIZE_24_DP);

    // Return to call
    actions.add(
        Action.builder()
            .setIconDrawable(
                context.getDrawable(R.drawable.quantum_ic_exit_to_app_flip_vd_theme_24))
            .setIntent(fullScreen)
            .setName(context.getText(R.string.bubble_return_to_call))
            .setCheckable(false)
            .build());
    // Mute/unmute
    actions.add(
        Action.builder()
            .setIconDrawable(context.getDrawable(R.drawable.quantum_ic_mic_off_white_24))
            .setChecked(audioState.isMuted())
            .setIntent(toggleMute)
            .setName(context.getText(R.string.incall_label_mute))
            .build());
    // Speaker/audio selector
    actions.add(
        Action.builder()
            .setIconDrawable(context.getDrawable(speakerButtonInfo.icon))
            .setSecondaryIconDrawable(
                speakerButtonInfo.checkable
                    ? null
                    : context.getDrawable(R.drawable.quantum_ic_arrow_drop_down_vd_theme_24))
            .setName(context.getText(speakerButtonInfo.label))
            .setCheckable(speakerButtonInfo.checkable)
            .setChecked(speakerButtonInfo.isChecked)
            .setIntent(speakerButtonInfo.checkable ? toggleSpeaker : showSpeakerSelect)
            .build());
    // End call
    actions.add(
        Action.builder()
            .setIconDrawable(context.getDrawable(R.drawable.quantum_ic_call_end_vd_theme_24))
            .setIntent(endCall)
            .setName(context.getText(R.string.incall_label_end_call))
            .setCheckable(false)
            .build());
    return actions;
  }

  @NonNull
  private PendingIntent createActionIntent(String action) {
    Intent intent = new Intent(context, NewReturnToCallActionReceiver.class);
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
  }

  @NonNull
  private LetterTileDrawable createLettleTileDrawable(
      DialerCall dialerCall, ContactCacheEntry entry) {
    String preferredName =
        ContactDisplayUtils.getPreferredDisplayName(
            entry.namePrimary,
            entry.nameAlternative,
            ContactsPreferencesFactory.newContactsPreferences(context));
    if (TextUtils.isEmpty(preferredName)) {
      preferredName = entry.number;
    }

    LetterTileDrawable letterTile = new LetterTileDrawable(context.getResources());
    letterTile.setCanonicalDialerLetterTileDetails(
        dialerCall.updateNameIfRestricted(preferredName),
        entry.lookupKey,
        LetterTileDrawable.SHAPE_CIRCLE,
        LetterTileDrawable.getContactTypeFromPrimitives(
            dialerCall.isVoiceMailNumber(),
            dialerCall.isSpam(),
            entry.isBusiness,
            dialerCall.getNumberPresentation(),
            dialerCall.isConferenceCall()));
    return letterTile;
  }

  private static class ReturnToCallContactInfoCacheCallback implements ContactInfoCacheCallback {

    private final WeakReference<NewReturnToCallController> newReturnToCallControllerWeakReference;

    private ReturnToCallContactInfoCacheCallback(
        NewReturnToCallController newReturnToCallController) {
      newReturnToCallControllerWeakReference = new WeakReference<>(newReturnToCallController);
    }

    @Override
    public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
      NewReturnToCallController newReturnToCallController =
          newReturnToCallControllerWeakReference.get();
      if (newReturnToCallController == null) {
        return;
      }
      if (entry.photo != null) {
        newReturnToCallController.onPhotoAvatarReceived(entry.photo);
      } else {
        DialerCall dialerCall = CallList.getInstance().getCallById(callId);
        if (dialerCall != null) {
          newReturnToCallController.onLetterTileAvatarReceived(
              newReturnToCallController.createLettleTileDrawable(dialerCall, entry));
        }
      }
    }

    @Override
    public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
      NewReturnToCallController newReturnToCallController =
          newReturnToCallControllerWeakReference.get();
      if (newReturnToCallController == null) {
        return;
      }
      if (entry.photo != null) {
        newReturnToCallController.onPhotoAvatarReceived(entry.photo);
      } else {
        DialerCall dialerCall = CallList.getInstance().getCallById(callId);
        if (dialerCall != null) {
          newReturnToCallController.onLetterTileAvatarReceived(
              newReturnToCallController.createLettleTileDrawable(dialerCall, entry));
        }
      }
    }
  }
}