Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 66c70984 authored by Brad Ebinger's avatar Brad Ebinger
Browse files

Add new DialogFragment and Controller for capability discovery opt-in

Adds a new controller to monitor the capability discovery opt-in
setting as well as a new DialogFragment, which displays a dialog
providing the user with more information before they enable the
setting.

Also removes multiple updateSubscriptions() happening when the
activity is first created from onStart() and onChanged() callbacks.

Bug: 111305845
Test: manual
Change-Id: I70821964bc618c3c389c9039cd7f5028e34c7ebb
parent 8b031276
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -137,6 +137,8 @@
                  android:theme="@style/Theme.Settings.Home"
                  android:launchMode="singleTask">
            <intent-filter android:priority="1">
                <!-- Displays the MobileNetworkActivity and opt-in dialog for capability discovery. -->
                <action android:name="android.telephony.ims.action.SHOW_CAPABILITY_DISCOVERY_OPT_IN" />
                <action android:name="android.settings.NETWORK_OPERATOR_SETTINGS" />
                <action android:name="android.settings.DATA_ROAMING_SETTINGS" />
                <action android:name="android.settings.MMS_MESSAGE_SETTING" />
+13 −0
Original line number Diff line number Diff line
@@ -7204,6 +7204,19 @@
    <string name="enhanced_4g_lte_mode_summary">Use LTE services to improve voice and other communications (recommended)</string>
    <!-- Enhaced 4G LTE Mode summary for 4g calling.  [CHAR LIMIT=100] -->
    <string name="enhanced_4g_lte_mode_summary_4g_calling">Use 4G services to improve voice and other communications (recommended)</string>
    <!-- Title of a preference determining whether or not the user has enabled contact discovery,
         which is a service that uses the phone numbers in your contacts to determine if your
         contacts support advanced calling features, such as video calling. [CHAR LIMIT=50]-->
    <string name="contact_discovery_opt_in_title">Contact discovery</string>
    <!-- Summary of a preference determining whether or not the user has enabled contact discovery.
         [CHAR LIMIT=100] -->
    <string name="contact_discovery_opt_in_summary">Allows your carrier to discover which calling features your contacts support.</string>
    <!-- Title of the dialog shown when the user tries to enable Contact Discovery.
         [CHAR LIMIT=50] -->
    <string name="contact_discovery_opt_in_dialog_title">Enable contact discovery?</string>
    <!-- Text displayed in the dialog shown when the user tries to enable Contact Discovery.
         [CHAR LIMIT=NONE]-->
    <string name="contact_discovery_opt_in_dialog_message">Enabling this feature will allow your carrier access to phone numbers in your contacts in order to discover which calling features they support.</string>
    <!-- Preferred network type title.  [CHAR LIMIT=50] -->
    <string name="preferred_network_type_title">Preferred network type</string>
    <!-- Preferred network type summary.  [CHAR LIMIT=100] -->
+7 −0
Original line number Diff line number Diff line
@@ -116,6 +116,13 @@
            settings:keywords="@string/keywords_enhance_4g_lte"
            settings:controller="com.android.settings.network.telephony.Enhanced4gAdvancedCallingPreferenceController"/>

        <SwitchPreference
            android:key="contact_discovery_opt_in"
            android:title="@string/contact_discovery_opt_in_title"
            android:persistent="false"
            android:summary="@string/contact_discovery_opt_in_summary"
            settings:controller="com.android.settings.network.telephony.ContactDiscoveryPreferenceController"/>

        <ListPreference
            android:key="preferred_network_mode_key"
            android:title="@string/preferred_network_mode_title"
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.telephony.ims.ImsManager;
import android.telephony.ims.ImsRcsManager;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;

import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;

/**
 * Fragment for the "Contact Discovery" dialog that appears when the user enables
 * "Contact Discovery" in MobileNetworkSettings or an application starts MobileNetworkSettings with
 * {@link ImsRcsManager#ACTION_SHOW_CAPABILITY_DISCOVERY_OPT_IN}.
 */
