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

Commit bfefb801 authored by Weng Su's avatar Weng Su Committed by Android (Google) Code Review
Browse files

Merge "[Provider Model] Update internet preference icon and summary"

parents 4c34eb85 46837dc4
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
 <!--
  ~ Copyright (C) 2021 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.
  -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="24dp"
         android:height="24dp"
         android:viewportWidth="24"
         android:viewportHeight="24"
         android:tint="?attr/colorControlNormal">
     <path
         android:fillColor="@android:color/white"
         android:pathData="M17,6l-1.41,1.41L20.17,12l-4.58,4.59L17,18l6,-6zM8.41,7.41L7,6l-6,6 6,6 1.41,-1.41L3.83,12z"/>
     <path
         android:fillColor="@android:color/white"
         android:pathData="M8,12m-1,0a1,1 0,1 1,2 0a1,1 0,1 1,-2 0"/>
     <path
         android:fillColor="@android:color/white"
         android:pathData="M12,12m-1,0a1,1 0,1 1,2 0a1,1 0,1 1,-2 0"/>
     <path
         android:fillColor="@android:color/white"
         android:pathData="M16,12m-1,0a1,1 0,1 1,2 0a1,1 0,1 1,-2 0"/>
 </vector>
+4 −0
Original line number Diff line number Diff line
@@ -12550,6 +12550,10 @@
    <string name="resetting_internet_text">Resetting your internet\u2026</string>
    <!-- Menu option for data connectivity recovery for all requested technologies. [CHAR_LIMIT=NONE] -->
    <string name="fix_connectivity">Fix connectivity</string>
    <!-- Summary for airplane mode networks available. [CHAR LIMIT=60] -->
    <string name="airplane_mode_network_available">Airplane mode networks available</string>
    <!-- Summary for warning to disconnect ethernet first then switch to other networks. [CHAR LIMIT=60] -->
    <string name="to_switch_networks_disconnect_ethernet">To switch networks, disconnect ethernet</string>
    <!-- Summary text separator for preferences including a short description
         (eg. "Connected / 5G"). [CHAR LIMIT=50] -->
+102 −14
Original line number Diff line number Diff line
@@ -16,41 +16,107 @@

package com.android.settings.network;

import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;

import static com.android.settings.network.InternetUpdater.INTERNET_APM;
import static com.android.settings.network.InternetUpdater.INTERNET_APM_NETWORKS;
import static com.android.settings.network.InternetUpdater.INTERNET_CELLULAR;
import static com.android.settings.network.InternetUpdater.INTERNET_ETHERNET;
import static com.android.settings.network.InternetUpdater.INTERNET_WIFI;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;

import androidx.annotation.IdRes;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.R;
import com.android.settings.widget.SummaryUpdater;
import com.android.settings.wifi.WifiSummaryUpdater;
import com.android.settingslib.Utils;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;

import java.util.HashMap;
import java.util.Map;

/**
 * PreferenceController to update the internet state.
 */
