Home | History | Annotate | Download | only in wifi
      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.wifi;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.DialogInterface.OnClickListener;
     24 import android.content.Intent;
     25 import android.support.annotation.NonNull;
     26 import android.telecom.DisconnectCause;
     27 import android.util.Pair;
     28 import com.android.dialer.common.Assert;
     29 import com.android.dialer.common.LogUtil;
     30 
     31 /** Prompts the user to enable Wi-Fi calling. */
     32 public class EnableWifiCallingPrompt {
     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   public static boolean shouldShowPrompt(@NonNull DisconnectCause cause) {
     42     Assert.isNotNull(cause);
     43     if (cause.getReason() != null && cause.getReason().startsWith(REASON_WIFI_ON_BUT_WFC_OFF)) {
     44       LogUtil.i(
     45           "EnableWifiCallingPrompt.shouldShowPrompt",
     46           "showing prompt for disconnect cause: %s",
     47           cause);
     48       return true;
     49     }
     50     return false;
     51   }
     52 
     53   @NonNull
     54   public static Pair<Dialog, CharSequence> createDialog(
     55       final @NonNull Context context, @NonNull DisconnectCause cause) {
     56     Assert.isNotNull(context);
     57     Assert.isNotNull(cause);
     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                 new OnClickListener() {
     65                   @Override
     66                   public void onClick(DialogInterface dialog, int which) {
     67                     openWifiCallingSettings(context);
     68                   }
     69                 })
     70             .setNegativeButton(android.R.string.cancel, null)
     71             .create();
     72     return new Pair<Dialog, CharSequence>(dialog, message);
     73   }
     74 
     75   private static void openWifiCallingSettings(@NonNull Context context) {
     76     LogUtil.i("EnableWifiCallingPrompt.openWifiCallingSettings", "opening settings");
     77     context.startActivity(
     78         new Intent(ACTION_WIFI_CALLING_SETTINGS).setPackage(ANDROID_SETTINGS_PACKAGE));
     79   }
     80 
     81   private EnableWifiCallingPrompt() {}
     82 }
     83