1 /* 2 * Copyright (C) 2020 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.settings.network.telephony; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.os.Bundle; 24 import android.telephony.ims.ImsManager; 25 import android.text.TextUtils; 26 27 import androidx.annotation.VisibleForTesting; 28 29 import com.android.settings.R; 30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 31 32 /** 33 * Fragment for the "Contact Discovery" dialog that appears when the user enables 34 * "Contact Discovery" in MobileNetworkSettings or an application starts MobileNetworkSettings with 35 * {@link ImsRcsManager#ACTION_SHOW_CAPABILITY_DISCOVERY_OPT_IN}. 36 */ 37 public class ContactDiscoveryDialogFragment extends InstrumentedDialogFragment 38 implements DialogInterface.OnClickListener { 39 40 private static final String SUB_ID_KEY = "sub_id_key"; 41 private static final String CARRIER_NAME_KEY = "carrier_name_key"; 42 private static final String DIALOG_TAG = "discovery_dialog:"; 43 44 private int mSubId; 45 private CharSequence mCarrierName; 46 private ImsManager mImsManager; 47 48 /** 49 * Create a new Fragment, which will create a new Dialog when 50 * {@link #show(FragmentManager, String)} is called. 51 * @param subId The subscription ID to associate with this Dialog. 52 * @return a new instance of ContactDiscoveryDialogFragment. 53 */ newInstance(int subId, CharSequence carrierName)54 public static ContactDiscoveryDialogFragment newInstance(int subId, CharSequence carrierName) { 55 final ContactDiscoveryDialogFragment dialogFragment = new ContactDiscoveryDialogFragment(); 56 final Bundle args = new Bundle(); 57 args.putInt(SUB_ID_KEY, subId); 58 args.putCharSequence(CARRIER_NAME_KEY, carrierName); 59 dialogFragment.setArguments(args); 60 61 return dialogFragment; 62 } 63 64 @Override onAttach(Context context)65 public void onAttach(Context context) { 66 super.onAttach(context); 67 final Bundle args = getArguments(); 68 mSubId = args.getInt(SUB_ID_KEY); 69 mCarrierName = args.getCharSequence(CARRIER_NAME_KEY); 70 mImsManager = getImsManager(context); 71 } 72 73 @Override onCreateDialog(Bundle savedInstanceState)74 public Dialog onCreateDialog(Bundle savedInstanceState) { 75 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 76 CharSequence title; 77 CharSequence message; 78 if (!TextUtils.isEmpty(mCarrierName)) { 79 title = getContext().getString( 80 R.string.contact_discovery_opt_in_dialog_title, mCarrierName); 81 message = getContext().getString( 82 R.string.contact_discovery_opt_in_dialog_message, mCarrierName); 83 } else { 84 title = getContext().getString( 85 R.string.contact_discovery_opt_in_dialog_title_no_carrier_defined); 86 message = getContext().getString( 87 R.string.contact_discovery_opt_in_dialog_message_no_carrier_defined); 88 } 89 builder.setMessage(message) 90 .setTitle(title) 91 .setIconAttribute(android.R.attr.alertDialogIcon) 92 .setPositiveButton(R.string.confirmation_turn_on, this) 93 .setNegativeButton(android.R.string.cancel, this); 94 return builder.create(); 95 } 96 97 @Override onClick(DialogInterface dialog, int which)98 public void onClick(DialogInterface dialog, int which) { 99 // let the host know that the positive button has been clicked 100 if (which == dialog.BUTTON_POSITIVE) { 101 MobileNetworkUtils.setContactDiscoveryEnabled(mImsManager, mSubId, true /*isEnabled*/); 102 } 103 } 104 105 @Override getMetricsCategory()106 public int getMetricsCategory() { 107 return METRICS_CATEGORY_UNKNOWN; 108 } 109 110 @VisibleForTesting getImsManager(Context context)111 public ImsManager getImsManager(Context context) { 112 return context.getSystemService(ImsManager.class); 113 } 114 getFragmentTag(int subId)115 public static String getFragmentTag(int subId) { 116 return DIALOG_TAG + subId; 117 } 118 } 119