public class ContactDiscoveryDialogFragment extends InstrumentedDialogFragment
        implements DialogInterface.OnClickListener {

    private static final String SUB_ID_KEY = "sub_id_key";
    private static final String DIALOG_TAG = "discovery_dialog:";

    private int mSubId;
    private ImsManager mImsManager;

    /**
     * Create a new Fragment, which will create a new Dialog when
     * {@link #show(FragmentManager, String)} is called.
     * @param subId The subscription ID to associate with this Dialog.
     * @return a new instance of ContactDiscoveryDialogFragment.
     */
    public static ContactDiscoveryDialogFragment newInstance(int subId) {
        final ContactDiscoveryDialogFragment dialogFragment = new ContactDiscoveryDialogFragment();
        final Bundle args = new Bundle();
        args.putInt(SUB_ID_KEY, subId);
        dialogFragment.setArguments(args);

        return dialogFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        final Bundle args = getArguments();
        mSubId = args.getInt(SUB_ID_KEY);
        mImsManager = getImsManager(context);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        final int title = R.string.contact_discovery_opt_in_dialog_title;
        int message = R.string.contact_discovery_opt_in_dialog_message;
        builder.setMessage(getResources().getString(message))
                .setTitle(title)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setPositiveButton(android.R.string.ok, this)
                .setNegativeButton(android.R.string.cancel, this);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // let the host know that the positive button has been clicked
        if (which == dialog.BUTTON_POSITIVE) {
            MobileNetworkUtils.setContactDiscoveryEnabled(mImsManager, mSubId, true /*isEnabled*/);
        }
    }

    @Override
    public int getMetricsCategory() {
        return METRICS_CATEGORY_UNKNOWN;
    }

    @VisibleForTesting
    public ImsManager getImsManager(Context context) {
        return context.getSystemService(ImsManager.class);
    }

    public static String getFragmentTag(int subId) {
        return DIALOG_TAG + subId;
    }
}
+136 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.PersistableBundle;
import android.provider.Telephony;
import android.telephony.CarrierConfigManager;
import android.telephony.ims.ImsManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;


/**
 * Controller for the "Contact Discovery" option present in MobileNetworkSettings.
 */
public class ContactDiscoveryPreferenceController extends TelephonyTogglePreferenceController
        implements LifecycleObserver {
    private static final String TAG = "ContactDiscoveryPref";
    private static final Uri UCE_URI = Uri.withAppendedPath(Telephony.SimInfo.CONTENT_URI,
            Telephony.SimInfo.IMS_RCS_UCE_ENABLED);

    private ImsManager mImsManager;
    private CarrierConfigManager mCarrierConfigManager;
    private ContentObserver mUceSettingObserver;
    private FragmentManager mFragmentManager;

    @VisibleForTesting
    public Preference preference;

    public ContactDiscoveryPreferenceController(Context context, String key) {
        super(context, key);
        mImsManager = mContext.getSystemService(ImsManager.class);
        mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class);
    }

    public ContactDiscoveryPreferenceController init(FragmentManager fragmentManager, int subId,
            Lifecycle lifecycle) {
        mFragmentManager = fragmentManager;
        mSubId = subId;
        lifecycle.addObserver(this);
        return this;
    }

    @Override
    public boolean isChecked() {
        return MobileNetworkUtils.isContactDiscoveryEnabled(mImsManager, mSubId);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        registerUceObserver();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        unregisterUceObserver();
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        if (isChecked) {
            showContentDiscoveryDialog();
            // launch dialog and wait for activity to return and ContentObserver to fire to update.
            return false;
        }
        MobileNetworkUtils.setContactDiscoveryEnabled(mImsManager, mSubId, false /*isEnabled*/);
        return true;
    }

    @Override
    public int getAvailabilityStatus(int subId) {
        PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId);
        boolean shouldShowPresence = bundle.getBoolean(
                CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false /*default*/);
        return shouldShowPresence ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        preference = screen.findPreference(getPreferenceKey());
    }

    private void registerUceObserver() {
        mUceSettingObserver = new ContentObserver(mContext.getMainThreadHandler()) {
            @Override
            public void onChange(boolean selfChange) {
                onChange(selfChange, null /*uri*/);
            }

            @Override
            public void onChange(boolean selfChange, Uri uri) {
                Log.d(TAG, "UCE setting changed, re-evaluating.");
                SwitchPreference switchPref = (SwitchPreference) preference;
                switchPref.setChecked(isChecked());
            }
        };
        mContext.getContentResolver().registerContentObserver(UCE_URI, true /*notifyForDecendants*/,
                mUceSettingObserver);
    }

    private void unregisterUceObserver() {
        mContext.getContentResolver().unregisterContentObserver(mUceSettingObserver);
    }

    private void showContentDiscoveryDialog() {
        ContactDiscoveryDialogFragment dialog = ContactDiscoveryDialogFragment.newInstance(
                mSubId);
        dialog.show(mFragmentManager, ContactDiscoveryDialogFragment.getFragmentTag(mSubId));
    }
}
Loading