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

Commit cd3f9e2a authored by Jan Nordqvist's avatar Jan Nordqvist
Browse files

Fall back to Wi-Fi data display in data usage screen with no SIM.

Bug: 70950124

Test: manual
Test: make RunSettingsRoboTests
Change-Id: I06bf78e54119819be87e15baca7e5b6a241958cb
Merged-In: I06bf78e54119819be87e15baca7e5b6a241958cb
parent a8c53f7d
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -8710,6 +8710,9 @@
    <!-- Summary of data usage [CHAR LIMIT=NONE] -->
    <string name="data_usage_summary_format"><xliff:g id="amount" example="50%">%1$s</xliff:g> of data used</string>
    <!-- Summary of data usage [CHAR LIMIT=NONE] -->
    <string name="data_usage_wifi_format"><xliff:g id="amount" example="50%">^1</xliff:g> used on Wi\u2011Fi</string>
    <!-- Summary of notifications [CHAR LIMIT=NONE] -->
    <plurals name="notification_summary">
        <item quantity="one">Off for 1 app</item>
@@ -8947,7 +8950,7 @@
    <string name="app_cellular_data_usage">App data usage</string>
    <!-- Label for wifi data usage in data usage screen [CHAR LIMIT=60] -->
    <string name="wifi_data_usage">Wi-Fi data usage</string>
    <string name="wifi_data_usage">Wi\u2011Fi data usage</string>
    <!-- Label for ethernet data usage in data usage screen [CHAR LIMIT=60] -->
    <string name="ethernet_data_usage">Ethernet data usage</string>
@@ -9040,6 +9043,9 @@
    <!-- Data usage title text [CHAR LIMIT=30] -->
    <string name="data_usage_title">Primary data</string>
    <!-- Alternative data usage title text for Wi-Fi only mode [CHAR LIMIT=30] -->
    <string name="data_usage_wifi_title">Wi\u2011Fi data</string>
    <!-- Data usage remaining string [CHAR LIMIT=30] -->
    <string name="data_used"><xliff:g name="bytes" example="2 GB">^1</xliff:g> used</string>
@@ -9079,6 +9085,9 @@
    <!-- Button to launch external data plan app [CHAR LIMIT=30] -->
    <string name="launch_mdp_app_text">View plan</string>
    <!-- Alternate launch button text for Wi-Fi only mode [CHAR LIMIT=30] -->
    <string name="launch_wifi_text">View details</string>
    <!-- Name of Data Saver screens [CHAR LIMIT=30] -->
    <string name="data_saver_title">Data Saver</string>
