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