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