+2 −1
Original line number Diff line number Diff line
@@ -375,7 +375,8 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
        return mPreferenceCache != null ? mPreferenceCache.size() : 0;
    }

    protected boolean removePreference(String key) {
    @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
    public boolean removePreference(String key) {
        return removePreference(getPreferenceScreen(), key);
    }

+4 −2
Original line number Diff line number Diff line
@@ -48,13 +48,14 @@ public abstract class DataUsageBaseFragment extends DashboardFragment {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Context context = getActivity();
        Context context = getContext();

        services.mNetworkService = INetworkManagementService.Stub.asInterface(
                ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
        services.mStatsService = INetworkStatsService.Stub.asInterface(
                ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
        services.mPolicyManager = NetworkPolicyManager.from(context);
        services.mPolicyManager = (NetworkPolicyManager)context
                .getSystemService(Context.NETWORK_POLICY_SERVICE);

        services.mPolicyEditor = new NetworkPolicyEditor(services.mPolicyManager);

@@ -100,6 +101,7 @@ public abstract class DataUsageBaseFragment extends DashboardFragment {

    /**
     * Test if device has an ethernet network connection.
     * TODO(b/77590489): Remove this method when DataUsageSummaryLegacy is deprecated.
     */
    public boolean hasEthernet(Context context) {
        if (DataUsageUtils.TEST_RADIOS) {
+33 −20
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@

package com.android.settings.datausage;

import android.util.Log;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
@@ -85,8 +86,7 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        final Context context = getContext();
        Context context = getContext();

        boolean hasMobileData = DataUsageUtils.hasMobileData(context);

@@ -100,18 +100,21 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
        if (!hasMobileData || !isAdmin()) {
            removePreference(KEY_RESTRICT_BACKGROUND);
        }
        boolean hasWifiRadio = DataUsageUtils.hasWifiRadio(context);
        if (hasMobileData) {
            SubscriptionInfo subInfo
                    = services.mSubscriptionManager.getDefaultDataSubscriptionInfo();
            if (subInfo != null) {
                addMobileSection(subInfo.getSubscriptionId());
            }
            addMobileSection(defaultSubId);
            if (DataUsageUtils.hasSim(context) && hasWifiRadio) {
                // If the device has a SIM installed, the data usage section shows usage for mobile,
                // and the WiFi section is added if there is a WiFi radio - legacy behavior.
                addWifiSection();
            }
        boolean hasWifiRadio = DataUsageUtils.hasWifiRadio(context);
        if (hasWifiRadio) {
            // Do not add the WiFi section if either there is no WiFi radio (obviously) or if no
            // SIM is installed. In the latter case the data usage section will show WiFi usage and
            // there should be no explicit WiFi section added.
        } else if (hasWifiRadio) {
            addWifiSection();
        }
        if (hasEthernet(context)) {
        if (DataUsageUtils.hasEthernet(context)) {
            addEthernetSection();
        }
        setHasOptionsMenu(true);
@@ -161,7 +164,8 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
        return controllers;
    }

    private void addMobileSection(int subId) {
    @VisibleForTesting
    void addMobileSection(int subId) {
        addMobileSection(subId, null);
    }

@@ -176,7 +180,8 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
        }
    }

    private void addWifiSection() {
    @VisibleForTesting
    void addWifiSection() {
        TemplatePreferenceCategory category = (TemplatePreferenceCategory)
                inflatePreferences(R.xml.data_usage_wifi);
        category.setTemplate(NetworkTemplate.buildTemplateWifiWildcard(), 0, services);
@@ -289,17 +294,25 @@ public class DataUsageSummary extends DataUsageBaseFragment implements Indexable
        @Override
        public void setListening(boolean listening) {
            if (listening) {
                TelephonyManager telephonyManager = (TelephonyManager) mActivity
                        .getSystemService(Context.TELEPHONY_SERVICE);
                final int simState = telephonyManager.getSimState();
                // Note that pulling the SIM card returns UNKNOWN, not ABSENT.
                if (simState == TelephonyManager.SIM_STATE_ABSENT
                        || simState == TelephonyManager.SIM_STATE_UNKNOWN) {
                    mSummaryLoader.setSummary(this, null);
                } else {
                if (DataUsageUtils.hasSim(mActivity)) {
                    mSummaryLoader.setSummary(this,
                            mActivity.getString(R.string.data_usage_summary_format,
                                    formatUsedData()));
                } else {
                    final DataUsageController.DataUsageInfo info =
                            mDataController
                                    .getDataUsageInfo(NetworkTemplate.buildTemplateWifiWildcard());

                    if (info == null) {
                        mSummaryLoader.setSummary(this, null);
                    } else {
                        final CharSequence wifiFormat = mActivity
                                .getText(R.string.data_usage_wifi_format);
                        final CharSequence sizeText =
                                Formatter.formatFileSize(mActivity, info.usageLevel);
                        mSummaryLoader.setSummary(this,
                                TextUtils.expandTemplate(wifiFormat, sizeText));
                    }
                }
            }
        }
+54 −16
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.annotation.AttrRes;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.NetworkTemplate;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.Spannable;
@@ -34,7 +36,10 @@ import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settingslib.AppItem;
import com.android.settingslib.Utils;
import com.android.settingslib.utils.StringUtil;

@@ -83,6 +88,10 @@ public class DataUsageSummaryPreference extends Preference {
    /** The number of bytes used since the start of the cycle. */
    private long mDataplanUse;

    /** WiFi only mode */
    private boolean mWifiMode;
    private String mUsagePeriod;

    public DataUsageSummaryPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.data_usage_summary_preference);
@@ -130,6 +139,12 @@ public class DataUsageSummaryPreference extends Preference {
        notifyChanged();
    }

    void setWifiMode(boolean isWifiMode, String usagePeriod) {
        mWifiMode = isWifiMode;
        mUsagePeriod = usagePeriod;
        notifyChanged();
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
@@ -149,29 +164,52 @@ public class DataUsageSummaryPreference extends Preference {
        updateDataUsageLabels(holder);

        TextView usageTitle = (TextView) holder.findViewById(R.id.usage_title);
        usageTitle.setVisibility(mNumPlans > 1 ? View.VISIBLE : View.GONE);

        updateCycleTimeText(holder);

        TextView carrierInfo = (TextView) holder.findViewById(R.id.carrier_and_update);
        Button launchButton = (Button) holder.findViewById(R.id.launch_mdp_app_button);
        TextView limitInfo = (TextView) holder.findViewById(R.id.data_limits);

        updateCarrierInfo((TextView) holder.findViewById(R.id.carrier_and_update));
        if (mWifiMode) {
            usageTitle.setText(R.string.data_usage_wifi_title);
            usageTitle.setVisibility(View.VISIBLE);
            TextView cycleTime = (TextView) holder.findViewById(R.id.cycle_left_time);
            cycleTime.setText(mUsagePeriod);
            carrierInfo.setVisibility(View.GONE);
            limitInfo.setVisibility(View.GONE);

        Button launchButton = (Button) holder.findViewById(R.id.launch_mdp_app_button);
            launchButton.setOnClickListener((view) -> {
            getContext().sendBroadcast(mLaunchIntent);
                launchWifiDataUsage(getContext());
            });
            launchButton.setText(R.string.launch_wifi_text);
            launchButton.setVisibility(View.VISIBLE);
        } else {
            usageTitle.setVisibility(mNumPlans > 1 ? View.VISIBLE : View.GONE);
            updateCycleTimeText(holder);
            updateCarrierInfo(carrierInfo);
            if (mLaunchIntent != null) {
                launchButton.setOnClickListener((view) -> {
                    getContext().sendBroadcast(mLaunchIntent);
                });
                launchButton.setVisibility(View.VISIBLE);
            } else {
                launchButton.setVisibility(View.GONE);
            }

        TextView limitInfo = (TextView) holder.findViewById(R.id.data_limits);
            limitInfo.setVisibility(
                mLimitInfoText == null || mLimitInfoText.isEmpty() ? View.GONE : View.VISIBLE);
                    TextUtils.isEmpty(mLimitInfoText) ? View.GONE : View.VISIBLE);
            limitInfo.setText(mLimitInfoText);
        }
    }

    private static void launchWifiDataUsage(Context context) {
        final Bundle args = new Bundle(1);
        args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE,
                NetworkTemplate.buildTemplateWifiWildcard());
        final SubSettingLauncher launcher = new SubSettingLauncher(context)
                .setArguments(args)
                .setDestination(DataUsageList.class.getName())
                .setSourceMetricsCategory(MetricsProto.MetricsEvent.VIEW_UNKNOWN);
        launcher.setTitle(context.getString(R.string.wifi_data_usage));
        launcher.launch();
    }

    private void updateDataUsageLabels(PreferenceViewHolder holder) {
        TextView usageNumberField = (TextView) holder.findViewById(R.id.data_usage_view);
Loading