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.content.DialogInterface.OnClickListener;
23 import android.content.Intent;
24 import android.support.annotation.NonNull;
25 import android.telecom.DisconnectCause;
26 import android.util.Pair;
27 import com.android.dialer.common.Assert;
28 import com.android.dialer.common.LogUtil;
29 import com.android.incallui.call.DialerCall;
30 
31 /** Prompts the user to enable Wi-Fi calling. */
32 public class EnableWifiCallingPrompt implements DisconnectDialog {
33   // This is a hidden constant in android.telecom.DisconnectCause. Telecom sets this as a disconnect
34   // reason if it wants us to prompt the user to enable Wi-Fi calling. In Android-O we might
35   // consider using a more explicit way to signal this.
36   private static final String REASON_WIFI_ON_BUT_WFC_OFF = "REASON_WIFI_ON_BUT_WFC_OFF";
37   private static final String ACTION_WIFI_CALLING_SETTINGS =
38       "android.settings.WIFI_CALLING_SETTINGS";
39   private static final String ANDROID_SETTINGS_PACKAGE = "com.android.settings";
40 
41   @Override
shouldShow(DisconnectCause disconnectCause)42   public boolean shouldShow(DisconnectCause disconnectCause) {
43     String reason = disconnectCause.getReason();
44     if (reason != null && reason.startsWith(REASON_WIFI_ON_BUT_WFC_OFF)) {
45       LogUtil.i(
46           "EnableWifiCallingPrompt.shouldShowPrompt",
47           "showing prompt for disconnect cause: %s",
48           reason);
49       return true;
50     }
51     return false;
52   }
53 
54   @Override
createDialog(final @NonNull Context context, DialerCall call)55   public Pair<Dialog, CharSequence> createDialog(final @NonNull Context context, DialerCall call) {
56     Assert.isNotNull(context);
57     DisconnectCause cause = call.getDisconnectCause();
58     CharSequence message = cause.getDescription();
59     Dialog dialog =
60         new AlertDialog.Builder(context)
61             .setMessage(message)
62             .setPositiveButton(
63                 R.string.incall_enable_wifi_calling_button,
64                 (OnClickListener) (dialog1, which) -> openWifiCallingSettings(context))
65             .setNegativeButton(android.R.string.cancel, null)
66             .create();
67     return new Pair<>(dialog, message);
68   }
69 
openWifiCallingSettings(@onNull Context context)70   private static void openWifiCallingSettings(@NonNull Context context) {
71     LogUtil.i("EnableWifiCallingPrompt.openWifiCallingSettings", "opening settings");
72     context.startActivity(
73         new Intent(ACTION_WIFI_CALLING_SETTINGS).setPackage(ANDROID_SETTINGS_PACKAGE));
74   }
75 }
76