public class InternetPreferenceController extends AbstractPreferenceController implements
        PreferenceControllerMixin, SummaryUpdater.OnSummaryChangeListener,
        LifecycleObserver, OnResume, OnPause {
        LifecycleObserver, SummaryUpdater.OnSummaryChangeListener,
        InternetUpdater.OnInternetTypeChangedListener {

    public static final String KEY_INTERNET_SETTINGS = "internet_settings";
    public static final String KEY = "internet_settings";

    private Preference mPreference;
    private final WifiSummaryUpdater mSummaryHelper;
    private InternetUpdater mInternetUpdater;
    private @InternetUpdater.InternetType int mInternetType;

    private static Map<Integer, Integer> sIconMap = new HashMap<>();
    static {
        sIconMap.put(INTERNET_APM, R.drawable.ic_airplanemode_active);
        sIconMap.put(INTERNET_APM_NETWORKS, R.drawable.ic_airplane_safe_networks_24dp);
        sIconMap.put(INTERNET_WIFI, R.drawable.ic_wifi_signal_4);
        sIconMap.put(INTERNET_CELLULAR, R.drawable.ic_network_cell);
        sIconMap.put(INTERNET_ETHERNET, R.drawable.ic_settings_ethernet);
    }

    private static Map<Integer, Integer> sSummaryMap = new HashMap<>();
    static {
        sSummaryMap.put(INTERNET_APM, R.string.condition_airplane_title);
        sSummaryMap.put(INTERNET_APM_NETWORKS, R.string.airplane_mode_network_available);
        sSummaryMap.put(INTERNET_WIFI, 0);
        sSummaryMap.put(INTERNET_CELLULAR, 0);
        sSummaryMap.put(INTERNET_ETHERNET, R.string.to_switch_networks_disconnect_ethernet);
    }

    public InternetPreferenceController(Context context) {
    public InternetPreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        if (lifecycle == null) {
            throw new IllegalArgumentException("Lifecycle must be set");
        }
        mSummaryHelper = new WifiSummaryUpdater(mContext, this);
        mInternetUpdater = new InternetUpdater(context, lifecycle, this);
        mInternetType = mInternetUpdater.getInternetType();
        lifecycle.addObserver(this);
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(KEY_INTERNET_SETTINGS);
        mPreference = screen.findPreference(KEY);
    }

    @Override
    public void updateState(Preference preference) {
        if (mPreference == null) {
            return;
        }
        final @IdRes int icon = sIconMap.get(mInternetType);
        if (icon != 0) {
            final Drawable drawable = mContext.getDrawable(icon);
            if (drawable != null) {
                drawable.setTintList(
                        Utils.getColorAttr(mContext, android.R.attr.colorControlNormal));
                mPreference.setIcon(drawable);
            }
        }
        if (mInternetType == INTERNET_CELLULAR) {
            updateCellularSummary();
            return;
        }
        final @IdRes int summary = sSummaryMap.get(mInternetType);
        if (summary != 0) {
            mPreference.setSummary(summary);
        }
    }

    @Override
@@ -60,23 +126,45 @@ public class InternetPreferenceController extends AbstractPreferenceController i

    @Override
    public String getPreferenceKey() {
        return KEY_INTERNET_SETTINGS;
        return KEY;
    }

    @Override
    /** @OnLifecycleEvent(ON_RESUME) */
    @OnLifecycleEvent(ON_RESUME)
    public void onResume() {
        mSummaryHelper.register(true);
    }

    @Override
    /** @OnLifecycleEvent(ON_PAUSE) */
    @OnLifecycleEvent(ON_PAUSE)
    public void onPause() {
        mSummaryHelper.register(false);
    }

    /**
     * Called when internet type is changed.
     *
     * @param internetType the internet type
     */
    public void onInternetTypeChanged(@InternetUpdater.InternetType int internetType) {
        mInternetType = internetType;
        updateState(mPreference);
    }

    @Override
    public void onSummaryChanged(String summary) {
        if (mPreference != null) {
        if (mPreference != null && mInternetType == INTERNET_WIFI) {
            mPreference.setSummary(summary);
        }
    }

    private void updateCellularSummary() {
        final SubscriptionManager subscriptionManager =
                mContext.getSystemService(SubscriptionManager.class);
        if (subscriptionManager == null) {
            return;
        }
        SubscriptionInfo subInfo = subscriptionManager.getDefaultDataSubscriptionInfo();
        mPreference.setSummary(subInfo.getDisplayName());
    }
}
+203 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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;

import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;

import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;

import android.annotation.NonNull;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkCapabilities.Transport;
import android.net.wifi.WifiManager;

import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.android.settings.AirplaneModeEnabler;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;

/**
 * Helper class to update the internet type for connected network preference
 */
public class InternetUpdater implements AirplaneModeEnabler.OnAirplaneModeChangedListener,
        LifecycleObserver {

    private static final String TAG = "InternetUpdater";

    private OnInternetTypeChangedListener mOnInternetTypeChangedListener;

    /** Interface that handles the internet type changed callback */
    public interface OnInternetTypeChangedListener {
        /**
         * Called when internet type is changed.
         *
         * @param internetType the internet type
         */
        void onInternetTypeChanged(@InternetType int internetType);
    }

    /**
     * Indicates this internet is unavailable type in airplane mode on.
     */
    public static final int INTERNET_APM = 0;

    /**
     * Indicates this internet uses an airplane mode network type.
     */
    public static final int INTERNET_APM_NETWORKS = 1;

    /**
     * Indicates this internet uses a Wi-Fi network type.
     */
    public static final int INTERNET_WIFI = 2;

    /**
     * Indicates this internet uses a Cellular network type.
     */
    public static final int INTERNET_CELLULAR = 3;

    /**
     * Indicates this internet uses a Ethernet network type.
     */
    public static final int INTERNET_ETHERNET = 4;

    @Retention(RetentionPolicy.SOURCE)
    @android.annotation.IntDef(prefix = { "INTERNET_" }, value = {
            INTERNET_APM,
            INTERNET_APM_NETWORKS,
            INTERNET_WIFI,
            INTERNET_CELLULAR,
            INTERNET_ETHERNET,
    })
    public @interface InternetType { }
    private @InternetType int mInternetType;

    private final Context mContext;
    private final ConnectivityManager mConnectivityManager;
    private final WifiManager mWifiManager;
    private final IntentFilter mWifiStateFilter;
    @VisibleForTesting
    AirplaneModeEnabler mAirplaneModeEnabler;

    @VisibleForTesting
    @Transport int mTransport;
    private static Map<Integer, Integer> sTransportMap = new HashMap<>();
    static {
        sTransportMap.put(TRANSPORT_WIFI, INTERNET_WIFI);
        sTransportMap.put(TRANSPORT_CELLULAR, INTERNET_CELLULAR);
        sTransportMap.put(TRANSPORT_ETHERNET, INTERNET_ETHERNET);
    }

    private NetworkCallback mNetworkCallback = new NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            if (network == null) {
                return;
            }
            final NetworkCapabilities networkCapabilities =
                    mConnectivityManager.getNetworkCapabilities(network);
            if (networkCapabilities == null) {
                return;
            }
            for (@Transport int transport : networkCapabilities.getTransportTypes()) {
                if (sTransportMap.containsKey(transport)) {
                    mTransport = transport;
                    break;
                }
            }
            update();
        }
    };

    private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            update();
        }
    };

    public InternetUpdater(Context context, Lifecycle lifecycle,
            OnInternetTypeChangedListener listener) {
        if (lifecycle == null) {
            throw new IllegalArgumentException("Lifecycle must be set");
        }
        mContext = context;
        mAirplaneModeEnabler = new AirplaneModeEnabler(mContext, this);
        mConnectivityManager = mContext.getSystemService(ConnectivityManager.class);
        mWifiManager = mContext.getSystemService(WifiManager.class);
        mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mOnInternetTypeChangedListener = listener;
        lifecycle.addObserver(this);
    }

    /** @OnLifecycleEvent(ON_RESUME) */
    @OnLifecycleEvent(ON_RESUME)
    public void onResume() {
        mAirplaneModeEnabler.start();
        mConnectivityManager.registerDefaultNetworkCallback(mNetworkCallback);
        mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter);
    }

    /** @OnLifecycleEvent(ON_PAUSE) */
    @OnLifecycleEvent(ON_PAUSE)
    public void onPause() {
        mAirplaneModeEnabler.stop();
        mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
        mContext.unregisterReceiver(mWifiStateReceiver);
    }

    @Override
    public void onAirplaneModeChanged(boolean isAirplaneModeOn) {
        update();
    }

    @VisibleForTesting
    void update() {
        if (mAirplaneModeEnabler.isAirplaneModeOn()) {
            mInternetType = mWifiManager.isWifiEnabled() ? INTERNET_APM_NETWORKS : INTERNET_APM;
        } else {
            mInternetType = sTransportMap.get(mTransport);
        }
        if (mOnInternetTypeChangedListener != null) {
            mOnInternetTypeChangedListener.onInternetTypeChanged(mInternetType);
        }
    }

    /**
     * Get the internet type.
     */
    public @InternetType int getInternetType() {
        return mInternetType;
    }
}
+1 −4
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
                                metricsFeatureProvider);
        final InternetPreferenceController internetPreferenceController =
                Utils.isProviderModelEnabled(context)
                        ? new InternetPreferenceController(context)
                        ? new InternetPreferenceController(context, lifecycle)
                        : null;

        final VpnPreferenceController vpnPreferenceController =
@@ -126,9 +126,6 @@ public class NetworkDashboardFragment extends DashboardFragment implements
            if (wifiPreferenceController != null) {
                lifecycle.addObserver(wifiPreferenceController);
            }
            if (internetPreferenceController != null) {
                lifecycle.addObserver(internetPreferenceController);
            }
            lifecycle.addObserver(vpnPreferenceController);
            lifecycle.addObserver(privateDnsPreferenceController);
        }
Loading