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.incallui.disconnectdialog;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.support.annotation.NonNull;
23 import android.telecom.DisconnectCause;
24 import android.telecom.PhoneAccountHandle;
25 import android.util.Pair;
26 import com.android.contacts.common.compat.telecom.TelecomManagerCompat;
27 import com.android.dialer.callintent.CallInitiationType;
28 import com.android.dialer.callintent.CallIntentBuilder;
29 import com.android.dialer.common.LogUtil;
30 import com.android.dialer.precall.PreCall;
31 import com.android.incallui.call.DialerCall;
32 
33 /** Prompt user to make voice call if video call is not currently available. */
34 public class VideoCallNotAvailablePrompt implements DisconnectDialog {
35 
36   @Override
shouldShow(DisconnectCause disconnectCause)37   public boolean shouldShow(DisconnectCause disconnectCause) {
38     if (disconnectCause.getCode() == DisconnectCause.ERROR
39         && TelecomManagerCompat.REASON_IMS_ACCESS_BLOCKED.equals(disconnectCause.getReason())) {
40       LogUtil.i(
41           "VideoCallNotAvailablePrompt.shouldShowPrompt",
42           "showing prompt for disconnect cause: %s",
43           disconnectCause.getReason());
44       return true;
45     } else {
46       return false;
47     }
48   }
49 
50   @Override
createDialog(@onNull Context context, DialerCall call)51   public Pair<Dialog, CharSequence> createDialog(@NonNull Context context, DialerCall call) {
52     CharSequence title = context.getString(R.string.video_call_not_available_title);
53 
54     Dialog dialog =
55         new AlertDialog.Builder(context)
56             .setTitle(title)
57             .setMessage(context.getString(R.string.video_call_not_available_message))
58             .setPositiveButton(
59                 R.string.voice_call,
60                 (dialog1, which) ->
61                     makeVoiceCall(context, call.getNumber(), call.getAccountHandle()))
62             .setNegativeButton(android.R.string.cancel, null)
63             .create();
64     return new Pair<>(dialog, title);
65   }
66 
makeVoiceCall(Context context, String number, PhoneAccountHandle accountHandle)67   private void makeVoiceCall(Context context, String number, PhoneAccountHandle accountHandle) {
68     LogUtil.enterBlock("VideoCallNotAvailablePrompt.makeVoiceCall");
69     PreCall.start(
70         context,
71         new CallIntentBuilder(number, CallInitiationType.Type.IMS_VIDEO_BLOCKED_FALLBACK_TO_VOICE)
72             .setPhoneAccountHandle(accountHandle));
73   }
74 }
75